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 unsigned int req_size = sizeof(struct aead_request) + 61c46234ebSDave Watson crypto_aead_reqsize(ctx->aead_recv); 62c46234ebSDave Watson 63c46234ebSDave Watson aead_req = kzalloc(req_size, flags); 64c46234ebSDave Watson if (!aead_req) 65c46234ebSDave Watson return -ENOMEM; 66c46234ebSDave Watson 67c46234ebSDave Watson aead_request_set_tfm(aead_req, ctx->aead_recv); 68c46234ebSDave Watson aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); 69c46234ebSDave Watson aead_request_set_crypt(aead_req, sgin, sgout, 70c46234ebSDave Watson data_len + tls_ctx->rx.tag_size, 71c46234ebSDave Watson (u8 *)iv_recv); 72c46234ebSDave Watson aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, 73c46234ebSDave Watson crypto_req_done, &ctx->async_wait); 74c46234ebSDave Watson 75c46234ebSDave Watson ret = crypto_wait_req(crypto_aead_decrypt(aead_req), &ctx->async_wait); 76c46234ebSDave Watson 77c46234ebSDave Watson if (ret < 0) 78c46234ebSDave Watson goto out; 79c46234ebSDave Watson 80c46234ebSDave Watson rxm->offset += tls_ctx->rx.prepend_size; 81c46234ebSDave Watson rxm->full_len -= tls_ctx->rx.overhead_size; 82c46234ebSDave Watson tls_advance_record_sn(sk, &tls_ctx->rx); 83c46234ebSDave Watson 84c46234ebSDave Watson ctx->decrypted = true; 85c46234ebSDave Watson 86c46234ebSDave Watson ctx->saved_data_ready(sk); 87c46234ebSDave Watson 88c46234ebSDave Watson out: 89c46234ebSDave Watson kfree(aead_req); 90c46234ebSDave Watson return ret; 91c46234ebSDave Watson } 92c46234ebSDave Watson 933c4d7559SDave Watson static void trim_sg(struct sock *sk, struct scatterlist *sg, 943c4d7559SDave Watson int *sg_num_elem, unsigned int *sg_size, int target_size) 953c4d7559SDave Watson { 963c4d7559SDave Watson int i = *sg_num_elem - 1; 973c4d7559SDave Watson int trim = *sg_size - target_size; 983c4d7559SDave Watson 993c4d7559SDave Watson if (trim <= 0) { 1003c4d7559SDave Watson WARN_ON(trim < 0); 1013c4d7559SDave Watson return; 1023c4d7559SDave Watson } 1033c4d7559SDave Watson 1043c4d7559SDave Watson *sg_size = target_size; 1053c4d7559SDave Watson while (trim >= sg[i].length) { 1063c4d7559SDave Watson trim -= sg[i].length; 1073c4d7559SDave Watson sk_mem_uncharge(sk, sg[i].length); 1083c4d7559SDave Watson put_page(sg_page(&sg[i])); 1093c4d7559SDave Watson i--; 1103c4d7559SDave Watson 1113c4d7559SDave Watson if (i < 0) 1123c4d7559SDave Watson goto out; 1133c4d7559SDave Watson } 1143c4d7559SDave Watson 1153c4d7559SDave Watson sg[i].length -= trim; 1163c4d7559SDave Watson sk_mem_uncharge(sk, trim); 1173c4d7559SDave Watson 1183c4d7559SDave Watson out: 1193c4d7559SDave Watson *sg_num_elem = i + 1; 1203c4d7559SDave Watson } 1213c4d7559SDave Watson 1223c4d7559SDave Watson static void trim_both_sgl(struct sock *sk, int target_size) 1233c4d7559SDave Watson { 1243c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 125f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1263c4d7559SDave Watson 1273c4d7559SDave Watson trim_sg(sk, ctx->sg_plaintext_data, 1283c4d7559SDave Watson &ctx->sg_plaintext_num_elem, 1293c4d7559SDave Watson &ctx->sg_plaintext_size, 1303c4d7559SDave Watson target_size); 1313c4d7559SDave Watson 1323c4d7559SDave Watson if (target_size > 0) 133dbe42559SDave Watson target_size += tls_ctx->tx.overhead_size; 1343c4d7559SDave Watson 1353c4d7559SDave Watson trim_sg(sk, ctx->sg_encrypted_data, 1363c4d7559SDave Watson &ctx->sg_encrypted_num_elem, 1373c4d7559SDave Watson &ctx->sg_encrypted_size, 1383c4d7559SDave Watson target_size); 1393c4d7559SDave Watson } 1403c4d7559SDave Watson 1413c4d7559SDave Watson static int alloc_encrypted_sg(struct sock *sk, int len) 1423c4d7559SDave Watson { 1433c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 144f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1453c4d7559SDave Watson int rc = 0; 1463c4d7559SDave Watson 1472c3682f0SJohn Fastabend rc = sk_alloc_sg(sk, len, 1488c05dbf0SJohn Fastabend ctx->sg_encrypted_data, 0, 1492c3682f0SJohn Fastabend &ctx->sg_encrypted_num_elem, 1502c3682f0SJohn Fastabend &ctx->sg_encrypted_size, 0); 1513c4d7559SDave Watson 1523c4d7559SDave Watson return rc; 1533c4d7559SDave Watson } 1543c4d7559SDave Watson 1553c4d7559SDave Watson static int alloc_plaintext_sg(struct sock *sk, int len) 1563c4d7559SDave Watson { 1573c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 158f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1593c4d7559SDave Watson int rc = 0; 1603c4d7559SDave Watson 1618c05dbf0SJohn Fastabend rc = sk_alloc_sg(sk, len, ctx->sg_plaintext_data, 0, 1623c4d7559SDave Watson &ctx->sg_plaintext_num_elem, &ctx->sg_plaintext_size, 1633c4d7559SDave Watson tls_ctx->pending_open_record_frags); 1643c4d7559SDave Watson 1653c4d7559SDave Watson return rc; 1663c4d7559SDave Watson } 1673c4d7559SDave Watson 1683c4d7559SDave Watson static void free_sg(struct sock *sk, struct scatterlist *sg, 1693c4d7559SDave Watson int *sg_num_elem, unsigned int *sg_size) 1703c4d7559SDave Watson { 1713c4d7559SDave Watson int i, n = *sg_num_elem; 1723c4d7559SDave Watson 1733c4d7559SDave Watson for (i = 0; i < n; ++i) { 1743c4d7559SDave Watson sk_mem_uncharge(sk, sg[i].length); 1753c4d7559SDave Watson put_page(sg_page(&sg[i])); 1763c4d7559SDave Watson } 1773c4d7559SDave Watson *sg_num_elem = 0; 1783c4d7559SDave Watson *sg_size = 0; 1793c4d7559SDave Watson } 1803c4d7559SDave Watson 1813c4d7559SDave Watson static void tls_free_both_sg(struct sock *sk) 1823c4d7559SDave Watson { 1833c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 184f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 1853c4d7559SDave Watson 1863c4d7559SDave Watson free_sg(sk, ctx->sg_encrypted_data, &ctx->sg_encrypted_num_elem, 1873c4d7559SDave Watson &ctx->sg_encrypted_size); 1883c4d7559SDave Watson 1893c4d7559SDave Watson free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem, 1903c4d7559SDave Watson &ctx->sg_plaintext_size); 1913c4d7559SDave Watson } 1923c4d7559SDave Watson 1933c4d7559SDave Watson static int tls_do_encryption(struct tls_context *tls_ctx, 194a447da7dSDaniel Borkmann struct tls_sw_context_tx *ctx, 195a447da7dSDaniel Borkmann struct aead_request *aead_req, 196a447da7dSDaniel Borkmann size_t data_len) 1973c4d7559SDave Watson { 1983c4d7559SDave Watson int rc; 1993c4d7559SDave Watson 200dbe42559SDave Watson ctx->sg_encrypted_data[0].offset += tls_ctx->tx.prepend_size; 201dbe42559SDave Watson ctx->sg_encrypted_data[0].length -= tls_ctx->tx.prepend_size; 2023c4d7559SDave Watson 2033c4d7559SDave Watson aead_request_set_tfm(aead_req, ctx->aead_send); 2043c4d7559SDave Watson aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); 2053c4d7559SDave Watson aead_request_set_crypt(aead_req, ctx->sg_aead_in, ctx->sg_aead_out, 206dbe42559SDave Watson data_len, tls_ctx->tx.iv); 207a54667f6SVakul Garg 208a54667f6SVakul Garg aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, 209a54667f6SVakul Garg crypto_req_done, &ctx->async_wait); 210a54667f6SVakul Garg 211a54667f6SVakul Garg rc = crypto_wait_req(crypto_aead_encrypt(aead_req), &ctx->async_wait); 2123c4d7559SDave Watson 213dbe42559SDave Watson ctx->sg_encrypted_data[0].offset -= tls_ctx->tx.prepend_size; 214dbe42559SDave Watson ctx->sg_encrypted_data[0].length += tls_ctx->tx.prepend_size; 2153c4d7559SDave Watson 2163c4d7559SDave Watson return rc; 2173c4d7559SDave Watson } 2183c4d7559SDave Watson 2193c4d7559SDave Watson static int tls_push_record(struct sock *sk, int flags, 2203c4d7559SDave Watson unsigned char record_type) 2213c4d7559SDave Watson { 2223c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 223f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 224a447da7dSDaniel Borkmann struct aead_request *req; 2253c4d7559SDave Watson int rc; 2263c4d7559SDave Watson 227a447da7dSDaniel Borkmann req = kzalloc(sizeof(struct aead_request) + 228a447da7dSDaniel Borkmann crypto_aead_reqsize(ctx->aead_send), sk->sk_allocation); 229a447da7dSDaniel Borkmann if (!req) 230a447da7dSDaniel Borkmann return -ENOMEM; 231a447da7dSDaniel Borkmann 2323c4d7559SDave Watson sg_mark_end(ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem - 1); 2333c4d7559SDave Watson sg_mark_end(ctx->sg_encrypted_data + ctx->sg_encrypted_num_elem - 1); 2343c4d7559SDave Watson 235213ef6e7SIlya Lesokhin tls_make_aad(ctx->aad_space, ctx->sg_plaintext_size, 236dbe42559SDave Watson tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size, 2373c4d7559SDave Watson record_type); 2383c4d7559SDave Watson 2393c4d7559SDave Watson tls_fill_prepend(tls_ctx, 2403c4d7559SDave Watson page_address(sg_page(&ctx->sg_encrypted_data[0])) + 2413c4d7559SDave Watson ctx->sg_encrypted_data[0].offset, 2423c4d7559SDave Watson ctx->sg_plaintext_size, record_type); 2433c4d7559SDave Watson 2443c4d7559SDave Watson tls_ctx->pending_open_record_frags = 0; 2453c4d7559SDave Watson set_bit(TLS_PENDING_CLOSED_RECORD, &tls_ctx->flags); 2463c4d7559SDave Watson 247a447da7dSDaniel Borkmann rc = tls_do_encryption(tls_ctx, ctx, req, ctx->sg_plaintext_size); 2483c4d7559SDave Watson if (rc < 0) { 2493c4d7559SDave Watson /* If we are called from write_space and 2503c4d7559SDave Watson * we fail, we need to set this SOCK_NOSPACE 2513c4d7559SDave Watson * to trigger another write_space in the future. 2523c4d7559SDave Watson */ 2533c4d7559SDave Watson set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 254a447da7dSDaniel Borkmann goto out_req; 2553c4d7559SDave Watson } 2563c4d7559SDave Watson 2573c4d7559SDave Watson free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem, 2583c4d7559SDave Watson &ctx->sg_plaintext_size); 2593c4d7559SDave Watson 2603c4d7559SDave Watson ctx->sg_encrypted_num_elem = 0; 2613c4d7559SDave Watson ctx->sg_encrypted_size = 0; 2623c4d7559SDave Watson 2633c4d7559SDave Watson /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */ 2643c4d7559SDave Watson rc = tls_push_sg(sk, tls_ctx, ctx->sg_encrypted_data, 0, flags); 2653c4d7559SDave Watson if (rc < 0 && rc != -EAGAIN) 266f4a8e43fSDave Watson tls_err_abort(sk, EBADMSG); 2673c4d7559SDave Watson 268dbe42559SDave Watson tls_advance_record_sn(sk, &tls_ctx->tx); 269a447da7dSDaniel Borkmann out_req: 270a447da7dSDaniel Borkmann kfree(req); 2713c4d7559SDave Watson return rc; 2723c4d7559SDave Watson } 2733c4d7559SDave Watson 2743c4d7559SDave Watson static int tls_sw_push_pending_record(struct sock *sk, int flags) 2753c4d7559SDave Watson { 2763c4d7559SDave Watson return tls_push_record(sk, flags, TLS_RECORD_TYPE_DATA); 2773c4d7559SDave Watson } 2783c4d7559SDave Watson 2793c4d7559SDave Watson static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from, 28069ca9293SDave Watson int length, int *pages_used, 28169ca9293SDave Watson unsigned int *size_used, 28269ca9293SDave Watson struct scatterlist *to, int to_max_pages, 28369ca9293SDave Watson bool charge) 2843c4d7559SDave Watson { 2853c4d7559SDave Watson struct page *pages[MAX_SKB_FRAGS]; 2863c4d7559SDave Watson 2873c4d7559SDave Watson size_t offset; 2883c4d7559SDave Watson ssize_t copied, use; 2893c4d7559SDave Watson int i = 0; 29069ca9293SDave Watson unsigned int size = *size_used; 29169ca9293SDave Watson int num_elem = *pages_used; 2923c4d7559SDave Watson int rc = 0; 2933c4d7559SDave Watson int maxpages; 2943c4d7559SDave Watson 2953c4d7559SDave Watson while (length > 0) { 2963c4d7559SDave Watson i = 0; 29769ca9293SDave Watson maxpages = to_max_pages - num_elem; 2983c4d7559SDave Watson if (maxpages == 0) { 2993c4d7559SDave Watson rc = -EFAULT; 3003c4d7559SDave Watson goto out; 3013c4d7559SDave Watson } 3023c4d7559SDave Watson copied = iov_iter_get_pages(from, pages, 3033c4d7559SDave Watson length, 3043c4d7559SDave Watson maxpages, &offset); 3053c4d7559SDave Watson if (copied <= 0) { 3063c4d7559SDave Watson rc = -EFAULT; 3073c4d7559SDave Watson goto out; 3083c4d7559SDave Watson } 3093c4d7559SDave Watson 3103c4d7559SDave Watson iov_iter_advance(from, copied); 3113c4d7559SDave Watson 3123c4d7559SDave Watson length -= copied; 3133c4d7559SDave Watson size += copied; 3143c4d7559SDave Watson while (copied) { 3153c4d7559SDave Watson use = min_t(int, copied, PAGE_SIZE - offset); 3163c4d7559SDave Watson 31769ca9293SDave Watson sg_set_page(&to[num_elem], 3183c4d7559SDave Watson pages[i], use, offset); 31969ca9293SDave Watson sg_unmark_end(&to[num_elem]); 32069ca9293SDave Watson if (charge) 3213c4d7559SDave Watson sk_mem_charge(sk, use); 3223c4d7559SDave Watson 3233c4d7559SDave Watson offset = 0; 3243c4d7559SDave Watson copied -= use; 3253c4d7559SDave Watson 3263c4d7559SDave Watson ++i; 3273c4d7559SDave Watson ++num_elem; 3283c4d7559SDave Watson } 3293c4d7559SDave Watson } 3303c4d7559SDave Watson 3313c4d7559SDave Watson out: 33269ca9293SDave Watson *size_used = size; 33369ca9293SDave Watson *pages_used = num_elem; 33469ca9293SDave Watson 3353c4d7559SDave Watson return rc; 3363c4d7559SDave Watson } 3373c4d7559SDave Watson 3383c4d7559SDave Watson static int memcopy_from_iter(struct sock *sk, struct iov_iter *from, 3393c4d7559SDave Watson int bytes) 3403c4d7559SDave Watson { 3413c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 342f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 3433c4d7559SDave Watson struct scatterlist *sg = ctx->sg_plaintext_data; 3443c4d7559SDave Watson int copy, i, rc = 0; 3453c4d7559SDave Watson 3463c4d7559SDave Watson for (i = tls_ctx->pending_open_record_frags; 3473c4d7559SDave Watson i < ctx->sg_plaintext_num_elem; ++i) { 3483c4d7559SDave Watson copy = sg[i].length; 3493c4d7559SDave Watson if (copy_from_iter( 3503c4d7559SDave Watson page_address(sg_page(&sg[i])) + sg[i].offset, 3513c4d7559SDave Watson copy, from) != copy) { 3523c4d7559SDave Watson rc = -EFAULT; 3533c4d7559SDave Watson goto out; 3543c4d7559SDave Watson } 3553c4d7559SDave Watson bytes -= copy; 3563c4d7559SDave Watson 3573c4d7559SDave Watson ++tls_ctx->pending_open_record_frags; 3583c4d7559SDave Watson 3593c4d7559SDave Watson if (!bytes) 3603c4d7559SDave Watson break; 3613c4d7559SDave Watson } 3623c4d7559SDave Watson 3633c4d7559SDave Watson out: 3643c4d7559SDave Watson return rc; 3653c4d7559SDave Watson } 3663c4d7559SDave Watson 3673c4d7559SDave Watson int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) 3683c4d7559SDave Watson { 3693c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 370f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 3713c4d7559SDave Watson int ret = 0; 3723c4d7559SDave Watson int required_size; 3733c4d7559SDave Watson long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); 3743c4d7559SDave Watson bool eor = !(msg->msg_flags & MSG_MORE); 3753c4d7559SDave Watson size_t try_to_copy, copied = 0; 3763c4d7559SDave Watson unsigned char record_type = TLS_RECORD_TYPE_DATA; 3773c4d7559SDave Watson int record_room; 3783c4d7559SDave Watson bool full_record; 3793c4d7559SDave Watson int orig_size; 3803c4d7559SDave Watson 3813c4d7559SDave Watson if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) 3823c4d7559SDave Watson return -ENOTSUPP; 3833c4d7559SDave Watson 3843c4d7559SDave Watson lock_sock(sk); 3853c4d7559SDave Watson 3863c4d7559SDave Watson if (tls_complete_pending_work(sk, tls_ctx, msg->msg_flags, &timeo)) 3873c4d7559SDave Watson goto send_end; 3883c4d7559SDave Watson 3893c4d7559SDave Watson if (unlikely(msg->msg_controllen)) { 3903c4d7559SDave Watson ret = tls_proccess_cmsg(sk, msg, &record_type); 3913c4d7559SDave Watson if (ret) 3923c4d7559SDave Watson goto send_end; 3933c4d7559SDave Watson } 3943c4d7559SDave Watson 3953c4d7559SDave Watson while (msg_data_left(msg)) { 3963c4d7559SDave Watson if (sk->sk_err) { 39730be8f8dSr.hering@avm.de ret = -sk->sk_err; 3983c4d7559SDave Watson goto send_end; 3993c4d7559SDave Watson } 4003c4d7559SDave Watson 4013c4d7559SDave Watson orig_size = ctx->sg_plaintext_size; 4023c4d7559SDave Watson full_record = false; 4033c4d7559SDave Watson try_to_copy = msg_data_left(msg); 4043c4d7559SDave Watson record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size; 4053c4d7559SDave Watson if (try_to_copy >= record_room) { 4063c4d7559SDave Watson try_to_copy = record_room; 4073c4d7559SDave Watson full_record = true; 4083c4d7559SDave Watson } 4093c4d7559SDave Watson 4103c4d7559SDave Watson required_size = ctx->sg_plaintext_size + try_to_copy + 411dbe42559SDave Watson tls_ctx->tx.overhead_size; 4123c4d7559SDave Watson 4133c4d7559SDave Watson if (!sk_stream_memory_free(sk)) 4143c4d7559SDave Watson goto wait_for_sndbuf; 4153c4d7559SDave Watson alloc_encrypted: 4163c4d7559SDave Watson ret = alloc_encrypted_sg(sk, required_size); 4173c4d7559SDave Watson if (ret) { 4183c4d7559SDave Watson if (ret != -ENOSPC) 4193c4d7559SDave Watson goto wait_for_memory; 4203c4d7559SDave Watson 4213c4d7559SDave Watson /* Adjust try_to_copy according to the amount that was 4223c4d7559SDave Watson * actually allocated. The difference is due 4233c4d7559SDave Watson * to max sg elements limit 4243c4d7559SDave Watson */ 4253c4d7559SDave Watson try_to_copy -= required_size - ctx->sg_encrypted_size; 4263c4d7559SDave Watson full_record = true; 4273c4d7559SDave Watson } 4283c4d7559SDave Watson 4293c4d7559SDave Watson if (full_record || eor) { 4303c4d7559SDave Watson ret = zerocopy_from_iter(sk, &msg->msg_iter, 43169ca9293SDave Watson try_to_copy, &ctx->sg_plaintext_num_elem, 43269ca9293SDave Watson &ctx->sg_plaintext_size, 43369ca9293SDave Watson ctx->sg_plaintext_data, 43469ca9293SDave Watson ARRAY_SIZE(ctx->sg_plaintext_data), 43569ca9293SDave Watson true); 4363c4d7559SDave Watson if (ret) 4373c4d7559SDave Watson goto fallback_to_reg_send; 4383c4d7559SDave Watson 4393c4d7559SDave Watson copied += try_to_copy; 4403c4d7559SDave Watson ret = tls_push_record(sk, msg->msg_flags, record_type); 4413c4d7559SDave Watson if (!ret) 4423c4d7559SDave Watson continue; 4433c4d7559SDave Watson if (ret == -EAGAIN) 4443c4d7559SDave Watson goto send_end; 4453c4d7559SDave Watson 4463c4d7559SDave Watson copied -= try_to_copy; 4473c4d7559SDave Watson fallback_to_reg_send: 4483c4d7559SDave Watson iov_iter_revert(&msg->msg_iter, 4493c4d7559SDave Watson ctx->sg_plaintext_size - orig_size); 4503c4d7559SDave Watson trim_sg(sk, ctx->sg_plaintext_data, 4513c4d7559SDave Watson &ctx->sg_plaintext_num_elem, 4523c4d7559SDave Watson &ctx->sg_plaintext_size, 4533c4d7559SDave Watson orig_size); 4543c4d7559SDave Watson } 4553c4d7559SDave Watson 4563c4d7559SDave Watson required_size = ctx->sg_plaintext_size + try_to_copy; 4573c4d7559SDave Watson alloc_plaintext: 4583c4d7559SDave Watson ret = alloc_plaintext_sg(sk, required_size); 4593c4d7559SDave Watson if (ret) { 4603c4d7559SDave Watson if (ret != -ENOSPC) 4613c4d7559SDave Watson goto wait_for_memory; 4623c4d7559SDave Watson 4633c4d7559SDave Watson /* Adjust try_to_copy according to the amount that was 4643c4d7559SDave Watson * actually allocated. The difference is due 4653c4d7559SDave Watson * to max sg elements limit 4663c4d7559SDave Watson */ 4673c4d7559SDave Watson try_to_copy -= required_size - ctx->sg_plaintext_size; 4683c4d7559SDave Watson full_record = true; 4693c4d7559SDave Watson 4703c4d7559SDave Watson trim_sg(sk, ctx->sg_encrypted_data, 4713c4d7559SDave Watson &ctx->sg_encrypted_num_elem, 4723c4d7559SDave Watson &ctx->sg_encrypted_size, 4733c4d7559SDave Watson ctx->sg_plaintext_size + 474dbe42559SDave Watson tls_ctx->tx.overhead_size); 4753c4d7559SDave Watson } 4763c4d7559SDave Watson 4773c4d7559SDave Watson ret = memcopy_from_iter(sk, &msg->msg_iter, try_to_copy); 4783c4d7559SDave Watson if (ret) 4793c4d7559SDave Watson goto trim_sgl; 4803c4d7559SDave Watson 4813c4d7559SDave Watson copied += try_to_copy; 4823c4d7559SDave Watson if (full_record || eor) { 4833c4d7559SDave Watson push_record: 4843c4d7559SDave Watson ret = tls_push_record(sk, msg->msg_flags, record_type); 4853c4d7559SDave Watson if (ret) { 4863c4d7559SDave Watson if (ret == -ENOMEM) 4873c4d7559SDave Watson goto wait_for_memory; 4883c4d7559SDave Watson 4893c4d7559SDave Watson goto send_end; 4903c4d7559SDave Watson } 4913c4d7559SDave Watson } 4923c4d7559SDave Watson 4933c4d7559SDave Watson continue; 4943c4d7559SDave Watson 4953c4d7559SDave Watson wait_for_sndbuf: 4963c4d7559SDave Watson set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 4973c4d7559SDave Watson wait_for_memory: 4983c4d7559SDave Watson ret = sk_stream_wait_memory(sk, &timeo); 4993c4d7559SDave Watson if (ret) { 5003c4d7559SDave Watson trim_sgl: 5013c4d7559SDave Watson trim_both_sgl(sk, orig_size); 5023c4d7559SDave Watson goto send_end; 5033c4d7559SDave Watson } 5043c4d7559SDave Watson 5053c4d7559SDave Watson if (tls_is_pending_closed_record(tls_ctx)) 5063c4d7559SDave Watson goto push_record; 5073c4d7559SDave Watson 5083c4d7559SDave Watson if (ctx->sg_encrypted_size < required_size) 5093c4d7559SDave Watson goto alloc_encrypted; 5103c4d7559SDave Watson 5113c4d7559SDave Watson goto alloc_plaintext; 5123c4d7559SDave Watson } 5133c4d7559SDave Watson 5143c4d7559SDave Watson send_end: 5153c4d7559SDave Watson ret = sk_stream_error(sk, msg->msg_flags, ret); 5163c4d7559SDave Watson 5173c4d7559SDave Watson release_sock(sk); 5183c4d7559SDave Watson return copied ? copied : ret; 5193c4d7559SDave Watson } 5203c4d7559SDave Watson 5213c4d7559SDave Watson int tls_sw_sendpage(struct sock *sk, struct page *page, 5223c4d7559SDave Watson int offset, size_t size, int flags) 5233c4d7559SDave Watson { 5243c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 525f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 5263c4d7559SDave Watson int ret = 0; 5273c4d7559SDave Watson long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); 5283c4d7559SDave Watson bool eor; 5293c4d7559SDave Watson size_t orig_size = size; 5303c4d7559SDave Watson unsigned char record_type = TLS_RECORD_TYPE_DATA; 5313c4d7559SDave Watson struct scatterlist *sg; 5323c4d7559SDave Watson bool full_record; 5333c4d7559SDave Watson int record_room; 5343c4d7559SDave Watson 5353c4d7559SDave Watson if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | 5363c4d7559SDave Watson MSG_SENDPAGE_NOTLAST)) 5373c4d7559SDave Watson return -ENOTSUPP; 5383c4d7559SDave Watson 5393c4d7559SDave Watson /* No MSG_EOR from splice, only look at MSG_MORE */ 5403c4d7559SDave Watson eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST)); 5413c4d7559SDave Watson 5423c4d7559SDave Watson lock_sock(sk); 5433c4d7559SDave Watson 5443c4d7559SDave Watson sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); 5453c4d7559SDave Watson 5463c4d7559SDave Watson if (tls_complete_pending_work(sk, tls_ctx, flags, &timeo)) 5473c4d7559SDave Watson goto sendpage_end; 5483c4d7559SDave Watson 5493c4d7559SDave Watson /* Call the sk_stream functions to manage the sndbuf mem. */ 5503c4d7559SDave Watson while (size > 0) { 5513c4d7559SDave Watson size_t copy, required_size; 5523c4d7559SDave Watson 5533c4d7559SDave Watson if (sk->sk_err) { 55430be8f8dSr.hering@avm.de ret = -sk->sk_err; 5553c4d7559SDave Watson goto sendpage_end; 5563c4d7559SDave Watson } 5573c4d7559SDave Watson 5583c4d7559SDave Watson full_record = false; 5593c4d7559SDave Watson record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size; 5603c4d7559SDave Watson copy = size; 5613c4d7559SDave Watson if (copy >= record_room) { 5623c4d7559SDave Watson copy = record_room; 5633c4d7559SDave Watson full_record = true; 5643c4d7559SDave Watson } 5653c4d7559SDave Watson required_size = ctx->sg_plaintext_size + copy + 566dbe42559SDave Watson tls_ctx->tx.overhead_size; 5673c4d7559SDave Watson 5683c4d7559SDave Watson if (!sk_stream_memory_free(sk)) 5693c4d7559SDave Watson goto wait_for_sndbuf; 5703c4d7559SDave Watson alloc_payload: 5713c4d7559SDave Watson ret = alloc_encrypted_sg(sk, required_size); 5723c4d7559SDave Watson if (ret) { 5733c4d7559SDave Watson if (ret != -ENOSPC) 5743c4d7559SDave Watson goto wait_for_memory; 5753c4d7559SDave Watson 5763c4d7559SDave Watson /* Adjust copy according to the amount that was 5773c4d7559SDave Watson * actually allocated. The difference is due 5783c4d7559SDave Watson * to max sg elements limit 5793c4d7559SDave Watson */ 5803c4d7559SDave Watson copy -= required_size - ctx->sg_plaintext_size; 5813c4d7559SDave Watson full_record = true; 5823c4d7559SDave Watson } 5833c4d7559SDave Watson 5843c4d7559SDave Watson get_page(page); 5853c4d7559SDave Watson sg = ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem; 5863c4d7559SDave Watson sg_set_page(sg, page, copy, offset); 5877a8c4dd9SDave Watson sg_unmark_end(sg); 5887a8c4dd9SDave Watson 5893c4d7559SDave Watson ctx->sg_plaintext_num_elem++; 5903c4d7559SDave Watson 5913c4d7559SDave Watson sk_mem_charge(sk, copy); 5923c4d7559SDave Watson offset += copy; 5933c4d7559SDave Watson size -= copy; 5943c4d7559SDave Watson ctx->sg_plaintext_size += copy; 5953c4d7559SDave Watson tls_ctx->pending_open_record_frags = ctx->sg_plaintext_num_elem; 5963c4d7559SDave Watson 5973c4d7559SDave Watson if (full_record || eor || 5983c4d7559SDave Watson ctx->sg_plaintext_num_elem == 5993c4d7559SDave Watson ARRAY_SIZE(ctx->sg_plaintext_data)) { 6003c4d7559SDave Watson push_record: 6013c4d7559SDave Watson ret = tls_push_record(sk, flags, record_type); 6023c4d7559SDave Watson if (ret) { 6033c4d7559SDave Watson if (ret == -ENOMEM) 6043c4d7559SDave Watson goto wait_for_memory; 6053c4d7559SDave Watson 6063c4d7559SDave Watson goto sendpage_end; 6073c4d7559SDave Watson } 6083c4d7559SDave Watson } 6093c4d7559SDave Watson continue; 6103c4d7559SDave Watson wait_for_sndbuf: 6113c4d7559SDave Watson set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); 6123c4d7559SDave Watson wait_for_memory: 6133c4d7559SDave Watson ret = sk_stream_wait_memory(sk, &timeo); 6143c4d7559SDave Watson if (ret) { 6153c4d7559SDave Watson trim_both_sgl(sk, ctx->sg_plaintext_size); 6163c4d7559SDave Watson goto sendpage_end; 6173c4d7559SDave Watson } 6183c4d7559SDave Watson 6193c4d7559SDave Watson if (tls_is_pending_closed_record(tls_ctx)) 6203c4d7559SDave Watson goto push_record; 6213c4d7559SDave Watson 6223c4d7559SDave Watson goto alloc_payload; 6233c4d7559SDave Watson } 6243c4d7559SDave Watson 6253c4d7559SDave Watson sendpage_end: 6263c4d7559SDave Watson if (orig_size > size) 6273c4d7559SDave Watson ret = orig_size - size; 6283c4d7559SDave Watson else 6293c4d7559SDave Watson ret = sk_stream_error(sk, flags, ret); 6303c4d7559SDave Watson 6313c4d7559SDave Watson release_sock(sk); 6323c4d7559SDave Watson return ret; 6333c4d7559SDave Watson } 6343c4d7559SDave Watson 635c46234ebSDave Watson static struct sk_buff *tls_wait_data(struct sock *sk, int flags, 636c46234ebSDave Watson long timeo, int *err) 637c46234ebSDave Watson { 638c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 639f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 640c46234ebSDave Watson struct sk_buff *skb; 641c46234ebSDave Watson DEFINE_WAIT_FUNC(wait, woken_wake_function); 642c46234ebSDave Watson 643c46234ebSDave Watson while (!(skb = ctx->recv_pkt)) { 644c46234ebSDave Watson if (sk->sk_err) { 645c46234ebSDave Watson *err = sock_error(sk); 646c46234ebSDave Watson return NULL; 647c46234ebSDave Watson } 648c46234ebSDave Watson 649c46234ebSDave Watson if (sock_flag(sk, SOCK_DONE)) 650c46234ebSDave Watson return NULL; 651c46234ebSDave Watson 652c46234ebSDave Watson if ((flags & MSG_DONTWAIT) || !timeo) { 653c46234ebSDave Watson *err = -EAGAIN; 654c46234ebSDave Watson return NULL; 655c46234ebSDave Watson } 656c46234ebSDave Watson 657c46234ebSDave Watson add_wait_queue(sk_sleep(sk), &wait); 658c46234ebSDave Watson sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); 659c46234ebSDave Watson sk_wait_event(sk, &timeo, ctx->recv_pkt != skb, &wait); 660c46234ebSDave Watson sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); 661c46234ebSDave Watson remove_wait_queue(sk_sleep(sk), &wait); 662c46234ebSDave Watson 663c46234ebSDave Watson /* Handle signals */ 664c46234ebSDave Watson if (signal_pending(current)) { 665c46234ebSDave Watson *err = sock_intr_errno(timeo); 666c46234ebSDave Watson return NULL; 667c46234ebSDave Watson } 668c46234ebSDave Watson } 669c46234ebSDave Watson 670c46234ebSDave Watson return skb; 671c46234ebSDave Watson } 672c46234ebSDave Watson 673c46234ebSDave Watson static int decrypt_skb(struct sock *sk, struct sk_buff *skb, 674c46234ebSDave Watson struct scatterlist *sgout) 675c46234ebSDave Watson { 676c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 677f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 678b16520f7SKees Cook char iv[TLS_CIPHER_AES_GCM_128_SALT_SIZE + MAX_IV_SIZE]; 679c46234ebSDave Watson struct scatterlist sgin_arr[MAX_SKB_FRAGS + 2]; 680c46234ebSDave Watson struct scatterlist *sgin = &sgin_arr[0]; 681c46234ebSDave Watson struct strp_msg *rxm = strp_msg(skb); 682c46234ebSDave Watson int ret, nsg = ARRAY_SIZE(sgin_arr); 683c46234ebSDave Watson struct sk_buff *unused; 684c46234ebSDave Watson 685c46234ebSDave Watson ret = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE, 686c46234ebSDave Watson iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, 687c46234ebSDave Watson tls_ctx->rx.iv_size); 688c46234ebSDave Watson if (ret < 0) 689c46234ebSDave Watson return ret; 690c46234ebSDave Watson 691c46234ebSDave Watson memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE); 692c46234ebSDave Watson if (!sgout) { 693c46234ebSDave Watson nsg = skb_cow_data(skb, 0, &unused) + 1; 694c46234ebSDave Watson sgin = kmalloc_array(nsg, sizeof(*sgin), sk->sk_allocation); 695c46234ebSDave Watson sgout = sgin; 696c46234ebSDave Watson } 697c46234ebSDave Watson 698c46234ebSDave Watson sg_init_table(sgin, nsg); 6998ab6ffbaSMatt Mullins sg_set_buf(&sgin[0], ctx->rx_aad_ciphertext, TLS_AAD_SPACE_SIZE); 700c46234ebSDave Watson 701c46234ebSDave Watson nsg = skb_to_sgvec(skb, &sgin[1], 702c46234ebSDave Watson rxm->offset + tls_ctx->rx.prepend_size, 703c46234ebSDave Watson rxm->full_len - tls_ctx->rx.prepend_size); 704c46234ebSDave Watson 7058ab6ffbaSMatt Mullins tls_make_aad(ctx->rx_aad_ciphertext, 706c46234ebSDave Watson rxm->full_len - tls_ctx->rx.overhead_size, 707c46234ebSDave Watson tls_ctx->rx.rec_seq, 708c46234ebSDave Watson tls_ctx->rx.rec_seq_size, 709c46234ebSDave Watson ctx->control); 710c46234ebSDave Watson 711c46234ebSDave Watson ret = tls_do_decryption(sk, sgin, sgout, iv, 712c46234ebSDave Watson rxm->full_len - tls_ctx->rx.overhead_size, 713c46234ebSDave Watson skb, sk->sk_allocation); 714c46234ebSDave Watson 715c46234ebSDave Watson if (sgin != &sgin_arr[0]) 716c46234ebSDave Watson kfree(sgin); 717c46234ebSDave Watson 718c46234ebSDave Watson return ret; 719c46234ebSDave Watson } 720c46234ebSDave Watson 721c46234ebSDave Watson static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb, 722c46234ebSDave Watson unsigned int len) 723c46234ebSDave Watson { 724c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 725f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 726c46234ebSDave Watson struct strp_msg *rxm = strp_msg(skb); 727c46234ebSDave Watson 728c46234ebSDave Watson if (len < rxm->full_len) { 729c46234ebSDave Watson rxm->offset += len; 730c46234ebSDave Watson rxm->full_len -= len; 731c46234ebSDave Watson 732c46234ebSDave Watson return false; 733c46234ebSDave Watson } 734c46234ebSDave Watson 735c46234ebSDave Watson /* Finished with message */ 736c46234ebSDave Watson ctx->recv_pkt = NULL; 737c46234ebSDave Watson kfree_skb(skb); 7387170e604SDoron Roberts-Kedes __strp_unpause(&ctx->strp); 739c46234ebSDave Watson 740c46234ebSDave Watson return true; 741c46234ebSDave Watson } 742c46234ebSDave Watson 743c46234ebSDave Watson int tls_sw_recvmsg(struct sock *sk, 744c46234ebSDave Watson struct msghdr *msg, 745c46234ebSDave Watson size_t len, 746c46234ebSDave Watson int nonblock, 747c46234ebSDave Watson int flags, 748c46234ebSDave Watson int *addr_len) 749c46234ebSDave Watson { 750c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 751f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 752c46234ebSDave Watson unsigned char control; 753c46234ebSDave Watson struct strp_msg *rxm; 754c46234ebSDave Watson struct sk_buff *skb; 755c46234ebSDave Watson ssize_t copied = 0; 756c46234ebSDave Watson bool cmsg = false; 75706030dbaSDaniel Borkmann int target, err = 0; 758c46234ebSDave Watson long timeo; 759c46234ebSDave Watson 760c46234ebSDave Watson flags |= nonblock; 761c46234ebSDave Watson 762c46234ebSDave Watson if (unlikely(flags & MSG_ERRQUEUE)) 763c46234ebSDave Watson return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR); 764c46234ebSDave Watson 765c46234ebSDave Watson lock_sock(sk); 766c46234ebSDave Watson 76706030dbaSDaniel Borkmann target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); 768c46234ebSDave Watson timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 769c46234ebSDave Watson do { 770c46234ebSDave Watson bool zc = false; 771c46234ebSDave Watson int chunk = 0; 772c46234ebSDave Watson 773c46234ebSDave Watson skb = tls_wait_data(sk, flags, timeo, &err); 774c46234ebSDave Watson if (!skb) 775c46234ebSDave Watson goto recv_end; 776c46234ebSDave Watson 777c46234ebSDave Watson rxm = strp_msg(skb); 778c46234ebSDave Watson if (!cmsg) { 779c46234ebSDave Watson int cerr; 780c46234ebSDave Watson 781c46234ebSDave Watson cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, 782c46234ebSDave Watson sizeof(ctx->control), &ctx->control); 783c46234ebSDave Watson cmsg = true; 784c46234ebSDave Watson control = ctx->control; 785c46234ebSDave Watson if (ctx->control != TLS_RECORD_TYPE_DATA) { 786c46234ebSDave Watson if (cerr || msg->msg_flags & MSG_CTRUNC) { 787c46234ebSDave Watson err = -EIO; 788c46234ebSDave Watson goto recv_end; 789c46234ebSDave Watson } 790c46234ebSDave Watson } 791c46234ebSDave Watson } else if (control != ctx->control) { 792c46234ebSDave Watson goto recv_end; 793c46234ebSDave Watson } 794c46234ebSDave Watson 795c46234ebSDave Watson if (!ctx->decrypted) { 796c46234ebSDave Watson int page_count; 797c46234ebSDave Watson int to_copy; 798c46234ebSDave Watson 799c46234ebSDave Watson page_count = iov_iter_npages(&msg->msg_iter, 800c46234ebSDave Watson MAX_SKB_FRAGS); 801c46234ebSDave Watson to_copy = rxm->full_len - tls_ctx->rx.overhead_size; 802c46234ebSDave Watson if (to_copy <= len && page_count < MAX_SKB_FRAGS && 803c46234ebSDave Watson likely(!(flags & MSG_PEEK))) { 804c46234ebSDave Watson struct scatterlist sgin[MAX_SKB_FRAGS + 1]; 805c46234ebSDave Watson int pages = 0; 806c46234ebSDave Watson 807c46234ebSDave Watson zc = true; 808c46234ebSDave Watson sg_init_table(sgin, MAX_SKB_FRAGS + 1); 8098ab6ffbaSMatt Mullins sg_set_buf(&sgin[0], ctx->rx_aad_plaintext, 8108ab6ffbaSMatt Mullins TLS_AAD_SPACE_SIZE); 811c46234ebSDave Watson 812c46234ebSDave Watson err = zerocopy_from_iter(sk, &msg->msg_iter, 813c46234ebSDave Watson to_copy, &pages, 814c46234ebSDave Watson &chunk, &sgin[1], 815c46234ebSDave Watson MAX_SKB_FRAGS, false); 816c46234ebSDave Watson if (err < 0) 817c46234ebSDave Watson goto fallback_to_reg_recv; 818c46234ebSDave Watson 819c46234ebSDave Watson err = decrypt_skb(sk, skb, sgin); 820c46234ebSDave Watson for (; pages > 0; pages--) 821c46234ebSDave Watson put_page(sg_page(&sgin[pages])); 822c46234ebSDave Watson if (err < 0) { 823c46234ebSDave Watson tls_err_abort(sk, EBADMSG); 824c46234ebSDave Watson goto recv_end; 825c46234ebSDave Watson } 826c46234ebSDave Watson } else { 827c46234ebSDave Watson fallback_to_reg_recv: 828c46234ebSDave Watson err = decrypt_skb(sk, skb, NULL); 829c46234ebSDave Watson if (err < 0) { 830c46234ebSDave Watson tls_err_abort(sk, EBADMSG); 831c46234ebSDave Watson goto recv_end; 832c46234ebSDave Watson } 833c46234ebSDave Watson } 834c46234ebSDave Watson ctx->decrypted = true; 835c46234ebSDave Watson } 836c46234ebSDave Watson 837c46234ebSDave Watson if (!zc) { 838c46234ebSDave Watson chunk = min_t(unsigned int, rxm->full_len, len); 839c46234ebSDave Watson err = skb_copy_datagram_msg(skb, rxm->offset, msg, 840c46234ebSDave Watson chunk); 841c46234ebSDave Watson if (err < 0) 842c46234ebSDave Watson goto recv_end; 843c46234ebSDave Watson } 844c46234ebSDave Watson 845c46234ebSDave Watson copied += chunk; 846c46234ebSDave Watson len -= chunk; 847c46234ebSDave Watson if (likely(!(flags & MSG_PEEK))) { 848c46234ebSDave Watson u8 control = ctx->control; 849c46234ebSDave Watson 850c46234ebSDave Watson if (tls_sw_advance_skb(sk, skb, chunk)) { 851c46234ebSDave Watson /* Return full control message to 852c46234ebSDave Watson * userspace before trying to parse 853c46234ebSDave Watson * another message type 854c46234ebSDave Watson */ 855c46234ebSDave Watson msg->msg_flags |= MSG_EOR; 856c46234ebSDave Watson if (control != TLS_RECORD_TYPE_DATA) 857c46234ebSDave Watson goto recv_end; 858c46234ebSDave Watson } 859c46234ebSDave Watson } 86006030dbaSDaniel Borkmann /* If we have a new message from strparser, continue now. */ 86106030dbaSDaniel Borkmann if (copied >= target && !ctx->recv_pkt) 86206030dbaSDaniel Borkmann break; 863c46234ebSDave Watson } while (len); 864c46234ebSDave Watson 865c46234ebSDave Watson recv_end: 866c46234ebSDave Watson release_sock(sk); 867c46234ebSDave Watson return copied ? : err; 868c46234ebSDave Watson } 869c46234ebSDave Watson 870c46234ebSDave Watson ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, 871c46234ebSDave Watson struct pipe_inode_info *pipe, 872c46234ebSDave Watson size_t len, unsigned int flags) 873c46234ebSDave Watson { 874c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sock->sk); 875f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 876c46234ebSDave Watson struct strp_msg *rxm = NULL; 877c46234ebSDave Watson struct sock *sk = sock->sk; 878c46234ebSDave Watson struct sk_buff *skb; 879c46234ebSDave Watson ssize_t copied = 0; 880c46234ebSDave Watson int err = 0; 881c46234ebSDave Watson long timeo; 882c46234ebSDave Watson int chunk; 883c46234ebSDave Watson 884c46234ebSDave Watson lock_sock(sk); 885c46234ebSDave Watson 886c46234ebSDave Watson timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 887c46234ebSDave Watson 888c46234ebSDave Watson skb = tls_wait_data(sk, flags, timeo, &err); 889c46234ebSDave Watson if (!skb) 890c46234ebSDave Watson goto splice_read_end; 891c46234ebSDave Watson 892c46234ebSDave Watson /* splice does not support reading control messages */ 893c46234ebSDave Watson if (ctx->control != TLS_RECORD_TYPE_DATA) { 894c46234ebSDave Watson err = -ENOTSUPP; 895c46234ebSDave Watson goto splice_read_end; 896c46234ebSDave Watson } 897c46234ebSDave Watson 898c46234ebSDave Watson if (!ctx->decrypted) { 899c46234ebSDave Watson err = decrypt_skb(sk, skb, NULL); 900c46234ebSDave Watson 901c46234ebSDave Watson if (err < 0) { 902c46234ebSDave Watson tls_err_abort(sk, EBADMSG); 903c46234ebSDave Watson goto splice_read_end; 904c46234ebSDave Watson } 905c46234ebSDave Watson ctx->decrypted = true; 906c46234ebSDave Watson } 907c46234ebSDave Watson rxm = strp_msg(skb); 908c46234ebSDave Watson 909c46234ebSDave Watson chunk = min_t(unsigned int, rxm->full_len, len); 910c46234ebSDave Watson copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags); 911c46234ebSDave Watson if (copied < 0) 912c46234ebSDave Watson goto splice_read_end; 913c46234ebSDave Watson 914c46234ebSDave Watson if (likely(!(flags & MSG_PEEK))) 915c46234ebSDave Watson tls_sw_advance_skb(sk, skb, copied); 916c46234ebSDave Watson 917c46234ebSDave Watson splice_read_end: 918c46234ebSDave Watson release_sock(sk); 919c46234ebSDave Watson return copied ? : err; 920c46234ebSDave Watson } 921c46234ebSDave Watson 922a11e1d43SLinus Torvalds unsigned int tls_sw_poll(struct file *file, struct socket *sock, 923a11e1d43SLinus Torvalds struct poll_table_struct *wait) 924c46234ebSDave Watson { 925a11e1d43SLinus Torvalds unsigned int ret; 926c46234ebSDave Watson struct sock *sk = sock->sk; 927c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 928f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 929c46234ebSDave Watson 930a11e1d43SLinus Torvalds /* Grab POLLOUT and POLLHUP from the underlying socket */ 931a11e1d43SLinus Torvalds ret = ctx->sk_poll(file, sock, wait); 932c46234ebSDave Watson 933a11e1d43SLinus Torvalds /* Clear POLLIN bits, and set based on recv_pkt */ 934a11e1d43SLinus Torvalds ret &= ~(POLLIN | POLLRDNORM); 935c46234ebSDave Watson if (ctx->recv_pkt) 936a11e1d43SLinus Torvalds ret |= POLLIN | POLLRDNORM; 937c46234ebSDave Watson 938a11e1d43SLinus Torvalds return ret; 939c46234ebSDave Watson } 940c46234ebSDave Watson 941c46234ebSDave Watson static int tls_read_size(struct strparser *strp, struct sk_buff *skb) 942c46234ebSDave Watson { 943c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(strp->sk); 944f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 945c46234ebSDave Watson char header[tls_ctx->rx.prepend_size]; 946c46234ebSDave Watson struct strp_msg *rxm = strp_msg(skb); 947c46234ebSDave Watson size_t cipher_overhead; 948c46234ebSDave Watson size_t data_len = 0; 949c46234ebSDave Watson int ret; 950c46234ebSDave Watson 951c46234ebSDave Watson /* Verify that we have a full TLS header, or wait for more data */ 952c46234ebSDave Watson if (rxm->offset + tls_ctx->rx.prepend_size > skb->len) 953c46234ebSDave Watson return 0; 954c46234ebSDave Watson 955c46234ebSDave Watson /* Linearize header to local buffer */ 956c46234ebSDave Watson ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size); 957c46234ebSDave Watson 958c46234ebSDave Watson if (ret < 0) 959c46234ebSDave Watson goto read_failure; 960c46234ebSDave Watson 961c46234ebSDave Watson ctx->control = header[0]; 962c46234ebSDave Watson 963c46234ebSDave Watson data_len = ((header[4] & 0xFF) | (header[3] << 8)); 964c46234ebSDave Watson 965c46234ebSDave Watson cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size; 966c46234ebSDave Watson 967c46234ebSDave Watson if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) { 968c46234ebSDave Watson ret = -EMSGSIZE; 969c46234ebSDave Watson goto read_failure; 970c46234ebSDave Watson } 971c46234ebSDave Watson if (data_len < cipher_overhead) { 972c46234ebSDave Watson ret = -EBADMSG; 973c46234ebSDave Watson goto read_failure; 974c46234ebSDave Watson } 975c46234ebSDave Watson 976c46234ebSDave Watson if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.version) || 977c46234ebSDave Watson header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.version)) { 978c46234ebSDave Watson ret = -EINVAL; 979c46234ebSDave Watson goto read_failure; 980c46234ebSDave Watson } 981c46234ebSDave Watson 982c46234ebSDave Watson return data_len + TLS_HEADER_SIZE; 983c46234ebSDave Watson 984c46234ebSDave Watson read_failure: 985c46234ebSDave Watson tls_err_abort(strp->sk, ret); 986c46234ebSDave Watson 987c46234ebSDave Watson return ret; 988c46234ebSDave Watson } 989c46234ebSDave Watson 990c46234ebSDave Watson static void tls_queue(struct strparser *strp, struct sk_buff *skb) 991c46234ebSDave Watson { 992c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(strp->sk); 993f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 994c46234ebSDave Watson struct strp_msg *rxm; 995c46234ebSDave Watson 996c46234ebSDave Watson rxm = strp_msg(skb); 997c46234ebSDave Watson 998c46234ebSDave Watson ctx->decrypted = false; 999c46234ebSDave Watson 1000c46234ebSDave Watson ctx->recv_pkt = skb; 1001c46234ebSDave Watson strp_pause(strp); 1002c46234ebSDave Watson 1003c46234ebSDave Watson strp->sk->sk_state_change(strp->sk); 1004c46234ebSDave Watson } 1005c46234ebSDave Watson 1006c46234ebSDave Watson static void tls_data_ready(struct sock *sk) 1007c46234ebSDave Watson { 1008c46234ebSDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 1009f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 1010c46234ebSDave Watson 1011c46234ebSDave Watson strp_data_ready(&ctx->strp); 1012c46234ebSDave Watson } 1013c46234ebSDave Watson 1014f66de3eeSBoris Pismenny void tls_sw_free_resources_tx(struct sock *sk) 10153c4d7559SDave Watson { 10163c4d7559SDave Watson struct tls_context *tls_ctx = tls_get_ctx(sk); 1017f66de3eeSBoris Pismenny struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); 10183c4d7559SDave Watson 10193c4d7559SDave Watson if (ctx->aead_send) 10203c4d7559SDave Watson crypto_free_aead(ctx->aead_send); 1021f66de3eeSBoris Pismenny tls_free_both_sg(sk); 1022f66de3eeSBoris Pismenny 1023f66de3eeSBoris Pismenny kfree(ctx); 1024f66de3eeSBoris Pismenny } 1025f66de3eeSBoris Pismenny 1026f66de3eeSBoris Pismenny void tls_sw_free_resources_rx(struct sock *sk) 1027f66de3eeSBoris Pismenny { 1028f66de3eeSBoris Pismenny struct tls_context *tls_ctx = tls_get_ctx(sk); 1029f66de3eeSBoris Pismenny struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); 1030f66de3eeSBoris Pismenny 1031c46234ebSDave Watson if (ctx->aead_recv) { 1032c46234ebSDave Watson if (ctx->recv_pkt) { 1033c46234ebSDave Watson kfree_skb(ctx->recv_pkt); 1034c46234ebSDave Watson ctx->recv_pkt = NULL; 1035c46234ebSDave Watson } 1036c46234ebSDave Watson crypto_free_aead(ctx->aead_recv); 1037c46234ebSDave Watson strp_stop(&ctx->strp); 1038c46234ebSDave Watson write_lock_bh(&sk->sk_callback_lock); 1039c46234ebSDave Watson sk->sk_data_ready = ctx->saved_data_ready; 1040c46234ebSDave Watson write_unlock_bh(&sk->sk_callback_lock); 1041c46234ebSDave Watson release_sock(sk); 1042c46234ebSDave Watson strp_done(&ctx->strp); 1043c46234ebSDave Watson lock_sock(sk); 1044c46234ebSDave Watson } 10453c4d7559SDave Watson 10463c4d7559SDave Watson kfree(ctx); 10473c4d7559SDave Watson } 10483c4d7559SDave Watson 1049c46234ebSDave Watson int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) 10503c4d7559SDave Watson { 10513c4d7559SDave Watson char keyval[TLS_CIPHER_AES_GCM_128_KEY_SIZE]; 10523c4d7559SDave Watson struct tls_crypto_info *crypto_info; 10533c4d7559SDave Watson struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; 1054f66de3eeSBoris Pismenny struct tls_sw_context_tx *sw_ctx_tx = NULL; 1055f66de3eeSBoris Pismenny struct tls_sw_context_rx *sw_ctx_rx = NULL; 1056c46234ebSDave Watson struct cipher_context *cctx; 1057c46234ebSDave Watson struct crypto_aead **aead; 1058c46234ebSDave Watson struct strp_callbacks cb; 10593c4d7559SDave Watson u16 nonce_size, tag_size, iv_size, rec_seq_size; 10603c4d7559SDave Watson char *iv, *rec_seq; 10613c4d7559SDave Watson int rc = 0; 10623c4d7559SDave Watson 10633c4d7559SDave Watson if (!ctx) { 10643c4d7559SDave Watson rc = -EINVAL; 10653c4d7559SDave Watson goto out; 10663c4d7559SDave Watson } 10673c4d7559SDave Watson 1068f66de3eeSBoris Pismenny if (tx) { 1069f66de3eeSBoris Pismenny sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL); 1070f66de3eeSBoris Pismenny if (!sw_ctx_tx) { 10713c4d7559SDave Watson rc = -ENOMEM; 10723c4d7559SDave Watson goto out; 10733c4d7559SDave Watson } 1074f66de3eeSBoris Pismenny crypto_init_wait(&sw_ctx_tx->async_wait); 1075f66de3eeSBoris Pismenny ctx->priv_ctx_tx = sw_ctx_tx; 1076c46234ebSDave Watson } else { 1077f66de3eeSBoris Pismenny sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL); 1078f66de3eeSBoris Pismenny if (!sw_ctx_rx) { 1079f66de3eeSBoris Pismenny rc = -ENOMEM; 1080f66de3eeSBoris Pismenny goto out; 1081c46234ebSDave Watson } 1082f66de3eeSBoris Pismenny crypto_init_wait(&sw_ctx_rx->async_wait); 1083f66de3eeSBoris Pismenny ctx->priv_ctx_rx = sw_ctx_rx; 1084f66de3eeSBoris Pismenny } 10853c4d7559SDave Watson 1086c46234ebSDave Watson if (tx) { 10873c4d7559SDave Watson crypto_info = &ctx->crypto_send; 1088c46234ebSDave Watson cctx = &ctx->tx; 1089f66de3eeSBoris Pismenny aead = &sw_ctx_tx->aead_send; 1090c46234ebSDave Watson } else { 1091c46234ebSDave Watson crypto_info = &ctx->crypto_recv; 1092c46234ebSDave Watson cctx = &ctx->rx; 1093f66de3eeSBoris Pismenny aead = &sw_ctx_rx->aead_recv; 1094c46234ebSDave Watson } 1095c46234ebSDave Watson 10963c4d7559SDave Watson switch (crypto_info->cipher_type) { 10973c4d7559SDave Watson case TLS_CIPHER_AES_GCM_128: { 10983c4d7559SDave Watson nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; 10993c4d7559SDave Watson tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE; 11003c4d7559SDave Watson iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; 11013c4d7559SDave Watson iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; 11023c4d7559SDave Watson rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE; 11033c4d7559SDave Watson rec_seq = 11043c4d7559SDave Watson ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; 11053c4d7559SDave Watson gcm_128_info = 11063c4d7559SDave Watson (struct tls12_crypto_info_aes_gcm_128 *)crypto_info; 11073c4d7559SDave Watson break; 11083c4d7559SDave Watson } 11093c4d7559SDave Watson default: 11103c4d7559SDave Watson rc = -EINVAL; 1111cf6d43efSSabrina Dubroca goto free_priv; 11123c4d7559SDave Watson } 11133c4d7559SDave Watson 1114b16520f7SKees Cook /* Sanity-check the IV size for stack allocations. */ 1115b16520f7SKees Cook if (iv_size > MAX_IV_SIZE) { 1116b16520f7SKees Cook rc = -EINVAL; 1117b16520f7SKees Cook goto free_priv; 1118b16520f7SKees Cook } 1119b16520f7SKees Cook 1120c46234ebSDave Watson cctx->prepend_size = TLS_HEADER_SIZE + nonce_size; 1121c46234ebSDave Watson cctx->tag_size = tag_size; 1122c46234ebSDave Watson cctx->overhead_size = cctx->prepend_size + cctx->tag_size; 1123c46234ebSDave Watson cctx->iv_size = iv_size; 1124c46234ebSDave Watson cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE, 1125dbe42559SDave Watson GFP_KERNEL); 1126c46234ebSDave Watson if (!cctx->iv) { 11273c4d7559SDave Watson rc = -ENOMEM; 1128cf6d43efSSabrina Dubroca goto free_priv; 11293c4d7559SDave Watson } 1130c46234ebSDave Watson memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE); 1131c46234ebSDave Watson memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size); 1132c46234ebSDave Watson cctx->rec_seq_size = rec_seq_size; 1133c46234ebSDave Watson cctx->rec_seq = kmalloc(rec_seq_size, GFP_KERNEL); 1134c46234ebSDave Watson if (!cctx->rec_seq) { 11353c4d7559SDave Watson rc = -ENOMEM; 11363c4d7559SDave Watson goto free_iv; 11373c4d7559SDave Watson } 1138c46234ebSDave Watson memcpy(cctx->rec_seq, rec_seq, rec_seq_size); 11393c4d7559SDave Watson 1140f66de3eeSBoris Pismenny if (sw_ctx_tx) { 1141f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_encrypted_data, 1142f66de3eeSBoris Pismenny ARRAY_SIZE(sw_ctx_tx->sg_encrypted_data)); 1143f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_plaintext_data, 1144f66de3eeSBoris Pismenny ARRAY_SIZE(sw_ctx_tx->sg_plaintext_data)); 11453c4d7559SDave Watson 1146f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_aead_in, 2); 1147f66de3eeSBoris Pismenny sg_set_buf(&sw_ctx_tx->sg_aead_in[0], sw_ctx_tx->aad_space, 1148f66de3eeSBoris Pismenny sizeof(sw_ctx_tx->aad_space)); 1149f66de3eeSBoris Pismenny sg_unmark_end(&sw_ctx_tx->sg_aead_in[1]); 1150f66de3eeSBoris Pismenny sg_chain(sw_ctx_tx->sg_aead_in, 2, 1151f66de3eeSBoris Pismenny sw_ctx_tx->sg_plaintext_data); 1152f66de3eeSBoris Pismenny sg_init_table(sw_ctx_tx->sg_aead_out, 2); 1153f66de3eeSBoris Pismenny sg_set_buf(&sw_ctx_tx->sg_aead_out[0], sw_ctx_tx->aad_space, 1154f66de3eeSBoris Pismenny sizeof(sw_ctx_tx->aad_space)); 1155f66de3eeSBoris Pismenny sg_unmark_end(&sw_ctx_tx->sg_aead_out[1]); 1156f66de3eeSBoris Pismenny sg_chain(sw_ctx_tx->sg_aead_out, 2, 1157f66de3eeSBoris Pismenny sw_ctx_tx->sg_encrypted_data); 1158c46234ebSDave Watson } 11593c4d7559SDave Watson 1160c46234ebSDave Watson if (!*aead) { 1161c46234ebSDave Watson *aead = crypto_alloc_aead("gcm(aes)", 0, 0); 1162c46234ebSDave Watson if (IS_ERR(*aead)) { 1163c46234ebSDave Watson rc = PTR_ERR(*aead); 1164c46234ebSDave Watson *aead = NULL; 11653c4d7559SDave Watson goto free_rec_seq; 11663c4d7559SDave Watson } 11673c4d7559SDave Watson } 11683c4d7559SDave Watson 11693c4d7559SDave Watson ctx->push_pending_record = tls_sw_push_pending_record; 11703c4d7559SDave Watson 11713c4d7559SDave Watson memcpy(keyval, gcm_128_info->key, TLS_CIPHER_AES_GCM_128_KEY_SIZE); 11723c4d7559SDave Watson 1173c46234ebSDave Watson rc = crypto_aead_setkey(*aead, keyval, 11743c4d7559SDave Watson TLS_CIPHER_AES_GCM_128_KEY_SIZE); 11753c4d7559SDave Watson if (rc) 11763c4d7559SDave Watson goto free_aead; 11773c4d7559SDave Watson 1178c46234ebSDave Watson rc = crypto_aead_setauthsize(*aead, cctx->tag_size); 1179c46234ebSDave Watson if (rc) 1180c46234ebSDave Watson goto free_aead; 1181c46234ebSDave Watson 1182f66de3eeSBoris Pismenny if (sw_ctx_rx) { 1183c46234ebSDave Watson /* Set up strparser */ 1184c46234ebSDave Watson memset(&cb, 0, sizeof(cb)); 1185c46234ebSDave Watson cb.rcv_msg = tls_queue; 1186c46234ebSDave Watson cb.parse_msg = tls_read_size; 1187c46234ebSDave Watson 1188f66de3eeSBoris Pismenny strp_init(&sw_ctx_rx->strp, sk, &cb); 1189c46234ebSDave Watson 1190c46234ebSDave Watson write_lock_bh(&sk->sk_callback_lock); 1191f66de3eeSBoris Pismenny sw_ctx_rx->saved_data_ready = sk->sk_data_ready; 1192c46234ebSDave Watson sk->sk_data_ready = tls_data_ready; 1193c46234ebSDave Watson write_unlock_bh(&sk->sk_callback_lock); 1194c46234ebSDave Watson 1195a11e1d43SLinus Torvalds sw_ctx_rx->sk_poll = sk->sk_socket->ops->poll; 1196c46234ebSDave Watson 1197f66de3eeSBoris Pismenny strp_check_rcv(&sw_ctx_rx->strp); 1198c46234ebSDave Watson } 1199c46234ebSDave Watson 1200c46234ebSDave Watson goto out; 12013c4d7559SDave Watson 12023c4d7559SDave Watson free_aead: 1203c46234ebSDave Watson crypto_free_aead(*aead); 1204c46234ebSDave Watson *aead = NULL; 12053c4d7559SDave Watson free_rec_seq: 1206c46234ebSDave Watson kfree(cctx->rec_seq); 1207c46234ebSDave Watson cctx->rec_seq = NULL; 12083c4d7559SDave Watson free_iv: 1209f66de3eeSBoris Pismenny kfree(cctx->iv); 1210f66de3eeSBoris Pismenny cctx->iv = NULL; 1211cf6d43efSSabrina Dubroca free_priv: 1212f66de3eeSBoris Pismenny if (tx) { 1213f66de3eeSBoris Pismenny kfree(ctx->priv_ctx_tx); 1214f66de3eeSBoris Pismenny ctx->priv_ctx_tx = NULL; 1215f66de3eeSBoris Pismenny } else { 1216f66de3eeSBoris Pismenny kfree(ctx->priv_ctx_rx); 1217f66de3eeSBoris Pismenny ctx->priv_ctx_rx = NULL; 1218f66de3eeSBoris Pismenny } 12193c4d7559SDave Watson out: 12203c4d7559SDave Watson return rc; 12213c4d7559SDave Watson } 1222