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 {
59 struct tls_decrypt_ctx {
61 u8 aad[TLS_MAX_AAD_SIZE];
63 struct scatterlist sg[];
66 noinline void tls_err_abort(struct sock *sk, int err)
68 WARN_ON_ONCE(err >= 0);
69 /* sk->sk_err should contain a positive error code. */
74 static int __skb_nsg(struct sk_buff *skb, int offset, int len,
75 unsigned int recursion_level)
77 int start = skb_headlen(skb);
78 int i, chunk = start - offset;
79 struct sk_buff *frag_iter;
82 if (unlikely(recursion_level >= 24))
95 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
98 WARN_ON(start > offset + len);
100 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
101 chunk = end - offset;
114 if (unlikely(skb_has_frag_list(skb))) {
115 skb_walk_frags(skb, frag_iter) {
118 WARN_ON(start > offset + len);
120 end = start + frag_iter->len;
121 chunk = end - offset;
125 ret = __skb_nsg(frag_iter, offset - start, chunk,
126 recursion_level + 1);
127 if (unlikely(ret < 0))
142 /* Return the number of scatterlist elements required to completely map the
143 * skb, or -EMSGSIZE if the recursion depth is exceeded.
145 static int skb_nsg(struct sk_buff *skb, int offset, int len)
147 return __skb_nsg(skb, offset, len, 0);
150 static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb,
151 struct tls_decrypt_arg *darg)
153 struct strp_msg *rxm = strp_msg(skb);
154 struct tls_msg *tlm = tls_msg(skb);
157 /* Determine zero-padding length */
158 if (prot->version == TLS_1_3_VERSION) {
159 int offset = rxm->full_len - TLS_TAG_SIZE - 1;
160 char content_type = darg->zc ? darg->tail : 0;
163 while (content_type == 0) {
164 if (offset < prot->prepend_size)
166 err = skb_copy_bits(skb, rxm->offset + offset,
175 tlm->control = content_type;
180 static void tls_decrypt_done(struct crypto_async_request *req, int err)
182 struct aead_request *aead_req = (struct aead_request *)req;
183 struct scatterlist *sgout = aead_req->dst;
184 struct scatterlist *sgin = aead_req->src;
185 struct tls_sw_context_rx *ctx;
186 struct tls_context *tls_ctx;
187 struct tls_prot_info *prot;
188 struct scatterlist *sg;
192 skb = (struct sk_buff *)req->data;
193 tls_ctx = tls_get_ctx(skb->sk);
194 ctx = tls_sw_ctx_rx(tls_ctx);
195 prot = &tls_ctx->prot_info;
197 /* Propagate if there was an err */
200 TLS_INC_STATS(sock_net(skb->sk),
201 LINUX_MIB_TLSDECRYPTERROR);
202 ctx->async_wait.err = err;
203 tls_err_abort(skb->sk, err);
205 struct strp_msg *rxm = strp_msg(skb);
207 /* No TLS 1.3 support with async crypto */
208 WARN_ON(prot->tail_size);
210 rxm->offset += prot->prepend_size;
211 rxm->full_len -= prot->overhead_size;
214 /* After using skb->sk to propagate sk through crypto async callback
215 * we need to NULL it again.
220 /* Free the destination pages if skb was not decrypted inplace */
222 /* Skip the first S/G entry as it points to AAD */
223 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
226 put_page(sg_page(sg));
232 spin_lock_bh(&ctx->decrypt_compl_lock);
233 if (!atomic_dec_return(&ctx->decrypt_pending))
234 complete(&ctx->async_wait.completion);
235 spin_unlock_bh(&ctx->decrypt_compl_lock);
238 static int tls_do_decryption(struct sock *sk,
240 struct scatterlist *sgin,
241 struct scatterlist *sgout,
244 struct aead_request *aead_req,
245 struct tls_decrypt_arg *darg)
247 struct tls_context *tls_ctx = tls_get_ctx(sk);
248 struct tls_prot_info *prot = &tls_ctx->prot_info;
249 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
252 aead_request_set_tfm(aead_req, ctx->aead_recv);
253 aead_request_set_ad(aead_req, prot->aad_size);
254 aead_request_set_crypt(aead_req, sgin, sgout,
255 data_len + prot->tag_size,
259 /* Using skb->sk to push sk through to crypto async callback
260 * handler. This allows propagating errors up to the socket
261 * if needed. It _must_ be cleared in the async handler
262 * before consume_skb is called. We _know_ skb->sk is NULL
263 * because it is a clone from strparser.
266 aead_request_set_callback(aead_req,
267 CRYPTO_TFM_REQ_MAY_BACKLOG,
268 tls_decrypt_done, skb);
269 atomic_inc(&ctx->decrypt_pending);
271 aead_request_set_callback(aead_req,
272 CRYPTO_TFM_REQ_MAY_BACKLOG,
273 crypto_req_done, &ctx->async_wait);
276 ret = crypto_aead_decrypt(aead_req);
277 if (ret == -EINPROGRESS) {
281 ret = crypto_wait_req(ret, &ctx->async_wait);
288 static void tls_trim_both_msgs(struct sock *sk, int target_size)
290 struct tls_context *tls_ctx = tls_get_ctx(sk);
291 struct tls_prot_info *prot = &tls_ctx->prot_info;
292 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
293 struct tls_rec *rec = ctx->open_rec;
295 sk_msg_trim(sk, &rec->msg_plaintext, target_size);
297 target_size += prot->overhead_size;
298 sk_msg_trim(sk, &rec->msg_encrypted, target_size);
301 static int tls_alloc_encrypted_msg(struct sock *sk, int len)
303 struct tls_context *tls_ctx = tls_get_ctx(sk);
304 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
305 struct tls_rec *rec = ctx->open_rec;
306 struct sk_msg *msg_en = &rec->msg_encrypted;
308 return sk_msg_alloc(sk, msg_en, len, 0);
311 static int tls_clone_plaintext_msg(struct sock *sk, int required)
313 struct tls_context *tls_ctx = tls_get_ctx(sk);
314 struct tls_prot_info *prot = &tls_ctx->prot_info;
315 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
316 struct tls_rec *rec = ctx->open_rec;
317 struct sk_msg *msg_pl = &rec->msg_plaintext;
318 struct sk_msg *msg_en = &rec->msg_encrypted;
321 /* We add page references worth len bytes from encrypted sg
322 * at the end of plaintext sg. It is guaranteed that msg_en
323 * has enough required room (ensured by caller).
325 len = required - msg_pl->sg.size;
327 /* Skip initial bytes in msg_en's data to be able to use
328 * same offset of both plain and encrypted data.
330 skip = prot->prepend_size + msg_pl->sg.size;
332 return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
335 static struct tls_rec *tls_get_rec(struct sock *sk)
337 struct tls_context *tls_ctx = tls_get_ctx(sk);
338 struct tls_prot_info *prot = &tls_ctx->prot_info;
339 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
340 struct sk_msg *msg_pl, *msg_en;
344 mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
346 rec = kzalloc(mem_size, sk->sk_allocation);
350 msg_pl = &rec->msg_plaintext;
351 msg_en = &rec->msg_encrypted;
356 sg_init_table(rec->sg_aead_in, 2);
357 sg_set_buf(&rec->sg_aead_in[0], rec->aad_space, prot->aad_size);
358 sg_unmark_end(&rec->sg_aead_in[1]);
360 sg_init_table(rec->sg_aead_out, 2);
361 sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size);
362 sg_unmark_end(&rec->sg_aead_out[1]);
367 static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
369 sk_msg_free(sk, &rec->msg_encrypted);
370 sk_msg_free(sk, &rec->msg_plaintext);
374 static void tls_free_open_rec(struct sock *sk)
376 struct tls_context *tls_ctx = tls_get_ctx(sk);
377 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
378 struct tls_rec *rec = ctx->open_rec;
381 tls_free_rec(sk, rec);
382 ctx->open_rec = NULL;
386 int tls_tx_records(struct sock *sk, int flags)
388 struct tls_context *tls_ctx = tls_get_ctx(sk);
389 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
390 struct tls_rec *rec, *tmp;
391 struct sk_msg *msg_en;
392 int tx_flags, rc = 0;
394 if (tls_is_partially_sent_record(tls_ctx)) {
395 rec = list_first_entry(&ctx->tx_list,
396 struct tls_rec, list);
399 tx_flags = rec->tx_flags;
403 rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
407 /* Full record has been transmitted.
408 * Remove the head of tx_list
410 list_del(&rec->list);
411 sk_msg_free(sk, &rec->msg_plaintext);
415 /* Tx all ready records */
416 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
417 if (READ_ONCE(rec->tx_ready)) {
419 tx_flags = rec->tx_flags;
423 msg_en = &rec->msg_encrypted;
424 rc = tls_push_sg(sk, tls_ctx,
425 &msg_en->sg.data[msg_en->sg.curr],
430 list_del(&rec->list);
431 sk_msg_free(sk, &rec->msg_plaintext);
439 if (rc < 0 && rc != -EAGAIN)
440 tls_err_abort(sk, -EBADMSG);
445 static void tls_encrypt_done(struct crypto_async_request *req, int err)
447 struct aead_request *aead_req = (struct aead_request *)req;
448 struct sock *sk = req->data;
449 struct tls_context *tls_ctx = tls_get_ctx(sk);
450 struct tls_prot_info *prot = &tls_ctx->prot_info;
451 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
452 struct scatterlist *sge;
453 struct sk_msg *msg_en;
458 rec = container_of(aead_req, struct tls_rec, aead_req);
459 msg_en = &rec->msg_encrypted;
461 sge = sk_msg_elem(msg_en, msg_en->sg.curr);
462 sge->offset -= prot->prepend_size;
463 sge->length += prot->prepend_size;
465 /* Check if error is previously set on socket */
466 if (err || sk->sk_err) {
469 /* If err is already set on socket, return the same code */
471 ctx->async_wait.err = -sk->sk_err;
473 ctx->async_wait.err = err;
474 tls_err_abort(sk, err);
479 struct tls_rec *first_rec;
481 /* Mark the record as ready for transmission */
482 smp_store_mb(rec->tx_ready, true);
484 /* If received record is at head of tx_list, schedule tx */
485 first_rec = list_first_entry(&ctx->tx_list,
486 struct tls_rec, list);
487 if (rec == first_rec)
491 spin_lock_bh(&ctx->encrypt_compl_lock);
492 pending = atomic_dec_return(&ctx->encrypt_pending);
494 if (!pending && ctx->async_notify)
495 complete(&ctx->async_wait.completion);
496 spin_unlock_bh(&ctx->encrypt_compl_lock);
501 /* Schedule the transmission */
502 if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
503 schedule_delayed_work(&ctx->tx_work.work, 1);
506 static int tls_do_encryption(struct sock *sk,
507 struct tls_context *tls_ctx,
508 struct tls_sw_context_tx *ctx,
509 struct aead_request *aead_req,
510 size_t data_len, u32 start)
512 struct tls_prot_info *prot = &tls_ctx->prot_info;
513 struct tls_rec *rec = ctx->open_rec;
514 struct sk_msg *msg_en = &rec->msg_encrypted;
515 struct scatterlist *sge = sk_msg_elem(msg_en, start);
516 int rc, iv_offset = 0;
518 /* For CCM based ciphers, first byte of IV is a constant */
519 switch (prot->cipher_type) {
520 case TLS_CIPHER_AES_CCM_128:
521 rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE;
524 case TLS_CIPHER_SM4_CCM:
525 rec->iv_data[0] = TLS_SM4_CCM_IV_B0_BYTE;
530 memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
531 prot->iv_size + prot->salt_size);
533 tls_xor_iv_with_seq(prot, rec->iv_data + iv_offset,
534 tls_ctx->tx.rec_seq);
536 sge->offset += prot->prepend_size;
537 sge->length -= prot->prepend_size;
539 msg_en->sg.curr = start;
541 aead_request_set_tfm(aead_req, ctx->aead_send);
542 aead_request_set_ad(aead_req, prot->aad_size);
543 aead_request_set_crypt(aead_req, rec->sg_aead_in,
545 data_len, rec->iv_data);
547 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
548 tls_encrypt_done, sk);
550 /* Add the record in tx_list */
551 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
552 atomic_inc(&ctx->encrypt_pending);
554 rc = crypto_aead_encrypt(aead_req);
555 if (!rc || rc != -EINPROGRESS) {
556 atomic_dec(&ctx->encrypt_pending);
557 sge->offset -= prot->prepend_size;
558 sge->length += prot->prepend_size;
562 WRITE_ONCE(rec->tx_ready, true);
563 } else if (rc != -EINPROGRESS) {
564 list_del(&rec->list);
568 /* Unhook the record from context if encryption is not failure */
569 ctx->open_rec = NULL;
570 tls_advance_record_sn(sk, prot, &tls_ctx->tx);
574 static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
575 struct tls_rec **to, struct sk_msg *msg_opl,
576 struct sk_msg *msg_oen, u32 split_point,
577 u32 tx_overhead_size, u32 *orig_end)
579 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
580 struct scatterlist *sge, *osge, *nsge;
581 u32 orig_size = msg_opl->sg.size;
582 struct scatterlist tmp = { };
583 struct sk_msg *msg_npl;
587 new = tls_get_rec(sk);
590 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
591 tx_overhead_size, 0);
593 tls_free_rec(sk, new);
597 *orig_end = msg_opl->sg.end;
598 i = msg_opl->sg.start;
599 sge = sk_msg_elem(msg_opl, i);
600 while (apply && sge->length) {
601 if (sge->length > apply) {
602 u32 len = sge->length - apply;
604 get_page(sg_page(sge));
605 sg_set_page(&tmp, sg_page(sge), len,
606 sge->offset + apply);
611 apply -= sge->length;
612 bytes += sge->length;
615 sk_msg_iter_var_next(i);
616 if (i == msg_opl->sg.end)
618 sge = sk_msg_elem(msg_opl, i);
622 msg_opl->sg.curr = i;
623 msg_opl->sg.copybreak = 0;
624 msg_opl->apply_bytes = 0;
625 msg_opl->sg.size = bytes;
627 msg_npl = &new->msg_plaintext;
628 msg_npl->apply_bytes = apply;
629 msg_npl->sg.size = orig_size - bytes;
631 j = msg_npl->sg.start;
632 nsge = sk_msg_elem(msg_npl, j);
634 memcpy(nsge, &tmp, sizeof(*nsge));
635 sk_msg_iter_var_next(j);
636 nsge = sk_msg_elem(msg_npl, j);
639 osge = sk_msg_elem(msg_opl, i);
640 while (osge->length) {
641 memcpy(nsge, osge, sizeof(*nsge));
643 sk_msg_iter_var_next(i);
644 sk_msg_iter_var_next(j);
647 osge = sk_msg_elem(msg_opl, i);
648 nsge = sk_msg_elem(msg_npl, j);
652 msg_npl->sg.curr = j;
653 msg_npl->sg.copybreak = 0;
659 static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
660 struct tls_rec *from, u32 orig_end)
662 struct sk_msg *msg_npl = &from->msg_plaintext;
663 struct sk_msg *msg_opl = &to->msg_plaintext;
664 struct scatterlist *osge, *nsge;
668 sk_msg_iter_var_prev(i);
669 j = msg_npl->sg.start;
671 osge = sk_msg_elem(msg_opl, i);
672 nsge = sk_msg_elem(msg_npl, j);
674 if (sg_page(osge) == sg_page(nsge) &&
675 osge->offset + osge->length == nsge->offset) {
676 osge->length += nsge->length;
677 put_page(sg_page(nsge));
680 msg_opl->sg.end = orig_end;
681 msg_opl->sg.curr = orig_end;
682 msg_opl->sg.copybreak = 0;
683 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
684 msg_opl->sg.size += msg_npl->sg.size;
686 sk_msg_free(sk, &to->msg_encrypted);
687 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
692 static int tls_push_record(struct sock *sk, int flags,
693 unsigned char record_type)
695 struct tls_context *tls_ctx = tls_get_ctx(sk);
696 struct tls_prot_info *prot = &tls_ctx->prot_info;
697 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
698 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
699 u32 i, split_point, orig_end;
700 struct sk_msg *msg_pl, *msg_en;
701 struct aead_request *req;
708 msg_pl = &rec->msg_plaintext;
709 msg_en = &rec->msg_encrypted;
711 split_point = msg_pl->apply_bytes;
712 split = split_point && split_point < msg_pl->sg.size;
713 if (unlikely((!split &&
715 prot->overhead_size > msg_en->sg.size) ||
718 prot->overhead_size > msg_en->sg.size))) {
720 split_point = msg_en->sg.size;
723 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
724 split_point, prot->overhead_size,
728 /* This can happen if above tls_split_open_record allocates
729 * a single large encryption buffer instead of two smaller
730 * ones. In this case adjust pointers and continue without
733 if (!msg_pl->sg.size) {
734 tls_merge_open_record(sk, rec, tmp, orig_end);
735 msg_pl = &rec->msg_plaintext;
736 msg_en = &rec->msg_encrypted;
739 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
740 prot->overhead_size);
743 rec->tx_flags = flags;
744 req = &rec->aead_req;
747 sk_msg_iter_var_prev(i);
749 rec->content_type = record_type;
750 if (prot->version == TLS_1_3_VERSION) {
751 /* Add content type to end of message. No padding added */
752 sg_set_buf(&rec->sg_content_type, &rec->content_type, 1);
753 sg_mark_end(&rec->sg_content_type);
754 sg_chain(msg_pl->sg.data, msg_pl->sg.end + 1,
755 &rec->sg_content_type);
757 sg_mark_end(sk_msg_elem(msg_pl, i));
760 if (msg_pl->sg.end < msg_pl->sg.start) {
761 sg_chain(&msg_pl->sg.data[msg_pl->sg.start],
762 MAX_SKB_FRAGS - msg_pl->sg.start + 1,
766 i = msg_pl->sg.start;
767 sg_chain(rec->sg_aead_in, 2, &msg_pl->sg.data[i]);
770 sk_msg_iter_var_prev(i);
771 sg_mark_end(sk_msg_elem(msg_en, i));
773 i = msg_en->sg.start;
774 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
776 tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size,
777 tls_ctx->tx.rec_seq, record_type, prot);
779 tls_fill_prepend(tls_ctx,
780 page_address(sg_page(&msg_en->sg.data[i])) +
781 msg_en->sg.data[i].offset,
782 msg_pl->sg.size + prot->tail_size,
785 tls_ctx->pending_open_record_frags = false;
787 rc = tls_do_encryption(sk, tls_ctx, ctx, req,
788 msg_pl->sg.size + prot->tail_size, i);
790 if (rc != -EINPROGRESS) {
791 tls_err_abort(sk, -EBADMSG);
793 tls_ctx->pending_open_record_frags = true;
794 tls_merge_open_record(sk, rec, tmp, orig_end);
797 ctx->async_capable = 1;
800 msg_pl = &tmp->msg_plaintext;
801 msg_en = &tmp->msg_encrypted;
802 sk_msg_trim(sk, msg_en, msg_pl->sg.size + prot->overhead_size);
803 tls_ctx->pending_open_record_frags = true;
807 return tls_tx_records(sk, flags);
810 static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
811 bool full_record, u8 record_type,
812 ssize_t *copied, int flags)
814 struct tls_context *tls_ctx = tls_get_ctx(sk);
815 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
816 struct sk_msg msg_redir = { };
817 struct sk_psock *psock;
818 struct sock *sk_redir;
824 policy = !(flags & MSG_SENDPAGE_NOPOLICY);
825 psock = sk_psock_get(sk);
826 if (!psock || !policy) {
827 err = tls_push_record(sk, flags, record_type);
828 if (err && sk->sk_err == EBADMSG) {
829 *copied -= sk_msg_free(sk, msg);
830 tls_free_open_rec(sk);
834 sk_psock_put(sk, psock);
838 enospc = sk_msg_full(msg);
839 if (psock->eval == __SK_NONE) {
840 delta = msg->sg.size;
841 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
842 delta -= msg->sg.size;
844 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
845 !enospc && !full_record) {
851 if (msg->apply_bytes && msg->apply_bytes < send)
852 send = msg->apply_bytes;
854 switch (psock->eval) {
856 err = tls_push_record(sk, flags, record_type);
857 if (err && sk->sk_err == EBADMSG) {
858 *copied -= sk_msg_free(sk, msg);
859 tls_free_open_rec(sk);
865 sk_redir = psock->sk_redir;
866 memcpy(&msg_redir, msg, sizeof(*msg));
867 if (msg->apply_bytes < send)
868 msg->apply_bytes = 0;
870 msg->apply_bytes -= send;
871 sk_msg_return_zero(sk, msg, send);
872 msg->sg.size -= send;
874 err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
877 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
880 if (msg->sg.size == 0)
881 tls_free_open_rec(sk);
885 sk_msg_free_partial(sk, msg, send);
886 if (msg->apply_bytes < send)
887 msg->apply_bytes = 0;
889 msg->apply_bytes -= send;
890 if (msg->sg.size == 0)
891 tls_free_open_rec(sk);
892 *copied -= (send + delta);
897 bool reset_eval = !ctx->open_rec;
901 msg = &rec->msg_plaintext;
902 if (!msg->apply_bytes)
906 psock->eval = __SK_NONE;
907 if (psock->sk_redir) {
908 sock_put(psock->sk_redir);
909 psock->sk_redir = NULL;
916 sk_psock_put(sk, psock);
920 static int tls_sw_push_pending_record(struct sock *sk, int flags)
922 struct tls_context *tls_ctx = tls_get_ctx(sk);
923 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
924 struct tls_rec *rec = ctx->open_rec;
925 struct sk_msg *msg_pl;
931 msg_pl = &rec->msg_plaintext;
932 copied = msg_pl->sg.size;
936 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
940 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
942 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
943 struct tls_context *tls_ctx = tls_get_ctx(sk);
944 struct tls_prot_info *prot = &tls_ctx->prot_info;
945 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
946 bool async_capable = ctx->async_capable;
947 unsigned char record_type = TLS_RECORD_TYPE_DATA;
948 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
949 bool eor = !(msg->msg_flags & MSG_MORE);
952 struct sk_msg *msg_pl, *msg_en;
963 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
967 mutex_lock(&tls_ctx->tx_lock);
970 if (unlikely(msg->msg_controllen)) {
971 ret = tls_process_cmsg(sk, msg, &record_type);
973 if (ret == -EINPROGRESS)
975 else if (ret != -EAGAIN)
980 while (msg_data_left(msg)) {
989 rec = ctx->open_rec = tls_get_rec(sk);
995 msg_pl = &rec->msg_plaintext;
996 msg_en = &rec->msg_encrypted;
998 orig_size = msg_pl->sg.size;
1000 try_to_copy = msg_data_left(msg);
1001 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
1002 if (try_to_copy >= record_room) {
1003 try_to_copy = record_room;
1007 required_size = msg_pl->sg.size + try_to_copy +
1008 prot->overhead_size;
1010 if (!sk_stream_memory_free(sk))
1011 goto wait_for_sndbuf;
1014 ret = tls_alloc_encrypted_msg(sk, required_size);
1017 goto wait_for_memory;
1019 /* Adjust try_to_copy according to the amount that was
1020 * actually allocated. The difference is due
1021 * to max sg elements limit
1023 try_to_copy -= required_size - msg_en->sg.size;
1027 if (!is_kvec && (full_record || eor) && !async_capable) {
1028 u32 first = msg_pl->sg.end;
1030 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
1031 msg_pl, try_to_copy);
1033 goto fallback_to_reg_send;
1036 copied += try_to_copy;
1038 sk_msg_sg_copy_set(msg_pl, first);
1039 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1040 record_type, &copied,
1043 if (ret == -EINPROGRESS)
1045 else if (ret == -ENOMEM)
1046 goto wait_for_memory;
1047 else if (ctx->open_rec && ret == -ENOSPC)
1049 else if (ret != -EAGAIN)
1054 copied -= try_to_copy;
1055 sk_msg_sg_copy_clear(msg_pl, first);
1056 iov_iter_revert(&msg->msg_iter,
1057 msg_pl->sg.size - orig_size);
1058 fallback_to_reg_send:
1059 sk_msg_trim(sk, msg_pl, orig_size);
1062 required_size = msg_pl->sg.size + try_to_copy;
1064 ret = tls_clone_plaintext_msg(sk, required_size);
1069 /* Adjust try_to_copy according to the amount that was
1070 * actually allocated. The difference is due
1071 * to max sg elements limit
1073 try_to_copy -= required_size - msg_pl->sg.size;
1075 sk_msg_trim(sk, msg_en,
1076 msg_pl->sg.size + prot->overhead_size);
1080 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
1081 msg_pl, try_to_copy);
1086 /* Open records defined only if successfully copied, otherwise
1087 * we would trim the sg but not reset the open record frags.
1089 tls_ctx->pending_open_record_frags = true;
1090 copied += try_to_copy;
1091 if (full_record || eor) {
1092 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1093 record_type, &copied,
1096 if (ret == -EINPROGRESS)
1098 else if (ret == -ENOMEM)
1099 goto wait_for_memory;
1100 else if (ret != -EAGAIN) {
1111 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1113 ret = sk_stream_wait_memory(sk, &timeo);
1117 tls_trim_both_msgs(sk, orig_size);
1121 if (ctx->open_rec && msg_en->sg.size < required_size)
1122 goto alloc_encrypted;
1127 } else if (num_zc) {
1128 /* Wait for pending encryptions to get completed */
1129 spin_lock_bh(&ctx->encrypt_compl_lock);
1130 ctx->async_notify = true;
1132 pending = atomic_read(&ctx->encrypt_pending);
1133 spin_unlock_bh(&ctx->encrypt_compl_lock);
1135 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1137 reinit_completion(&ctx->async_wait.completion);
1139 /* There can be no concurrent accesses, since we have no
1140 * pending encrypt operations
1142 WRITE_ONCE(ctx->async_notify, false);
1144 if (ctx->async_wait.err) {
1145 ret = ctx->async_wait.err;
1150 /* Transmit if any encryptions have completed */
1151 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1152 cancel_delayed_work(&ctx->tx_work.work);
1153 tls_tx_records(sk, msg->msg_flags);
1157 ret = sk_stream_error(sk, msg->msg_flags, ret);
1160 mutex_unlock(&tls_ctx->tx_lock);
1161 return copied > 0 ? copied : ret;
1164 static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
1165 int offset, size_t size, int flags)
1167 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1168 struct tls_context *tls_ctx = tls_get_ctx(sk);
1169 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1170 struct tls_prot_info *prot = &tls_ctx->prot_info;
1171 unsigned char record_type = TLS_RECORD_TYPE_DATA;
1172 struct sk_msg *msg_pl;
1173 struct tls_rec *rec;
1181 eor = !(flags & MSG_SENDPAGE_NOTLAST);
1182 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1184 /* Call the sk_stream functions to manage the sndbuf mem. */
1186 size_t copy, required_size;
1194 rec = ctx->open_rec;
1196 rec = ctx->open_rec = tls_get_rec(sk);
1202 msg_pl = &rec->msg_plaintext;
1204 full_record = false;
1205 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
1207 if (copy >= record_room) {
1212 required_size = msg_pl->sg.size + copy + prot->overhead_size;
1214 if (!sk_stream_memory_free(sk))
1215 goto wait_for_sndbuf;
1217 ret = tls_alloc_encrypted_msg(sk, required_size);
1220 goto wait_for_memory;
1222 /* Adjust copy according to the amount that was
1223 * actually allocated. The difference is due
1224 * to max sg elements limit
1226 copy -= required_size - msg_pl->sg.size;
1230 sk_msg_page_add(msg_pl, page, copy, offset);
1231 sk_mem_charge(sk, copy);
1237 tls_ctx->pending_open_record_frags = true;
1238 if (full_record || eor || sk_msg_full(msg_pl)) {
1239 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1240 record_type, &copied, flags);
1242 if (ret == -EINPROGRESS)
1244 else if (ret == -ENOMEM)
1245 goto wait_for_memory;
1246 else if (ret != -EAGAIN) {
1255 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1257 ret = sk_stream_wait_memory(sk, &timeo);
1260 tls_trim_both_msgs(sk, msg_pl->sg.size);
1269 /* Transmit if any encryptions have completed */
1270 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1271 cancel_delayed_work(&ctx->tx_work.work);
1272 tls_tx_records(sk, flags);
1276 ret = sk_stream_error(sk, flags, ret);
1277 return copied > 0 ? copied : ret;
1280 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
1281 int offset, size_t size, int flags)
1283 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1284 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY |
1285 MSG_NO_SHARED_FRAGS))
1288 return tls_sw_do_sendpage(sk, page, offset, size, flags);
1291 int tls_sw_sendpage(struct sock *sk, struct page *page,
1292 int offset, size_t size, int flags)
1294 struct tls_context *tls_ctx = tls_get_ctx(sk);
1297 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1298 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1301 mutex_lock(&tls_ctx->tx_lock);
1303 ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
1305 mutex_unlock(&tls_ctx->tx_lock);
1310 tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
1313 struct tls_context *tls_ctx = tls_get_ctx(sk);
1314 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1315 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1317 while (!ctx->recv_pkt) {
1318 if (!sk_psock_queue_empty(psock))
1322 return sock_error(sk);
1324 if (!skb_queue_empty(&sk->sk_receive_queue)) {
1325 __strp_unpause(&ctx->strp);
1330 if (sk->sk_shutdown & RCV_SHUTDOWN)
1333 if (sock_flag(sk, SOCK_DONE))
1336 if (nonblock || !timeo)
1339 add_wait_queue(sk_sleep(sk), &wait);
1340 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1341 sk_wait_event(sk, &timeo,
1342 ctx->recv_pkt || !sk_psock_queue_empty(psock),
1344 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1345 remove_wait_queue(sk_sleep(sk), &wait);
1347 /* Handle signals */
1348 if (signal_pending(current))
1349 return sock_intr_errno(timeo);
1355 static int tls_setup_from_iter(struct iov_iter *from,
1356 int length, int *pages_used,
1357 struct scatterlist *to,
1360 int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1361 struct page *pages[MAX_SKB_FRAGS];
1362 unsigned int size = 0;
1363 ssize_t copied, use;
1366 while (length > 0) {
1368 maxpages = to_max_pages - num_elem;
1369 if (maxpages == 0) {
1373 copied = iov_iter_get_pages(from, pages,
1381 iov_iter_advance(from, copied);
1386 use = min_t(int, copied, PAGE_SIZE - offset);
1388 sg_set_page(&to[num_elem],
1389 pages[i], use, offset);
1390 sg_unmark_end(&to[num_elem]);
1391 /* We do not uncharge memory from this API */
1400 /* Mark the end in the last sg entry if newly added */
1401 if (num_elem > *pages_used)
1402 sg_mark_end(&to[num_elem - 1]);
1405 iov_iter_revert(from, size);
1406 *pages_used = num_elem;
1413 * tls_decrypt_sg() and tls_decrypt_device() are decrypt handlers.
1414 * They must transform the darg in/out argument are as follows:
1416 * -------------------------------------------------------------------
1417 * zc | Zero-copy decrypt allowed | Zero-copy performed
1418 * async | Async decrypt allowed | Async crypto used / in progress
1419 * skb | * | Output skb
1422 /* This function decrypts the input skb into either out_iov or in out_sg
1423 * or in skb buffers itself. The input parameter 'darg->zc' indicates if
1424 * zero-copy mode needs to be tried or not. With zero-copy mode, either
1425 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
1426 * NULL, then the decryption happens inside skb buffers itself, i.e.
1427 * zero-copy gets disabled and 'darg->zc' is updated.
1429 static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
1430 struct scatterlist *out_sg,
1431 struct tls_decrypt_arg *darg)
1433 struct tls_context *tls_ctx = tls_get_ctx(sk);
1434 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1435 struct tls_prot_info *prot = &tls_ctx->prot_info;
1436 int n_sgin, n_sgout, aead_size, err, pages = 0;
1437 struct sk_buff *skb = tls_strp_msg(ctx);
1438 struct strp_msg *rxm = strp_msg(skb);
1439 struct tls_msg *tlm = tls_msg(skb);
1440 struct aead_request *aead_req;
1441 struct sk_buff *unused;
1442 struct scatterlist *sgin = NULL;
1443 struct scatterlist *sgout = NULL;
1444 const int data_len = rxm->full_len - prot->overhead_size;
1445 int tail_pages = !!prot->tail_size;
1446 struct tls_decrypt_ctx *dctx;
1450 if (darg->zc && (out_iov || out_sg)) {
1452 n_sgout = 1 + tail_pages +
1453 iov_iter_npages_cap(out_iov, INT_MAX, data_len);
1455 n_sgout = sg_nents(out_sg);
1456 n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size,
1457 rxm->full_len - prot->prepend_size);
1461 n_sgin = skb_cow_data(skb, 0, &unused);
1467 /* Increment to accommodate AAD */
1468 n_sgin = n_sgin + 1;
1470 /* Allocate a single block of memory which contains
1471 * aead_req || tls_decrypt_ctx.
1472 * Both structs are variable length.
1474 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
1475 mem = kmalloc(aead_size + struct_size(dctx, sg, n_sgin + n_sgout),
1480 /* Segment the allocated memory */
1481 aead_req = (struct aead_request *)mem;
1482 dctx = (struct tls_decrypt_ctx *)(mem + aead_size);
1483 sgin = &dctx->sg[0];
1484 sgout = &dctx->sg[n_sgin];
1486 /* For CCM based ciphers, first byte of nonce+iv is a constant */
1487 switch (prot->cipher_type) {
1488 case TLS_CIPHER_AES_CCM_128:
1489 dctx->iv[0] = TLS_AES_CCM_IV_B0_BYTE;
1492 case TLS_CIPHER_SM4_CCM:
1493 dctx->iv[0] = TLS_SM4_CCM_IV_B0_BYTE;
1499 if (prot->version == TLS_1_3_VERSION ||
1500 prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
1501 memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv,
1502 prot->iv_size + prot->salt_size);
1504 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1505 &dctx->iv[iv_offset] + prot->salt_size,
1509 memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv, prot->salt_size);
1511 tls_xor_iv_with_seq(prot, &dctx->iv[iv_offset], tls_ctx->rx.rec_seq);
1514 tls_make_aad(dctx->aad, rxm->full_len - prot->overhead_size +
1516 tls_ctx->rx.rec_seq, tlm->control, prot);
1519 sg_init_table(sgin, n_sgin);
1520 sg_set_buf(&sgin[0], dctx->aad, prot->aad_size);
1521 err = skb_to_sgvec(skb, &sgin[1],
1522 rxm->offset + prot->prepend_size,
1523 rxm->full_len - prot->prepend_size);
1529 sg_init_table(sgout, n_sgout);
1530 sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
1532 err = tls_setup_from_iter(out_iov, data_len,
1534 (n_sgout - 1 - tail_pages));
1536 goto fallback_to_reg_recv;
1538 if (prot->tail_size) {
1539 sg_unmark_end(&sgout[pages]);
1540 sg_set_buf(&sgout[pages + 1], &dctx->tail,
1542 sg_mark_end(&sgout[pages + 1]);
1544 } else if (out_sg) {
1545 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
1547 goto fallback_to_reg_recv;
1550 fallback_to_reg_recv:
1556 /* Prepare and submit AEAD request */
1557 err = tls_do_decryption(sk, skb, sgin, sgout, dctx->iv,
1558 data_len + prot->tail_size, aead_req, darg);
1560 goto exit_free_pages;
1562 darg->skb = tls_strp_msg(ctx);
1566 if (prot->tail_size)
1567 darg->tail = dctx->tail;
1570 /* Release the pages in case iov was mapped to pages */
1571 for (; pages > 0; pages--)
1572 put_page(sg_page(&sgout[pages]));
1579 tls_decrypt_device(struct sock *sk, struct tls_context *tls_ctx,
1580 struct tls_decrypt_arg *darg)
1582 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1585 if (tls_ctx->rx_conf != TLS_HW)
1588 err = tls_device_decrypted(sk, tls_ctx);
1593 darg->async = false;
1594 darg->skb = tls_strp_msg(ctx);
1595 ctx->recv_pkt = NULL;
1599 static int tls_rx_one_record(struct sock *sk, struct iov_iter *dest,
1600 struct tls_decrypt_arg *darg)
1602 struct tls_context *tls_ctx = tls_get_ctx(sk);
1603 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1604 struct tls_prot_info *prot = &tls_ctx->prot_info;
1605 struct strp_msg *rxm;
1608 err = tls_decrypt_device(sk, tls_ctx, darg);
1614 err = tls_decrypt_sg(sk, dest, NULL, darg);
1616 if (err == -EBADMSG)
1617 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
1621 if (darg->skb == ctx->recv_pkt)
1622 ctx->recv_pkt = NULL;
1625 /* If opportunistic TLS 1.3 ZC failed retry without ZC */
1626 if (unlikely(darg->zc && prot->version == TLS_1_3_VERSION &&
1627 darg->tail != TLS_RECORD_TYPE_DATA)) {
1630 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXNOPADVIOL);
1631 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTRETRY);
1632 return tls_rx_one_record(sk, dest, darg);
1635 if (darg->skb == ctx->recv_pkt)
1636 ctx->recv_pkt = NULL;
1639 pad = tls_padding_length(prot, darg->skb, darg);
1641 consume_skb(darg->skb);
1645 rxm = strp_msg(darg->skb);
1646 rxm->full_len -= pad;
1647 rxm->offset += prot->prepend_size;
1648 rxm->full_len -= prot->overhead_size;
1650 tls_advance_record_sn(sk, prot, &tls_ctx->rx);
1655 int decrypt_skb(struct sock *sk, struct scatterlist *sgout)
1657 struct tls_decrypt_arg darg = { .zc = true, };
1659 return tls_decrypt_sg(sk, NULL, sgout, &darg);
1662 static int tls_record_content_type(struct msghdr *msg, struct tls_msg *tlm,
1668 *control = tlm->control;
1672 err = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1673 sizeof(*control), control);
1674 if (*control != TLS_RECORD_TYPE_DATA) {
1675 if (err || msg->msg_flags & MSG_CTRUNC)
1678 } else if (*control != tlm->control) {
1685 static void tls_rx_rec_done(struct tls_sw_context_rx *ctx)
1687 consume_skb(ctx->recv_pkt);
1688 ctx->recv_pkt = NULL;
1689 __strp_unpause(&ctx->strp);
1692 /* This function traverses the rx_list in tls receive context to copies the
1693 * decrypted records into the buffer provided by caller zero copy is not
1694 * true. Further, the records are removed from the rx_list if it is not a peek
1695 * case and the record has been consumed completely.
1697 static int process_rx_list(struct tls_sw_context_rx *ctx,
1705 struct sk_buff *skb = skb_peek(&ctx->rx_list);
1706 struct tls_msg *tlm;
1710 while (skip && skb) {
1711 struct strp_msg *rxm = strp_msg(skb);
1714 err = tls_record_content_type(msg, tlm, control);
1718 if (skip < rxm->full_len)
1721 skip = skip - rxm->full_len;
1722 skb = skb_peek_next(skb, &ctx->rx_list);
1725 while (len && skb) {
1726 struct sk_buff *next_skb;
1727 struct strp_msg *rxm = strp_msg(skb);
1728 int chunk = min_t(unsigned int, rxm->full_len - skip, len);
1732 err = tls_record_content_type(msg, tlm, control);
1736 if (!zc || (rxm->full_len - skip) > len) {
1737 err = skb_copy_datagram_msg(skb, rxm->offset + skip,
1744 copied = copied + chunk;
1746 /* Consume the data from record if it is non-peek case*/
1748 rxm->offset = rxm->offset + chunk;
1749 rxm->full_len = rxm->full_len - chunk;
1751 /* Return if there is unconsumed data in the record */
1752 if (rxm->full_len - skip)
1756 /* The remaining skip-bytes must lie in 1st record in rx_list.
1757 * So from the 2nd record, 'skip' should be 0.
1762 msg->msg_flags |= MSG_EOR;
1764 next_skb = skb_peek_next(skb, &ctx->rx_list);
1767 __skb_unlink(skb, &ctx->rx_list);
1776 return copied ? : err;
1780 tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot,
1781 size_t len_left, size_t decrypted, ssize_t done,
1786 if (len_left <= decrypted)
1789 max_rec = prot->overhead_size - prot->tail_size + TLS_MAX_PAYLOAD_SIZE;
1790 if (done - *flushed_at < SZ_128K && tcp_inq(sk) > max_rec)
1794 sk_flush_backlog(sk);
1797 static long tls_rx_reader_lock(struct sock *sk, struct tls_sw_context_rx *ctx,
1804 timeo = sock_rcvtimeo(sk, nonblock);
1806 while (unlikely(ctx->reader_present)) {
1807 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1809 ctx->reader_contended = 1;
1811 add_wait_queue(&ctx->wq, &wait);
1812 sk_wait_event(sk, &timeo,
1813 !READ_ONCE(ctx->reader_present), &wait);
1814 remove_wait_queue(&ctx->wq, &wait);
1818 if (signal_pending(current))
1819 return sock_intr_errno(timeo);
1822 WRITE_ONCE(ctx->reader_present, 1);
1827 static void tls_rx_reader_unlock(struct sock *sk, struct tls_sw_context_rx *ctx)
1829 if (unlikely(ctx->reader_contended)) {
1830 if (wq_has_sleeper(&ctx->wq))
1833 ctx->reader_contended = 0;
1835 WARN_ON_ONCE(!ctx->reader_present);
1838 WRITE_ONCE(ctx->reader_present, 0);
1842 int tls_sw_recvmsg(struct sock *sk,
1848 struct tls_context *tls_ctx = tls_get_ctx(sk);
1849 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1850 struct tls_prot_info *prot = &tls_ctx->prot_info;
1851 struct sk_psock *psock;
1852 unsigned char control = 0;
1853 ssize_t decrypted = 0;
1854 size_t flushed_at = 0;
1855 struct strp_msg *rxm;
1856 struct tls_msg *tlm;
1857 struct sk_buff *skb;
1860 int target, err = 0;
1862 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
1863 bool is_peek = flags & MSG_PEEK;
1864 bool bpf_strp_enabled;
1867 if (unlikely(flags & MSG_ERRQUEUE))
1868 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1870 psock = sk_psock_get(sk);
1871 timeo = tls_rx_reader_lock(sk, ctx, flags & MSG_DONTWAIT);
1874 bpf_strp_enabled = sk_psock_strp_enabled(psock);
1876 /* If crypto failed the connection is broken */
1877 err = ctx->async_wait.err;
1881 /* Process pending decrypted records. It must be non-zero-copy */
1882 err = process_rx_list(ctx, msg, &control, 0, len, false, is_peek);
1890 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1893 zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek &&
1896 while (len && (decrypted + copied < target || ctx->recv_pkt)) {
1897 struct tls_decrypt_arg darg;
1898 int to_decrypt, chunk;
1900 err = tls_rx_rec_wait(sk, psock, flags & MSG_DONTWAIT, timeo);
1903 chunk = sk_msg_recvmsg(sk, psock, msg, len,
1914 memset(&darg.inargs, 0, sizeof(darg.inargs));
1916 rxm = strp_msg(ctx->recv_pkt);
1917 tlm = tls_msg(ctx->recv_pkt);
1919 to_decrypt = rxm->full_len - prot->overhead_size;
1921 if (zc_capable && to_decrypt <= len &&
1922 tlm->control == TLS_RECORD_TYPE_DATA)
1925 /* Do not use async mode if record is non-data */
1926 if (tlm->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled)
1927 darg.async = ctx->async_capable;
1931 err = tls_rx_one_record(sk, &msg->msg_iter, &darg);
1933 tls_err_abort(sk, -EBADMSG);
1938 rxm = strp_msg(skb);
1941 async |= darg.async;
1943 /* If the type of records being processed is not known yet,
1944 * set it to record type just dequeued. If it is already known,
1945 * but does not match the record type just dequeued, go to end.
1946 * We always get record type here since for tls1.2, record type
1947 * is known just after record is dequeued from stream parser.
1948 * For tls1.3, we disable async.
1950 err = tls_record_content_type(msg, tlm, &control);
1952 tls_rx_rec_done(ctx);
1954 __skb_queue_tail(&ctx->rx_list, skb);
1958 /* periodically flush backlog, and feed strparser */
1959 tls_read_flush_backlog(sk, prot, len, to_decrypt,
1960 decrypted + copied, &flushed_at);
1962 /* TLS 1.3 may have updated the length by more than overhead */
1963 chunk = rxm->full_len;
1964 tls_rx_rec_done(ctx);
1967 /* TLS 1.2-only, to_decrypt must be text length */
1968 chunk = min_t(int, to_decrypt, len);
1972 __skb_queue_tail(&ctx->rx_list, skb);
1977 bool partially_consumed = chunk > len;
1979 if (bpf_strp_enabled) {
1980 err = sk_psock_tls_strp_read(psock, skb);
1981 if (err != __SK_PASS) {
1982 rxm->offset = rxm->offset + rxm->full_len;
1984 if (err == __SK_DROP)
1990 if (partially_consumed)
1993 err = skb_copy_datagram_msg(skb, rxm->offset,
1996 goto put_on_rx_list_err;
1999 goto put_on_rx_list;
2001 if (partially_consumed) {
2002 rxm->offset += chunk;
2003 rxm->full_len -= chunk;
2004 goto put_on_rx_list;
2013 /* Return full control message to userspace before trying
2014 * to parse another message type
2016 msg->msg_flags |= MSG_EOR;
2017 if (control != TLS_RECORD_TYPE_DATA)
2025 /* Wait for all previously submitted records to be decrypted */
2026 spin_lock_bh(&ctx->decrypt_compl_lock);
2027 reinit_completion(&ctx->async_wait.completion);
2028 pending = atomic_read(&ctx->decrypt_pending);
2029 spin_unlock_bh(&ctx->decrypt_compl_lock);
2031 ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
2033 if (err >= 0 || err == -EINPROGRESS)
2040 /* Drain records from the rx_list & copy if required */
2041 if (is_peek || is_kvec)
2042 err = process_rx_list(ctx, msg, &control, copied,
2043 decrypted, false, is_peek);
2045 err = process_rx_list(ctx, msg, &control, 0,
2046 decrypted, true, is_peek);
2047 decrypted = max(err, 0);
2050 copied += decrypted;
2053 tls_rx_reader_unlock(sk, ctx);
2055 sk_psock_put(sk, psock);
2056 return copied ? : err;
2059 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
2060 struct pipe_inode_info *pipe,
2061 size_t len, unsigned int flags)
2063 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
2064 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2065 struct strp_msg *rxm = NULL;
2066 struct sock *sk = sock->sk;
2067 struct tls_msg *tlm;
2068 struct sk_buff *skb;
2074 timeo = tls_rx_reader_lock(sk, ctx, flags & SPLICE_F_NONBLOCK);
2078 if (!skb_queue_empty(&ctx->rx_list)) {
2079 skb = __skb_dequeue(&ctx->rx_list);
2081 struct tls_decrypt_arg darg;
2083 err = tls_rx_rec_wait(sk, NULL, flags & SPLICE_F_NONBLOCK,
2086 goto splice_read_end;
2088 memset(&darg.inargs, 0, sizeof(darg.inargs));
2090 err = tls_rx_one_record(sk, NULL, &darg);
2092 tls_err_abort(sk, -EBADMSG);
2093 goto splice_read_end;
2096 tls_rx_rec_done(ctx);
2100 rxm = strp_msg(skb);
2103 /* splice does not support reading control messages */
2104 if (tlm->control != TLS_RECORD_TYPE_DATA) {
2106 goto splice_requeue;
2109 chunk = min_t(unsigned int, rxm->full_len, len);
2110 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
2112 goto splice_requeue;
2114 if (chunk < rxm->full_len) {
2116 rxm->full_len -= len;
2117 goto splice_requeue;
2123 tls_rx_reader_unlock(sk, ctx);
2124 return copied ? : err;
2127 __skb_queue_head(&ctx->rx_list, skb);
2128 goto splice_read_end;
2131 bool tls_sw_sock_is_readable(struct sock *sk)
2133 struct tls_context *tls_ctx = tls_get_ctx(sk);
2134 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2135 bool ingress_empty = true;
2136 struct sk_psock *psock;
2139 psock = sk_psock(sk);
2141 ingress_empty = list_empty(&psock->ingress_msg);
2144 return !ingress_empty || ctx->recv_pkt ||
2145 !skb_queue_empty(&ctx->rx_list);
2148 static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
2150 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2151 struct tls_prot_info *prot = &tls_ctx->prot_info;
2152 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
2153 struct strp_msg *rxm = strp_msg(skb);
2154 struct tls_msg *tlm = tls_msg(skb);
2155 size_t cipher_overhead;
2156 size_t data_len = 0;
2159 /* Verify that we have a full TLS header, or wait for more data */
2160 if (rxm->offset + prot->prepend_size > skb->len)
2163 /* Sanity-check size of on-stack buffer. */
2164 if (WARN_ON(prot->prepend_size > sizeof(header))) {
2169 /* Linearize header to local buffer */
2170 ret = skb_copy_bits(skb, rxm->offset, header, prot->prepend_size);
2174 tlm->control = header[0];
2176 data_len = ((header[4] & 0xFF) | (header[3] << 8));
2178 cipher_overhead = prot->tag_size;
2179 if (prot->version != TLS_1_3_VERSION &&
2180 prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
2181 cipher_overhead += prot->iv_size;
2183 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
2188 if (data_len < cipher_overhead) {
2193 /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
2194 if (header[1] != TLS_1_2_VERSION_MINOR ||
2195 header[2] != TLS_1_2_VERSION_MAJOR) {
2200 tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE,
2201 TCP_SKB_CB(skb)->seq + rxm->offset);
2202 return data_len + TLS_HEADER_SIZE;
2205 tls_err_abort(strp->sk, ret);
2210 static void tls_queue(struct strparser *strp, struct sk_buff *skb)
2212 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2213 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2215 ctx->recv_pkt = skb;
2218 ctx->saved_data_ready(strp->sk);
2221 static void tls_data_ready(struct sock *sk)
2223 struct tls_context *tls_ctx = tls_get_ctx(sk);
2224 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2225 struct sk_psock *psock;
2227 strp_data_ready(&ctx->strp);
2229 psock = sk_psock_get(sk);
2231 if (!list_empty(&psock->ingress_msg))
2232 ctx->saved_data_ready(sk);
2233 sk_psock_put(sk, psock);
2237 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx)
2239 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2241 set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask);
2242 set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask);
2243 cancel_delayed_work_sync(&ctx->tx_work.work);
2246 void tls_sw_release_resources_tx(struct sock *sk)
2248 struct tls_context *tls_ctx = tls_get_ctx(sk);
2249 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2250 struct tls_rec *rec, *tmp;
2253 /* Wait for any pending async encryptions to complete */
2254 spin_lock_bh(&ctx->encrypt_compl_lock);
2255 ctx->async_notify = true;
2256 pending = atomic_read(&ctx->encrypt_pending);
2257 spin_unlock_bh(&ctx->encrypt_compl_lock);
2260 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
2262 tls_tx_records(sk, -1);
2264 /* Free up un-sent records in tx_list. First, free
2265 * the partially sent record if any at head of tx_list.
2267 if (tls_ctx->partially_sent_record) {
2268 tls_free_partial_record(sk, tls_ctx);
2269 rec = list_first_entry(&ctx->tx_list,
2270 struct tls_rec, list);
2271 list_del(&rec->list);
2272 sk_msg_free(sk, &rec->msg_plaintext);
2276 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
2277 list_del(&rec->list);
2278 sk_msg_free(sk, &rec->msg_encrypted);
2279 sk_msg_free(sk, &rec->msg_plaintext);
2283 crypto_free_aead(ctx->aead_send);
2284 tls_free_open_rec(sk);
2287 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx)
2289 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2294 void tls_sw_release_resources_rx(struct sock *sk)
2296 struct tls_context *tls_ctx = tls_get_ctx(sk);
2297 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2299 kfree(tls_ctx->rx.rec_seq);
2300 kfree(tls_ctx->rx.iv);
2302 if (ctx->aead_recv) {
2303 kfree_skb(ctx->recv_pkt);
2304 ctx->recv_pkt = NULL;
2305 __skb_queue_purge(&ctx->rx_list);
2306 crypto_free_aead(ctx->aead_recv);
2307 strp_stop(&ctx->strp);
2308 /* If tls_sw_strparser_arm() was not called (cleanup paths)
2309 * we still want to strp_stop(), but sk->sk_data_ready was
2312 if (ctx->saved_data_ready) {
2313 write_lock_bh(&sk->sk_callback_lock);
2314 sk->sk_data_ready = ctx->saved_data_ready;
2315 write_unlock_bh(&sk->sk_callback_lock);
2320 void tls_sw_strparser_done(struct tls_context *tls_ctx)
2322 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2324 strp_done(&ctx->strp);
2327 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx)
2329 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2334 void tls_sw_free_resources_rx(struct sock *sk)
2336 struct tls_context *tls_ctx = tls_get_ctx(sk);
2338 tls_sw_release_resources_rx(sk);
2339 tls_sw_free_ctx_rx(tls_ctx);
2342 /* The work handler to transmitt the encrypted records in tx_list */
2343 static void tx_work_handler(struct work_struct *work)
2345 struct delayed_work *delayed_work = to_delayed_work(work);
2346 struct tx_work *tx_work = container_of(delayed_work,
2347 struct tx_work, work);
2348 struct sock *sk = tx_work->sk;
2349 struct tls_context *tls_ctx = tls_get_ctx(sk);
2350 struct tls_sw_context_tx *ctx;
2352 if (unlikely(!tls_ctx))
2355 ctx = tls_sw_ctx_tx(tls_ctx);
2356 if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask))
2359 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
2361 mutex_lock(&tls_ctx->tx_lock);
2363 tls_tx_records(sk, -1);
2365 mutex_unlock(&tls_ctx->tx_lock);
2368 static bool tls_is_tx_ready(struct tls_sw_context_tx *ctx)
2370 struct tls_rec *rec;
2372 rec = list_first_entry(&ctx->tx_list, struct tls_rec, list);
2376 return READ_ONCE(rec->tx_ready);
2379 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx)
2381 struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);
2383 /* Schedule the transmission if tx list is ready */
2384 if (tls_is_tx_ready(tx_ctx) &&
2385 !test_and_set_bit(BIT_TX_SCHEDULED, &tx_ctx->tx_bitmask))
2386 schedule_delayed_work(&tx_ctx->tx_work.work, 0);
2389 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx)
2391 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2393 write_lock_bh(&sk->sk_callback_lock);
2394 rx_ctx->saved_data_ready = sk->sk_data_ready;
2395 sk->sk_data_ready = tls_data_ready;
2396 write_unlock_bh(&sk->sk_callback_lock);
2398 strp_check_rcv(&rx_ctx->strp);
2401 void tls_update_rx_zc_capable(struct tls_context *tls_ctx)
2403 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2405 rx_ctx->zc_capable = tls_ctx->rx_no_pad ||
2406 tls_ctx->prot_info.version != TLS_1_3_VERSION;
2409 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2411 struct tls_context *tls_ctx = tls_get_ctx(sk);
2412 struct tls_prot_info *prot = &tls_ctx->prot_info;
2413 struct tls_crypto_info *crypto_info;
2414 struct tls_sw_context_tx *sw_ctx_tx = NULL;
2415 struct tls_sw_context_rx *sw_ctx_rx = NULL;
2416 struct cipher_context *cctx;
2417 struct crypto_aead **aead;
2418 struct strp_callbacks cb;
2419 u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
2420 struct crypto_tfm *tfm;
2421 char *iv, *rec_seq, *key, *salt, *cipher_name;
2431 if (!ctx->priv_ctx_tx) {
2432 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
2437 ctx->priv_ctx_tx = sw_ctx_tx;
2440 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
2443 if (!ctx->priv_ctx_rx) {
2444 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
2449 ctx->priv_ctx_rx = sw_ctx_rx;
2452 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
2457 crypto_init_wait(&sw_ctx_tx->async_wait);
2458 spin_lock_init(&sw_ctx_tx->encrypt_compl_lock);
2459 crypto_info = &ctx->crypto_send.info;
2461 aead = &sw_ctx_tx->aead_send;
2462 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
2463 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
2464 sw_ctx_tx->tx_work.sk = sk;
2466 crypto_init_wait(&sw_ctx_rx->async_wait);
2467 spin_lock_init(&sw_ctx_rx->decrypt_compl_lock);
2468 init_waitqueue_head(&sw_ctx_rx->wq);
2469 crypto_info = &ctx->crypto_recv.info;
2471 skb_queue_head_init(&sw_ctx_rx->rx_list);
2472 aead = &sw_ctx_rx->aead_recv;
2475 switch (crypto_info->cipher_type) {
2476 case TLS_CIPHER_AES_GCM_128: {
2477 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
2479 gcm_128_info = (void *)crypto_info;
2480 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2481 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
2482 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2483 iv = gcm_128_info->iv;
2484 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
2485 rec_seq = gcm_128_info->rec_seq;
2486 keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE;
2487 key = gcm_128_info->key;
2488 salt = gcm_128_info->salt;
2489 salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
2490 cipher_name = "gcm(aes)";
2493 case TLS_CIPHER_AES_GCM_256: {
2494 struct tls12_crypto_info_aes_gcm_256 *gcm_256_info;
2496 gcm_256_info = (void *)crypto_info;
2497 nonce_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2498 tag_size = TLS_CIPHER_AES_GCM_256_TAG_SIZE;
2499 iv_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2500 iv = gcm_256_info->iv;
2501 rec_seq_size = TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE;
2502 rec_seq = gcm_256_info->rec_seq;
2503 keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE;
2504 key = gcm_256_info->key;
2505 salt = gcm_256_info->salt;
2506 salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE;
2507 cipher_name = "gcm(aes)";
2510 case TLS_CIPHER_AES_CCM_128: {
2511 struct tls12_crypto_info_aes_ccm_128 *ccm_128_info;
2513 ccm_128_info = (void *)crypto_info;
2514 nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2515 tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE;
2516 iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2517 iv = ccm_128_info->iv;
2518 rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE;
2519 rec_seq = ccm_128_info->rec_seq;
2520 keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE;
2521 key = ccm_128_info->key;
2522 salt = ccm_128_info->salt;
2523 salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE;
2524 cipher_name = "ccm(aes)";
2527 case TLS_CIPHER_CHACHA20_POLY1305: {
2528 struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305_info;
2530 chacha20_poly1305_info = (void *)crypto_info;
2532 tag_size = TLS_CIPHER_CHACHA20_POLY1305_TAG_SIZE;
2533 iv_size = TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE;
2534 iv = chacha20_poly1305_info->iv;
2535 rec_seq_size = TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE;
2536 rec_seq = chacha20_poly1305_info->rec_seq;
2537 keysize = TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE;
2538 key = chacha20_poly1305_info->key;
2539 salt = chacha20_poly1305_info->salt;
2540 salt_size = TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE;
2541 cipher_name = "rfc7539(chacha20,poly1305)";
2544 case TLS_CIPHER_SM4_GCM: {
2545 struct tls12_crypto_info_sm4_gcm *sm4_gcm_info;
2547 sm4_gcm_info = (void *)crypto_info;
2548 nonce_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2549 tag_size = TLS_CIPHER_SM4_GCM_TAG_SIZE;
2550 iv_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2551 iv = sm4_gcm_info->iv;
2552 rec_seq_size = TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE;
2553 rec_seq = sm4_gcm_info->rec_seq;
2554 keysize = TLS_CIPHER_SM4_GCM_KEY_SIZE;
2555 key = sm4_gcm_info->key;
2556 salt = sm4_gcm_info->salt;
2557 salt_size = TLS_CIPHER_SM4_GCM_SALT_SIZE;
2558 cipher_name = "gcm(sm4)";
2561 case TLS_CIPHER_SM4_CCM: {
2562 struct tls12_crypto_info_sm4_ccm *sm4_ccm_info;
2564 sm4_ccm_info = (void *)crypto_info;
2565 nonce_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2566 tag_size = TLS_CIPHER_SM4_CCM_TAG_SIZE;
2567 iv_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2568 iv = sm4_ccm_info->iv;
2569 rec_seq_size = TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE;
2570 rec_seq = sm4_ccm_info->rec_seq;
2571 keysize = TLS_CIPHER_SM4_CCM_KEY_SIZE;
2572 key = sm4_ccm_info->key;
2573 salt = sm4_ccm_info->salt;
2574 salt_size = TLS_CIPHER_SM4_CCM_SALT_SIZE;
2575 cipher_name = "ccm(sm4)";
2583 if (crypto_info->version == TLS_1_3_VERSION) {
2585 prot->aad_size = TLS_HEADER_SIZE;
2586 prot->tail_size = 1;
2588 prot->aad_size = TLS_AAD_SPACE_SIZE;
2589 prot->tail_size = 0;
2592 /* Sanity-check the sizes for stack allocations. */
2593 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE ||
2594 rec_seq_size > TLS_MAX_REC_SEQ_SIZE || tag_size != TLS_TAG_SIZE ||
2595 prot->aad_size > TLS_MAX_AAD_SIZE) {
2600 prot->version = crypto_info->version;
2601 prot->cipher_type = crypto_info->cipher_type;
2602 prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
2603 prot->tag_size = tag_size;
2604 prot->overhead_size = prot->prepend_size +
2605 prot->tag_size + prot->tail_size;
2606 prot->iv_size = iv_size;
2607 prot->salt_size = salt_size;
2608 cctx->iv = kmalloc(iv_size + salt_size, GFP_KERNEL);
2613 /* Note: 128 & 256 bit salt are the same size */
2614 prot->rec_seq_size = rec_seq_size;
2615 memcpy(cctx->iv, salt, salt_size);
2616 memcpy(cctx->iv + salt_size, iv, iv_size);
2617 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
2618 if (!cctx->rec_seq) {
2624 *aead = crypto_alloc_aead(cipher_name, 0, 0);
2625 if (IS_ERR(*aead)) {
2626 rc = PTR_ERR(*aead);
2632 ctx->push_pending_record = tls_sw_push_pending_record;
2634 rc = crypto_aead_setkey(*aead, key, keysize);
2639 rc = crypto_aead_setauthsize(*aead, prot->tag_size);
2644 tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
2646 tls_update_rx_zc_capable(ctx);
2647 sw_ctx_rx->async_capable =
2648 crypto_info->version != TLS_1_3_VERSION &&
2649 !!(tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC);
2651 /* Set up strparser */
2652 memset(&cb, 0, sizeof(cb));
2653 cb.rcv_msg = tls_queue;
2654 cb.parse_msg = tls_read_size;
2656 strp_init(&sw_ctx_rx->strp, sk, &cb);
2662 crypto_free_aead(*aead);
2665 kfree(cctx->rec_seq);
2666 cctx->rec_seq = NULL;
2672 kfree(ctx->priv_ctx_tx);
2673 ctx->priv_ctx_tx = NULL;
2675 kfree(ctx->priv_ctx_rx);
2676 ctx->priv_ctx_rx = NULL;