13c4d7559SDave Watson /* 23c4d7559SDave Watson * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. 33c4d7559SDave Watson * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved. 43c4d7559SDave Watson * Copyright (c) 2016-2017, Lance Chao <lancerchao@fb.com>. All rights reserved. 53c4d7559SDave Watson * Copyright (c) 2016, Fridolin Pokorny <fridolin.pokorny@gmail.com>. All rights reserved. 63c4d7559SDave Watson * Copyright (c) 2016, Nikos Mavrogiannopoulos <nmav@gnutls.org>. All rights reserved. 73c4d7559SDave Watson * 83c4d7559SDave Watson * This software is available to you under a choice of one of two 93c4d7559SDave Watson * licenses. You may choose to be licensed under the terms of the GNU 103c4d7559SDave Watson * General Public License (GPL) Version 2, available from the file 113c4d7559SDave Watson * COPYING in the main directory of this source tree, or the 123c4d7559SDave Watson * OpenIB.org BSD license below: 133c4d7559SDave Watson * 143c4d7559SDave Watson * Redistribution and use in source and binary forms, with or 153c4d7559SDave Watson * without modification, are permitted provided that the following 163c4d7559SDave Watson * conditions are met: 173c4d7559SDave Watson * 183c4d7559SDave Watson * - Redistributions of source code must retain the above 193c4d7559SDave Watson * copyright notice, this list of conditions and the following 203c4d7559SDave Watson * disclaimer. 213c4d7559SDave Watson * 223c4d7559SDave Watson * - Redistributions in binary form must reproduce the above 233c4d7559SDave Watson * copyright notice, this list of conditions and the following 243c4d7559SDave Watson * disclaimer in the documentation and/or other materials 253c4d7559SDave Watson * provided with the distribution. 263c4d7559SDave Watson * 273c4d7559SDave Watson * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 283c4d7559SDave Watson * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 293c4d7559SDave Watson * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 303c4d7559SDave Watson * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 313c4d7559SDave Watson * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 323c4d7559SDave Watson * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 333c4d7559SDave Watson * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 343c4d7559SDave Watson * SOFTWARE. 353c4d7559SDave Watson */ 363c4d7559SDave Watson 37c46234ebSDave Watson #include <linux/sched/signal.h> 383c4d7559SDave Watson #include <linux/module.h> 393c4d7559SDave Watson #include <crypto/aead.h> 403c4d7559SDave Watson 41c46234ebSDave Watson #include <net/strparser.h> 423c4d7559SDave Watson #include <net/tls.h> 433c4d7559SDave Watson 44b16520f7SKees Cook #define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE 45b16520f7SKees Cook 46c46234ebSDave Watson static int tls_do_decryption(struct sock *sk, 47c46234ebSDave Watson struct scatterlist *sgin, 48c46234ebSDave Watson struct scatterlist *sgout, 49c46234ebSDave Watson char *iv_recv, 50c46234ebSDave Watson size_t data_len, 51c46234ebSDave Watson struct sk_buff *skb, 52c46234ebSDave Watson gfp_t flags) 53c46234ebSDave Watson { 54c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 55f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 56c46234ebSDave Watson struct strp_msg *rxm = strp_msg(skb); 57c46234ebSDave Watson struct aead_request *aead_req; 58c46234ebSDave Watson 59c46234ebSDave Watson int ret; 60c46234ebSDave Watson 61d2bdd268SVakul Garg aead_req = aead_request_alloc(ctx->aead_recv, flags); 62c46234ebSDave Watson if (!aead_req) 63c46234ebSDave Watson return -ENOMEM; 64c46234ebSDave Watson 65c46234ebSDave Watson aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); 66c46234ebSDave Watson aead_request_set_crypt(aead_req, sgin, sgout, 67c46234ebSDave Watson data_len + tls_ctx->rx.tag_size, 68c46234ebSDave Watson (u8 *)iv_recv); 69c46234ebSDave Watson aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, 70c46234ebSDave Watson crypto_req_done, &ctx->async_wait); 71c46234ebSDave Watson 72c46234ebSDave Watson ret = crypto_wait_req(crypto_aead_decrypt(aead_req), &ctx->async_wait); 73c46234ebSDave Watson 74c46234ebSDave Watson if (ret < 0) 75c46234ebSDave Watson goto out; 76c46234ebSDave Watson 77c46234ebSDave Watson rxm->offset += tls_ctx->rx.prepend_size; 78c46234ebSDave Watson rxm->full_len -= tls_ctx->rx.overhead_size; 79c46234ebSDave Watson tls_advance_record_sn(sk, &tls_ctx->rx); 80c46234ebSDave Watson 81c46234ebSDave Watson ctx->decrypted = true; 82c46234ebSDave Watson 83c46234ebSDave Watson ctx->saved_data_ready(sk); 84c46234ebSDave Watson 85c46234ebSDave Watson out: 86d2bdd268SVakul Garg aead_request_free(aead_req); 87c46234ebSDave Watson return ret; 88c46234ebSDave Watson } 89c46234ebSDave Watson 903c4d7559SDave Watson static void trim_sg(struct sock *sk, struct scatterlist *sg, 913c4d7559SDave Watson int *sg_num_elem, unsigned int *sg_size, int target_size) 923c4d7559SDave Watson { 933c4d7559SDave Watson int i = *sg_num_elem - 1; 943c4d7559SDave Watson int trim = *sg_size - target_size; 953c4d7559SDave Watson 963c4d7559SDave Watson if (trim <= 0) { 973c4d7559SDave Watson WARN_ON(trim < 0); 983c4d7559SDave Watson return; 993c4d7559SDave Watson } 1003c4d7559SDave Watson 1013c4d7559SDave Watson *sg_size = target_size; 1023c4d7559SDave Watson while (trim >= sg[i].length) { 1033c4d7559SDave Watson trim -= sg[i].length; 1043c4d7559SDave Watson sk_mem_uncharge(sk, sg[i].length); 1053c4d7559SDave Watson put_page(sg_page(&sg[i])); 1063c4d7559SDave Watson i--; 1073c4d7559SDave Watson 1083c4d7559SDave Watson if (i < 0) 1093c4d7559SDave Watson goto out; 1103c4d7559SDave Watson } 1113c4d7559SDave Watson 1123c4d7559SDave Watson sg[i].length -= trim; 1133c4d7559SDave Watson sk_mem_uncharge(sk, trim); 1143c4d7559SDave Watson 1153c4d7559SDave Watson out: 1163c4d7559SDave Watson *sg_num_elem = i + 1; 1173c4d7559SDave Watson } 1183c4d7559SDave Watson 1193c4d7559SDave Watson static void trim_both_sgl(struct sock *sk, int target_size) 1203c4d7559SDave Watson { 1213c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 122f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1233c4d7559SDave Watson 1243c4d7559SDave Watson trim_sg(sk, ctx->sg_plaintext_data, 1253c4d7559SDave Watson &ctx->sg_plaintext_num_elem, 1263c4d7559SDave Watson &ctx->sg_plaintext_size, 1273c4d7559SDave Watson target_size); 1283c4d7559SDave Watson 1293c4d7559SDave Watson if (target_size > 0) 130dbe42559SDave Watson target_size += tls_ctx->tx.overhead_size; 1313c4d7559SDave Watson 1323c4d7559SDave Watson trim_sg(sk, ctx->sg_encrypted_data, 1333c4d7559SDave Watson &ctx->sg_encrypted_num_elem, 1343c4d7559SDave Watson &ctx->sg_encrypted_size, 1353c4d7559SDave Watson target_size); 1363c4d7559SDave Watson } 1373c4d7559SDave Watson 1383c4d7559SDave Watson static int alloc_encrypted_sg(struct sock *sk, int len) 1393c4d7559SDave Watson { 1403c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 141f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1423c4d7559SDave Watson int rc = 0; 1433c4d7559SDave Watson 1442c3682f0SJohn Fastabend rc = sk_alloc_sg(sk, len, 1458c05dbf0SJohn Fastabend ctx->sg_encrypted_data, 0, 1462c3682f0SJohn Fastabend &ctx->sg_encrypted_num_elem, 1472c3682f0SJohn Fastabend &ctx->sg_encrypted_size, 0); 1483c4d7559SDave Watson 1493c4d7559SDave Watson return rc; 1503c4d7559SDave Watson } 1513c4d7559SDave Watson 1523c4d7559SDave Watson static int alloc_plaintext_sg(struct sock *sk, int len) 1533c4d7559SDave Watson { 1543c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 155f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1563c4d7559SDave Watson int rc = 0; 1573c4d7559SDave Watson 1588c05dbf0SJohn Fastabend rc = sk_alloc_sg(sk, len, ctx->sg_plaintext_data, 0, 1593c4d7559SDave Watson &ctx->sg_plaintext_num_elem, &ctx->sg_plaintext_size, 1603c4d7559SDave Watson tls_ctx->pending_open_record_frags); 1613c4d7559SDave Watson 1623c4d7559SDave Watson return rc; 1633c4d7559SDave Watson } 1643c4d7559SDave Watson 1653c4d7559SDave Watson static void free_sg(struct sock *sk, struct scatterlist *sg, 1663c4d7559SDave Watson int *sg_num_elem, unsigned int *sg_size) 1673c4d7559SDave Watson { 1683c4d7559SDave Watson int i, n = *sg_num_elem; 1693c4d7559SDave Watson 1703c4d7559SDave Watson for (i = 0; i < n; ++i) { 1713c4d7559SDave Watson sk_mem_uncharge(sk, sg[i].length); 1723c4d7559SDave Watson put_page(sg_page(&sg[i])); 1733c4d7559SDave Watson } 1743c4d7559SDave Watson *sg_num_elem = 0; 1753c4d7559SDave Watson *sg_size = 0; 1763c4d7559SDave Watson } 1773c4d7559SDave Watson 1783c4d7559SDave Watson static void tls_free_both_sg(struct sock *sk) 1793c4d7559SDave Watson { 1803c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 181f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1823c4d7559SDave Watson 1833c4d7559SDave Watson free_sg(sk, ctx->sg_encrypted_data, &ctx->sg_encrypted_num_elem, 1843c4d7559SDave Watson &ctx->sg_encrypted_size); 1853c4d7559SDave Watson 1863c4d7559SDave Watson free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem, 1873c4d7559SDave Watson &ctx->sg_plaintext_size); 1883c4d7559SDave Watson } 1893c4d7559SDave Watson 1903c4d7559SDave Watson static int tls_do_encryption(struct tls_context *tls_ctx, 191a447da7dSDaniel Borkmann struct tls_sw_context_tx *ctx, 192a447da7dSDaniel Borkmann struct aead_request *aead_req, 193a447da7dSDaniel Borkmann size_t data_len) 1943c4d7559SDave Watson { 1953c4d7559SDave Watson int rc; 1963c4d7559SDave Watson 197dbe42559SDave Watson ctx->sg_encrypted_data[0].offset += tls_ctx->tx.prepend_size; 198dbe42559SDave Watson ctx->sg_encrypted_data[0].length -= tls_ctx->tx.prepend_size; 1993c4d7559SDave Watson 2003c4d7559SDave Watson aead_request_set_tfm(aead_req, ctx->aead_send); 2013c4d7559SDave Watson aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); 2023c4d7559SDave Watson aead_request_set_crypt(aead_req, ctx->sg_aead_in, ctx->sg_aead_out, 203dbe42559SDave Watson data_len, tls_ctx->tx.iv); 204a54667f6SVakul Garg 205a54667f6SVakul Garg aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, 206a54667f6SVakul Garg crypto_req_done, &ctx->async_wait); 207a54667f6SVakul Garg 208a54667f6SVakul Garg rc = crypto_wait_req(crypto_aead_encrypt(aead_req), &ctx->async_wait); 2093c4d7559SDave Watson 210dbe42559SDave Watson ctx->sg_encrypted_data[0].offset -= tls_ctx->tx.prepend_size; 211dbe42559SDave Watson ctx->sg_encrypted_data[0].length += tls_ctx->tx.prepend_size; 2123c4d7559SDave Watson 2133c4d7559SDave Watson return rc; 2143c4d7559SDave Watson } 2153c4d7559SDave Watson 2163c4d7559SDave Watson static int tls_push_record(struct sock *sk, int flags, 2173c4d7559SDave Watson unsigned char record_type) 2183c4d7559SDave Watson { 2193c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 220f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 221a447da7dSDaniel Borkmann struct aead_request *req; 2223c4d7559SDave Watson int rc; 2233c4d7559SDave Watson 224d2bdd268SVakul Garg req = aead_request_alloc(ctx->aead_send, sk->sk_allocation); 225a447da7dSDaniel Borkmann if (!req) 226a447da7dSDaniel Borkmann return -ENOMEM; 227a447da7dSDaniel Borkmann 2283c4d7559SDave Watson sg_mark_end(ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem - 1); 2293c4d7559SDave Watson sg_mark_end(ctx->sg_encrypted_data + ctx->sg_encrypted_num_elem - 1); 2303c4d7559SDave Watson 231213ef6e7SIlya Lesokhin tls_make_aad(ctx->aad_space, ctx->sg_plaintext_size, 232dbe42559SDave Watson tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size, 2333c4d7559SDave Watson record_type); 2343c4d7559SDave Watson 2353c4d7559SDave Watson tls_fill_prepend(tls_ctx, 2363c4d7559SDave Watson page_address(sg_page(&ctx->sg_encrypted_data[0])) + 2373c4d7559SDave Watson ctx->sg_encrypted_data[0].offset, 2383c4d7559SDave Watson ctx->sg_plaintext_size, record_type); 2393c4d7559SDave Watson 2403c4d7559SDave Watson tls_ctx->pending_open_record_frags = 0; 2413c4d7559SDave Watson set_bit(TLS_PENDING_CLOSED_RECORD, &tls_ctx->flags); 2423c4d7559SDave Watson 243a447da7dSDaniel Borkmann rc = tls_do_encryption(tls_ctx, ctx, req, ctx->sg_plaintext_size); 2443c4d7559SDave Watson if (rc < 0) { 2453c4d7559SDave Watson /* If we are called from write_space and 2463c4d7559SDave Watson * we fail, we need to set this SOCK_NOSPACE 2473c4d7559SDave Watson * to trigger another write_space in the future. 2483c4d7559SDave Watson */ 2493c4d7559SDave Watson set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 250a447da7dSDaniel Borkmann goto out_req; 2513c4d7559SDave Watson } 2523c4d7559SDave Watson 2533c4d7559SDave Watson free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem, 2543c4d7559SDave Watson &ctx->sg_plaintext_size); 2553c4d7559SDave Watson 2563c4d7559SDave Watson ctx->sg_encrypted_num_elem = 0; 2573c4d7559SDave Watson ctx->sg_encrypted_size = 0; 2583c4d7559SDave Watson 2593c4d7559SDave Watson /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */ 2603c4d7559SDave Watson rc = tls_push_sg(sk, tls_ctx, ctx->sg_encrypted_data, 0, flags); 2613c4d7559SDave Watson if (rc < 0 && rc != -EAGAIN) 262f4a8e43fSDave Watson tls_err_abort(sk, EBADMSG); 2633c4d7559SDave Watson 264dbe42559SDave Watson tls_advance_record_sn(sk, &tls_ctx->tx); 265a447da7dSDaniel Borkmann out_req: 266d2bdd268SVakul Garg aead_request_free(req); 2673c4d7559SDave Watson return rc; 2683c4d7559SDave Watson } 2693c4d7559SDave Watson 2703c4d7559SDave Watson static int tls_sw_push_pending_record(struct sock *sk, int flags) 2713c4d7559SDave Watson { 2723c4d7559SDave Watson return tls_push_record(sk, flags, TLS_RECORD_TYPE_DATA); 2733c4d7559SDave Watson } 2743c4d7559SDave Watson 2753c4d7559SDave Watson static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from, 27669ca9293SDave Watson int length, int *pages_used, 27769ca9293SDave Watson unsigned int *size_used, 27869ca9293SDave Watson struct scatterlist *to, int to_max_pages, 27969ca9293SDave Watson bool charge) 2803c4d7559SDave Watson { 2813c4d7559SDave Watson struct page *pages[MAX_SKB_FRAGS]; 2823c4d7559SDave Watson 2833c4d7559SDave Watson size_t offset; 2843c4d7559SDave Watson ssize_t copied, use; 2853c4d7559SDave Watson int i = 0; 28669ca9293SDave Watson unsigned int size = *size_used; 28769ca9293SDave Watson int num_elem = *pages_used; 2883c4d7559SDave Watson int rc = 0; 2893c4d7559SDave Watson int maxpages; 2903c4d7559SDave Watson 2913c4d7559SDave Watson while (length > 0) { 2923c4d7559SDave Watson i = 0; 29369ca9293SDave Watson maxpages = to_max_pages - num_elem; 2943c4d7559SDave Watson if (maxpages == 0) { 2953c4d7559SDave Watson rc = -EFAULT; 2963c4d7559SDave Watson goto out; 2973c4d7559SDave Watson } 2983c4d7559SDave Watson copied = iov_iter_get_pages(from, pages, 2993c4d7559SDave Watson length, 3003c4d7559SDave Watson maxpages, &offset); 3013c4d7559SDave Watson if (copied <= 0) { 3023c4d7559SDave Watson rc = -EFAULT; 3033c4d7559SDave Watson goto out; 3043c4d7559SDave Watson } 3053c4d7559SDave Watson 3063c4d7559SDave Watson iov_iter_advance(from, copied); 3073c4d7559SDave Watson 3083c4d7559SDave Watson length -= copied; 3093c4d7559SDave Watson size += copied; 3103c4d7559SDave Watson while (copied) { 3113c4d7559SDave Watson use = min_t(int, copied, PAGE_SIZE - offset); 3123c4d7559SDave Watson 31369ca9293SDave Watson sg_set_page(&to[num_elem], 3143c4d7559SDave Watson pages[i], use, offset); 31569ca9293SDave Watson sg_unmark_end(&to[num_elem]); 31669ca9293SDave Watson if (charge) 3173c4d7559SDave Watson sk_mem_charge(sk, use); 3183c4d7559SDave Watson 3193c4d7559SDave Watson offset = 0; 3203c4d7559SDave Watson copied -= use; 3213c4d7559SDave Watson 3223c4d7559SDave Watson ++i; 3233c4d7559SDave Watson ++num_elem; 3243c4d7559SDave Watson } 3253c4d7559SDave Watson } 3263c4d7559SDave Watson 3273c4d7559SDave Watson out: 32869ca9293SDave Watson *size_used = size; 32969ca9293SDave Watson *pages_used = num_elem; 33069ca9293SDave Watson 3313c4d7559SDave Watson return rc; 3323c4d7559SDave Watson } 3333c4d7559SDave Watson 3343c4d7559SDave Watson static int memcopy_from_iter(struct sock *sk, struct iov_iter *from, 3353c4d7559SDave Watson int bytes) 3363c4d7559SDave Watson { 3373c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 338f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 3393c4d7559SDave Watson struct scatterlist *sg = ctx->sg_plaintext_data; 3403c4d7559SDave Watson int copy, i, rc = 0; 3413c4d7559SDave Watson 3423c4d7559SDave Watson for (i = tls_ctx->pending_open_record_frags; 3433c4d7559SDave Watson i < ctx->sg_plaintext_num_elem; ++i) { 3443c4d7559SDave Watson copy = sg[i].length; 3453c4d7559SDave Watson if (copy_from_iter( 3463c4d7559SDave Watson page_address(sg_page(&sg[i])) + sg[i].offset, 3473c4d7559SDave Watson copy, from) != copy) { 3483c4d7559SDave Watson rc = -EFAULT; 3493c4d7559SDave Watson goto out; 3503c4d7559SDave Watson } 3513c4d7559SDave Watson bytes -= copy; 3523c4d7559SDave Watson 3533c4d7559SDave Watson ++tls_ctx->pending_open_record_frags; 3543c4d7559SDave Watson 3553c4d7559SDave Watson if (!bytes) 3563c4d7559SDave Watson break; 3573c4d7559SDave Watson } 3583c4d7559SDave Watson 3593c4d7559SDave Watson out: 3603c4d7559SDave Watson return rc; 3613c4d7559SDave Watson } 3623c4d7559SDave Watson 3633c4d7559SDave Watson int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) 3643c4d7559SDave Watson { 3653c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 366f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 3673c4d7559SDave Watson int ret = 0; 3683c4d7559SDave Watson int required_size; 3693c4d7559SDave Watson long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 3703c4d7559SDave Watson bool eor = !(msg->msg_flags & MSG_MORE); 3713c4d7559SDave Watson size_t try_to_copy, copied = 0; 3723c4d7559SDave Watson unsigned char record_type = TLS_RECORD_TYPE_DATA; 3733c4d7559SDave Watson int record_room; 3743c4d7559SDave Watson bool full_record; 3753c4d7559SDave Watson int orig_size; 3763c4d7559SDave Watson 3773c4d7559SDave Watson if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 3783c4d7559SDave Watson return -ENOTSUPP; 3793c4d7559SDave Watson 3803c4d7559SDave Watson lock_sock(sk); 3813c4d7559SDave Watson 3823c4d7559SDave Watson if (tls_complete_pending_work(sk, tls_ctx, msg->msg_flags, &timeo)) 3833c4d7559SDave Watson goto send_end; 3843c4d7559SDave Watson 3853c4d7559SDave Watson if (unlikely(msg->msg_controllen)) { 3863c4d7559SDave Watson ret = tls_proccess_cmsg(sk, msg, &record_type); 3873c4d7559SDave Watson if (ret) 3883c4d7559SDave Watson goto send_end; 3893c4d7559SDave Watson } 3903c4d7559SDave Watson 3913c4d7559SDave Watson while (msg_data_left(msg)) { 3923c4d7559SDave Watson if (sk->sk_err) { 39330be8f8dSr.hering@avm.de ret = -sk->sk_err; 3943c4d7559SDave Watson goto send_end; 3953c4d7559SDave Watson } 3963c4d7559SDave Watson 3973c4d7559SDave Watson orig_size = ctx->sg_plaintext_size; 3983c4d7559SDave Watson full_record = false; 3993c4d7559SDave Watson try_to_copy = msg_data_left(msg); 4003c4d7559SDave Watson record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size; 4013c4d7559SDave Watson if (try_to_copy >= record_room) { 4023c4d7559SDave Watson try_to_copy = record_room; 4033c4d7559SDave Watson full_record = true; 4043c4d7559SDave Watson } 4053c4d7559SDave Watson 4063c4d7559SDave Watson required_size = ctx->sg_plaintext_size + try_to_copy + 407dbe42559SDave Watson tls_ctx->tx.overhead_size; 4083c4d7559SDave Watson 4093c4d7559SDave Watson if (!sk_stream_memory_free(sk)) 4103c4d7559SDave Watson goto wait_for_sndbuf; 4113c4d7559SDave Watson alloc_encrypted: 4123c4d7559SDave Watson ret = alloc_encrypted_sg(sk, required_size); 4133c4d7559SDave Watson if (ret) { 4143c4d7559SDave Watson if (ret != -ENOSPC) 4153c4d7559SDave Watson goto wait_for_memory; 4163c4d7559SDave Watson 4173c4d7559SDave Watson /* Adjust try_to_copy according to the amount that was 4183c4d7559SDave Watson * actually allocated. The difference is due 4193c4d7559SDave Watson * to max sg elements limit 4203c4d7559SDave Watson */ 4213c4d7559SDave Watson try_to_copy -= required_size - ctx->sg_encrypted_size; 4223c4d7559SDave Watson full_record = true; 4233c4d7559SDave Watson } 4243c4d7559SDave Watson 4253c4d7559SDave Watson if (full_record || eor) { 4263c4d7559SDave Watson ret = zerocopy_from_iter(sk, &msg->msg_iter, 42769ca9293SDave Watson try_to_copy, &ctx->sg_plaintext_num_elem, 42869ca9293SDave Watson &ctx->sg_plaintext_size, 42969ca9293SDave Watson ctx->sg_plaintext_data, 43069ca9293SDave Watson ARRAY_SIZE(ctx->sg_plaintext_data), 43169ca9293SDave Watson true); 4323c4d7559SDave Watson if (ret) 4333c4d7559SDave Watson goto fallback_to_reg_send; 4343c4d7559SDave Watson 4353c4d7559SDave Watson copied += try_to_copy; 4363c4d7559SDave Watson ret = tls_push_record(sk, msg->msg_flags, record_type); 4373c4d7559SDave Watson if (!ret) 4383c4d7559SDave Watson continue; 4393c4d7559SDave Watson if (ret == -EAGAIN) 4403c4d7559SDave Watson goto send_end; 4413c4d7559SDave Watson 4423c4d7559SDave Watson copied -= try_to_copy; 4433c4d7559SDave Watson fallback_to_reg_send: 4443c4d7559SDave Watson iov_iter_revert(&msg->msg_iter, 4453c4d7559SDave Watson ctx->sg_plaintext_size - orig_size); 4463c4d7559SDave Watson trim_sg(sk, ctx->sg_plaintext_data, 4473c4d7559SDave Watson &ctx->sg_plaintext_num_elem, 4483c4d7559SDave Watson &ctx->sg_plaintext_size, 4493c4d7559SDave Watson orig_size); 4503c4d7559SDave Watson } 4513c4d7559SDave Watson 4523c4d7559SDave Watson required_size = ctx->sg_plaintext_size + try_to_copy; 4533c4d7559SDave Watson alloc_plaintext: 4543c4d7559SDave Watson ret = alloc_plaintext_sg(sk, required_size); 4553c4d7559SDave Watson if (ret) { 4563c4d7559SDave Watson if (ret != -ENOSPC) 4573c4d7559SDave Watson goto wait_for_memory; 4583c4d7559SDave Watson 4593c4d7559SDave Watson /* Adjust try_to_copy according to the amount that was 4603c4d7559SDave Watson * actually allocated. The difference is due 4613c4d7559SDave Watson * to max sg elements limit 4623c4d7559SDave Watson */ 4633c4d7559SDave Watson try_to_copy -= required_size - ctx->sg_plaintext_size; 4643c4d7559SDave Watson full_record = true; 4653c4d7559SDave Watson 4663c4d7559SDave Watson trim_sg(sk, ctx->sg_encrypted_data, 4673c4d7559SDave Watson &ctx->sg_encrypted_num_elem, 4683c4d7559SDave Watson &ctx->sg_encrypted_size, 4693c4d7559SDave Watson ctx->sg_plaintext_size + 470dbe42559SDave Watson tls_ctx->tx.overhead_size); 4713c4d7559SDave Watson } 4723c4d7559SDave Watson 4733c4d7559SDave Watson ret = memcopy_from_iter(sk, &msg->msg_iter, try_to_copy); 4743c4d7559SDave Watson if (ret) 4753c4d7559SDave Watson goto trim_sgl; 4763c4d7559SDave Watson 4773c4d7559SDave Watson copied += try_to_copy; 4783c4d7559SDave Watson if (full_record || eor) { 4793c4d7559SDave Watson push_record: 4803c4d7559SDave Watson ret = tls_push_record(sk, msg->msg_flags, record_type); 4813c4d7559SDave Watson if (ret) { 4823c4d7559SDave Watson if (ret == -ENOMEM) 4833c4d7559SDave Watson goto wait_for_memory; 4843c4d7559SDave Watson 4853c4d7559SDave Watson goto send_end; 4863c4d7559SDave Watson } 4873c4d7559SDave Watson } 4883c4d7559SDave Watson 4893c4d7559SDave Watson continue; 4903c4d7559SDave Watson 4913c4d7559SDave Watson wait_for_sndbuf: 4923c4d7559SDave Watson set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 4933c4d7559SDave Watson wait_for_memory: 4943c4d7559SDave Watson ret = sk_stream_wait_memory(sk, &timeo); 4953c4d7559SDave Watson if (ret) { 4963c4d7559SDave Watson trim_sgl: 4973c4d7559SDave Watson trim_both_sgl(sk, orig_size); 4983c4d7559SDave Watson goto send_end; 4993c4d7559SDave Watson } 5003c4d7559SDave Watson 5013c4d7559SDave Watson if (tls_is_pending_closed_record(tls_ctx)) 5023c4d7559SDave Watson goto push_record; 5033c4d7559SDave Watson 5043c4d7559SDave Watson if (ctx->sg_encrypted_size < required_size) 5053c4d7559SDave Watson goto alloc_encrypted; 5063c4d7559SDave Watson 5073c4d7559SDave Watson goto alloc_plaintext; 5083c4d7559SDave Watson } 5093c4d7559SDave Watson 5103c4d7559SDave Watson send_end: 5113c4d7559SDave Watson ret = sk_stream_error(sk, msg->msg_flags, ret); 5123c4d7559SDave Watson 5133c4d7559SDave Watson release_sock(sk); 5143c4d7559SDave Watson return copied ? copied : ret; 5153c4d7559SDave Watson } 5163c4d7559SDave Watson 5173c4d7559SDave Watson int tls_sw_sendpage(struct sock *sk, struct page *page, 5183c4d7559SDave Watson int offset, size_t size, int flags) 5193c4d7559SDave Watson { 5203c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 521f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 5223c4d7559SDave Watson int ret = 0; 5233c4d7559SDave Watson long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); 5243c4d7559SDave Watson bool eor; 5253c4d7559SDave Watson size_t orig_size = size; 5263c4d7559SDave Watson unsigned char record_type = TLS_RECORD_TYPE_DATA; 5273c4d7559SDave Watson struct scatterlist *sg; 5283c4d7559SDave Watson bool full_record; 5293c4d7559SDave Watson int record_room; 5303c4d7559SDave Watson 5313c4d7559SDave Watson if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | 5323c4d7559SDave Watson MSG_SENDPAGE_NOTLAST)) 5333c4d7559SDave Watson return -ENOTSUPP; 5343c4d7559SDave Watson 5353c4d7559SDave Watson /* No MSG_EOR from splice, only look at MSG_MORE */ 5363c4d7559SDave Watson eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST)); 5373c4d7559SDave Watson 5383c4d7559SDave Watson lock_sock(sk); 5393c4d7559SDave Watson 5403c4d7559SDave Watson sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 5413c4d7559SDave Watson 5423c4d7559SDave Watson if (tls_complete_pending_work(sk, tls_ctx, flags, &timeo)) 5433c4d7559SDave Watson goto sendpage_end; 5443c4d7559SDave Watson 5453c4d7559SDave Watson /* Call the sk_stream functions to manage the sndbuf mem. */ 5463c4d7559SDave Watson while (size > 0) { 5473c4d7559SDave Watson size_t copy, required_size; 5483c4d7559SDave Watson 5493c4d7559SDave Watson if (sk->sk_err) { 55030be8f8dSr.hering@avm.de ret = -sk->sk_err; 5513c4d7559SDave Watson goto sendpage_end; 5523c4d7559SDave Watson } 5533c4d7559SDave Watson 5543c4d7559SDave Watson full_record = false; 5553c4d7559SDave Watson record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size; 5563c4d7559SDave Watson copy = size; 5573c4d7559SDave Watson if (copy >= record_room) { 5583c4d7559SDave Watson copy = record_room; 5593c4d7559SDave Watson full_record = true; 5603c4d7559SDave Watson } 5613c4d7559SDave Watson required_size = ctx->sg_plaintext_size + copy + 562dbe42559SDave Watson tls_ctx->tx.overhead_size; 5633c4d7559SDave Watson 5643c4d7559SDave Watson if (!sk_stream_memory_free(sk)) 5653c4d7559SDave Watson goto wait_for_sndbuf; 5663c4d7559SDave Watson alloc_payload: 5673c4d7559SDave Watson ret = alloc_encrypted_sg(sk, required_size); 5683c4d7559SDave Watson if (ret) { 5693c4d7559SDave Watson if (ret != -ENOSPC) 5703c4d7559SDave Watson goto wait_for_memory; 5713c4d7559SDave Watson 5723c4d7559SDave Watson /* Adjust copy according to the amount that was 5733c4d7559SDave Watson * actually allocated. The difference is due 5743c4d7559SDave Watson * to max sg elements limit 5753c4d7559SDave Watson */ 5763c4d7559SDave Watson copy -= required_size - ctx->sg_plaintext_size; 5773c4d7559SDave Watson full_record = true; 5783c4d7559SDave Watson } 5793c4d7559SDave Watson 5803c4d7559SDave Watson get_page(page); 5813c4d7559SDave Watson sg = ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem; 5823c4d7559SDave Watson sg_set_page(sg, page, copy, offset); 5837a8c4dd9SDave Watson sg_unmark_end(sg); 5847a8c4dd9SDave Watson 5853c4d7559SDave Watson ctx->sg_plaintext_num_elem++; 5863c4d7559SDave Watson 5873c4d7559SDave Watson sk_mem_charge(sk, copy); 5883c4d7559SDave Watson offset += copy; 5893c4d7559SDave Watson size -= copy; 5903c4d7559SDave Watson ctx->sg_plaintext_size += copy; 5913c4d7559SDave Watson tls_ctx->pending_open_record_frags = ctx->sg_plaintext_num_elem; 5923c4d7559SDave Watson 5933c4d7559SDave Watson if (full_record || eor || 5943c4d7559SDave Watson ctx->sg_plaintext_num_elem == 5953c4d7559SDave Watson ARRAY_SIZE(ctx->sg_plaintext_data)) { 5963c4d7559SDave Watson push_record: 5973c4d7559SDave Watson ret = tls_push_record(sk, flags, record_type); 5983c4d7559SDave Watson if (ret) { 5993c4d7559SDave Watson if (ret == -ENOMEM) 6003c4d7559SDave Watson goto wait_for_memory; 6013c4d7559SDave Watson 6023c4d7559SDave Watson goto sendpage_end; 6033c4d7559SDave Watson } 6043c4d7559SDave Watson } 6053c4d7559SDave Watson continue; 6063c4d7559SDave Watson wait_for_sndbuf: 6073c4d7559SDave Watson set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 6083c4d7559SDave Watson wait_for_memory: 6093c4d7559SDave Watson ret = sk_stream_wait_memory(sk, &timeo); 6103c4d7559SDave Watson if (ret) { 6113c4d7559SDave Watson trim_both_sgl(sk, ctx->sg_plaintext_size); 6123c4d7559SDave Watson goto sendpage_end; 6133c4d7559SDave Watson } 6143c4d7559SDave Watson 6153c4d7559SDave Watson if (tls_is_pending_closed_record(tls_ctx)) 6163c4d7559SDave Watson goto push_record; 6173c4d7559SDave Watson 6183c4d7559SDave Watson goto alloc_payload; 6193c4d7559SDave Watson } 6203c4d7559SDave Watson 6213c4d7559SDave Watson sendpage_end: 6223c4d7559SDave Watson if (orig_size > size) 6233c4d7559SDave Watson ret = orig_size - size; 6243c4d7559SDave Watson else 6253c4d7559SDave Watson ret = sk_stream_error(sk, flags, ret); 6263c4d7559SDave Watson 6273c4d7559SDave Watson release_sock(sk); 6283c4d7559SDave Watson return ret; 6293c4d7559SDave Watson } 6303c4d7559SDave Watson 631c46234ebSDave Watson static struct sk_buff *tls_wait_data(struct sock *sk, int flags, 632c46234ebSDave Watson long timeo, int *err) 633c46234ebSDave Watson { 634c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 635f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 636c46234ebSDave Watson struct sk_buff *skb; 637c46234ebSDave Watson DEFINE_WAIT_FUNC(wait, woken_wake_function); 638c46234ebSDave Watson 639c46234ebSDave Watson while (!(skb = ctx->recv_pkt)) { 640c46234ebSDave Watson if (sk->sk_err) { 641c46234ebSDave Watson *err = sock_error(sk); 642c46234ebSDave Watson return NULL; 643c46234ebSDave Watson } 644c46234ebSDave Watson 645c46234ebSDave Watson if (sock_flag(sk, SOCK_DONE)) 646c46234ebSDave Watson return NULL; 647c46234ebSDave Watson 648c46234ebSDave Watson if ((flags & MSG_DONTWAIT) || !timeo) { 649c46234ebSDave Watson *err = -EAGAIN; 650c46234ebSDave Watson return NULL; 651c46234ebSDave Watson } 652c46234ebSDave Watson 653c46234ebSDave Watson add_wait_queue(sk_sleep(sk), &wait); 654c46234ebSDave Watson sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 655c46234ebSDave Watson sk_wait_event(sk, &timeo, ctx->recv_pkt != skb, &wait); 656c46234ebSDave Watson sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 657c46234ebSDave Watson remove_wait_queue(sk_sleep(sk), &wait); 658c46234ebSDave Watson 659c46234ebSDave Watson /* Handle signals */ 660c46234ebSDave Watson if (signal_pending(current)) { 661c46234ebSDave Watson *err = sock_intr_errno(timeo); 662c46234ebSDave Watson return NULL; 663c46234ebSDave Watson } 664c46234ebSDave Watson } 665c46234ebSDave Watson 666c46234ebSDave Watson return skb; 667c46234ebSDave Watson } 668c46234ebSDave Watson 669c46234ebSDave Watson static int decrypt_skb(struct sock *sk, struct sk_buff *skb, 670c46234ebSDave Watson struct scatterlist *sgout) 671c46234ebSDave Watson { 672c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 673f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 674b16520f7SKees Cook char iv[TLS_CIPHER_AES_GCM_128_SALT_SIZE + MAX_IV_SIZE]; 675c46234ebSDave Watson struct scatterlist sgin_arr[MAX_SKB_FRAGS + 2]; 676c46234ebSDave Watson struct scatterlist *sgin = &sgin_arr[0]; 677c46234ebSDave Watson struct strp_msg *rxm = strp_msg(skb); 678c46234ebSDave Watson int ret, nsg = ARRAY_SIZE(sgin_arr); 679c46234ebSDave Watson struct sk_buff *unused; 680c46234ebSDave Watson 681c46234ebSDave Watson ret = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE, 682c46234ebSDave Watson iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, 683c46234ebSDave Watson tls_ctx->rx.iv_size); 684c46234ebSDave Watson if (ret < 0) 685c46234ebSDave Watson return ret; 686c46234ebSDave Watson 687c46234ebSDave Watson memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE); 688c46234ebSDave Watson if (!sgout) { 689c46234ebSDave Watson nsg = skb_cow_data(skb, 0, &unused) + 1; 690c46234ebSDave Watson sgin = kmalloc_array(nsg, sizeof(*sgin), sk->sk_allocation); 691c46234ebSDave Watson sgout = sgin; 692c46234ebSDave Watson } 693c46234ebSDave Watson 694c46234ebSDave Watson sg_init_table(sgin, nsg); 6958ab6ffbaSMatt Mullins sg_set_buf(&sgin[0], ctx->rx_aad_ciphertext, TLS_AAD_SPACE_SIZE); 696c46234ebSDave Watson 697c46234ebSDave Watson nsg = skb_to_sgvec(skb, &sgin[1], 698c46234ebSDave Watson rxm->offset + tls_ctx->rx.prepend_size, 699c46234ebSDave Watson rxm->full_len - tls_ctx->rx.prepend_size); 700c46234ebSDave Watson 7018ab6ffbaSMatt Mullins tls_make_aad(ctx->rx_aad_ciphertext, 702c46234ebSDave Watson rxm->full_len - tls_ctx->rx.overhead_size, 703c46234ebSDave Watson tls_ctx->rx.rec_seq, 704c46234ebSDave Watson tls_ctx->rx.rec_seq_size, 705c46234ebSDave Watson ctx->control); 706c46234ebSDave Watson 707c46234ebSDave Watson ret = tls_do_decryption(sk, sgin, sgout, iv, 708c46234ebSDave Watson rxm->full_len - tls_ctx->rx.overhead_size, 709c46234ebSDave Watson skb, sk->sk_allocation); 710c46234ebSDave Watson 711c46234ebSDave Watson if (sgin != &sgin_arr[0]) 712c46234ebSDave Watson kfree(sgin); 713c46234ebSDave Watson 714c46234ebSDave Watson return ret; 715c46234ebSDave Watson } 716c46234ebSDave Watson 717c46234ebSDave Watson static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb, 718c46234ebSDave Watson unsigned int len) 719c46234ebSDave Watson { 720c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 721f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 722c46234ebSDave Watson struct strp_msg *rxm = strp_msg(skb); 723c46234ebSDave Watson 724c46234ebSDave Watson if (len < rxm->full_len) { 725c46234ebSDave Watson rxm->offset += len; 726c46234ebSDave Watson rxm->full_len -= len; 727c46234ebSDave Watson 728c46234ebSDave Watson return false; 729c46234ebSDave Watson } 730c46234ebSDave Watson 731c46234ebSDave Watson /* Finished with message */ 732c46234ebSDave Watson ctx->recv_pkt = NULL; 733c46234ebSDave Watson kfree_skb(skb); 7347170e604SDoron Roberts-Kedes __strp_unpause(&ctx->strp); 735c46234ebSDave Watson 736c46234ebSDave Watson return true; 737c46234ebSDave Watson } 738c46234ebSDave Watson 739c46234ebSDave Watson int tls_sw_recvmsg(struct sock *sk, 740c46234ebSDave Watson struct msghdr *msg, 741c46234ebSDave Watson size_t len, 742c46234ebSDave Watson int nonblock, 743c46234ebSDave Watson int flags, 744c46234ebSDave Watson int *addr_len) 745c46234ebSDave Watson { 746c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 747f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 748c46234ebSDave Watson unsigned char control; 749c46234ebSDave Watson struct strp_msg *rxm; 750c46234ebSDave Watson struct sk_buff *skb; 751c46234ebSDave Watson ssize_t copied = 0; 752c46234ebSDave Watson bool cmsg = false; 75306030dbaSDaniel Borkmann int target, err = 0; 754c46234ebSDave Watson long timeo; 755c46234ebSDave Watson 756c46234ebSDave Watson flags |= nonblock; 757c46234ebSDave Watson 758c46234ebSDave Watson if (unlikely(flags & MSG_ERRQUEUE)) 759c46234ebSDave Watson return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR); 760c46234ebSDave Watson 761c46234ebSDave Watson lock_sock(sk); 762c46234ebSDave Watson 76306030dbaSDaniel Borkmann target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 764c46234ebSDave Watson timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 765c46234ebSDave Watson do { 766c46234ebSDave Watson bool zc = false; 767c46234ebSDave Watson int chunk = 0; 768c46234ebSDave Watson 769c46234ebSDave Watson skb = tls_wait_data(sk, flags, timeo, &err); 770c46234ebSDave Watson if (!skb) 771c46234ebSDave Watson goto recv_end; 772c46234ebSDave Watson 773c46234ebSDave Watson rxm = strp_msg(skb); 774c46234ebSDave Watson if (!cmsg) { 775c46234ebSDave Watson int cerr; 776c46234ebSDave Watson 777c46234ebSDave Watson cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, 778c46234ebSDave Watson sizeof(ctx->control), &ctx->control); 779c46234ebSDave Watson cmsg = true; 780c46234ebSDave Watson control = ctx->control; 781c46234ebSDave Watson if (ctx->control != TLS_RECORD_TYPE_DATA) { 782c46234ebSDave Watson if (cerr || msg->msg_flags & MSG_CTRUNC) { 783c46234ebSDave Watson err = -EIO; 784c46234ebSDave Watson goto recv_end; 785c46234ebSDave Watson } 786c46234ebSDave Watson } 787c46234ebSDave Watson } else if (control != ctx->control) { 788c46234ebSDave Watson goto recv_end; 789c46234ebSDave Watson } 790c46234ebSDave Watson 791c46234ebSDave Watson if (!ctx->decrypted) { 792c46234ebSDave Watson int page_count; 793c46234ebSDave Watson int to_copy; 794c46234ebSDave Watson 795c46234ebSDave Watson page_count = iov_iter_npages(&msg->msg_iter, 796c46234ebSDave Watson MAX_SKB_FRAGS); 797c46234ebSDave Watson to_copy = rxm->full_len - tls_ctx->rx.overhead_size; 798c46234ebSDave Watson if (to_copy <= len && page_count < MAX_SKB_FRAGS && 799c46234ebSDave Watson likely(!(flags & MSG_PEEK))) { 800c46234ebSDave Watson struct scatterlist sgin[MAX_SKB_FRAGS + 1]; 801c46234ebSDave Watson int pages = 0; 802c46234ebSDave Watson 803c46234ebSDave Watson zc = true; 804c46234ebSDave Watson sg_init_table(sgin, MAX_SKB_FRAGS + 1); 8058ab6ffbaSMatt Mullins sg_set_buf(&sgin[0], ctx->rx_aad_plaintext, 8068ab6ffbaSMatt Mullins TLS_AAD_SPACE_SIZE); 807c46234ebSDave Watson 808c46234ebSDave Watson err = zerocopy_from_iter(sk, &msg->msg_iter, 809c46234ebSDave Watson to_copy, &pages, 810c46234ebSDave Watson &chunk, &sgin[1], 811c46234ebSDave Watson MAX_SKB_FRAGS, false); 812c46234ebSDave Watson if (err < 0) 813c46234ebSDave Watson goto fallback_to_reg_recv; 814c46234ebSDave Watson 815c46234ebSDave Watson err = decrypt_skb(sk, skb, sgin); 816c46234ebSDave Watson for (; pages > 0; pages--) 817c46234ebSDave Watson put_page(sg_page(&sgin[pages])); 818c46234ebSDave Watson if (err < 0) { 819c46234ebSDave Watson tls_err_abort(sk, EBADMSG); 820c46234ebSDave Watson goto recv_end; 821c46234ebSDave Watson } 822c46234ebSDave Watson } else { 823c46234ebSDave Watson fallback_to_reg_recv: 824c46234ebSDave Watson err = decrypt_skb(sk, skb, NULL); 825c46234ebSDave Watson if (err < 0) { 826c46234ebSDave Watson tls_err_abort(sk, EBADMSG); 827c46234ebSDave Watson goto recv_end; 828c46234ebSDave Watson } 829c46234ebSDave Watson } 830c46234ebSDave Watson ctx->decrypted = true; 831c46234ebSDave Watson } 832c46234ebSDave Watson 833c46234ebSDave Watson if (!zc) { 834c46234ebSDave Watson chunk = min_t(unsigned int, rxm->full_len, len); 835c46234ebSDave Watson err = skb_copy_datagram_msg(skb, rxm->offset, msg, 836c46234ebSDave Watson chunk); 837c46234ebSDave Watson if (err < 0) 838c46234ebSDave Watson goto recv_end; 839c46234ebSDave Watson } 840c46234ebSDave Watson 841c46234ebSDave Watson copied += chunk; 842c46234ebSDave Watson len -= chunk; 843c46234ebSDave Watson if (likely(!(flags & MSG_PEEK))) { 844c46234ebSDave Watson u8 control = ctx->control; 845c46234ebSDave Watson 846c46234ebSDave Watson if (tls_sw_advance_skb(sk, skb, chunk)) { 847c46234ebSDave Watson /* Return full control message to 848c46234ebSDave Watson * userspace before trying to parse 849c46234ebSDave Watson * another message type 850c46234ebSDave Watson */ 851c46234ebSDave Watson msg->msg_flags |= MSG_EOR; 852c46234ebSDave Watson if (control != TLS_RECORD_TYPE_DATA) 853c46234ebSDave Watson goto recv_end; 854c46234ebSDave Watson } 855c46234ebSDave Watson } 85606030dbaSDaniel Borkmann /* If we have a new message from strparser, continue now. */ 85706030dbaSDaniel Borkmann if (copied >= target && !ctx->recv_pkt) 85806030dbaSDaniel Borkmann break; 859c46234ebSDave Watson } while (len); 860c46234ebSDave Watson 861c46234ebSDave Watson recv_end: 862c46234ebSDave Watson release_sock(sk); 863c46234ebSDave Watson return copied ? : err; 864c46234ebSDave Watson } 865c46234ebSDave Watson 866c46234ebSDave Watson ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, 867c46234ebSDave Watson struct pipe_inode_info *pipe, 868c46234ebSDave Watson size_t len, unsigned int flags) 869c46234ebSDave Watson { 870c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sock->sk); 871f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 872c46234ebSDave Watson struct strp_msg *rxm = NULL; 873c46234ebSDave Watson struct sock *sk = sock->sk; 874c46234ebSDave Watson struct sk_buff *skb; 875c46234ebSDave Watson ssize_t copied = 0; 876c46234ebSDave Watson int err = 0; 877c46234ebSDave Watson long timeo; 878c46234ebSDave Watson int chunk; 879c46234ebSDave Watson 880c46234ebSDave Watson lock_sock(sk); 881c46234ebSDave Watson 882c46234ebSDave Watson timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 883c46234ebSDave Watson 884c46234ebSDave Watson skb = tls_wait_data(sk, flags, timeo, &err); 885c46234ebSDave Watson if (!skb) 886c46234ebSDave Watson goto splice_read_end; 887c46234ebSDave Watson 888c46234ebSDave Watson /* splice does not support reading control messages */ 889c46234ebSDave Watson if (ctx->control != TLS_RECORD_TYPE_DATA) { 890c46234ebSDave Watson err = -ENOTSUPP; 891c46234ebSDave Watson goto splice_read_end; 892c46234ebSDave Watson } 893c46234ebSDave Watson 894c46234ebSDave Watson if (!ctx->decrypted) { 895c46234ebSDave Watson err = decrypt_skb(sk, skb, NULL); 896c46234ebSDave Watson 897c46234ebSDave Watson if (err < 0) { 898c46234ebSDave Watson tls_err_abort(sk, EBADMSG); 899c46234ebSDave Watson goto splice_read_end; 900c46234ebSDave Watson } 901c46234ebSDave Watson ctx->decrypted = true; 902c46234ebSDave Watson } 903c46234ebSDave Watson rxm = strp_msg(skb); 904c46234ebSDave Watson 905c46234ebSDave Watson chunk = min_t(unsigned int, rxm->full_len, len); 906c46234ebSDave Watson copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags); 907c46234ebSDave Watson if (copied < 0) 908c46234ebSDave Watson goto splice_read_end; 909c46234ebSDave Watson 910c46234ebSDave Watson if (likely(!(flags & MSG_PEEK))) 911c46234ebSDave Watson tls_sw_advance_skb(sk, skb, copied); 912c46234ebSDave Watson 913c46234ebSDave Watson splice_read_end: 914c46234ebSDave Watson release_sock(sk); 915c46234ebSDave Watson return copied ? : err; 916c46234ebSDave Watson } 917c46234ebSDave Watson 918a11e1d43SLinus Torvalds unsigned int tls_sw_poll(struct file *file, struct socket *sock, 919a11e1d43SLinus Torvalds struct poll_table_struct *wait) 920c46234ebSDave Watson { 921a11e1d43SLinus Torvalds unsigned int ret; 922c46234ebSDave Watson struct sock *sk = sock->sk; 923c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 924f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 925c46234ebSDave Watson 926a11e1d43SLinus Torvalds /* Grab POLLOUT and POLLHUP from the underlying socket */ 927a11e1d43SLinus Torvalds ret = ctx->sk_poll(file, sock, wait); 928c46234ebSDave Watson 929a11e1d43SLinus Torvalds /* Clear POLLIN bits, and set based on recv_pkt */ 930a11e1d43SLinus Torvalds ret &= ~(POLLIN | POLLRDNORM); 931c46234ebSDave Watson if (ctx->recv_pkt) 932a11e1d43SLinus Torvalds ret |= POLLIN | POLLRDNORM; 933c46234ebSDave Watson 934a11e1d43SLinus Torvalds return ret; 935c46234ebSDave Watson } 936c46234ebSDave Watson 937c46234ebSDave Watson static int tls_read_size(struct strparser *strp, struct sk_buff *skb) 938c46234ebSDave Watson { 939c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(strp->sk); 940f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 9413463e51dSKees Cook char header[TLS_HEADER_SIZE + MAX_IV_SIZE]; 942c46234ebSDave Watson struct strp_msg *rxm = strp_msg(skb); 943c46234ebSDave Watson size_t cipher_overhead; 944c46234ebSDave Watson size_t data_len = 0; 945c46234ebSDave Watson int ret; 946c46234ebSDave Watson 947c46234ebSDave Watson /* Verify that we have a full TLS header, or wait for more data */ 948c46234ebSDave Watson if (rxm->offset + tls_ctx->rx.prepend_size > skb->len) 949c46234ebSDave Watson return 0; 950c46234ebSDave Watson 9513463e51dSKees Cook /* Sanity-check size of on-stack buffer. */ 9523463e51dSKees Cook if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) { 9533463e51dSKees Cook ret = -EINVAL; 9543463e51dSKees Cook goto read_failure; 9553463e51dSKees Cook } 9563463e51dSKees Cook 957c46234ebSDave Watson /* Linearize header to local buffer */ 958c46234ebSDave Watson ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size); 959c46234ebSDave Watson 960c46234ebSDave Watson if (ret < 0) 961c46234ebSDave Watson goto read_failure; 962c46234ebSDave Watson 963c46234ebSDave Watson ctx->control = header[0]; 964c46234ebSDave Watson 965c46234ebSDave Watson data_len = ((header[4] & 0xFF) | (header[3] << 8)); 966c46234ebSDave Watson 967c46234ebSDave Watson cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size; 968c46234ebSDave Watson 969c46234ebSDave Watson if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) { 970c46234ebSDave Watson ret = -EMSGSIZE; 971c46234ebSDave Watson goto read_failure; 972c46234ebSDave Watson } 973c46234ebSDave Watson if (data_len < cipher_overhead) { 974c46234ebSDave Watson ret = -EBADMSG; 975c46234ebSDave Watson goto read_failure; 976c46234ebSDave Watson } 977c46234ebSDave Watson 978c46234ebSDave Watson if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.version) || 979c46234ebSDave Watson header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.version)) { 980c46234ebSDave Watson ret = -EINVAL; 981c46234ebSDave Watson goto read_failure; 982c46234ebSDave Watson } 983c46234ebSDave Watson 984c46234ebSDave Watson return data_len + TLS_HEADER_SIZE; 985c46234ebSDave Watson 986c46234ebSDave Watson read_failure: 987c46234ebSDave Watson tls_err_abort(strp->sk, ret); 988c46234ebSDave Watson 989c46234ebSDave Watson return ret; 990c46234ebSDave Watson } 991c46234ebSDave Watson 992c46234ebSDave Watson static void tls_queue(struct strparser *strp, struct sk_buff *skb) 993c46234ebSDave Watson { 994c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(strp->sk); 995f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 996c46234ebSDave Watson 997c46234ebSDave Watson ctx->decrypted = false; 998c46234ebSDave Watson 999c46234ebSDave Watson ctx->recv_pkt = skb; 1000c46234ebSDave Watson strp_pause(strp); 1001c46234ebSDave Watson 1002c46234ebSDave Watson strp->sk->sk_state_change(strp->sk); 1003c46234ebSDave Watson } 1004c46234ebSDave Watson 1005c46234ebSDave Watson static void tls_data_ready(struct sock *sk) 1006c46234ebSDave Watson { 1007c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 1008f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 1009c46234ebSDave Watson 1010c46234ebSDave Watson strp_data_ready(&ctx->strp); 1011c46234ebSDave Watson } 1012c46234ebSDave Watson 1013f66de3eeSBoris Pismenny void tls_sw_free_resources_tx(struct sock *sk) 10143c4d7559SDave Watson { 10153c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 1016f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 10173c4d7559SDave Watson 10183c4d7559SDave Watson if (ctx->aead_send) 10193c4d7559SDave Watson crypto_free_aead(ctx->aead_send); 1020f66de3eeSBoris Pismenny tls_free_both_sg(sk); 1021f66de3eeSBoris Pismenny 1022f66de3eeSBoris Pismenny kfree(ctx); 1023f66de3eeSBoris Pismenny } 1024f66de3eeSBoris Pismenny 1025f66de3eeSBoris Pismenny void tls_sw_free_resources_rx(struct sock *sk) 1026f66de3eeSBoris Pismenny { 1027f66de3eeSBoris Pismenny struct tls_context *tls_ctx = tls_get_ctx(sk); 1028f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 1029f66de3eeSBoris Pismenny 1030c46234ebSDave Watson if (ctx->aead_recv) { 1031c46234ebSDave Watson if (ctx->recv_pkt) { 1032c46234ebSDave Watson kfree_skb(ctx->recv_pkt); 1033c46234ebSDave Watson ctx->recv_pkt = NULL; 1034c46234ebSDave Watson } 1035c46234ebSDave Watson crypto_free_aead(ctx->aead_recv); 1036c46234ebSDave Watson strp_stop(&ctx->strp); 1037c46234ebSDave Watson write_lock_bh(&sk->sk_callback_lock); 1038c46234ebSDave Watson sk->sk_data_ready = ctx->saved_data_ready; 1039c46234ebSDave Watson write_unlock_bh(&sk->sk_callback_lock); 1040c46234ebSDave Watson release_sock(sk); 1041c46234ebSDave Watson strp_done(&ctx->strp); 1042c46234ebSDave Watson lock_sock(sk); 1043c46234ebSDave Watson } 10443c4d7559SDave Watson 10453c4d7559SDave Watson kfree(ctx); 10463c4d7559SDave Watson } 10473c4d7559SDave Watson 1048c46234ebSDave Watson int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) 10493c4d7559SDave Watson { 10503c4d7559SDave Watson char keyval[TLS_CIPHER_AES_GCM_128_KEY_SIZE]; 10513c4d7559SDave Watson struct tls_crypto_info *crypto_info; 10523c4d7559SDave Watson struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; 1053f66de3eeSBoris Pismenny struct tls_sw_context_tx *sw_ctx_tx = NULL; 1054f66de3eeSBoris Pismenny struct tls_sw_context_rx *sw_ctx_rx = NULL; 1055c46234ebSDave Watson struct cipher_context *cctx; 1056c46234ebSDave Watson struct crypto_aead **aead; 1057c46234ebSDave Watson struct strp_callbacks cb; 10583c4d7559SDave Watson u16 nonce_size, tag_size, iv_size, rec_seq_size; 10593c4d7559SDave Watson char *iv, *rec_seq; 10603c4d7559SDave Watson int rc = 0; 10613c4d7559SDave Watson 10623c4d7559SDave Watson if (!ctx) { 10633c4d7559SDave Watson rc = -EINVAL; 10643c4d7559SDave Watson goto out; 10653c4d7559SDave Watson } 10663c4d7559SDave Watson 1067f66de3eeSBoris Pismenny if (tx) { 1068f66de3eeSBoris Pismenny sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL); 1069f66de3eeSBoris Pismenny if (!sw_ctx_tx) { 10703c4d7559SDave Watson rc = -ENOMEM; 10713c4d7559SDave Watson goto out; 10723c4d7559SDave Watson } 1073f66de3eeSBoris Pismenny crypto_init_wait(&sw_ctx_tx->async_wait); 1074f66de3eeSBoris Pismenny ctx->priv_ctx_tx = sw_ctx_tx; 1075c46234ebSDave Watson } else { 1076f66de3eeSBoris Pismenny sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL); 1077f66de3eeSBoris Pismenny if (!sw_ctx_rx) { 1078f66de3eeSBoris Pismenny rc = -ENOMEM; 1079f66de3eeSBoris Pismenny goto out; 1080c46234ebSDave Watson } 1081f66de3eeSBoris Pismenny crypto_init_wait(&sw_ctx_rx->async_wait); 1082f66de3eeSBoris Pismenny ctx->priv_ctx_rx = sw_ctx_rx; 1083f66de3eeSBoris Pismenny } 10843c4d7559SDave Watson 1085c46234ebSDave Watson if (tx) { 10863c4d7559SDave Watson crypto_info = &ctx->crypto_send; 1087c46234ebSDave Watson cctx = &ctx->tx; 1088f66de3eeSBoris Pismenny aead = &sw_ctx_tx->aead_send; 1089c46234ebSDave Watson } else { 1090c46234ebSDave Watson crypto_info = &ctx->crypto_recv; 1091c46234ebSDave Watson cctx = &ctx->rx; 1092f66de3eeSBoris Pismenny aead = &sw_ctx_rx->aead_recv; 1093c46234ebSDave Watson } 1094c46234ebSDave Watson 10953c4d7559SDave Watson switch (crypto_info->cipher_type) { 10963c4d7559SDave Watson case TLS_CIPHER_AES_GCM_128: { 10973c4d7559SDave Watson nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; 10983c4d7559SDave Watson tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE; 10993c4d7559SDave Watson iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; 11003c4d7559SDave Watson iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; 11013c4d7559SDave Watson rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE; 11023c4d7559SDave Watson rec_seq = 11033c4d7559SDave Watson ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; 11043c4d7559SDave Watson gcm_128_info = 11053c4d7559SDave Watson (struct tls12_crypto_info_aes_gcm_128 *)crypto_info; 11063c4d7559SDave Watson break; 11073c4d7559SDave Watson } 11083c4d7559SDave Watson default: 11093c4d7559SDave Watson rc = -EINVAL; 1110cf6d43efSSabrina Dubroca goto free_priv; 11113c4d7559SDave Watson } 11123c4d7559SDave Watson 1113b16520f7SKees Cook /* Sanity-check the IV size for stack allocations. */ 11143463e51dSKees Cook if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) { 1115b16520f7SKees Cook rc = -EINVAL; 1116b16520f7SKees Cook goto free_priv; 1117b16520f7SKees Cook } 1118b16520f7SKees Cook 1119c46234ebSDave Watson cctx->prepend_size = TLS_HEADER_SIZE + nonce_size; 1120c46234ebSDave Watson cctx->tag_size = tag_size; 1121c46234ebSDave Watson cctx->overhead_size = cctx->prepend_size + cctx->tag_size; 1122c46234ebSDave Watson cctx->iv_size = iv_size; 1123c46234ebSDave Watson cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE, 1124dbe42559SDave Watson GFP_KERNEL); 1125c46234ebSDave Watson if (!cctx->iv) { 11263c4d7559SDave Watson rc = -ENOMEM; 1127cf6d43efSSabrina Dubroca goto free_priv; 11283c4d7559SDave Watson } 1129c46234ebSDave Watson memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE); 1130c46234ebSDave Watson memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size); 1131c46234ebSDave Watson cctx->rec_seq_size = rec_seq_size; 1132c46234ebSDave Watson cctx->rec_seq = kmalloc(rec_seq_size, GFP_KERNEL); 1133c46234ebSDave Watson if (!cctx->rec_seq) { 11343c4d7559SDave Watson rc = -ENOMEM; 11353c4d7559SDave Watson goto free_iv; 11363c4d7559SDave Watson } 1137c46234ebSDave Watson memcpy(cctx->rec_seq, rec_seq, rec_seq_size); 11383c4d7559SDave Watson 1139f66de3eeSBoris Pismenny if (sw_ctx_tx) { 1140f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_encrypted_data, 1141f66de3eeSBoris Pismenny ARRAY_SIZE(sw_ctx_tx->sg_encrypted_data)); 1142f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_plaintext_data, 1143f66de3eeSBoris Pismenny ARRAY_SIZE(sw_ctx_tx->sg_plaintext_data)); 11443c4d7559SDave Watson 1145f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_aead_in, 2); 1146f66de3eeSBoris Pismenny sg_set_buf(&sw_ctx_tx->sg_aead_in[0], sw_ctx_tx->aad_space, 1147f66de3eeSBoris Pismenny sizeof(sw_ctx_tx->aad_space)); 1148f66de3eeSBoris Pismenny sg_unmark_end(&sw_ctx_tx->sg_aead_in[1]); 1149f66de3eeSBoris Pismenny sg_chain(sw_ctx_tx->sg_aead_in, 2, 1150f66de3eeSBoris Pismenny sw_ctx_tx->sg_plaintext_data); 1151f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_aead_out, 2); 1152f66de3eeSBoris Pismenny sg_set_buf(&sw_ctx_tx->sg_aead_out[0], sw_ctx_tx->aad_space, 1153f66de3eeSBoris Pismenny sizeof(sw_ctx_tx->aad_space)); 1154f66de3eeSBoris Pismenny sg_unmark_end(&sw_ctx_tx->sg_aead_out[1]); 1155f66de3eeSBoris Pismenny sg_chain(sw_ctx_tx->sg_aead_out, 2, 1156f66de3eeSBoris Pismenny sw_ctx_tx->sg_encrypted_data); 1157c46234ebSDave Watson } 11583c4d7559SDave Watson 1159c46234ebSDave Watson if (!*aead) { 1160c46234ebSDave Watson *aead = crypto_alloc_aead("gcm(aes)", 0, 0); 1161c46234ebSDave Watson if (IS_ERR(*aead)) { 1162c46234ebSDave Watson rc = PTR_ERR(*aead); 1163c46234ebSDave Watson *aead = NULL; 11643c4d7559SDave Watson goto free_rec_seq; 11653c4d7559SDave Watson } 11663c4d7559SDave Watson } 11673c4d7559SDave Watson 11683c4d7559SDave Watson ctx->push_pending_record = tls_sw_push_pending_record; 11693c4d7559SDave Watson 11703c4d7559SDave Watson memcpy(keyval, gcm_128_info->key, TLS_CIPHER_AES_GCM_128_KEY_SIZE); 11713c4d7559SDave Watson 1172c46234ebSDave Watson rc = crypto_aead_setkey(*aead, keyval, 11733c4d7559SDave Watson TLS_CIPHER_AES_GCM_128_KEY_SIZE); 11743c4d7559SDave Watson if (rc) 11753c4d7559SDave Watson goto free_aead; 11763c4d7559SDave Watson 1177c46234ebSDave Watson rc = crypto_aead_setauthsize(*aead, cctx->tag_size); 1178c46234ebSDave Watson if (rc) 1179c46234ebSDave Watson goto free_aead; 1180c46234ebSDave Watson 1181f66de3eeSBoris Pismenny if (sw_ctx_rx) { 1182c46234ebSDave Watson /* Set up strparser */ 1183c46234ebSDave Watson memset(&cb, 0, sizeof(cb)); 1184c46234ebSDave Watson cb.rcv_msg = tls_queue; 1185c46234ebSDave Watson cb.parse_msg = tls_read_size; 1186c46234ebSDave Watson 1187f66de3eeSBoris Pismenny strp_init(&sw_ctx_rx->strp, sk, &cb); 1188c46234ebSDave Watson 1189c46234ebSDave Watson write_lock_bh(&sk->sk_callback_lock); 1190f66de3eeSBoris Pismenny sw_ctx_rx->saved_data_ready = sk->sk_data_ready; 1191c46234ebSDave Watson sk->sk_data_ready = tls_data_ready; 1192c46234ebSDave Watson write_unlock_bh(&sk->sk_callback_lock); 1193c46234ebSDave Watson 1194a11e1d43SLinus Torvalds sw_ctx_rx->sk_poll = sk->sk_socket->ops->poll; 1195c46234ebSDave Watson 1196f66de3eeSBoris Pismenny strp_check_rcv(&sw_ctx_rx->strp); 1197c46234ebSDave Watson } 1198c46234ebSDave Watson 1199c46234ebSDave Watson goto out; 12003c4d7559SDave Watson 12013c4d7559SDave Watson free_aead: 1202c46234ebSDave Watson crypto_free_aead(*aead); 1203c46234ebSDave Watson *aead = NULL; 12043c4d7559SDave Watson free_rec_seq: 1205c46234ebSDave Watson kfree(cctx->rec_seq); 1206c46234ebSDave Watson cctx->rec_seq = NULL; 12073c4d7559SDave Watson free_iv: 1208f66de3eeSBoris Pismenny kfree(cctx->iv); 1209f66de3eeSBoris Pismenny cctx->iv = NULL; 1210cf6d43efSSabrina Dubroca free_priv: 1211f66de3eeSBoris Pismenny if (tx) { 1212f66de3eeSBoris Pismenny kfree(ctx->priv_ctx_tx); 1213f66de3eeSBoris Pismenny ctx->priv_ctx_tx = NULL; 1214f66de3eeSBoris Pismenny } else { 1215f66de3eeSBoris Pismenny kfree(ctx->priv_ctx_rx); 1216f66de3eeSBoris Pismenny ctx->priv_ctx_rx = NULL; 1217f66de3eeSBoris Pismenny } 12183c4d7559SDave Watson out: 12193c4d7559SDave Watson return rc; 12203c4d7559SDave Watson } 1221