2 * Copyright (c) 2016-2017, Mellanox Technologies. 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>
46 #include <trace/events/sock.h>
50 struct tls_decrypt_arg {
60 struct tls_decrypt_ctx {
62 u8 aad[TLS_MAX_AAD_SIZE];
64 struct scatterlist sg[];
67 noinline void tls_err_abort(struct sock *sk, int err)
69 WARN_ON_ONCE(err >= 0);
70 /* sk->sk_err should contain a positive error code. */
75 static int __skb_nsg(struct sk_buff *skb, int offset, int len,
76 unsigned int recursion_level)
78 int start = skb_headlen(skb);
79 int i, chunk = start - offset;
80 struct sk_buff *frag_iter;
83 if (unlikely(recursion_level >= 24))
96 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
99 WARN_ON(start > offset + len);
101 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
102 chunk = end - offset;
115 if (unlikely(skb_has_frag_list(skb))) {
116 skb_walk_frags(skb, frag_iter) {
119 WARN_ON(start > offset + len);
121 end = start + frag_iter->len;
122 chunk = end - offset;
126 ret = __skb_nsg(frag_iter, offset - start, chunk,
127 recursion_level + 1);
128 if (unlikely(ret < 0))
143 /* Return the number of scatterlist elements required to completely map the
144 * skb, or -EMSGSIZE if the recursion depth is exceeded.
146 static int skb_nsg(struct sk_buff *skb, int offset, int len)
148 return __skb_nsg(skb, offset, len, 0);
151 static int tls_padding_length(struct tls_prot_info *prot, struct sk_buff *skb,
152 struct tls_decrypt_arg *darg)
154 struct strp_msg *rxm = strp_msg(skb);
155 struct tls_msg *tlm = tls_msg(skb);
158 /* Determine zero-padding length */
159 if (prot->version == TLS_1_3_VERSION) {
160 int offset = rxm->full_len - TLS_TAG_SIZE - 1;
161 char content_type = darg->zc ? darg->tail : 0;
164 while (content_type == 0) {
165 if (offset < prot->prepend_size)
167 err = skb_copy_bits(skb, rxm->offset + offset,
176 tlm->control = content_type;
181 static void tls_decrypt_done(struct crypto_async_request *req, int err)
183 struct aead_request *aead_req = (struct aead_request *)req;
184 struct scatterlist *sgout = aead_req->dst;
185 struct scatterlist *sgin = aead_req->src;
186 struct tls_sw_context_rx *ctx;
187 struct tls_context *tls_ctx;
188 struct scatterlist *sg;
192 sk = (struct sock *)req->data;
193 tls_ctx = tls_get_ctx(sk);
194 ctx = tls_sw_ctx_rx(tls_ctx);
196 /* Propagate if there was an err */
199 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
200 ctx->async_wait.err = err;
201 tls_err_abort(sk, err);
204 /* Free the destination pages if skb was not decrypted inplace */
206 /* Skip the first S/G entry as it points to AAD */
207 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
210 put_page(sg_page(sg));
216 spin_lock_bh(&ctx->decrypt_compl_lock);
217 if (!atomic_dec_return(&ctx->decrypt_pending))
218 complete(&ctx->async_wait.completion);
219 spin_unlock_bh(&ctx->decrypt_compl_lock);
222 static int tls_do_decryption(struct sock *sk,
223 struct scatterlist *sgin,
224 struct scatterlist *sgout,
227 struct aead_request *aead_req,
228 struct tls_decrypt_arg *darg)
230 struct tls_context *tls_ctx = tls_get_ctx(sk);
231 struct tls_prot_info *prot = &tls_ctx->prot_info;
232 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
235 aead_request_set_tfm(aead_req, ctx->aead_recv);
236 aead_request_set_ad(aead_req, prot->aad_size);
237 aead_request_set_crypt(aead_req, sgin, sgout,
238 data_len + prot->tag_size,
242 aead_request_set_callback(aead_req,
243 CRYPTO_TFM_REQ_MAY_BACKLOG,
244 tls_decrypt_done, sk);
245 atomic_inc(&ctx->decrypt_pending);
247 aead_request_set_callback(aead_req,
248 CRYPTO_TFM_REQ_MAY_BACKLOG,
249 crypto_req_done, &ctx->async_wait);
252 ret = crypto_aead_decrypt(aead_req);
253 if (ret == -EINPROGRESS) {
257 ret = crypto_wait_req(ret, &ctx->async_wait);
264 static void tls_trim_both_msgs(struct sock *sk, int target_size)
266 struct tls_context *tls_ctx = tls_get_ctx(sk);
267 struct tls_prot_info *prot = &tls_ctx->prot_info;
268 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
269 struct tls_rec *rec = ctx->open_rec;
271 sk_msg_trim(sk, &rec->msg_plaintext, target_size);
273 target_size += prot->overhead_size;
274 sk_msg_trim(sk, &rec->msg_encrypted, target_size);
277 static int tls_alloc_encrypted_msg(struct sock *sk, int len)
279 struct tls_context *tls_ctx = tls_get_ctx(sk);
280 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
281 struct tls_rec *rec = ctx->open_rec;
282 struct sk_msg *msg_en = &rec->msg_encrypted;
284 return sk_msg_alloc(sk, msg_en, len, 0);
287 static int tls_clone_plaintext_msg(struct sock *sk, int required)
289 struct tls_context *tls_ctx = tls_get_ctx(sk);
290 struct tls_prot_info *prot = &tls_ctx->prot_info;
291 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
292 struct tls_rec *rec = ctx->open_rec;
293 struct sk_msg *msg_pl = &rec->msg_plaintext;
294 struct sk_msg *msg_en = &rec->msg_encrypted;
297 /* We add page references worth len bytes from encrypted sg
298 * at the end of plaintext sg. It is guaranteed that msg_en
299 * has enough required room (ensured by caller).
301 len = required - msg_pl->sg.size;
303 /* Skip initial bytes in msg_en's data to be able to use
304 * same offset of both plain and encrypted data.
306 skip = prot->prepend_size + msg_pl->sg.size;
308 return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
311 static struct tls_rec *tls_get_rec(struct sock *sk)
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 sk_msg *msg_pl, *msg_en;
320 mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
322 rec = kzalloc(mem_size, sk->sk_allocation);
326 msg_pl = &rec->msg_plaintext;
327 msg_en = &rec->msg_encrypted;
332 sg_init_table(rec->sg_aead_in, 2);
333 sg_set_buf(&rec->sg_aead_in[0], rec->aad_space, prot->aad_size);
334 sg_unmark_end(&rec->sg_aead_in[1]);
336 sg_init_table(rec->sg_aead_out, 2);
337 sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size);
338 sg_unmark_end(&rec->sg_aead_out[1]);
343 static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
345 sk_msg_free(sk, &rec->msg_encrypted);
346 sk_msg_free(sk, &rec->msg_plaintext);
350 static void tls_free_open_rec(struct sock *sk)
352 struct tls_context *tls_ctx = tls_get_ctx(sk);
353 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
354 struct tls_rec *rec = ctx->open_rec;
357 tls_free_rec(sk, rec);
358 ctx->open_rec = NULL;
362 int tls_tx_records(struct sock *sk, int flags)
364 struct tls_context *tls_ctx = tls_get_ctx(sk);
365 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
366 struct tls_rec *rec, *tmp;
367 struct sk_msg *msg_en;
368 int tx_flags, rc = 0;
370 if (tls_is_partially_sent_record(tls_ctx)) {
371 rec = list_first_entry(&ctx->tx_list,
372 struct tls_rec, list);
375 tx_flags = rec->tx_flags;
379 rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
383 /* Full record has been transmitted.
384 * Remove the head of tx_list
386 list_del(&rec->list);
387 sk_msg_free(sk, &rec->msg_plaintext);
391 /* Tx all ready records */
392 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
393 if (READ_ONCE(rec->tx_ready)) {
395 tx_flags = rec->tx_flags;
399 msg_en = &rec->msg_encrypted;
400 rc = tls_push_sg(sk, tls_ctx,
401 &msg_en->sg.data[msg_en->sg.curr],
406 list_del(&rec->list);
407 sk_msg_free(sk, &rec->msg_plaintext);
415 if (rc < 0 && rc != -EAGAIN)
416 tls_err_abort(sk, -EBADMSG);
421 static void tls_encrypt_done(struct crypto_async_request *req, int err)
423 struct aead_request *aead_req = (struct aead_request *)req;
424 struct sock *sk = req->data;
425 struct tls_context *tls_ctx = tls_get_ctx(sk);
426 struct tls_prot_info *prot = &tls_ctx->prot_info;
427 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
428 struct scatterlist *sge;
429 struct sk_msg *msg_en;
434 rec = container_of(aead_req, struct tls_rec, aead_req);
435 msg_en = &rec->msg_encrypted;
437 sge = sk_msg_elem(msg_en, msg_en->sg.curr);
438 sge->offset -= prot->prepend_size;
439 sge->length += prot->prepend_size;
441 /* Check if error is previously set on socket */
442 if (err || sk->sk_err) {
445 /* If err is already set on socket, return the same code */
447 ctx->async_wait.err = -sk->sk_err;
449 ctx->async_wait.err = err;
450 tls_err_abort(sk, err);
455 struct tls_rec *first_rec;
457 /* Mark the record as ready for transmission */
458 smp_store_mb(rec->tx_ready, true);
460 /* If received record is at head of tx_list, schedule tx */
461 first_rec = list_first_entry(&ctx->tx_list,
462 struct tls_rec, list);
463 if (rec == first_rec)
467 spin_lock_bh(&ctx->encrypt_compl_lock);
468 pending = atomic_dec_return(&ctx->encrypt_pending);
470 if (!pending && ctx->async_notify)
471 complete(&ctx->async_wait.completion);
472 spin_unlock_bh(&ctx->encrypt_compl_lock);
477 /* Schedule the transmission */
478 if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
479 schedule_delayed_work(&ctx->tx_work.work, 1);
482 static int tls_do_encryption(struct sock *sk,
483 struct tls_context *tls_ctx,
484 struct tls_sw_context_tx *ctx,
485 struct aead_request *aead_req,
486 size_t data_len, u32 start)
488 struct tls_prot_info *prot = &tls_ctx->prot_info;
489 struct tls_rec *rec = ctx->open_rec;
490 struct sk_msg *msg_en = &rec->msg_encrypted;
491 struct scatterlist *sge = sk_msg_elem(msg_en, start);
492 int rc, iv_offset = 0;
494 /* For CCM based ciphers, first byte of IV is a constant */
495 switch (prot->cipher_type) {
496 case TLS_CIPHER_AES_CCM_128:
497 rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE;
500 case TLS_CIPHER_SM4_CCM:
501 rec->iv_data[0] = TLS_SM4_CCM_IV_B0_BYTE;
506 memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv,
507 prot->iv_size + prot->salt_size);
509 tls_xor_iv_with_seq(prot, rec->iv_data + iv_offset,
510 tls_ctx->tx.rec_seq);
512 sge->offset += prot->prepend_size;
513 sge->length -= prot->prepend_size;
515 msg_en->sg.curr = start;
517 aead_request_set_tfm(aead_req, ctx->aead_send);
518 aead_request_set_ad(aead_req, prot->aad_size);
519 aead_request_set_crypt(aead_req, rec->sg_aead_in,
521 data_len, rec->iv_data);
523 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
524 tls_encrypt_done, sk);
526 /* Add the record in tx_list */
527 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
528 atomic_inc(&ctx->encrypt_pending);
530 rc = crypto_aead_encrypt(aead_req);
531 if (!rc || rc != -EINPROGRESS) {
532 atomic_dec(&ctx->encrypt_pending);
533 sge->offset -= prot->prepend_size;
534 sge->length += prot->prepend_size;
538 WRITE_ONCE(rec->tx_ready, true);
539 } else if (rc != -EINPROGRESS) {
540 list_del(&rec->list);
544 /* Unhook the record from context if encryption is not failure */
545 ctx->open_rec = NULL;
546 tls_advance_record_sn(sk, prot, &tls_ctx->tx);
550 static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
551 struct tls_rec **to, struct sk_msg *msg_opl,
552 struct sk_msg *msg_oen, u32 split_point,
553 u32 tx_overhead_size, u32 *orig_end)
555 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
556 struct scatterlist *sge, *osge, *nsge;
557 u32 orig_size = msg_opl->sg.size;
558 struct scatterlist tmp = { };
559 struct sk_msg *msg_npl;
563 new = tls_get_rec(sk);
566 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
567 tx_overhead_size, 0);
569 tls_free_rec(sk, new);
573 *orig_end = msg_opl->sg.end;
574 i = msg_opl->sg.start;
575 sge = sk_msg_elem(msg_opl, i);
576 while (apply && sge->length) {
577 if (sge->length > apply) {
578 u32 len = sge->length - apply;
580 get_page(sg_page(sge));
581 sg_set_page(&tmp, sg_page(sge), len,
582 sge->offset + apply);
587 apply -= sge->length;
588 bytes += sge->length;
591 sk_msg_iter_var_next(i);
592 if (i == msg_opl->sg.end)
594 sge = sk_msg_elem(msg_opl, i);
598 msg_opl->sg.curr = i;
599 msg_opl->sg.copybreak = 0;
600 msg_opl->apply_bytes = 0;
601 msg_opl->sg.size = bytes;
603 msg_npl = &new->msg_plaintext;
604 msg_npl->apply_bytes = apply;
605 msg_npl->sg.size = orig_size - bytes;
607 j = msg_npl->sg.start;
608 nsge = sk_msg_elem(msg_npl, j);
610 memcpy(nsge, &tmp, sizeof(*nsge));
611 sk_msg_iter_var_next(j);
612 nsge = sk_msg_elem(msg_npl, j);
615 osge = sk_msg_elem(msg_opl, i);
616 while (osge->length) {
617 memcpy(nsge, osge, sizeof(*nsge));
619 sk_msg_iter_var_next(i);
620 sk_msg_iter_var_next(j);
623 osge = sk_msg_elem(msg_opl, i);
624 nsge = sk_msg_elem(msg_npl, j);
628 msg_npl->sg.curr = j;
629 msg_npl->sg.copybreak = 0;
635 static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
636 struct tls_rec *from, u32 orig_end)
638 struct sk_msg *msg_npl = &from->msg_plaintext;
639 struct sk_msg *msg_opl = &to->msg_plaintext;
640 struct scatterlist *osge, *nsge;
644 sk_msg_iter_var_prev(i);
645 j = msg_npl->sg.start;
647 osge = sk_msg_elem(msg_opl, i);
648 nsge = sk_msg_elem(msg_npl, j);
650 if (sg_page(osge) == sg_page(nsge) &&
651 osge->offset + osge->length == nsge->offset) {
652 osge->length += nsge->length;
653 put_page(sg_page(nsge));
656 msg_opl->sg.end = orig_end;
657 msg_opl->sg.curr = orig_end;
658 msg_opl->sg.copybreak = 0;
659 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
660 msg_opl->sg.size += msg_npl->sg.size;
662 sk_msg_free(sk, &to->msg_encrypted);
663 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
668 static int tls_push_record(struct sock *sk, int flags,
669 unsigned char record_type)
671 struct tls_context *tls_ctx = tls_get_ctx(sk);
672 struct tls_prot_info *prot = &tls_ctx->prot_info;
673 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
674 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
675 u32 i, split_point, orig_end;
676 struct sk_msg *msg_pl, *msg_en;
677 struct aead_request *req;
684 msg_pl = &rec->msg_plaintext;
685 msg_en = &rec->msg_encrypted;
687 split_point = msg_pl->apply_bytes;
688 split = split_point && split_point < msg_pl->sg.size;
689 if (unlikely((!split &&
691 prot->overhead_size > msg_en->sg.size) ||
694 prot->overhead_size > msg_en->sg.size))) {
696 split_point = msg_en->sg.size;
699 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
700 split_point, prot->overhead_size,
704 /* This can happen if above tls_split_open_record allocates
705 * a single large encryption buffer instead of two smaller
706 * ones. In this case adjust pointers and continue without
709 if (!msg_pl->sg.size) {
710 tls_merge_open_record(sk, rec, tmp, orig_end);
711 msg_pl = &rec->msg_plaintext;
712 msg_en = &rec->msg_encrypted;
715 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
716 prot->overhead_size);
719 rec->tx_flags = flags;
720 req = &rec->aead_req;
723 sk_msg_iter_var_prev(i);
725 rec->content_type = record_type;
726 if (prot->version == TLS_1_3_VERSION) {
727 /* Add content type to end of message. No padding added */
728 sg_set_buf(&rec->sg_content_type, &rec->content_type, 1);
729 sg_mark_end(&rec->sg_content_type);
730 sg_chain(msg_pl->sg.data, msg_pl->sg.end + 1,
731 &rec->sg_content_type);
733 sg_mark_end(sk_msg_elem(msg_pl, i));
736 if (msg_pl->sg.end < msg_pl->sg.start) {
737 sg_chain(&msg_pl->sg.data[msg_pl->sg.start],
738 MAX_SKB_FRAGS - msg_pl->sg.start + 1,
742 i = msg_pl->sg.start;
743 sg_chain(rec->sg_aead_in, 2, &msg_pl->sg.data[i]);
746 sk_msg_iter_var_prev(i);
747 sg_mark_end(sk_msg_elem(msg_en, i));
749 i = msg_en->sg.start;
750 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
752 tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size,
753 tls_ctx->tx.rec_seq, record_type, prot);
755 tls_fill_prepend(tls_ctx,
756 page_address(sg_page(&msg_en->sg.data[i])) +
757 msg_en->sg.data[i].offset,
758 msg_pl->sg.size + prot->tail_size,
761 tls_ctx->pending_open_record_frags = false;
763 rc = tls_do_encryption(sk, tls_ctx, ctx, req,
764 msg_pl->sg.size + prot->tail_size, i);
766 if (rc != -EINPROGRESS) {
767 tls_err_abort(sk, -EBADMSG);
769 tls_ctx->pending_open_record_frags = true;
770 tls_merge_open_record(sk, rec, tmp, orig_end);
773 ctx->async_capable = 1;
776 msg_pl = &tmp->msg_plaintext;
777 msg_en = &tmp->msg_encrypted;
778 sk_msg_trim(sk, msg_en, msg_pl->sg.size + prot->overhead_size);
779 tls_ctx->pending_open_record_frags = true;
783 return tls_tx_records(sk, flags);
786 static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
787 bool full_record, u8 record_type,
788 ssize_t *copied, int flags)
790 struct tls_context *tls_ctx = tls_get_ctx(sk);
791 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
792 struct sk_msg msg_redir = { };
793 struct sk_psock *psock;
794 struct sock *sk_redir;
796 bool enospc, policy, redir_ingress;
800 policy = !(flags & MSG_SENDPAGE_NOPOLICY);
801 psock = sk_psock_get(sk);
802 if (!psock || !policy) {
803 err = tls_push_record(sk, flags, record_type);
804 if (err && sk->sk_err == EBADMSG) {
805 *copied -= sk_msg_free(sk, msg);
806 tls_free_open_rec(sk);
810 sk_psock_put(sk, psock);
814 enospc = sk_msg_full(msg);
815 if (psock->eval == __SK_NONE) {
816 delta = msg->sg.size;
817 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
818 delta -= msg->sg.size;
820 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
821 !enospc && !full_record) {
827 if (msg->apply_bytes && msg->apply_bytes < send)
828 send = msg->apply_bytes;
830 switch (psock->eval) {
832 err = tls_push_record(sk, flags, record_type);
833 if (err && sk->sk_err == EBADMSG) {
834 *copied -= sk_msg_free(sk, msg);
835 tls_free_open_rec(sk);
841 redir_ingress = psock->redir_ingress;
842 sk_redir = psock->sk_redir;
843 memcpy(&msg_redir, msg, sizeof(*msg));
844 if (msg->apply_bytes < send)
845 msg->apply_bytes = 0;
847 msg->apply_bytes -= send;
848 sk_msg_return_zero(sk, msg, send);
849 msg->sg.size -= send;
851 err = tcp_bpf_sendmsg_redir(sk_redir, redir_ingress,
852 &msg_redir, send, flags);
855 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
858 if (msg->sg.size == 0)
859 tls_free_open_rec(sk);
863 sk_msg_free_partial(sk, msg, send);
864 if (msg->apply_bytes < send)
865 msg->apply_bytes = 0;
867 msg->apply_bytes -= send;
868 if (msg->sg.size == 0)
869 tls_free_open_rec(sk);
870 *copied -= (send + delta);
875 bool reset_eval = !ctx->open_rec;
879 msg = &rec->msg_plaintext;
880 if (!msg->apply_bytes)
884 psock->eval = __SK_NONE;
885 if (psock->sk_redir) {
886 sock_put(psock->sk_redir);
887 psock->sk_redir = NULL;
894 sk_psock_put(sk, psock);
898 static int tls_sw_push_pending_record(struct sock *sk, int flags)
900 struct tls_context *tls_ctx = tls_get_ctx(sk);
901 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
902 struct tls_rec *rec = ctx->open_rec;
903 struct sk_msg *msg_pl;
909 msg_pl = &rec->msg_plaintext;
910 copied = msg_pl->sg.size;
914 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
918 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
920 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
921 struct tls_context *tls_ctx = tls_get_ctx(sk);
922 struct tls_prot_info *prot = &tls_ctx->prot_info;
923 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
924 bool async_capable = ctx->async_capable;
925 unsigned char record_type = TLS_RECORD_TYPE_DATA;
926 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
927 bool eor = !(msg->msg_flags & MSG_MORE);
930 struct sk_msg *msg_pl, *msg_en;
941 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
945 mutex_lock(&tls_ctx->tx_lock);
948 if (unlikely(msg->msg_controllen)) {
949 ret = tls_process_cmsg(sk, msg, &record_type);
951 if (ret == -EINPROGRESS)
953 else if (ret != -EAGAIN)
958 while (msg_data_left(msg)) {
967 rec = ctx->open_rec = tls_get_rec(sk);
973 msg_pl = &rec->msg_plaintext;
974 msg_en = &rec->msg_encrypted;
976 orig_size = msg_pl->sg.size;
978 try_to_copy = msg_data_left(msg);
979 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
980 if (try_to_copy >= record_room) {
981 try_to_copy = record_room;
985 required_size = msg_pl->sg.size + try_to_copy +
988 if (!sk_stream_memory_free(sk))
989 goto wait_for_sndbuf;
992 ret = tls_alloc_encrypted_msg(sk, required_size);
995 goto wait_for_memory;
997 /* Adjust try_to_copy according to the amount that was
998 * actually allocated. The difference is due
999 * to max sg elements limit
1001 try_to_copy -= required_size - msg_en->sg.size;
1005 if (!is_kvec && (full_record || eor) && !async_capable) {
1006 u32 first = msg_pl->sg.end;
1008 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
1009 msg_pl, try_to_copy);
1011 goto fallback_to_reg_send;
1014 copied += try_to_copy;
1016 sk_msg_sg_copy_set(msg_pl, first);
1017 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1018 record_type, &copied,
1021 if (ret == -EINPROGRESS)
1023 else if (ret == -ENOMEM)
1024 goto wait_for_memory;
1025 else if (ctx->open_rec && ret == -ENOSPC)
1027 else if (ret != -EAGAIN)
1032 copied -= try_to_copy;
1033 sk_msg_sg_copy_clear(msg_pl, first);
1034 iov_iter_revert(&msg->msg_iter,
1035 msg_pl->sg.size - orig_size);
1036 fallback_to_reg_send:
1037 sk_msg_trim(sk, msg_pl, orig_size);
1040 required_size = msg_pl->sg.size + try_to_copy;
1042 ret = tls_clone_plaintext_msg(sk, required_size);
1047 /* Adjust try_to_copy according to the amount that was
1048 * actually allocated. The difference is due
1049 * to max sg elements limit
1051 try_to_copy -= required_size - msg_pl->sg.size;
1053 sk_msg_trim(sk, msg_en,
1054 msg_pl->sg.size + prot->overhead_size);
1058 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
1059 msg_pl, try_to_copy);
1064 /* Open records defined only if successfully copied, otherwise
1065 * we would trim the sg but not reset the open record frags.
1067 tls_ctx->pending_open_record_frags = true;
1068 copied += try_to_copy;
1069 if (full_record || eor) {
1070 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1071 record_type, &copied,
1074 if (ret == -EINPROGRESS)
1076 else if (ret == -ENOMEM)
1077 goto wait_for_memory;
1078 else if (ret != -EAGAIN) {
1089 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1091 ret = sk_stream_wait_memory(sk, &timeo);
1095 tls_trim_both_msgs(sk, orig_size);
1099 if (ctx->open_rec && msg_en->sg.size < required_size)
1100 goto alloc_encrypted;
1105 } else if (num_zc) {
1106 /* Wait for pending encryptions to get completed */
1107 spin_lock_bh(&ctx->encrypt_compl_lock);
1108 ctx->async_notify = true;
1110 pending = atomic_read(&ctx->encrypt_pending);
1111 spin_unlock_bh(&ctx->encrypt_compl_lock);
1113 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1115 reinit_completion(&ctx->async_wait.completion);
1117 /* There can be no concurrent accesses, since we have no
1118 * pending encrypt operations
1120 WRITE_ONCE(ctx->async_notify, false);
1122 if (ctx->async_wait.err) {
1123 ret = ctx->async_wait.err;
1128 /* Transmit if any encryptions have completed */
1129 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1130 cancel_delayed_work(&ctx->tx_work.work);
1131 tls_tx_records(sk, msg->msg_flags);
1135 ret = sk_stream_error(sk, msg->msg_flags, ret);
1138 mutex_unlock(&tls_ctx->tx_lock);
1139 return copied > 0 ? copied : ret;
1142 static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
1143 int offset, size_t size, int flags)
1145 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
1146 struct tls_context *tls_ctx = tls_get_ctx(sk);
1147 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1148 struct tls_prot_info *prot = &tls_ctx->prot_info;
1149 unsigned char record_type = TLS_RECORD_TYPE_DATA;
1150 struct sk_msg *msg_pl;
1151 struct tls_rec *rec;
1159 eor = !(flags & MSG_SENDPAGE_NOTLAST);
1160 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1162 /* Call the sk_stream functions to manage the sndbuf mem. */
1164 size_t copy, required_size;
1172 rec = ctx->open_rec;
1174 rec = ctx->open_rec = tls_get_rec(sk);
1180 msg_pl = &rec->msg_plaintext;
1182 full_record = false;
1183 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
1185 if (copy >= record_room) {
1190 required_size = msg_pl->sg.size + copy + prot->overhead_size;
1192 if (!sk_stream_memory_free(sk))
1193 goto wait_for_sndbuf;
1195 ret = tls_alloc_encrypted_msg(sk, required_size);
1198 goto wait_for_memory;
1200 /* Adjust copy according to the amount that was
1201 * actually allocated. The difference is due
1202 * to max sg elements limit
1204 copy -= required_size - msg_pl->sg.size;
1208 sk_msg_page_add(msg_pl, page, copy, offset);
1209 sk_mem_charge(sk, copy);
1215 tls_ctx->pending_open_record_frags = true;
1216 if (full_record || eor || sk_msg_full(msg_pl)) {
1217 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1218 record_type, &copied, flags);
1220 if (ret == -EINPROGRESS)
1222 else if (ret == -ENOMEM)
1223 goto wait_for_memory;
1224 else if (ret != -EAGAIN) {
1233 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1235 ret = sk_stream_wait_memory(sk, &timeo);
1238 tls_trim_both_msgs(sk, msg_pl->sg.size);
1247 /* Transmit if any encryptions have completed */
1248 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1249 cancel_delayed_work(&ctx->tx_work.work);
1250 tls_tx_records(sk, flags);
1254 ret = sk_stream_error(sk, flags, ret);
1255 return copied > 0 ? copied : ret;
1258 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
1259 int offset, size_t size, int flags)
1261 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1262 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY |
1263 MSG_NO_SHARED_FRAGS))
1266 return tls_sw_do_sendpage(sk, page, offset, size, flags);
1269 int tls_sw_sendpage(struct sock *sk, struct page *page,
1270 int offset, size_t size, int flags)
1272 struct tls_context *tls_ctx = tls_get_ctx(sk);
1275 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1276 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1279 mutex_lock(&tls_ctx->tx_lock);
1281 ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
1283 mutex_unlock(&tls_ctx->tx_lock);
1288 tls_rx_rec_wait(struct sock *sk, struct sk_psock *psock, bool nonblock,
1291 struct tls_context *tls_ctx = tls_get_ctx(sk);
1292 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1293 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1296 timeo = sock_rcvtimeo(sk, nonblock);
1298 while (!tls_strp_msg_ready(ctx)) {
1299 if (!sk_psock_queue_empty(psock))
1303 return sock_error(sk);
1305 if (!skb_queue_empty(&sk->sk_receive_queue)) {
1306 tls_strp_check_rcv(&ctx->strp);
1307 if (tls_strp_msg_ready(ctx))
1311 if (sk->sk_shutdown & RCV_SHUTDOWN)
1314 if (sock_flag(sk, SOCK_DONE))
1321 add_wait_queue(sk_sleep(sk), &wait);
1322 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1323 sk_wait_event(sk, &timeo,
1324 tls_strp_msg_ready(ctx) ||
1325 !sk_psock_queue_empty(psock),
1327 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1328 remove_wait_queue(sk_sleep(sk), &wait);
1330 /* Handle signals */
1331 if (signal_pending(current))
1332 return sock_intr_errno(timeo);
1335 tls_strp_msg_load(&ctx->strp, released);
1340 static int tls_setup_from_iter(struct iov_iter *from,
1341 int length, int *pages_used,
1342 struct scatterlist *to,
1345 int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1346 struct page *pages[MAX_SKB_FRAGS];
1347 unsigned int size = 0;
1348 ssize_t copied, use;
1351 while (length > 0) {
1353 maxpages = to_max_pages - num_elem;
1354 if (maxpages == 0) {
1358 copied = iov_iter_get_pages2(from, pages,
1369 use = min_t(int, copied, PAGE_SIZE - offset);
1371 sg_set_page(&to[num_elem],
1372 pages[i], use, offset);
1373 sg_unmark_end(&to[num_elem]);
1374 /* We do not uncharge memory from this API */
1383 /* Mark the end in the last sg entry if newly added */
1384 if (num_elem > *pages_used)
1385 sg_mark_end(&to[num_elem - 1]);
1388 iov_iter_revert(from, size);
1389 *pages_used = num_elem;
1394 static struct sk_buff *
1395 tls_alloc_clrtxt_skb(struct sock *sk, struct sk_buff *skb,
1396 unsigned int full_len)
1398 struct strp_msg *clr_rxm;
1399 struct sk_buff *clr_skb;
1402 clr_skb = alloc_skb_with_frags(0, full_len, TLS_PAGE_ORDER,
1403 &err, sk->sk_allocation);
1407 skb_copy_header(clr_skb, skb);
1408 clr_skb->len = full_len;
1409 clr_skb->data_len = full_len;
1411 clr_rxm = strp_msg(clr_skb);
1412 clr_rxm->offset = 0;
1419 * tls_decrypt_sw() and tls_decrypt_device() are decrypt handlers.
1420 * They must transform the darg in/out argument are as follows:
1422 * -------------------------------------------------------------------
1423 * zc | Zero-copy decrypt allowed | Zero-copy performed
1424 * async | Async decrypt allowed | Async crypto used / in progress
1425 * skb | * | Output skb
1427 * If ZC decryption was performed darg.skb will point to the input skb.
1430 /* This function decrypts the input skb into either out_iov or in out_sg
1431 * or in skb buffers itself. The input parameter 'darg->zc' indicates if
1432 * zero-copy mode needs to be tried or not. With zero-copy mode, either
1433 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
1434 * NULL, then the decryption happens inside skb buffers itself, i.e.
1435 * zero-copy gets disabled and 'darg->zc' is updated.
1437 static int tls_decrypt_sg(struct sock *sk, struct iov_iter *out_iov,
1438 struct scatterlist *out_sg,
1439 struct tls_decrypt_arg *darg)
1441 struct tls_context *tls_ctx = tls_get_ctx(sk);
1442 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1443 struct tls_prot_info *prot = &tls_ctx->prot_info;
1444 int n_sgin, n_sgout, aead_size, err, pages = 0;
1445 struct sk_buff *skb = tls_strp_msg(ctx);
1446 const struct strp_msg *rxm = strp_msg(skb);
1447 const struct tls_msg *tlm = tls_msg(skb);
1448 struct aead_request *aead_req;
1449 struct scatterlist *sgin = NULL;
1450 struct scatterlist *sgout = NULL;
1451 const int data_len = rxm->full_len - prot->overhead_size;
1452 int tail_pages = !!prot->tail_size;
1453 struct tls_decrypt_ctx *dctx;
1454 struct sk_buff *clear_skb;
1458 n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size,
1459 rxm->full_len - prot->prepend_size);
1461 return n_sgin ?: -EBADMSG;
1463 if (darg->zc && (out_iov || out_sg)) {
1467 n_sgout = 1 + tail_pages +
1468 iov_iter_npages_cap(out_iov, INT_MAX, data_len);
1470 n_sgout = sg_nents(out_sg);
1474 clear_skb = tls_alloc_clrtxt_skb(sk, skb, rxm->full_len);
1478 n_sgout = 1 + skb_shinfo(clear_skb)->nr_frags;
1481 /* Increment to accommodate AAD */
1482 n_sgin = n_sgin + 1;
1484 /* Allocate a single block of memory which contains
1485 * aead_req || tls_decrypt_ctx.
1486 * Both structs are variable length.
1488 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
1489 mem = kmalloc(aead_size + struct_size(dctx, sg, n_sgin + n_sgout),
1496 /* Segment the allocated memory */
1497 aead_req = (struct aead_request *)mem;
1498 dctx = (struct tls_decrypt_ctx *)(mem + aead_size);
1499 sgin = &dctx->sg[0];
1500 sgout = &dctx->sg[n_sgin];
1502 /* For CCM based ciphers, first byte of nonce+iv is a constant */
1503 switch (prot->cipher_type) {
1504 case TLS_CIPHER_AES_CCM_128:
1505 dctx->iv[0] = TLS_AES_CCM_IV_B0_BYTE;
1508 case TLS_CIPHER_SM4_CCM:
1509 dctx->iv[0] = TLS_SM4_CCM_IV_B0_BYTE;
1515 if (prot->version == TLS_1_3_VERSION ||
1516 prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
1517 memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv,
1518 prot->iv_size + prot->salt_size);
1520 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1521 &dctx->iv[iv_offset] + prot->salt_size,
1525 memcpy(&dctx->iv[iv_offset], tls_ctx->rx.iv, prot->salt_size);
1527 tls_xor_iv_with_seq(prot, &dctx->iv[iv_offset], tls_ctx->rx.rec_seq);
1530 tls_make_aad(dctx->aad, rxm->full_len - prot->overhead_size +
1532 tls_ctx->rx.rec_seq, tlm->control, prot);
1535 sg_init_table(sgin, n_sgin);
1536 sg_set_buf(&sgin[0], dctx->aad, prot->aad_size);
1537 err = skb_to_sgvec(skb, &sgin[1],
1538 rxm->offset + prot->prepend_size,
1539 rxm->full_len - prot->prepend_size);
1544 sg_init_table(sgout, n_sgout);
1545 sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
1547 err = skb_to_sgvec(clear_skb, &sgout[1], prot->prepend_size,
1548 data_len + prot->tail_size);
1551 } else if (out_iov) {
1552 sg_init_table(sgout, n_sgout);
1553 sg_set_buf(&sgout[0], dctx->aad, prot->aad_size);
1555 err = tls_setup_from_iter(out_iov, data_len, &pages, &sgout[1],
1556 (n_sgout - 1 - tail_pages));
1558 goto exit_free_pages;
1560 if (prot->tail_size) {
1561 sg_unmark_end(&sgout[pages]);
1562 sg_set_buf(&sgout[pages + 1], &dctx->tail,
1564 sg_mark_end(&sgout[pages + 1]);
1566 } else if (out_sg) {
1567 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
1570 /* Prepare and submit AEAD request */
1571 err = tls_do_decryption(sk, sgin, sgout, dctx->iv,
1572 data_len + prot->tail_size, aead_req, darg);
1574 goto exit_free_pages;
1576 darg->skb = clear_skb ?: tls_strp_msg(ctx);
1579 if (unlikely(darg->async)) {
1580 err = tls_strp_msg_hold(&ctx->strp, &ctx->async_hold);
1582 __skb_queue_tail(&ctx->async_hold, darg->skb);
1586 if (prot->tail_size)
1587 darg->tail = dctx->tail;
1590 /* Release the pages in case iov was mapped to pages */
1591 for (; pages > 0; pages--)
1592 put_page(sg_page(&sgout[pages]));
1596 consume_skb(clear_skb);
1601 tls_decrypt_sw(struct sock *sk, struct tls_context *tls_ctx,
1602 struct msghdr *msg, struct tls_decrypt_arg *darg)
1604 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1605 struct tls_prot_info *prot = &tls_ctx->prot_info;
1606 struct strp_msg *rxm;
1609 err = tls_decrypt_sg(sk, &msg->msg_iter, NULL, darg);
1611 if (err == -EBADMSG)
1612 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTERROR);
1615 /* keep going even for ->async, the code below is TLS 1.3 */
1617 /* If opportunistic TLS 1.3 ZC failed retry without ZC */
1618 if (unlikely(darg->zc && prot->version == TLS_1_3_VERSION &&
1619 darg->tail != TLS_RECORD_TYPE_DATA)) {
1622 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXNOPADVIOL);
1623 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSDECRYPTRETRY);
1624 return tls_decrypt_sw(sk, tls_ctx, msg, darg);
1627 pad = tls_padding_length(prot, darg->skb, darg);
1629 if (darg->skb != tls_strp_msg(ctx))
1630 consume_skb(darg->skb);
1634 rxm = strp_msg(darg->skb);
1635 rxm->full_len -= pad;
1641 tls_decrypt_device(struct sock *sk, struct msghdr *msg,
1642 struct tls_context *tls_ctx, struct tls_decrypt_arg *darg)
1644 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1645 struct tls_prot_info *prot = &tls_ctx->prot_info;
1646 struct strp_msg *rxm;
1649 if (tls_ctx->rx_conf != TLS_HW)
1652 err = tls_device_decrypted(sk, tls_ctx);
1656 pad = tls_padding_length(prot, tls_strp_msg(ctx), darg);
1660 darg->async = false;
1661 darg->skb = tls_strp_msg(ctx);
1662 /* ->zc downgrade check, in case TLS 1.3 gets here */
1663 darg->zc &= !(prot->version == TLS_1_3_VERSION &&
1664 tls_msg(darg->skb)->control != TLS_RECORD_TYPE_DATA);
1666 rxm = strp_msg(darg->skb);
1667 rxm->full_len -= pad;
1670 /* Non-ZC case needs a real skb */
1671 darg->skb = tls_strp_msg_detach(ctx);
1675 unsigned int off, len;
1677 /* In ZC case nobody cares about the output skb.
1678 * Just copy the data here. Note the skb is not fully trimmed.
1680 off = rxm->offset + prot->prepend_size;
1681 len = rxm->full_len - prot->overhead_size;
1683 err = skb_copy_datagram_msg(darg->skb, off, msg, len);
1690 static int tls_rx_one_record(struct sock *sk, struct msghdr *msg,
1691 struct tls_decrypt_arg *darg)
1693 struct tls_context *tls_ctx = tls_get_ctx(sk);
1694 struct tls_prot_info *prot = &tls_ctx->prot_info;
1695 struct strp_msg *rxm;
1698 err = tls_decrypt_device(sk, msg, tls_ctx, darg);
1700 err = tls_decrypt_sw(sk, tls_ctx, msg, darg);
1704 rxm = strp_msg(darg->skb);
1705 rxm->offset += prot->prepend_size;
1706 rxm->full_len -= prot->overhead_size;
1707 tls_advance_record_sn(sk, prot, &tls_ctx->rx);
1712 int decrypt_skb(struct sock *sk, struct scatterlist *sgout)
1714 struct tls_decrypt_arg darg = { .zc = true, };
1716 return tls_decrypt_sg(sk, NULL, sgout, &darg);
1719 static int tls_record_content_type(struct msghdr *msg, struct tls_msg *tlm,
1725 *control = tlm->control;
1729 err = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1730 sizeof(*control), control);
1731 if (*control != TLS_RECORD_TYPE_DATA) {
1732 if (err || msg->msg_flags & MSG_CTRUNC)
1735 } else if (*control != tlm->control) {
1742 static void tls_rx_rec_done(struct tls_sw_context_rx *ctx)
1744 tls_strp_msg_done(&ctx->strp);
1747 /* This function traverses the rx_list in tls receive context to copies the
1748 * decrypted records into the buffer provided by caller zero copy is not
1749 * true. Further, the records are removed from the rx_list if it is not a peek
1750 * case and the record has been consumed completely.
1752 static int process_rx_list(struct tls_sw_context_rx *ctx,
1759 struct sk_buff *skb = skb_peek(&ctx->rx_list);
1760 struct tls_msg *tlm;
1764 while (skip && skb) {
1765 struct strp_msg *rxm = strp_msg(skb);
1768 err = tls_record_content_type(msg, tlm, control);
1772 if (skip < rxm->full_len)
1775 skip = skip - rxm->full_len;
1776 skb = skb_peek_next(skb, &ctx->rx_list);
1779 while (len && skb) {
1780 struct sk_buff *next_skb;
1781 struct strp_msg *rxm = strp_msg(skb);
1782 int chunk = min_t(unsigned int, rxm->full_len - skip, len);
1786 err = tls_record_content_type(msg, tlm, control);
1790 err = skb_copy_datagram_msg(skb, rxm->offset + skip,
1796 copied = copied + chunk;
1798 /* Consume the data from record if it is non-peek case*/
1800 rxm->offset = rxm->offset + chunk;
1801 rxm->full_len = rxm->full_len - chunk;
1803 /* Return if there is unconsumed data in the record */
1804 if (rxm->full_len - skip)
1808 /* The remaining skip-bytes must lie in 1st record in rx_list.
1809 * So from the 2nd record, 'skip' should be 0.
1814 msg->msg_flags |= MSG_EOR;
1816 next_skb = skb_peek_next(skb, &ctx->rx_list);
1819 __skb_unlink(skb, &ctx->rx_list);
1828 return copied ? : err;
1832 tls_read_flush_backlog(struct sock *sk, struct tls_prot_info *prot,
1833 size_t len_left, size_t decrypted, ssize_t done,
1838 if (len_left <= decrypted)
1841 max_rec = prot->overhead_size - prot->tail_size + TLS_MAX_PAYLOAD_SIZE;
1842 if (done - *flushed_at < SZ_128K && tcp_inq(sk) > max_rec)
1846 return sk_flush_backlog(sk);
1849 static int tls_rx_reader_lock(struct sock *sk, struct tls_sw_context_rx *ctx,
1857 timeo = sock_rcvtimeo(sk, nonblock);
1859 while (unlikely(ctx->reader_present)) {
1860 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1862 ctx->reader_contended = 1;
1864 add_wait_queue(&ctx->wq, &wait);
1865 sk_wait_event(sk, &timeo,
1866 !READ_ONCE(ctx->reader_present), &wait);
1867 remove_wait_queue(&ctx->wq, &wait);
1873 if (signal_pending(current)) {
1874 err = sock_intr_errno(timeo);
1879 WRITE_ONCE(ctx->reader_present, 1);
1888 static void tls_rx_reader_unlock(struct sock *sk, struct tls_sw_context_rx *ctx)
1890 if (unlikely(ctx->reader_contended)) {
1891 if (wq_has_sleeper(&ctx->wq))
1894 ctx->reader_contended = 0;
1896 WARN_ON_ONCE(!ctx->reader_present);
1899 WRITE_ONCE(ctx->reader_present, 0);
1903 int tls_sw_recvmsg(struct sock *sk,
1909 struct tls_context *tls_ctx = tls_get_ctx(sk);
1910 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1911 struct tls_prot_info *prot = &tls_ctx->prot_info;
1912 ssize_t decrypted = 0, async_copy_bytes = 0;
1913 struct sk_psock *psock;
1914 unsigned char control = 0;
1915 size_t flushed_at = 0;
1916 struct strp_msg *rxm;
1917 struct tls_msg *tlm;
1921 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
1922 bool is_peek = flags & MSG_PEEK;
1923 bool released = true;
1924 bool bpf_strp_enabled;
1927 if (unlikely(flags & MSG_ERRQUEUE))
1928 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1930 psock = sk_psock_get(sk);
1931 err = tls_rx_reader_lock(sk, ctx, flags & MSG_DONTWAIT);
1934 bpf_strp_enabled = sk_psock_strp_enabled(psock);
1936 /* If crypto failed the connection is broken */
1937 err = ctx->async_wait.err;
1941 /* Process pending decrypted records. It must be non-zero-copy */
1942 err = process_rx_list(ctx, msg, &control, 0, len, is_peek);
1950 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1953 zc_capable = !bpf_strp_enabled && !is_kvec && !is_peek &&
1956 while (len && (decrypted + copied < target || tls_strp_msg_ready(ctx))) {
1957 struct tls_decrypt_arg darg;
1958 int to_decrypt, chunk;
1960 err = tls_rx_rec_wait(sk, psock, flags & MSG_DONTWAIT,
1964 chunk = sk_msg_recvmsg(sk, psock, msg, len,
1975 memset(&darg.inargs, 0, sizeof(darg.inargs));
1977 rxm = strp_msg(tls_strp_msg(ctx));
1978 tlm = tls_msg(tls_strp_msg(ctx));
1980 to_decrypt = rxm->full_len - prot->overhead_size;
1982 if (zc_capable && to_decrypt <= len &&
1983 tlm->control == TLS_RECORD_TYPE_DATA)
1986 /* Do not use async mode if record is non-data */
1987 if (tlm->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled)
1988 darg.async = ctx->async_capable;
1992 err = tls_rx_one_record(sk, msg, &darg);
1994 tls_err_abort(sk, -EBADMSG);
1998 async |= darg.async;
2000 /* If the type of records being processed is not known yet,
2001 * set it to record type just dequeued. If it is already known,
2002 * but does not match the record type just dequeued, go to end.
2003 * We always get record type here since for tls1.2, record type
2004 * is known just after record is dequeued from stream parser.
2005 * For tls1.3, we disable async.
2007 err = tls_record_content_type(msg, tls_msg(darg.skb), &control);
2009 DEBUG_NET_WARN_ON_ONCE(darg.zc);
2010 tls_rx_rec_done(ctx);
2012 __skb_queue_tail(&ctx->rx_list, darg.skb);
2016 /* periodically flush backlog, and feed strparser */
2017 released = tls_read_flush_backlog(sk, prot, len, to_decrypt,
2021 /* TLS 1.3 may have updated the length by more than overhead */
2022 rxm = strp_msg(darg.skb);
2023 chunk = rxm->full_len;
2024 tls_rx_rec_done(ctx);
2027 bool partially_consumed = chunk > len;
2028 struct sk_buff *skb = darg.skb;
2030 DEBUG_NET_WARN_ON_ONCE(darg.skb == ctx->strp.anchor);
2033 /* TLS 1.2-only, to_decrypt must be text len */
2034 chunk = min_t(int, to_decrypt, len);
2035 async_copy_bytes += chunk;
2039 __skb_queue_tail(&ctx->rx_list, skb);
2043 if (bpf_strp_enabled) {
2045 err = sk_psock_tls_strp_read(psock, skb);
2046 if (err != __SK_PASS) {
2047 rxm->offset = rxm->offset + rxm->full_len;
2049 if (err == __SK_DROP)
2055 if (partially_consumed)
2058 err = skb_copy_datagram_msg(skb, rxm->offset,
2061 goto put_on_rx_list_err;
2064 goto put_on_rx_list;
2066 if (partially_consumed) {
2067 rxm->offset += chunk;
2068 rxm->full_len -= chunk;
2069 goto put_on_rx_list;
2078 /* Return full control message to userspace before trying
2079 * to parse another message type
2081 msg->msg_flags |= MSG_EOR;
2082 if (control != TLS_RECORD_TYPE_DATA)
2090 /* Wait for all previously submitted records to be decrypted */
2091 spin_lock_bh(&ctx->decrypt_compl_lock);
2092 reinit_completion(&ctx->async_wait.completion);
2093 pending = atomic_read(&ctx->decrypt_pending);
2094 spin_unlock_bh(&ctx->decrypt_compl_lock);
2097 ret = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
2098 __skb_queue_purge(&ctx->async_hold);
2101 if (err >= 0 || err == -EINPROGRESS)
2107 /* Drain records from the rx_list & copy if required */
2108 if (is_peek || is_kvec)
2109 err = process_rx_list(ctx, msg, &control, copied,
2110 decrypted, is_peek);
2112 err = process_rx_list(ctx, msg, &control, 0,
2113 async_copy_bytes, is_peek);
2114 decrypted = max(err, 0);
2117 copied += decrypted;
2120 tls_rx_reader_unlock(sk, ctx);
2122 sk_psock_put(sk, psock);
2123 return copied ? : err;
2126 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
2127 struct pipe_inode_info *pipe,
2128 size_t len, unsigned int flags)
2130 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
2131 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2132 struct strp_msg *rxm = NULL;
2133 struct sock *sk = sock->sk;
2134 struct tls_msg *tlm;
2135 struct sk_buff *skb;
2140 err = tls_rx_reader_lock(sk, ctx, flags & SPLICE_F_NONBLOCK);
2144 if (!skb_queue_empty(&ctx->rx_list)) {
2145 skb = __skb_dequeue(&ctx->rx_list);
2147 struct tls_decrypt_arg darg;
2149 err = tls_rx_rec_wait(sk, NULL, flags & SPLICE_F_NONBLOCK,
2152 goto splice_read_end;
2154 memset(&darg.inargs, 0, sizeof(darg.inargs));
2156 err = tls_rx_one_record(sk, NULL, &darg);
2158 tls_err_abort(sk, -EBADMSG);
2159 goto splice_read_end;
2162 tls_rx_rec_done(ctx);
2166 rxm = strp_msg(skb);
2169 /* splice does not support reading control messages */
2170 if (tlm->control != TLS_RECORD_TYPE_DATA) {
2172 goto splice_requeue;
2175 chunk = min_t(unsigned int, rxm->full_len, len);
2176 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
2178 goto splice_requeue;
2180 if (chunk < rxm->full_len) {
2182 rxm->full_len -= len;
2183 goto splice_requeue;
2189 tls_rx_reader_unlock(sk, ctx);
2190 return copied ? : err;
2193 __skb_queue_head(&ctx->rx_list, skb);
2194 goto splice_read_end;
2197 bool tls_sw_sock_is_readable(struct sock *sk)
2199 struct tls_context *tls_ctx = tls_get_ctx(sk);
2200 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2201 bool ingress_empty = true;
2202 struct sk_psock *psock;
2205 psock = sk_psock(sk);
2207 ingress_empty = list_empty(&psock->ingress_msg);
2210 return !ingress_empty || tls_strp_msg_ready(ctx) ||
2211 !skb_queue_empty(&ctx->rx_list);
2214 int tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb)
2216 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
2217 struct tls_prot_info *prot = &tls_ctx->prot_info;
2218 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
2219 size_t cipher_overhead;
2220 size_t data_len = 0;
2223 /* Verify that we have a full TLS header, or wait for more data */
2224 if (strp->stm.offset + prot->prepend_size > skb->len)
2227 /* Sanity-check size of on-stack buffer. */
2228 if (WARN_ON(prot->prepend_size > sizeof(header))) {
2233 /* Linearize header to local buffer */
2234 ret = skb_copy_bits(skb, strp->stm.offset, header, prot->prepend_size);
2238 strp->mark = header[0];
2240 data_len = ((header[4] & 0xFF) | (header[3] << 8));
2242 cipher_overhead = prot->tag_size;
2243 if (prot->version != TLS_1_3_VERSION &&
2244 prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
2245 cipher_overhead += prot->iv_size;
2247 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead +
2252 if (data_len < cipher_overhead) {
2257 /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */
2258 if (header[1] != TLS_1_2_VERSION_MINOR ||
2259 header[2] != TLS_1_2_VERSION_MAJOR) {
2264 tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE,
2265 TCP_SKB_CB(skb)->seq + strp->stm.offset);
2266 return data_len + TLS_HEADER_SIZE;
2269 tls_err_abort(strp->sk, ret);
2274 void tls_rx_msg_ready(struct tls_strparser *strp)
2276 struct tls_sw_context_rx *ctx;
2278 ctx = container_of(strp, struct tls_sw_context_rx, strp);
2279 ctx->saved_data_ready(strp->sk);
2282 static void tls_data_ready(struct sock *sk)
2284 struct tls_context *tls_ctx = tls_get_ctx(sk);
2285 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2286 struct sk_psock *psock;
2288 trace_sk_data_ready(sk);
2290 tls_strp_data_ready(&ctx->strp);
2292 psock = sk_psock_get(sk);
2294 if (!list_empty(&psock->ingress_msg))
2295 ctx->saved_data_ready(sk);
2296 sk_psock_put(sk, psock);
2300 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx)
2302 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2304 set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask);
2305 set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask);
2306 cancel_delayed_work_sync(&ctx->tx_work.work);
2309 void tls_sw_release_resources_tx(struct sock *sk)
2311 struct tls_context *tls_ctx = tls_get_ctx(sk);
2312 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2313 struct tls_rec *rec, *tmp;
2316 /* Wait for any pending async encryptions to complete */
2317 spin_lock_bh(&ctx->encrypt_compl_lock);
2318 ctx->async_notify = true;
2319 pending = atomic_read(&ctx->encrypt_pending);
2320 spin_unlock_bh(&ctx->encrypt_compl_lock);
2323 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
2325 tls_tx_records(sk, -1);
2327 /* Free up un-sent records in tx_list. First, free
2328 * the partially sent record if any at head of tx_list.
2330 if (tls_ctx->partially_sent_record) {
2331 tls_free_partial_record(sk, tls_ctx);
2332 rec = list_first_entry(&ctx->tx_list,
2333 struct tls_rec, list);
2334 list_del(&rec->list);
2335 sk_msg_free(sk, &rec->msg_plaintext);
2339 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
2340 list_del(&rec->list);
2341 sk_msg_free(sk, &rec->msg_encrypted);
2342 sk_msg_free(sk, &rec->msg_plaintext);
2346 crypto_free_aead(ctx->aead_send);
2347 tls_free_open_rec(sk);
2350 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx)
2352 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
2357 void tls_sw_release_resources_rx(struct sock *sk)
2359 struct tls_context *tls_ctx = tls_get_ctx(sk);
2360 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2362 kfree(tls_ctx->rx.rec_seq);
2363 kfree(tls_ctx->rx.iv);
2365 if (ctx->aead_recv) {
2366 __skb_queue_purge(&ctx->rx_list);
2367 crypto_free_aead(ctx->aead_recv);
2368 tls_strp_stop(&ctx->strp);
2369 /* If tls_sw_strparser_arm() was not called (cleanup paths)
2370 * we still want to tls_strp_stop(), but sk->sk_data_ready was
2373 if (ctx->saved_data_ready) {
2374 write_lock_bh(&sk->sk_callback_lock);
2375 sk->sk_data_ready = ctx->saved_data_ready;
2376 write_unlock_bh(&sk->sk_callback_lock);
2381 void tls_sw_strparser_done(struct tls_context *tls_ctx)
2383 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2385 tls_strp_done(&ctx->strp);
2388 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx)
2390 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
2395 void tls_sw_free_resources_rx(struct sock *sk)
2397 struct tls_context *tls_ctx = tls_get_ctx(sk);
2399 tls_sw_release_resources_rx(sk);
2400 tls_sw_free_ctx_rx(tls_ctx);
2403 /* The work handler to transmitt the encrypted records in tx_list */
2404 static void tx_work_handler(struct work_struct *work)
2406 struct delayed_work *delayed_work = to_delayed_work(work);
2407 struct tx_work *tx_work = container_of(delayed_work,
2408 struct tx_work, work);
2409 struct sock *sk = tx_work->sk;
2410 struct tls_context *tls_ctx = tls_get_ctx(sk);
2411 struct tls_sw_context_tx *ctx;
2413 if (unlikely(!tls_ctx))
2416 ctx = tls_sw_ctx_tx(tls_ctx);
2417 if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask))
2420 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
2422 mutex_lock(&tls_ctx->tx_lock);
2424 tls_tx_records(sk, -1);
2426 mutex_unlock(&tls_ctx->tx_lock);
2429 static bool tls_is_tx_ready(struct tls_sw_context_tx *ctx)
2431 struct tls_rec *rec;
2433 rec = list_first_entry_or_null(&ctx->tx_list, struct tls_rec, list);
2437 return READ_ONCE(rec->tx_ready);
2440 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx)
2442 struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx);
2444 /* Schedule the transmission if tx list is ready */
2445 if (tls_is_tx_ready(tx_ctx) &&
2446 !test_and_set_bit(BIT_TX_SCHEDULED, &tx_ctx->tx_bitmask))
2447 schedule_delayed_work(&tx_ctx->tx_work.work, 0);
2450 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx)
2452 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2454 write_lock_bh(&sk->sk_callback_lock);
2455 rx_ctx->saved_data_ready = sk->sk_data_ready;
2456 sk->sk_data_ready = tls_data_ready;
2457 write_unlock_bh(&sk->sk_callback_lock);
2460 void tls_update_rx_zc_capable(struct tls_context *tls_ctx)
2462 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx);
2464 rx_ctx->zc_capable = tls_ctx->rx_no_pad ||
2465 tls_ctx->prot_info.version != TLS_1_3_VERSION;
2468 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
2470 struct tls_context *tls_ctx = tls_get_ctx(sk);
2471 struct tls_prot_info *prot = &tls_ctx->prot_info;
2472 struct tls_crypto_info *crypto_info;
2473 struct tls_sw_context_tx *sw_ctx_tx = NULL;
2474 struct tls_sw_context_rx *sw_ctx_rx = NULL;
2475 struct cipher_context *cctx;
2476 struct crypto_aead **aead;
2477 u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size;
2478 struct crypto_tfm *tfm;
2479 char *iv, *rec_seq, *key, *salt, *cipher_name;
2489 if (!ctx->priv_ctx_tx) {
2490 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
2495 ctx->priv_ctx_tx = sw_ctx_tx;
2498 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
2501 if (!ctx->priv_ctx_rx) {
2502 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
2507 ctx->priv_ctx_rx = sw_ctx_rx;
2510 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
2515 crypto_init_wait(&sw_ctx_tx->async_wait);
2516 spin_lock_init(&sw_ctx_tx->encrypt_compl_lock);
2517 crypto_info = &ctx->crypto_send.info;
2519 aead = &sw_ctx_tx->aead_send;
2520 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
2521 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
2522 sw_ctx_tx->tx_work.sk = sk;
2524 crypto_init_wait(&sw_ctx_rx->async_wait);
2525 spin_lock_init(&sw_ctx_rx->decrypt_compl_lock);
2526 init_waitqueue_head(&sw_ctx_rx->wq);
2527 crypto_info = &ctx->crypto_recv.info;
2529 skb_queue_head_init(&sw_ctx_rx->rx_list);
2530 skb_queue_head_init(&sw_ctx_rx->async_hold);
2531 aead = &sw_ctx_rx->aead_recv;
2534 switch (crypto_info->cipher_type) {
2535 case TLS_CIPHER_AES_GCM_128: {
2536 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
2538 gcm_128_info = (void *)crypto_info;
2539 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2540 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
2541 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2542 iv = gcm_128_info->iv;
2543 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
2544 rec_seq = gcm_128_info->rec_seq;
2545 keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE;
2546 key = gcm_128_info->key;
2547 salt = gcm_128_info->salt;
2548 salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE;
2549 cipher_name = "gcm(aes)";
2552 case TLS_CIPHER_AES_GCM_256: {
2553 struct tls12_crypto_info_aes_gcm_256 *gcm_256_info;
2555 gcm_256_info = (void *)crypto_info;
2556 nonce_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2557 tag_size = TLS_CIPHER_AES_GCM_256_TAG_SIZE;
2558 iv_size = TLS_CIPHER_AES_GCM_256_IV_SIZE;
2559 iv = gcm_256_info->iv;
2560 rec_seq_size = TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE;
2561 rec_seq = gcm_256_info->rec_seq;
2562 keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE;
2563 key = gcm_256_info->key;
2564 salt = gcm_256_info->salt;
2565 salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE;
2566 cipher_name = "gcm(aes)";
2569 case TLS_CIPHER_AES_CCM_128: {
2570 struct tls12_crypto_info_aes_ccm_128 *ccm_128_info;
2572 ccm_128_info = (void *)crypto_info;
2573 nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2574 tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE;
2575 iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE;
2576 iv = ccm_128_info->iv;
2577 rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE;
2578 rec_seq = ccm_128_info->rec_seq;
2579 keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE;
2580 key = ccm_128_info->key;
2581 salt = ccm_128_info->salt;
2582 salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE;
2583 cipher_name = "ccm(aes)";
2586 case TLS_CIPHER_CHACHA20_POLY1305: {
2587 struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305_info;
2589 chacha20_poly1305_info = (void *)crypto_info;
2591 tag_size = TLS_CIPHER_CHACHA20_POLY1305_TAG_SIZE;
2592 iv_size = TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE;
2593 iv = chacha20_poly1305_info->iv;
2594 rec_seq_size = TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE;
2595 rec_seq = chacha20_poly1305_info->rec_seq;
2596 keysize = TLS_CIPHER_CHACHA20_POLY1305_KEY_SIZE;
2597 key = chacha20_poly1305_info->key;
2598 salt = chacha20_poly1305_info->salt;
2599 salt_size = TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE;
2600 cipher_name = "rfc7539(chacha20,poly1305)";
2603 case TLS_CIPHER_SM4_GCM: {
2604 struct tls12_crypto_info_sm4_gcm *sm4_gcm_info;
2606 sm4_gcm_info = (void *)crypto_info;
2607 nonce_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2608 tag_size = TLS_CIPHER_SM4_GCM_TAG_SIZE;
2609 iv_size = TLS_CIPHER_SM4_GCM_IV_SIZE;
2610 iv = sm4_gcm_info->iv;
2611 rec_seq_size = TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE;
2612 rec_seq = sm4_gcm_info->rec_seq;
2613 keysize = TLS_CIPHER_SM4_GCM_KEY_SIZE;
2614 key = sm4_gcm_info->key;
2615 salt = sm4_gcm_info->salt;
2616 salt_size = TLS_CIPHER_SM4_GCM_SALT_SIZE;
2617 cipher_name = "gcm(sm4)";
2620 case TLS_CIPHER_SM4_CCM: {
2621 struct tls12_crypto_info_sm4_ccm *sm4_ccm_info;
2623 sm4_ccm_info = (void *)crypto_info;
2624 nonce_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2625 tag_size = TLS_CIPHER_SM4_CCM_TAG_SIZE;
2626 iv_size = TLS_CIPHER_SM4_CCM_IV_SIZE;
2627 iv = sm4_ccm_info->iv;
2628 rec_seq_size = TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE;
2629 rec_seq = sm4_ccm_info->rec_seq;
2630 keysize = TLS_CIPHER_SM4_CCM_KEY_SIZE;
2631 key = sm4_ccm_info->key;
2632 salt = sm4_ccm_info->salt;
2633 salt_size = TLS_CIPHER_SM4_CCM_SALT_SIZE;
2634 cipher_name = "ccm(sm4)";
2637 case TLS_CIPHER_ARIA_GCM_128: {
2638 struct tls12_crypto_info_aria_gcm_128 *aria_gcm_128_info;
2640 aria_gcm_128_info = (void *)crypto_info;
2641 nonce_size = TLS_CIPHER_ARIA_GCM_128_IV_SIZE;
2642 tag_size = TLS_CIPHER_ARIA_GCM_128_TAG_SIZE;
2643 iv_size = TLS_CIPHER_ARIA_GCM_128_IV_SIZE;
2644 iv = aria_gcm_128_info->iv;
2645 rec_seq_size = TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE;
2646 rec_seq = aria_gcm_128_info->rec_seq;
2647 keysize = TLS_CIPHER_ARIA_GCM_128_KEY_SIZE;
2648 key = aria_gcm_128_info->key;
2649 salt = aria_gcm_128_info->salt;
2650 salt_size = TLS_CIPHER_ARIA_GCM_128_SALT_SIZE;
2651 cipher_name = "gcm(aria)";
2654 case TLS_CIPHER_ARIA_GCM_256: {
2655 struct tls12_crypto_info_aria_gcm_256 *gcm_256_info;
2657 gcm_256_info = (void *)crypto_info;
2658 nonce_size = TLS_CIPHER_ARIA_GCM_256_IV_SIZE;
2659 tag_size = TLS_CIPHER_ARIA_GCM_256_TAG_SIZE;
2660 iv_size = TLS_CIPHER_ARIA_GCM_256_IV_SIZE;
2661 iv = gcm_256_info->iv;
2662 rec_seq_size = TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE;
2663 rec_seq = gcm_256_info->rec_seq;
2664 keysize = TLS_CIPHER_ARIA_GCM_256_KEY_SIZE;
2665 key = gcm_256_info->key;
2666 salt = gcm_256_info->salt;
2667 salt_size = TLS_CIPHER_ARIA_GCM_256_SALT_SIZE;
2668 cipher_name = "gcm(aria)";
2676 if (crypto_info->version == TLS_1_3_VERSION) {
2678 prot->aad_size = TLS_HEADER_SIZE;
2679 prot->tail_size = 1;
2681 prot->aad_size = TLS_AAD_SPACE_SIZE;
2682 prot->tail_size = 0;
2685 /* Sanity-check the sizes for stack allocations. */
2686 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE ||
2687 rec_seq_size > TLS_MAX_REC_SEQ_SIZE || tag_size != TLS_TAG_SIZE ||
2688 prot->aad_size > TLS_MAX_AAD_SIZE) {
2693 prot->version = crypto_info->version;
2694 prot->cipher_type = crypto_info->cipher_type;
2695 prot->prepend_size = TLS_HEADER_SIZE + nonce_size;
2696 prot->tag_size = tag_size;
2697 prot->overhead_size = prot->prepend_size +
2698 prot->tag_size + prot->tail_size;
2699 prot->iv_size = iv_size;
2700 prot->salt_size = salt_size;
2701 cctx->iv = kmalloc(iv_size + salt_size, GFP_KERNEL);
2706 /* Note: 128 & 256 bit salt are the same size */
2707 prot->rec_seq_size = rec_seq_size;
2708 memcpy(cctx->iv, salt, salt_size);
2709 memcpy(cctx->iv + salt_size, iv, iv_size);
2710 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
2711 if (!cctx->rec_seq) {
2717 *aead = crypto_alloc_aead(cipher_name, 0, 0);
2718 if (IS_ERR(*aead)) {
2719 rc = PTR_ERR(*aead);
2725 ctx->push_pending_record = tls_sw_push_pending_record;
2727 rc = crypto_aead_setkey(*aead, key, keysize);
2732 rc = crypto_aead_setauthsize(*aead, prot->tag_size);
2737 tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
2739 tls_update_rx_zc_capable(ctx);
2740 sw_ctx_rx->async_capable =
2741 crypto_info->version != TLS_1_3_VERSION &&
2742 !!(tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC);
2744 rc = tls_strp_init(&sw_ctx_rx->strp, sk);
2752 crypto_free_aead(*aead);
2755 kfree(cctx->rec_seq);
2756 cctx->rec_seq = NULL;
2762 kfree(ctx->priv_ctx_tx);
2763 ctx->priv_ctx_tx = NULL;
2765 kfree(ctx->priv_ctx_rx);
2766 ctx->priv_ctx_rx = NULL;