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>
47 struct tls_decrypt_arg {
53 noinline void tls_err_abort(struct sock *sk, int err)
55 WARN_ON_ONCE(err >= 0);
56 /* sk->sk_err should contain a positive error code. */
61 static int __skb_nsg(struct sk_buff *skb, int offset, int len,
62 unsigned int recursion_level)
64 int start = skb_headlen(skb);
65 int i, chunk = start - offset;
66 struct sk_buff *frag_iter;
69 if (unlikely(recursion_level >= 24))
82 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
85 WARN_ON(start > offset + len);
87 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
101 if (unlikely(skb_has_frag_list(skb))) {
102 skb_walk_frags(skb, frag_iter) {
105 WARN_ON(start > offset + len);
107 end = start + frag_iter->len;
108 chunk = end - offset;
112 ret = __skb_nsg(frag_iter, offset - start, chunk,
113 recursion_level + 1);
114 if (unlikely(ret < 0))
129 /* Return the number of scatterlist elements required to completely map the
130 * skb, or -EMSGSIZE if the recursion depth is exceeded.
132 static int skb_nsg(struct sk_buff *skb, int offset, int len)
134 return __skb_nsg(skb, offset, len, 0);
137 static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb,
138 struct tls_decrypt_arg *darg)
140 struct strp_msg *rxm = strp_msg(skb);
141 struct tls_msg *tlm = tls_msg(skb);
144 /* Determine zero-padding length */
145 if (prot->version == TLS_1_3_VERSION) {
146 int offset = rxm->full_len - TLS_TAG_SIZE - 1;
147 char content_type = darg->zc ? darg->tail : 0;
150 while (content_type == 0) {
151 if (offset < prot->prepend_size)
153 err = skb_copy_bits(skb, rxm->offset + offset,
162 tlm->control = content_type;
167 static void tls_decrypt_done(struct crypto_async_request *req, int err)
169 struct aead_request *aead_req = (struct aead_request *)req;
170 struct scatterlist *sgout = aead_req->dst;
171 struct scatterlist *sgin = aead_req->src;
172 struct tls_sw_context_rx *ctx;
173 struct tls_context *tls_ctx;
174 struct tls_prot_info *prot;
175 struct scatterlist *sg;
179 skb = (struct sk_buff *)req->data;
180 tls_ctx = tls_get_ctx(skb->sk);
181 ctx = tls_sw_ctx_rx(tls_ctx);
182 prot = &tls_ctx->prot_info;
184 /* Propagate if there was an err */
187 TLS_INC_STATS(sock_net(skb->sk),
188 LINUX_MIB_TLSDECRYPTERROR);
189 ctx->async_wait.err = err;
190 tls_err_abort(skb->sk, err);
192 struct strp_msg *rxm = strp_msg(skb);
194 /* No TLS 1.3 support with async crypto */
195 WARN_ON(prot->tail_size);
197 rxm->offset += prot->prepend_size;
198 rxm->full_len -= prot->overhead_size;
201 /* After using skb->sk to propagate sk through crypto async callback
202 * we need to NULL it again.
207 /* Free the destination pages if skb was not decrypted inplace */
209 /* Skip the first S/G entry as it points to AAD */
210 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
213 put_page(sg_page(sg));
219 spin_lock_bh(&ctx->decrypt_compl_lock);
220 if (!atomic_dec_return(&ctx->decrypt_pending))
221 complete(&ctx->async_wait.completion);
222 spin_unlock_bh(&ctx->decrypt_compl_lock);
225 static int tls_do_decryption(struct sock *sk,
227 struct scatterlist *sgin,
228 struct scatterlist *sgout,
231 struct aead_request *aead_req,
232 struct tls_decrypt_arg *darg)
234 struct tls_context *tls_ctx = tls_get_ctx(sk);
235 struct tls_prot_info *prot = &tls_ctx->prot_info;
236 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
239 aead_request_set_tfm(aead_req, ctx->aead_recv);
240 aead_request_set_ad(aead_req, prot->aad_size);
241 aead_request_set_crypt(aead_req, sgin, sgout,
242 data_len + prot->tag_size,
246 /* Using skb->sk to push sk through to crypto async callback
247 * handler. This allows propagating errors up to the socket
248 * if needed. It _must_ be cleared in the async handler
249 * before consume_skb is called. We _know_ skb->sk is NULL
250 * because it is a clone from strparser.
253 aead_request_set_callback(aead_req,
254 CRYPTO_TFM_REQ_MAY_BACKLOG,
255 tls_decrypt_done, skb);
256 atomic_inc(&ctx->decrypt_pending);
258 aead_request_set_callback(aead_req,
259 CRYPTO_TFM_REQ_MAY_BACKLOG,
260 crypto_req_done, &ctx->async_wait);
263 ret = crypto_aead_decrypt(aead_req);
264 if (ret == -EINPROGRESS) {
268 ret = crypto_wait_req(ret, &ctx->async_wait);
273 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
278 static void tls_trim_both_msgs(struct sock *sk, int target_size)
280 struct tls_context *tls_ctx = tls_get_ctx(sk);
281 struct tls_prot_info *prot = &tls_ctx->prot_info;
282 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
283 struct tls_rec *rec = ctx->open_rec;
285 sk_msg_trim(sk, &rec->msg_plaintext, target_size);
287 target_size += prot->overhead_size;
288 sk_msg_trim(sk, &rec->msg_encrypted, target_size);
291 static int tls_alloc_encrypted_msg(struct sock *sk, int len)
293 struct tls_context *tls_ctx = tls_get_ctx(sk);
294 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
295 struct tls_rec *rec = ctx->open_rec;
296 struct sk_msg *msg_en = &rec->msg_encrypted;
298 return sk_msg_alloc(sk, msg_en, len, 0);
301 static int tls_clone_plaintext_msg(struct sock *sk, int required)
303 struct tls_context *tls_ctx = tls_get_ctx(sk);
304 struct tls_prot_info *prot = &tls_ctx->prot_info;
305 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
306 struct tls_rec *rec = ctx->open_rec;
307 struct sk_msg *msg_pl = &rec->msg_plaintext;
308 struct sk_msg *msg_en = &rec->msg_encrypted;
311 /* We add page references worth len bytes from encrypted sg
312 * at the end of plaintext sg. It is guaranteed that msg_en
313 * has enough required room (ensured by caller).
315 len = required - msg_pl->sg.size;
317 /* Skip initial bytes in msg_en's data to be able to use
318 * same offset of both plain and encrypted data.
320 skip = prot->prepend_size + msg_pl->sg.size;
322 return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
325 static struct tls_rec *tls_get_rec(struct sock *sk)
327 struct tls_context *tls_ctx = tls_get_ctx(sk);
328 struct tls_prot_info *prot = &tls_ctx->prot_info;
329 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
330 struct sk_msg *msg_pl, *msg_en;
334 mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
336 rec = kzalloc(mem_size, sk->sk_allocation);
340 msg_pl = &rec->msg_plaintext;
341 msg_en = &rec->msg_encrypted;
346 sg_init_table(rec->sg_aead_in, 2);
347 sg_set_buf(&rec->sg_aead_in[0], rec->aad_space, prot->aad_size);
348 sg_unmark_end(&rec->sg_aead_in[1]);
350 sg_init_table(rec->sg_aead_out, 2);
351 sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size);
352 sg_unmark_end(&rec->sg_aead_out[1]);
357 static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
359 sk_msg_free(sk, &rec->msg_encrypted);
360 sk_msg_free(sk, &rec->msg_plaintext);
364 static void tls_free_open_rec(struct sock *sk)
366 struct tls_context *tls_ctx = tls_get_ctx(sk);
367 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
368 struct tls_rec *rec = ctx->open_rec;
371 tls_free_rec(sk, rec);
372 ctx->open_rec = NULL;
376 int tls_tx_records(struct sock *sk, int flags)
378 struct tls_context *tls_ctx = tls_get_ctx(sk);
379 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
380 struct tls_rec *rec, *tmp;
381 struct sk_msg *msg_en;
382 int tx_flags, rc = 0;
384 if (tls_is_partially_sent_record(tls_ctx)) {
385 rec = list_first_entry(&ctx->tx_list,
386 struct tls_rec, list);
389 tx_flags = rec->tx_flags;
393 rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
397 /* Full record has been transmitted.
398 * Remove the head of tx_list
400 list_del(&rec->list);
401 sk_msg_free(sk, &rec->msg_plaintext);
405 /* Tx all ready records */
406 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
407 if (READ_ONCE(rec->tx_ready)) {
409 tx_flags = rec->tx_flags;
413 msg_en = &rec->msg_encrypted;
414 rc = tls_push_sg(sk, tls_ctx,
415 &msg_en->sg.data[msg_en->sg.curr],
420 list_del(&rec->list);
421 sk_msg_free(sk, &rec->msg_plaintext);
429 if (rc < 0 && rc != -EAGAIN)
430 tls_err_abort(sk, -EBADMSG);
435 static void tls_encrypt_done(struct crypto_async_request *req, int err)
437 struct aead_request *aead_req = (struct aead_request *)req;
438 struct sock *sk = req->data;
439 struct tls_context *tls_ctx = tls_get_ctx(sk);
440 struct tls_prot_info *prot = &tls_ctx->prot_info;
441 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
442 struct scatterlist *sge;
443 struct sk_msg *msg_en;
448 rec = container_of(aead_req, struct tls_rec, aead_req);
449 msg_en = &rec->msg_encrypted;
451 sge = sk_msg_elem(msg_en, msg_en->sg.curr);
452 sge->offset -= prot->prepend_size;
453 sge->length += prot->prepend_size;
455 /* Check if error is previously set on socket */
456 if (err || sk->sk_err) {
459 /* If err is already set on socket, return the same code */
461 ctx->async_wait.err = -sk->sk_err;
463 ctx->async_wait.err = err;
464 tls_err_abort(sk, err);
469 struct tls_rec *first_rec;
471 /* Mark the record as ready for transmission */
472 smp_store_mb(rec->tx_ready, true);
474 /* If received record is at head of tx_list, schedule tx */
475 first_rec = list_first_entry(&ctx->tx_list,
476 struct tls_rec, list);
477 if (rec == first_rec)
481 spin_lock_bh(&ctx->encrypt_compl_lock);
482 pending = atomic_dec_return(&ctx->encrypt_pending);
484 if (!pending && ctx->async_notify)
485 complete(&ctx->async_wait.completion);
486 spin_unlock_bh(&ctx->encrypt_compl_lock);
491 /* Schedule the transmission */
492 if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
493 schedule_delayed_work(&ctx->tx_work.work, 1);
496 static int tls_do_encryption(struct sock *sk,
497 struct tls_context *tls_ctx,
498 struct tls_sw_context_tx *ctx,
499 struct aead_request *aead_req,
500 size_t data_len, u32 start)
502 struct tls_prot_info *prot = &tls_ctx->prot_info;
503 struct tls_rec *rec = ctx->open_rec;
504 struct sk_msg *msg_en = &rec->msg_encrypted;
505 struct scatterlist *sge = sk_msg_elem(msg_en, start);
506 int rc, iv_offset = 0;
508 /* For CCM based ciphers, first byte of IV is a constant */
509 switch (prot->cipher_type) {
510 case TLS_CIPHER_AES_CCM_128:
511 rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE;
514 case TLS_CIPHER_SM4_CCM:
515 rec->iv_data[0] = TLS_SM4_CCM_IV_B0_BYTE;
520 memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
521 prot->iv_size + prot->salt_size);
523 xor_iv_with_seq(prot, rec->iv_data + iv_offset, tls_ctx->tx.rec_seq);
525 sge->offset += prot->prepend_size;
526 sge->length -= prot->prepend_size;
528 msg_en->sg.curr = start;
530 aead_request_set_tfm(aead_req, ctx->aead_send);
531 aead_request_set_ad(aead_req, prot->aad_size);
532 aead_request_set_crypt(aead_req, rec->sg_aead_in,
534 data_len, rec->iv_data);
536 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
537 tls_encrypt_done, sk);
539 /* Add the record in tx_list */
540 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
541 atomic_inc(&ctx->encrypt_pending);
543 rc = crypto_aead_encrypt(aead_req);
544 if (!rc || rc != -EINPROGRESS) {
545 atomic_dec(&ctx->encrypt_pending);
546 sge->offset -= prot->prepend_size;
547 sge->length += prot->prepend_size;
551 WRITE_ONCE(rec->tx_ready, true);
552 } else if (rc != -EINPROGRESS) {
553 list_del(&rec->list);
557 /* Unhook the record from context if encryption is not failure */
558 ctx->open_rec = NULL;
559 tls_advance_record_sn(sk, prot, &tls_ctx->tx);
563 static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
564 struct tls_rec **to, struct sk_msg *msg_opl,
565 struct sk_msg *msg_oen, u32 split_point,
566 u32 tx_overhead_size, u32 *orig_end)
568 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
569 struct scatterlist *sge, *osge, *nsge;
570 u32 orig_size = msg_opl->sg.size;
571 struct scatterlist tmp = { };
572 struct sk_msg *msg_npl;
576 new = tls_get_rec(sk);
579 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
580 tx_overhead_size, 0);
582 tls_free_rec(sk, new);
586 *orig_end = msg_opl->sg.end;
587 i = msg_opl->sg.start;
588 sge = sk_msg_elem(msg_opl, i);
589 while (apply && sge->length) {
590 if (sge->length > apply) {
591 u32 len = sge->length - apply;
593 get_page(sg_page(sge));
594 sg_set_page(&tmp, sg_page(sge), len,
595 sge->offset + apply);
600 apply -= sge->length;
601 bytes += sge->length;
604 sk_msg_iter_var_next(i);
605 if (i == msg_opl->sg.end)
607 sge = sk_msg_elem(msg_opl, i);
611 msg_opl->sg.curr = i;
612 msg_opl->sg.copybreak = 0;
613 msg_opl->apply_bytes = 0;
614 msg_opl->sg.size = bytes;
616 msg_npl = &new->msg_plaintext;
617 msg_npl->apply_bytes = apply;
618 msg_npl->sg.size = orig_size - bytes;
620 j = msg_npl->sg.start;
621 nsge = sk_msg_elem(msg_npl, j);
623 memcpy(nsge, &tmp, sizeof(*nsge));
624 sk_msg_iter_var_next(j);
625 nsge = sk_msg_elem(msg_npl, j);
628 osge = sk_msg_elem(msg_opl, i);
629 while (osge->length) {
630 memcpy(nsge, osge, sizeof(*nsge));
632 sk_msg_iter_var_next(i);
633 sk_msg_iter_var_next(j);
636 osge = sk_msg_elem(msg_opl, i);
637 nsge = sk_msg_elem(msg_npl, j);
641 msg_npl->sg.curr = j;
642 msg_npl->sg.copybreak = 0;
648 static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
649 struct tls_rec *from, u32 orig_end)
651 struct sk_msg *msg_npl = &from->msg_plaintext;
652 struct sk_msg *msg_opl = &to->msg_plaintext;
653 struct scatterlist *osge, *nsge;
657 sk_msg_iter_var_prev(i);
658 j = msg_npl->sg.start;
660 osge = sk_msg_elem(msg_opl, i);
661 nsge = sk_msg_elem(msg_npl, j);
663 if (sg_page(osge) == sg_page(nsge) &&
664 osge->offset + osge->length == nsge->offset) {
665 osge->length += nsge->length;
666 put_page(sg_page(nsge));
669 msg_opl->sg.end = orig_end;
670 msg_opl->sg.curr = orig_end;
671 msg_opl->sg.copybreak = 0;
672 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
673 msg_opl->sg.size += msg_npl->sg.size;
675 sk_msg_free(sk, &to->msg_encrypted);
676 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
681 static int tls_push_record(struct sock *sk, int flags,
682 unsigned char record_type)
684 struct tls_context *tls_ctx = tls_get_ctx(sk);
685 struct tls_prot_info *prot = &tls_ctx->prot_info;
686 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
687 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
688 u32 i, split_point, orig_end;
689 struct sk_msg *msg_pl, *msg_en;
690 struct aead_request *req;
697 msg_pl = &rec->msg_plaintext;
698 msg_en = &rec->msg_encrypted;
700 split_point = msg_pl->apply_bytes;
701 split = split_point && split_point < msg_pl->sg.size;
702 if (unlikely((!split &&
704 prot->overhead_size > msg_en->sg.size) ||
707 prot->overhead_size > msg_en->sg.size))) {
709 split_point = msg_en->sg.size;
712 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
713 split_point, prot->overhead_size,
717 /* This can happen if above tls_split_open_record allocates
718 * a single large encryption buffer instead of two smaller
719 * ones. In this case adjust pointers and continue without
722 if (!msg_pl->sg.size) {
723 tls_merge_open_record(sk, rec, tmp, orig_end);
724 msg_pl = &rec->msg_plaintext;
725 msg_en = &rec->msg_encrypted;
728 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
729 prot->overhead_size);
732 rec->tx_flags = flags;
733 req = &rec->aead_req;
736 sk_msg_iter_var_prev(i);
738 rec->content_type = record_type;
739 if (prot->version == TLS_1_3_VERSION) {
740 /* Add content type to end of message. No padding added */
741 sg_set_buf(&rec->sg_content_type, &rec->content_type, 1);
742 sg_mark_end(&rec->sg_content_type);
743 sg_chain(msg_pl->sg.data, msg_pl->sg.end + 1,
744 &rec->sg_content_type);
746 sg_mark_end(sk_msg_elem(msg_pl, i));
749 if (msg_pl->sg.end < msg_pl->sg.start) {
750 sg_chain(&msg_pl->sg.data[msg_pl->sg.start],
751 MAX_SKB_FRAGS - msg_pl->sg.start + 1,
755 i = msg_pl->sg.start;
756 sg_chain(rec->sg_aead_in, 2, &msg_pl->sg.data[i]);
759 sk_msg_iter_var_prev(i);
760 sg_mark_end(sk_msg_elem(msg_en, i));
762 i = msg_en->sg.start;
763 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
765 tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size,
766 tls_ctx->tx.rec_seq, record_type, prot);
768 tls_fill_prepend(tls_ctx,
769 page_address(sg_page(&msg_en->sg.data[i])) +
770 msg_en->sg.data[i].offset,
771 msg_pl->sg.size + prot->tail_size,
774 tls_ctx->pending_open_record_frags = false;
776 rc = tls_do_encryption(sk, tls_ctx, ctx, req,
777 msg_pl->sg.size + prot->tail_size, i);
779 if (rc != -EINPROGRESS) {
780 tls_err_abort(sk, -EBADMSG);
782 tls_ctx->pending_open_record_frags = true;
783 tls_merge_open_record(sk, rec, tmp, orig_end);
786 ctx->async_capable = 1;
789 msg_pl = &tmp->msg_plaintext;
790 msg_en = &tmp->msg_encrypted;
791 sk_msg_trim(sk, msg_en, msg_pl->sg.size + prot->overhead_size);
792 tls_ctx->pending_open_record_frags = true;
796 return tls_tx_records(sk, flags);
799 static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
800 bool full_record, u8 record_type,
801 ssize_t *copied, int flags)
803 struct tls_context *tls_ctx = tls_get_ctx(sk);
804 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
805 struct sk_msg msg_redir = { };
806 struct sk_psock *psock;
807 struct sock *sk_redir;
813 policy = !(flags & MSG_SENDPAGE_NOPOLICY);
814 psock = sk_psock_get(sk);
815 if (!psock || !policy) {
816 err = tls_push_record(sk, flags, record_type);
817 if (err && sk->sk_err == EBADMSG) {
818 *copied -= sk_msg_free(sk, msg);
819 tls_free_open_rec(sk);
823 sk_psock_put(sk, psock);
827 enospc = sk_msg_full(msg);
828 if (psock->eval == __SK_NONE) {
829 delta = msg->sg.size;
830 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
831 delta -= msg->sg.size;
833 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
834 !enospc && !full_record) {
840 if (msg->apply_bytes && msg->apply_bytes < send)
841 send = msg->apply_bytes;
843 switch (psock->eval) {
845 err = tls_push_record(sk, flags, record_type);
846 if (err && sk->sk_err == EBADMSG) {
847 *copied -= sk_msg_free(sk, msg);
848 tls_free_open_rec(sk);
854 sk_redir = psock->sk_redir;
855 memcpy(&msg_redir, msg, sizeof(*msg));
856 if (msg->apply_bytes < send)
857 msg->apply_bytes = 0;
859 msg->apply_bytes -= send;
860 sk_msg_return_zero(sk, msg, send);
861 msg->sg.size -= send;
863 err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
866 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
869 if (msg->sg.size == 0)
870 tls_free_open_rec(sk);
874 sk_msg_free_partial(sk, msg, send);
875 if (msg->apply_bytes < send)
876 msg->apply_bytes = 0;
878 msg->apply_bytes -= send;
879 if (msg->sg.size == 0)
880 tls_free_open_rec(sk);
881 *copied -= (send + delta);
886 bool reset_eval = !ctx->open_rec;
890 msg = &rec->msg_plaintext;
891 if (!msg->apply_bytes)
895 psock->eval = __SK_NONE;
896 if (psock->sk_redir) {
897 sock_put(psock->sk_redir);
898 psock->sk_redir = NULL;
905 sk_psock_put(sk, psock);
909 static int tls_sw_push_pending_record(struct sock *sk, int flags)
911 struct tls_context *tls_ctx = tls_get_ctx(sk);
912 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
913 struct tls_rec *rec = ctx->open_rec;
914 struct sk_msg *msg_pl;
920 msg_pl = &rec->msg_plaintext;
921 copied = msg_pl->sg.size;
925 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
929 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
931 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
932 struct tls_context *tls_ctx = tls_get_ctx(sk);
933 struct tls_prot_info *prot = &tls_ctx->prot_info;
934 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
935 bool async_capable = ctx->async_capable;
936 unsigned char record_type = TLS_RECORD_TYPE_DATA;
937 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
938 bool eor = !(msg->msg_flags & MSG_MORE);
941 struct sk_msg *msg_pl, *msg_en;
952 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
956 mutex_lock(&tls_ctx->tx_lock);
959 if (unlikely(msg->msg_controllen)) {
960 ret = tls_proccess_cmsg(sk, msg, &record_type);
962 if (ret == -EINPROGRESS)
964 else if (ret != -EAGAIN)
969 while (msg_data_left(msg)) {
978 rec = ctx->open_rec = tls_get_rec(sk);
984 msg_pl = &rec->msg_plaintext;
985 msg_en = &rec->msg_encrypted;
987 orig_size = msg_pl->sg.size;
989 try_to_copy = msg_data_left(msg);
990 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
991 if (try_to_copy >= record_room) {
992 try_to_copy = record_room;
996 required_size = msg_pl->sg.size + try_to_copy +
999 if (!sk_stream_memory_free(sk))
1000 goto wait_for_sndbuf;
1003 ret = tls_alloc_encrypted_msg(sk, required_size);
1006 goto wait_for_memory;
1008 /* Adjust try_to_copy according to the amount that was
1009 * actually allocated. The difference is due
1010 * to max sg elements limit
1012 try_to_copy -= required_size - msg_en->sg.size;
1016 if (!is_kvec && (full_record || eor) && !async_capable) {
1017 u32 first = msg_pl->sg.end;
1019 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
1020 msg_pl, try_to_copy);
1022 goto fallback_to_reg_send;
1025 copied += try_to_copy;
1027 sk_msg_sg_copy_set(msg_pl, first);
1028 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1029 record_type, &copied,
1032 if (ret == -EINPROGRESS)
1034 else if (ret == -ENOMEM)
1035 goto wait_for_memory;
1036 else if (ctx->open_rec && ret == -ENOSPC)
1038 else if (ret != -EAGAIN)
1043 copied -= try_to_copy;
1044 sk_msg_sg_copy_clear(msg_pl, first);
1045 iov_iter_revert(&msg->msg_iter,
1046 msg_pl->sg.size - orig_size);
1047 fallback_to_reg_send:
1048 sk_msg_trim(sk, msg_pl, orig_size);
1051 required_size = msg_pl->sg.size + try_to_copy;
1053 ret = tls_clone_plaintext_msg(sk, required_size);
1058 /* Adjust try_to_copy according to the amount that was
1059 * actually allocated. The difference is due
1060 * to max sg elements limit
1062 try_to_copy -= required_size - msg_pl->sg.size;
1064 sk_msg_trim(sk, msg_en,
1065 msg_pl->sg.size + prot->overhead_size);
1069 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
1070 msg_pl, try_to_copy);
1075 /* Open records defined only if successfully copied, otherwise
1076 * we would trim the sg but not reset the open record frags.
1078 tls_ctx->pending_open_record_frags = true;
1079 copied += try_to_copy;
1080 if (full_record || eor) {
1081 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1082 record_type, &copied,
1085 if (ret == -EINPROGRESS)
1087 else if (ret == -ENOMEM)
1088 goto wait_for_memory;
1089 else if (ret != -EAGAIN) {
1100 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1102 ret = sk_stream_wait_memory(sk, &timeo);
1106 tls_trim_both_msgs(sk, orig_size);
1110 if (ctx->open_rec && msg_en->sg.size < required_size)
1111 goto alloc_encrypted;
1116 } else if (num_zc) {
1117 /* Wait for pending encryptions to get completed */
1118 spin_lock_bh(&ctx->encrypt_compl_lock);
1119 ctx->async_notify = true;
1121 pending = atomic_read(&ctx->encrypt_pending);
1122 spin_unlock_bh(&ctx->encrypt_compl_lock);
1124 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1126 reinit_completion(&ctx->async_wait.completion);
1128 /* There can be no concurrent accesses, since we have no
1129 * pending encrypt operations
1131 WRITE_ONCE(ctx->async_notify, false);
1133 if (ctx->async_wait.err) {
1134 ret = ctx->async_wait.err;
1139 /* Transmit if any encryptions have completed */
1140 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1141 cancel_delayed_work(&ctx->tx_work.work);
1142 tls_tx_records(sk, msg->msg_flags);
1146 ret = sk_stream_error(sk, msg->msg_flags, ret);
1149 mutex_unlock(&tls_ctx->tx_lock);
1150 return copied > 0 ? copied : ret;
1153 static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
1154 int offset, size_t size, int flags)
1156 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1157 struct tls_context *tls_ctx = tls_get_ctx(sk);
1158 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1159 struct tls_prot_info *prot = &tls_ctx->prot_info;
1160 unsigned char record_type = TLS_RECORD_TYPE_DATA;
1161 struct sk_msg *msg_pl;
1162 struct tls_rec *rec;
1170 eor = !(flags & MSG_SENDPAGE_NOTLAST);
1171 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1173 /* Call the sk_stream functions to manage the sndbuf mem. */
1175 size_t copy, required_size;
1183 rec = ctx->open_rec;
1185 rec = ctx->open_rec = tls_get_rec(sk);
1191 msg_pl = &rec->msg_plaintext;
1193 full_record = false;
1194 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
1196 if (copy >= record_room) {
1201 required_size = msg_pl->sg.size + copy + prot->overhead_size;
1203 if (!sk_stream_memory_free(sk))
1204 goto wait_for_sndbuf;
1206 ret = tls_alloc_encrypted_msg(sk, required_size);
1209 goto wait_for_memory;
1211 /* Adjust copy according to the amount that was
1212 * actually allocated. The difference is due
1213 * to max sg elements limit
1215 copy -= required_size - msg_pl->sg.size;
1219 sk_msg_page_add(msg_pl, page, copy, offset);
1220 sk_mem_charge(sk, copy);
1226 tls_ctx->pending_open_record_frags = true;
1227 if (full_record || eor || sk_msg_full(msg_pl)) {
1228 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1229 record_type, &copied, flags);
1231 if (ret == -EINPROGRESS)
1233 else if (ret == -ENOMEM)
1234 goto wait_for_memory;
1235 else if (ret != -EAGAIN) {
1244 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1246 ret = sk_stream_wait_memory(sk, &timeo);
1249 tls_trim_both_msgs(sk, msg_pl->sg.size);
1258 /* Transmit if any encryptions have completed */
1259 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1260 cancel_delayed_work(&ctx->tx_work.work);
1261 tls_tx_records(sk, flags);
1265 ret = sk_stream_error(sk, flags, ret);
1266 return copied > 0 ? copied : ret;
1269 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
1270 int offset, size_t size, int flags)
1272 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1273 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY |
1274 MSG_NO_SHARED_FRAGS))
1277 return tls_sw_do_sendpage(sk, page, offset, size, flags);
1280 int tls_sw_sendpage(struct sock *sk, struct page *page,
1281 int offset, size_t size, int flags)
1283 struct tls_context *tls_ctx = tls_get_ctx(sk);
1286 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1287 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1290 mutex_lock(&tls_ctx->tx_lock);
1292 ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
1294 mutex_unlock(&tls_ctx->tx_lock);
1298 static struct sk_buff *tls_wait_data(struct sock *sk, struct sk_psock *psock,
1299 bool nonblock, long timeo, int *err)
1301 struct tls_context *tls_ctx = tls_get_ctx(sk);
1302 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1303 struct sk_buff *skb;
1304 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1306 while (!(skb = ctx->recv_pkt) && sk_psock_queue_empty(psock)) {
1308 *err = sock_error(sk);
1312 if (!skb_queue_empty(&sk->sk_receive_queue)) {
1313 __strp_unpause(&ctx->strp);
1315 return ctx->recv_pkt;
1318 if (sk->sk_shutdown & RCV_SHUTDOWN)
1321 if (sock_flag(sk, SOCK_DONE))
1324 if (nonblock || !timeo) {
1329 add_wait_queue(sk_sleep(sk), &wait);
1330 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1331 sk_wait_event(sk, &timeo,
1332 ctx->recv_pkt != skb ||
1333 !sk_psock_queue_empty(psock),
1335 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1336 remove_wait_queue(sk_sleep(sk), &wait);
1338 /* Handle signals */
1339 if (signal_pending(current)) {
1340 *err = sock_intr_errno(timeo);
1348 static int tls_setup_from_iter(struct iov_iter *from,
1349 int length, int *pages_used,
1350 struct scatterlist *to,
1353 int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1354 struct page *pages[MAX_SKB_FRAGS];
1355 unsigned int size = 0;
1356 ssize_t copied, use;
1359 while (length > 0) {
1361 maxpages = to_max_pages - num_elem;
1362 if (maxpages == 0) {
1366 copied = iov_iter_get_pages(from, pages,
1374 iov_iter_advance(from, copied);
1379 use = min_t(int, copied, PAGE_SIZE - offset);
1381 sg_set_page(&to[num_elem],
1382 pages[i], use, offset);
1383 sg_unmark_end(&to[num_elem]);
1384 /* We do not uncharge memory from this API */
1393 /* Mark the end in the last sg entry if newly added */
1394 if (num_elem > *pages_used)
1395 sg_mark_end(&to[num_elem - 1]);
1398 iov_iter_revert(from, size);
1399 *pages_used = num_elem;
1404 /* This function decrypts the input skb into either out_iov or in out_sg
1405 * or in skb buffers itself. The input parameter 'zc' indicates if
1406 * zero-copy mode needs to be tried or not. With zero-copy mode, either
1407 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
1408 * NULL, then the decryption happens inside skb buffers itself, i.e.
1409 * zero-copy gets disabled and 'zc' is updated.
1412 static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1413 struct iov_iter *out_iov,
1414 struct scatterlist *out_sg,
1415 struct tls_decrypt_arg *darg)
1417 struct tls_context *tls_ctx = tls_get_ctx(sk);
1418 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1419 struct tls_prot_info *prot = &tls_ctx->prot_info;
1420 struct strp_msg *rxm = strp_msg(skb);
1421 struct tls_msg *tlm = tls_msg(skb);
1422 int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0;
1423 u8 *aad, *iv, *tail, *mem = NULL;
1424 struct aead_request *aead_req;
1425 struct sk_buff *unused;
1426 struct scatterlist *sgin = NULL;
1427 struct scatterlist *sgout = NULL;
1428 const int data_len = rxm->full_len - prot->overhead_size;
1429 int tail_pages = !!prot->tail_size;
1432 if (darg->zc && (out_iov || out_sg)) {
1434 n_sgout = 1 + tail_pages +
1435 iov_iter_npages_cap(out_iov, INT_MAX, data_len);
1437 n_sgout = sg_nents(out_sg);
1438 n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size,
1439 rxm->full_len - prot->prepend_size);
1443 n_sgin = skb_cow_data(skb, 0, &unused);
1449 /* Increment to accommodate AAD */
1450 n_sgin = n_sgin + 1;
1452 nsg = n_sgin + n_sgout;
1454 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
1455 mem_size = aead_size + (nsg * sizeof(struct scatterlist));
1456 mem_size = mem_size + prot->aad_size;
1457 mem_size = mem_size + MAX_IV_SIZE;
1458 mem_size = mem_size + prot->tail_size;
1460 /* Allocate a single block of memory which contains
1461 * aead_req || sgin[] || sgout[] || aad || iv || tail.
1462 * This order achieves correct alignment for aead_req, sgin, sgout.
1464 mem = kmalloc(mem_size, sk->sk_allocation);
1468 /* Segment the allocated memory */
1469 aead_req = (struct aead_request *)mem;
1470 sgin = (struct scatterlist *)(mem + aead_size);
1471 sgout = sgin + n_sgin;
1472 aad = (u8 *)(sgout + n_sgout);
1473 iv = aad + prot->aad_size;
1474 tail = iv + MAX_IV_SIZE;
1476 /* For CCM based ciphers, first byte of nonce+iv is a constant */
1477 switch (prot->cipher_type) {
1478 case TLS_CIPHER_AES_CCM_128:
1479 iv[0] = TLS_AES_CCM_IV_B0_BYTE;
1482 case TLS_CIPHER_SM4_CCM:
1483 iv[0] = TLS_SM4_CCM_IV_B0_BYTE;
1489 if (prot->version == TLS_1_3_VERSION ||
1490 prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
1491 memcpy(iv + iv_offset, tls_ctx->rx.iv,
1492 prot->iv_size + prot->salt_size);
1494 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1495 iv + iv_offset + prot->salt_size,
1501 memcpy(iv + iv_offset, tls_ctx->rx.iv, prot->salt_size);
1503 xor_iv_with_seq(prot, iv + iv_offset, tls_ctx->rx.rec_seq);
1506 tls_make_aad(aad, rxm->full_len - prot->overhead_size +
1508 tls_ctx->rx.rec_seq, tlm->control, prot);
1511 sg_init_table(sgin, n_sgin);
1512 sg_set_buf(&sgin[0], aad, prot->aad_size);
1513 err = skb_to_sgvec(skb, &sgin[1],
1514 rxm->offset + prot->prepend_size,
1515 rxm->full_len - prot->prepend_size);
1523 sg_init_table(sgout, n_sgout);
1524 sg_set_buf(&sgout[0], aad, prot->aad_size);
1526 err = tls_setup_from_iter(out_iov, data_len,
1528 (n_sgout - 1 - tail_pages));
1530 goto fallback_to_reg_recv;
1532 if (prot->tail_size) {
1533 sg_unmark_end(&sgout[pages]);
1534 sg_set_buf(&sgout[pages + 1], tail,
1536 sg_mark_end(&sgout[pages + 1]);
1538 } else if (out_sg) {
1539 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
1541 goto fallback_to_reg_recv;
1544 fallback_to_reg_recv:
1550 /* Prepare and submit AEAD request */
1551 err = tls_do_decryption(sk, skb, sgin, sgout, iv,
1552 data_len + prot->tail_size, aead_req, darg);
1556 if (prot->tail_size)
1559 /* Release the pages in case iov was mapped to pages */
1560 for (; pages > 0; pages--)
1561 put_page(sg_page(&sgout[pages]));
1567 static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
1568 struct iov_iter *dest,
1569 struct tls_decrypt_arg *darg)
1571 struct tls_context *tls_ctx = tls_get_ctx(sk);
1572 struct tls_prot_info *prot = &tls_ctx->prot_info;
1573 struct strp_msg *rxm = strp_msg(skb);
1574 struct tls_msg *tlm = tls_msg(skb);
1577 if (tlm->decrypted) {
1579 darg->async = false;
1583 if (tls_ctx->rx_conf == TLS_HW) {
1584 err = tls_device_decrypted(sk, tls_ctx, skb, rxm);
1590 darg->async = false;
1595 err = decrypt_internal(sk, skb, dest, NULL, darg);
1600 /* If opportunistic TLS 1.3 ZC failed retry without ZC */
1601 if (unlikely(darg->zc && prot->version == TLS_1_3_VERSION &&
1602 darg->tail != TLS_RECORD_TYPE_DATA)) {
1604 TLS_INC_STATS(sock_net(sk), LINUX_MIN_TLSDECRYPTRETRY);
1605 return decrypt_skb_update(sk, skb, dest, darg);
1609 pad = tls_padding_length(prot, skb, darg);
1613 rxm->full_len -= pad;
1614 rxm->offset += prot->prepend_size;
1615 rxm->full_len -= prot->overhead_size;
1618 tls_advance_record_sn(sk, prot, &tls_ctx->rx);
1623 int decrypt_skb(struct sock *sk, struct sk_buff *skb,
1624 struct scatterlist *sgout)
1626 struct tls_decrypt_arg darg = { .zc = true, };
1628 return decrypt_internal(sk, skb, NULL, sgout, &darg);
1631 static int tls_record_content_type(struct msghdr *msg, struct tls_msg *tlm,
1637 *control = tlm->control;
1641 err = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1642 sizeof(*control), control);
1643 if (*control != TLS_RECORD_TYPE_DATA) {
1644 if (err || msg->msg_flags & MSG_CTRUNC)
1647 } else if (*control != tlm->control) {
1654 /* This function traverses the rx_list in tls receive context to copies the
1655 * decrypted records into the buffer provided by caller zero copy is not
1656 * true. Further, the records are removed from the rx_list if it is not a peek
1657 * case and the record has been consumed completely.
1659 static int process_rx_list(struct tls_sw_context_rx *ctx,
1667 struct sk_buff *skb = skb_peek(&ctx->rx_list);
1668 struct tls_msg *tlm;
1672 while (skip && skb) {
1673 struct strp_msg *rxm = strp_msg(skb);
1676 err = tls_record_content_type(msg, tlm, control);
1680 if (skip < rxm->full_len)
1683 skip = skip - rxm->full_len;
1684 skb = skb_peek_next(skb, &ctx->rx_list);
1687 while (len && skb) {
1688 struct sk_buff *next_skb;
1689 struct strp_msg *rxm = strp_msg(skb);
1690 int chunk = min_t(unsigned int, rxm->full_len - skip, len);
1694 err = tls_record_content_type(msg, tlm, control);
1698 if (!zc || (rxm->full_len - skip) > len) {
1699 err = skb_copy_datagram_msg(skb, rxm->offset + skip,
1706 copied = copied + chunk;
1708 /* Consume the data from record if it is non-peek case*/
1710 rxm->offset = rxm->offset + chunk;
1711 rxm->full_len = rxm->full_len - chunk;
1713 /* Return if there is unconsumed data in the record */
1714 if (rxm->full_len - skip)
1718 /* The remaining skip-bytes must lie in 1st record in rx_list.
1719 * So from the 2nd record, 'skip' should be 0.
1724 msg->msg_flags |= MSG_EOR;
1726 next_skb = skb_peek_next(skb, &ctx->rx_list);
1729 __skb_unlink(skb, &ctx->rx_list);
1738 return copied ? : err;
1742 tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot,
1743 size_t len_left, size_t decrypted, ssize_t done,
1748 if (len_left <= decrypted)
1751 max_rec = prot->overhead_size - prot->tail_size + TLS_MAX_PAYLOAD_SIZE;
1752 if (done - *flushed_at < SZ_128K && tcp_inq(sk) > max_rec)
1756 sk_flush_backlog(sk);
1759 int tls_sw_recvmsg(struct sock *sk,
1765 struct tls_context *tls_ctx = tls_get_ctx(sk);
1766 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1767 struct tls_prot_info *prot = &tls_ctx->prot_info;
1768 struct sk_psock *psock;
1769 unsigned char control = 0;
1770 ssize_t decrypted = 0;
1771 size_t flushed_at = 0;
1772 struct strp_msg *rxm;
1773 struct tls_msg *tlm;
1774 struct sk_buff *skb;
1777 int target, err = 0;
1779 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
1780 bool is_peek = flags & MSG_PEEK;
1781 bool bpf_strp_enabled;
1784 if (unlikely(flags & MSG_ERRQUEUE))
1785 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1787 psock = sk_psock_get(sk);
1789 bpf_strp_enabled = sk_psock_strp_enabled(psock);
1791 /* If crypto failed the connection is broken */
1792 err = ctx->async_wait.err;
1796 /* Process pending decrypted records. It must be non-zero-copy */
1797 err = process_rx_list(ctx, msg, &control, 0, len, false, is_peek);
1805 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1807 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1809 zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek &&
1812 while (len && (decrypted + copied < target || ctx->recv_pkt)) {
1813 struct tls_decrypt_arg darg = {};
1814 int to_decrypt, chunk;
1816 skb = tls_wait_data(sk, psock, flags & MSG_DONTWAIT, timeo, &err);
1819 chunk = sk_msg_recvmsg(sk, psock, msg, len,
1827 rxm = strp_msg(skb);
1830 to_decrypt = rxm->full_len - prot->overhead_size;
1832 if (zc_capable && to_decrypt <= len &&
1833 tlm->control == TLS_RECORD_TYPE_DATA)
1836 /* Do not use async mode if record is non-data */
1837 if (tlm->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled)
1838 darg.async = ctx->async_capable;
1842 err = decrypt_skb_update(sk, skb, &msg->msg_iter, &darg);
1844 tls_err_abort(sk, -EBADMSG);
1848 async |= darg.async;
1850 /* If the type of records being processed is not known yet,
1851 * set it to record type just dequeued. If it is already known,
1852 * but does not match the record type just dequeued, go to end.
1853 * We always get record type here since for tls1.2, record type
1854 * is known just after record is dequeued from stream parser.
1855 * For tls1.3, we disable async.
1857 err = tls_record_content_type(msg, tlm, &control);
1861 /* periodically flush backlog, and feed strparser */
1862 tls_read_flush_backlog(sk, prot, len, to_decrypt,
1863 decrypted + copied, &flushed_at);
1865 ctx->recv_pkt = NULL;
1866 __strp_unpause(&ctx->strp);
1867 __skb_queue_tail(&ctx->rx_list, skb);
1870 /* TLS 1.2-only, to_decrypt must be text length */
1871 chunk = min_t(int, to_decrypt, len);
1877 /* TLS 1.3 may have updated the length by more than overhead */
1878 chunk = rxm->full_len;
1881 bool partially_consumed = chunk > len;
1883 if (bpf_strp_enabled) {
1884 /* BPF may try to queue the skb */
1885 __skb_unlink(skb, &ctx->rx_list);
1886 err = sk_psock_tls_strp_read(psock, skb);
1887 if (err != __SK_PASS) {
1888 rxm->offset = rxm->offset + rxm->full_len;
1890 if (err == __SK_DROP)
1894 __skb_queue_tail(&ctx->rx_list, skb);
1897 if (partially_consumed)
1900 err = skb_copy_datagram_msg(skb, rxm->offset,
1908 if (partially_consumed) {
1909 rxm->offset += chunk;
1910 rxm->full_len -= chunk;
1918 __skb_unlink(skb, &ctx->rx_list);
1921 /* Return full control message to userspace before trying
1922 * to parse another message type
1924 msg->msg_flags |= MSG_EOR;
1925 if (control != TLS_RECORD_TYPE_DATA)
1933 /* Wait for all previously submitted records to be decrypted */
1934 spin_lock_bh(&ctx->decrypt_compl_lock);
1935 reinit_completion(&ctx->async_wait.completion);
1936 pending = atomic_read(&ctx->decrypt_pending);
1937 spin_unlock_bh(&ctx->decrypt_compl_lock);
1939 ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1941 if (err >= 0 || err == -EINPROGRESS)
1948 /* Drain records from the rx_list & copy if required */
1949 if (is_peek || is_kvec)
1950 err = process_rx_list(ctx, msg, &control, copied,
1951 decrypted, false, is_peek);
1953 err = process_rx_list(ctx, msg, &control, 0,
1954 decrypted, true, is_peek);
1955 decrypted = max(err, 0);
1958 copied += decrypted;
1963 sk_psock_put(sk, psock);
1964 return copied ? : err;
1967 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
1968 struct pipe_inode_info *pipe,
1969 size_t len, unsigned int flags)
1971 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
1972 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1973 struct strp_msg *rxm = NULL;
1974 struct sock *sk = sock->sk;
1975 struct tls_msg *tlm;
1976 struct sk_buff *skb;
1985 timeo = sock_rcvtimeo(sk, flags & SPLICE_F_NONBLOCK);
1987 from_queue = !skb_queue_empty(&ctx->rx_list);
1989 skb = __skb_dequeue(&ctx->rx_list);
1991 struct tls_decrypt_arg darg = {};
1993 skb = tls_wait_data(sk, NULL, flags & SPLICE_F_NONBLOCK, timeo,
1996 goto splice_read_end;
1998 err = decrypt_skb_update(sk, skb, NULL, &darg);
2000 tls_err_abort(sk, -EBADMSG);
2001 goto splice_read_end;
2005 rxm = strp_msg(skb);
2008 /* splice does not support reading control messages */
2009 if (tlm->control != TLS_RECORD_TYPE_DATA) {
2011 goto splice_read_end;
2014 chunk = min_t(unsigned int, rxm->full_len, len);
2015 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
2017 goto splice_read_end;
2020 ctx->recv_pkt = NULL;
2021 __strp_unpause(&ctx->strp);
2023 if (chunk < rxm->full_len) {
2024 __skb_queue_head(&ctx->rx_list, skb);
2026 rxm->full_len -= len;
2033 return copied ? : err;
2036 bool tls_sw_sock_is_readable(struct sock *sk)
2038 struct tls_context *tls_ctx = tls_get_ctx(sk);
2039 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2040 bool ingress_empty = true;
2041 struct sk_psock *psock;
2044 psock = sk_psock(sk);
2046 ingress_empty = list_empty(&psock->ingress_msg);
2049 return !ingress_empty || ctx->recv_pkt ||
2050 !skb_queue_empty(&ctx->rx_list);
2053 static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
2055 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2056 struct tls_prot_info *prot = &tls_ctx->prot_info;
2057 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
2058 struct strp_msg *rxm = strp_msg(skb);
2059 struct tls_msg *tlm = tls_msg(skb);
2060 size_t cipher_overhead;
2061 size_t data_len = 0;
2064 /* Verify that we have a full TLS header, or wait for more data */
2065 if (rxm->offset + prot->prepend_size > skb->len)
2068 /* Sanity-check size of on-stack buffer. */
2069 if (WARN_ON(prot->prepend_size > sizeof(header))) {
2074 /* Linearize header to local buffer */
2075 ret = skb_copy_bits(skb, rxm->offset, header, prot->prepend_size);
2080 tlm->control = header[0];
2082 data_len = ((header[4] & 0xFF) | (header[3] << 8));
2084 cipher_overhead = prot->tag_size;
2085 if (prot->version != TLS_1_3_VERSION &&
2086 prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
2087 cipher_overhead += prot->iv_size;
2089 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
2094 if (data_len < cipher_overhead) {
2099 /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
2100 if (header[1] != TLS_1_2_VERSION_MINOR ||
2101 header[2] != TLS_1_2_VERSION_MAJOR) {
2106 tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE,
2107 TCP_SKB_CB(skb)->seq + rxm->offset);
2108 return data_len + TLS_HEADER_SIZE;
2111 tls_err_abort(strp->sk, ret);
2116 static void tls_queue(struct strparser *strp, struct sk_buff *skb)
2118 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2119 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2121 ctx->recv_pkt = skb;
2124 ctx->saved_data_ready(strp->sk);
2127 static void tls_data_ready(struct sock *sk)
2129 struct tls_context *tls_ctx = tls_get_ctx(sk);
2130 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2131 struct sk_psock *psock;
2133 strp_data_ready(&ctx->strp);
2135 psock = sk_psock_get(sk);
2137 if (!list_empty(&psock->ingress_msg))
2138 ctx->saved_data_ready(sk);
2139 sk_psock_put(sk, psock);
2143 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx)
2145 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2147 set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask);
2148 set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask);
2149 cancel_delayed_work_sync(&ctx->tx_work.work);
2152 void tls_sw_release_resources_tx(struct sock *sk)
2154 struct tls_context *tls_ctx = tls_get_ctx(sk);
2155 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2156 struct tls_rec *rec, *tmp;
2159 /* Wait for any pending async encryptions to complete */
2160 spin_lock_bh(&ctx->encrypt_compl_lock);
2161 ctx->async_notify = true;
2162 pending = atomic_read(&ctx->encrypt_pending);
2163 spin_unlock_bh(&ctx->encrypt_compl_lock);
2166 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
2168 tls_tx_records(sk, -1);
2170 /* Free up un-sent records in tx_list. First, free
2171 * the partially sent record if any at head of tx_list.
2173 if (tls_ctx->partially_sent_record) {
2174 tls_free_partial_record(sk, tls_ctx);
2175 rec = list_first_entry(&ctx->tx_list,
2176 struct tls_rec, list);
2177 list_del(&rec->list);
2178 sk_msg_free(sk, &rec->msg_plaintext);
2182 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
2183 list_del(&rec->list);
2184 sk_msg_free(sk, &rec->msg_encrypted);
2185 sk_msg_free(sk, &rec->msg_plaintext);
2189 crypto_free_aead(ctx->aead_send);
2190 tls_free_open_rec(sk);
2193 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx)
2195 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2200 void tls_sw_release_resources_rx(struct sock *sk)
2202 struct tls_context *tls_ctx = tls_get_ctx(sk);
2203 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2205 kfree(tls_ctx->rx.rec_seq);
2206 kfree(tls_ctx->rx.iv);
2208 if (ctx->aead_recv) {
2209 kfree_skb(ctx->recv_pkt);
2210 ctx->recv_pkt = NULL;
2211 __skb_queue_purge(&ctx->rx_list);
2212 crypto_free_aead(ctx->aead_recv);
2213 strp_stop(&ctx->strp);
2214 /* If tls_sw_strparser_arm() was not called (cleanup paths)
2215 * we still want to strp_stop(), but sk->sk_data_ready was
2218 if (ctx->saved_data_ready) {
2219 write_lock_bh(&sk->sk_callback_lock);
2220 sk->sk_data_ready = ctx->saved_data_ready;
2221 write_unlock_bh(&sk->sk_callback_lock);
2226 void tls_sw_strparser_done(struct tls_context *tls_ctx)
2228 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2230 strp_done(&ctx->strp);
2233 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx)
2235 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2240 void tls_sw_free_resources_rx(struct sock *sk)
2242 struct tls_context *tls_ctx = tls_get_ctx(sk);
2244 tls_sw_release_resources_rx(sk);
2245 tls_sw_free_ctx_rx(tls_ctx);
2248 /* The work handler to transmitt the encrypted records in tx_list */
2249 static void tx_work_handler(struct work_struct *work)
2251 struct delayed_work *delayed_work = to_delayed_work(work);
2252 struct tx_work *tx_work = container_of(delayed_work,
2253 struct tx_work, work);
2254 struct sock *sk = tx_work->sk;
2255 struct tls_context *tls_ctx = tls_get_ctx(sk);
2256 struct tls_sw_context_tx *ctx;
2258 if (unlikely(!tls_ctx))
2261 ctx = tls_sw_ctx_tx(tls_ctx);
2262 if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask))
2265 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
2267 mutex_lock(&tls_ctx->tx_lock);
2269 tls_tx_records(sk, -1);
2271 mutex_unlock(&tls_ctx->tx_lock);
2274 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx)
2276 struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);
2278 /* Schedule the transmission if tx list is ready */
2279 if (is_tx_ready(tx_ctx) &&
2280 !test_and_set_bit(BIT_TX_SCHEDULED, &tx_ctx->tx_bitmask))
2281 schedule_delayed_work(&tx_ctx->tx_work.work, 0);
2284 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx)
2286 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2288 write_lock_bh(&sk->sk_callback_lock);
2289 rx_ctx->saved_data_ready = sk->sk_data_ready;
2290 sk->sk_data_ready = tls_data_ready;
2291 write_unlock_bh(&sk->sk_callback_lock);
2293 strp_check_rcv(&rx_ctx->strp);
2296 void tls_update_rx_zc_capable(struct tls_context *tls_ctx)
2298 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2300 rx_ctx->zc_capable = tls_ctx->rx_no_pad ||
2301 tls_ctx->prot_info.version != TLS_1_3_VERSION;
2304 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2306 struct tls_context *tls_ctx = tls_get_ctx(sk);
2307 struct tls_prot_info *prot = &tls_ctx->prot_info;
2308 struct tls_crypto_info *crypto_info;
2309 struct tls_sw_context_tx *sw_ctx_tx = NULL;
2310 struct tls_sw_context_rx *sw_ctx_rx = NULL;
2311 struct cipher_context *cctx;
2312 struct crypto_aead **aead;
2313 struct strp_callbacks cb;
2314 u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
2315 struct crypto_tfm *tfm;
2316 char *iv, *rec_seq, *key, *salt, *cipher_name;
2326 if (!ctx->priv_ctx_tx) {
2327 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
2332 ctx->priv_ctx_tx = sw_ctx_tx;
2335 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
2338 if (!ctx->priv_ctx_rx) {
2339 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
2344 ctx->priv_ctx_rx = sw_ctx_rx;
2347 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
2352 crypto_init_wait(&sw_ctx_tx->async_wait);
2353 spin_lock_init(&sw_ctx_tx->encrypt_compl_lock);
2354 crypto_info = &ctx->crypto_send.info;
2356 aead = &sw_ctx_tx->aead_send;
2357 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
2358 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
2359 sw_ctx_tx->tx_work.sk = sk;
2361 crypto_init_wait(&sw_ctx_rx->async_wait);
2362 spin_lock_init(&sw_ctx_rx->decrypt_compl_lock);
2363 crypto_info = &ctx->crypto_recv.info;
2365 skb_queue_head_init(&sw_ctx_rx->rx_list);
2366 aead = &sw_ctx_rx->aead_recv;
2369 switch (crypto_info->cipher_type) {
2370 case TLS_CIPHER_AES_GCM_128: {
2371 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
2373 gcm_128_info = (void *)crypto_info;
2374 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2375 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
2376 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2377 iv = gcm_128_info->iv;
2378 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
2379 rec_seq = gcm_128_info->rec_seq;
2380 keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE;
2381 key = gcm_128_info->key;
2382 salt = gcm_128_info->salt;
2383 salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
2384 cipher_name = "gcm(aes)";
2387 case TLS_CIPHER_AES_GCM_256: {
2388 struct tls12_crypto_info_aes_gcm_256 *gcm_256_info;
2390 gcm_256_info = (void *)crypto_info;
2391 nonce_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2392 tag_size = TLS_CIPHER_AES_GCM_256_TAG_SIZE;
2393 iv_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2394 iv = gcm_256_info->iv;
2395 rec_seq_size = TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE;
2396 rec_seq = gcm_256_info->rec_seq;
2397 keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE;
2398 key = gcm_256_info->key;
2399 salt = gcm_256_info->salt;
2400 salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE;
2401 cipher_name = "gcm(aes)";
2404 case TLS_CIPHER_AES_CCM_128: {
2405 struct tls12_crypto_info_aes_ccm_128 *ccm_128_info;
2407 ccm_128_info = (void *)crypto_info;
2408 nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2409 tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE;
2410 iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2411 iv = ccm_128_info->iv;
2412 rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE;
2413 rec_seq = ccm_128_info->rec_seq;
2414 keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE;
2415 key = ccm_128_info->key;
2416 salt = ccm_128_info->salt;
2417 salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE;
2418 cipher_name = "ccm(aes)";
2421 case TLS_CIPHER_CHACHA20_POLY1305: {
2422 struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305_info;
2424 chacha20_poly1305_info = (void *)crypto_info;
2426 tag_size = TLS_CIPHER_CHACHA20_POLY1305_TAG_SIZE;
2427 iv_size = TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE;
2428 iv = chacha20_poly1305_info->iv;
2429 rec_seq_size = TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE;
2430 rec_seq = chacha20_poly1305_info->rec_seq;
2431 keysize = TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE;
2432 key = chacha20_poly1305_info->key;
2433 salt = chacha20_poly1305_info->salt;
2434 salt_size = TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE;
2435 cipher_name = "rfc7539(chacha20,poly1305)";
2438 case TLS_CIPHER_SM4_GCM: {
2439 struct tls12_crypto_info_sm4_gcm *sm4_gcm_info;
2441 sm4_gcm_info = (void *)crypto_info;
2442 nonce_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2443 tag_size = TLS_CIPHER_SM4_GCM_TAG_SIZE;
2444 iv_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2445 iv = sm4_gcm_info->iv;
2446 rec_seq_size = TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE;
2447 rec_seq = sm4_gcm_info->rec_seq;
2448 keysize = TLS_CIPHER_SM4_GCM_KEY_SIZE;
2449 key = sm4_gcm_info->key;
2450 salt = sm4_gcm_info->salt;
2451 salt_size = TLS_CIPHER_SM4_GCM_SALT_SIZE;
2452 cipher_name = "gcm(sm4)";
2455 case TLS_CIPHER_SM4_CCM: {
2456 struct tls12_crypto_info_sm4_ccm *sm4_ccm_info;
2458 sm4_ccm_info = (void *)crypto_info;
2459 nonce_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2460 tag_size = TLS_CIPHER_SM4_CCM_TAG_SIZE;
2461 iv_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2462 iv = sm4_ccm_info->iv;
2463 rec_seq_size = TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE;
2464 rec_seq = sm4_ccm_info->rec_seq;
2465 keysize = TLS_CIPHER_SM4_CCM_KEY_SIZE;
2466 key = sm4_ccm_info->key;
2467 salt = sm4_ccm_info->salt;
2468 salt_size = TLS_CIPHER_SM4_CCM_SALT_SIZE;
2469 cipher_name = "ccm(sm4)";
2477 /* Sanity-check the sizes for stack allocations. */
2478 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE ||
2479 rec_seq_size > TLS_MAX_REC_SEQ_SIZE || tag_size != TLS_TAG_SIZE) {
2484 if (crypto_info->version == TLS_1_3_VERSION) {
2486 prot->aad_size = TLS_HEADER_SIZE;
2487 prot->tail_size = 1;
2489 prot->aad_size = TLS_AAD_SPACE_SIZE;
2490 prot->tail_size = 0;
2493 prot->version = crypto_info->version;
2494 prot->cipher_type = crypto_info->cipher_type;
2495 prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
2496 prot->tag_size = tag_size;
2497 prot->overhead_size = prot->prepend_size +
2498 prot->tag_size + prot->tail_size;
2499 prot->iv_size = iv_size;
2500 prot->salt_size = salt_size;
2501 cctx->iv = kmalloc(iv_size + salt_size, GFP_KERNEL);
2506 /* Note: 128 & 256 bit salt are the same size */
2507 prot->rec_seq_size = rec_seq_size;
2508 memcpy(cctx->iv, salt, salt_size);
2509 memcpy(cctx->iv + salt_size, iv, iv_size);
2510 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
2511 if (!cctx->rec_seq) {
2517 *aead = crypto_alloc_aead(cipher_name, 0, 0);
2518 if (IS_ERR(*aead)) {
2519 rc = PTR_ERR(*aead);
2525 ctx->push_pending_record = tls_sw_push_pending_record;
2527 rc = crypto_aead_setkey(*aead, key, keysize);
2532 rc = crypto_aead_setauthsize(*aead, prot->tag_size);
2537 tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
2539 tls_update_rx_zc_capable(ctx);
2540 sw_ctx_rx->async_capable =
2541 crypto_info->version != TLS_1_3_VERSION &&
2542 !!(tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC);
2544 /* Set up strparser */
2545 memset(&cb, 0, sizeof(cb));
2546 cb.rcv_msg = tls_queue;
2547 cb.parse_msg = tls_read_size;
2549 strp_init(&sw_ctx_rx->strp, sk, &cb);
2555 crypto_free_aead(*aead);
2558 kfree(cctx->rec_seq);
2559 cctx->rec_seq = NULL;
2565 kfree(ctx->priv_ctx_tx);
2566 ctx->priv_ctx_tx = NULL;
2568 kfree(ctx->priv_ctx_rx);
2569 ctx->priv_ctx_rx = NULL;