Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. |
| 3 | * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved. |
| 4 | * Copyright (c) 2016-2017, Lance Chao <lancerchao@fb.com>. All rights reserved. |
| 5 | * Copyright (c) 2016, Fridolin Pokorny <fridolin.pokorny@gmail.com>. All rights reserved. |
| 6 | * Copyright (c) 2016, Nikos Mavrogiannopoulos <nmav@gnutls.org>. All rights reserved. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 7 | * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | * |
| 9 | * This software is available to you under a choice of one of two |
| 10 | * licenses. You may choose to be licensed under the terms of the GNU |
| 11 | * General Public License (GPL) Version 2, available from the file |
| 12 | * COPYING in the main directory of this source tree, or the |
| 13 | * OpenIB.org BSD license below: |
| 14 | * |
| 15 | * Redistribution and use in source and binary forms, with or |
| 16 | * without modification, are permitted provided that the following |
| 17 | * conditions are met: |
| 18 | * |
| 19 | * - Redistributions of source code must retain the above |
| 20 | * copyright notice, this list of conditions and the following |
| 21 | * disclaimer. |
| 22 | * |
| 23 | * - Redistributions in binary form must reproduce the above |
| 24 | * copyright notice, this list of conditions and the following |
| 25 | * disclaimer in the documentation and/or other materials |
| 26 | * provided with the distribution. |
| 27 | * |
| 28 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 29 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 31 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 32 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 33 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 34 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | * SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #include <linux/sched/signal.h> |
| 39 | #include <linux/module.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 40 | #include <linux/splice.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | #include <crypto/aead.h> |
| 42 | |
| 43 | #include <net/strparser.h> |
| 44 | #include <net/tls.h> |
| 45 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 46 | static int __skb_nsg(struct sk_buff *skb, int offset, int len, |
| 47 | unsigned int recursion_level) |
| 48 | { |
| 49 | int start = skb_headlen(skb); |
| 50 | int i, chunk = start - offset; |
| 51 | struct sk_buff *frag_iter; |
| 52 | int elt = 0; |
| 53 | |
| 54 | if (unlikely(recursion_level >= 24)) |
| 55 | return -EMSGSIZE; |
| 56 | |
| 57 | if (chunk > 0) { |
| 58 | if (chunk > len) |
| 59 | chunk = len; |
| 60 | elt++; |
| 61 | len -= chunk; |
| 62 | if (len == 0) |
| 63 | return elt; |
| 64 | offset += chunk; |
| 65 | } |
| 66 | |
| 67 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 68 | int end; |
| 69 | |
| 70 | WARN_ON(start > offset + len); |
| 71 | |
| 72 | end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]); |
| 73 | chunk = end - offset; |
| 74 | if (chunk > 0) { |
| 75 | if (chunk > len) |
| 76 | chunk = len; |
| 77 | elt++; |
| 78 | len -= chunk; |
| 79 | if (len == 0) |
| 80 | return elt; |
| 81 | offset += chunk; |
| 82 | } |
| 83 | start = end; |
| 84 | } |
| 85 | |
| 86 | if (unlikely(skb_has_frag_list(skb))) { |
| 87 | skb_walk_frags(skb, frag_iter) { |
| 88 | int end, ret; |
| 89 | |
| 90 | WARN_ON(start > offset + len); |
| 91 | |
| 92 | end = start + frag_iter->len; |
| 93 | chunk = end - offset; |
| 94 | if (chunk > 0) { |
| 95 | if (chunk > len) |
| 96 | chunk = len; |
| 97 | ret = __skb_nsg(frag_iter, offset - start, chunk, |
| 98 | recursion_level + 1); |
| 99 | if (unlikely(ret < 0)) |
| 100 | return ret; |
| 101 | elt += ret; |
| 102 | len -= chunk; |
| 103 | if (len == 0) |
| 104 | return elt; |
| 105 | offset += chunk; |
| 106 | } |
| 107 | start = end; |
| 108 | } |
| 109 | } |
| 110 | BUG_ON(len); |
| 111 | return elt; |
| 112 | } |
| 113 | |
| 114 | /* Return the number of scatterlist elements required to completely map the |
| 115 | * skb, or -EMSGSIZE if the recursion depth is exceeded. |
| 116 | */ |
| 117 | static int skb_nsg(struct sk_buff *skb, int offset, int len) |
| 118 | { |
| 119 | return __skb_nsg(skb, offset, len, 0); |
| 120 | } |
| 121 | |
| 122 | static int padding_length(struct tls_sw_context_rx *ctx, |
| 123 | struct tls_prot_info *prot, struct sk_buff *skb) |
| 124 | { |
| 125 | struct strp_msg *rxm = strp_msg(skb); |
| 126 | int sub = 0; |
| 127 | |
| 128 | /* Determine zero-padding length */ |
| 129 | if (prot->version == TLS_1_3_VERSION) { |
| 130 | char content_type = 0; |
| 131 | int err; |
| 132 | int back = 17; |
| 133 | |
| 134 | while (content_type == 0) { |
| 135 | if (back > rxm->full_len - prot->prepend_size) |
| 136 | return -EBADMSG; |
| 137 | err = skb_copy_bits(skb, |
| 138 | rxm->offset + rxm->full_len - back, |
| 139 | &content_type, 1); |
| 140 | if (err) |
| 141 | return err; |
| 142 | if (content_type) |
| 143 | break; |
| 144 | sub++; |
| 145 | back++; |
| 146 | } |
| 147 | ctx->control = content_type; |
| 148 | } |
| 149 | return sub; |
| 150 | } |
| 151 | |
| 152 | static void tls_decrypt_done(struct crypto_async_request *req, int err) |
| 153 | { |
| 154 | struct aead_request *aead_req = (struct aead_request *)req; |
| 155 | struct scatterlist *sgout = aead_req->dst; |
| 156 | struct scatterlist *sgin = aead_req->src; |
| 157 | struct tls_sw_context_rx *ctx; |
| 158 | struct tls_context *tls_ctx; |
| 159 | struct tls_prot_info *prot; |
| 160 | struct scatterlist *sg; |
| 161 | struct sk_buff *skb; |
| 162 | unsigned int pages; |
| 163 | int pending; |
| 164 | |
| 165 | skb = (struct sk_buff *)req->data; |
| 166 | tls_ctx = tls_get_ctx(skb->sk); |
| 167 | ctx = tls_sw_ctx_rx(tls_ctx); |
| 168 | prot = &tls_ctx->prot_info; |
| 169 | |
| 170 | /* Propagate if there was an err */ |
| 171 | if (err) { |
| 172 | ctx->async_wait.err = err; |
| 173 | tls_err_abort(skb->sk, err); |
| 174 | } else { |
| 175 | struct strp_msg *rxm = strp_msg(skb); |
| 176 | int pad; |
| 177 | |
| 178 | pad = padding_length(ctx, prot, skb); |
| 179 | if (pad < 0) { |
| 180 | ctx->async_wait.err = pad; |
| 181 | tls_err_abort(skb->sk, pad); |
| 182 | } else { |
| 183 | rxm->full_len -= pad; |
| 184 | rxm->offset += prot->prepend_size; |
| 185 | rxm->full_len -= prot->overhead_size; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* After using skb->sk to propagate sk through crypto async callback |
| 190 | * we need to NULL it again. |
| 191 | */ |
| 192 | skb->sk = NULL; |
| 193 | |
| 194 | |
| 195 | /* Free the destination pages if skb was not decrypted inplace */ |
| 196 | if (sgout != sgin) { |
| 197 | /* Skip the first S/G entry as it points to AAD */ |
| 198 | for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) { |
| 199 | if (!sg) |
| 200 | break; |
| 201 | put_page(sg_page(sg)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | kfree(aead_req); |
| 206 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 207 | spin_lock_bh(&ctx->decrypt_compl_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 208 | pending = atomic_dec_return(&ctx->decrypt_pending); |
| 209 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 210 | if (!pending && ctx->async_notify) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 211 | complete(&ctx->async_wait.completion); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 212 | spin_unlock_bh(&ctx->decrypt_compl_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 213 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 214 | |
| 215 | static int tls_do_decryption(struct sock *sk, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 216 | struct sk_buff *skb, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 217 | struct scatterlist *sgin, |
| 218 | struct scatterlist *sgout, |
| 219 | char *iv_recv, |
| 220 | size_t data_len, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 221 | struct aead_request *aead_req, |
| 222 | bool async) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 223 | { |
| 224 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 226 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 227 | int ret; |
| 228 | |
| 229 | aead_request_set_tfm(aead_req, ctx->aead_recv); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 230 | aead_request_set_ad(aead_req, prot->aad_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 231 | aead_request_set_crypt(aead_req, sgin, sgout, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 232 | data_len + prot->tag_size, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 233 | (u8 *)iv_recv); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 234 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 235 | if (async) { |
| 236 | /* Using skb->sk to push sk through to crypto async callback |
| 237 | * handler. This allows propagating errors up to the socket |
| 238 | * if needed. It _must_ be cleared in the async handler |
| 239 | * before consume_skb is called. We _know_ skb->sk is NULL |
| 240 | * because it is a clone from strparser. |
| 241 | */ |
| 242 | skb->sk = sk; |
| 243 | aead_request_set_callback(aead_req, |
| 244 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 245 | tls_decrypt_done, skb); |
| 246 | atomic_inc(&ctx->decrypt_pending); |
| 247 | } else { |
| 248 | aead_request_set_callback(aead_req, |
| 249 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 250 | crypto_req_done, &ctx->async_wait); |
| 251 | } |
| 252 | |
| 253 | ret = crypto_aead_decrypt(aead_req); |
| 254 | if (ret == -EINPROGRESS) { |
| 255 | if (async) |
| 256 | return ret; |
| 257 | |
| 258 | ret = crypto_wait_req(ret, &ctx->async_wait); |
| 259 | } |
| 260 | |
| 261 | if (async) |
| 262 | atomic_dec(&ctx->decrypt_pending); |
| 263 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 264 | return ret; |
| 265 | } |
| 266 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 267 | static void tls_trim_both_msgs(struct sock *sk, int target_size) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 268 | { |
| 269 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 270 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 271 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 272 | struct tls_rec *rec = ctx->open_rec; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 273 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 274 | sk_msg_trim(sk, &rec->msg_plaintext, target_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 275 | if (target_size > 0) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 276 | target_size += prot->overhead_size; |
| 277 | sk_msg_trim(sk, &rec->msg_encrypted, target_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 278 | } |
| 279 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 280 | static int tls_alloc_encrypted_msg(struct sock *sk, int len) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 281 | { |
| 282 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 283 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 284 | struct tls_rec *rec = ctx->open_rec; |
| 285 | struct sk_msg *msg_en = &rec->msg_encrypted; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 286 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 287 | return sk_msg_alloc(sk, msg_en, len, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 288 | } |
| 289 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 290 | static int tls_clone_plaintext_msg(struct sock *sk, int required) |
| 291 | { |
| 292 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 293 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 294 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 295 | struct tls_rec *rec = ctx->open_rec; |
| 296 | struct sk_msg *msg_pl = &rec->msg_plaintext; |
| 297 | struct sk_msg *msg_en = &rec->msg_encrypted; |
| 298 | int skip, len; |
| 299 | |
| 300 | /* We add page references worth len bytes from encrypted sg |
| 301 | * at the end of plaintext sg. It is guaranteed that msg_en |
| 302 | * has enough required room (ensured by caller). |
| 303 | */ |
| 304 | len = required - msg_pl->sg.size; |
| 305 | |
| 306 | /* Skip initial bytes in msg_en's data to be able to use |
| 307 | * same offset of both plain and encrypted data. |
| 308 | */ |
| 309 | skip = prot->prepend_size + msg_pl->sg.size; |
| 310 | |
| 311 | return sk_msg_clone(sk, msg_pl, msg_en, skip, len); |
| 312 | } |
| 313 | |
| 314 | static struct tls_rec *tls_get_rec(struct sock *sk) |
| 315 | { |
| 316 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 317 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 318 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 319 | struct sk_msg *msg_pl, *msg_en; |
| 320 | struct tls_rec *rec; |
| 321 | int mem_size; |
| 322 | |
| 323 | mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send); |
| 324 | |
| 325 | rec = kzalloc(mem_size, sk->sk_allocation); |
| 326 | if (!rec) |
| 327 | return NULL; |
| 328 | |
| 329 | msg_pl = &rec->msg_plaintext; |
| 330 | msg_en = &rec->msg_encrypted; |
| 331 | |
| 332 | sk_msg_init(msg_pl); |
| 333 | sk_msg_init(msg_en); |
| 334 | |
| 335 | sg_init_table(rec->sg_aead_in, 2); |
| 336 | sg_set_buf(&rec->sg_aead_in[0], rec->aad_space, prot->aad_size); |
| 337 | sg_unmark_end(&rec->sg_aead_in[1]); |
| 338 | |
| 339 | sg_init_table(rec->sg_aead_out, 2); |
| 340 | sg_set_buf(&rec->sg_aead_out[0], rec->aad_space, prot->aad_size); |
| 341 | sg_unmark_end(&rec->sg_aead_out[1]); |
| 342 | |
| 343 | return rec; |
| 344 | } |
| 345 | |
| 346 | static void tls_free_rec(struct sock *sk, struct tls_rec *rec) |
| 347 | { |
| 348 | sk_msg_free(sk, &rec->msg_encrypted); |
| 349 | sk_msg_free(sk, &rec->msg_plaintext); |
| 350 | kfree(rec); |
| 351 | } |
| 352 | |
| 353 | static void tls_free_open_rec(struct sock *sk) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 354 | { |
| 355 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 356 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 357 | struct tls_rec *rec = ctx->open_rec; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 358 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 359 | if (rec) { |
| 360 | tls_free_rec(sk, rec); |
| 361 | ctx->open_rec = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 362 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 363 | } |
| 364 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 365 | int tls_tx_records(struct sock *sk, int flags) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 366 | { |
| 367 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 368 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 369 | struct tls_rec *rec, *tmp; |
| 370 | struct sk_msg *msg_en; |
| 371 | int tx_flags, rc = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 372 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 373 | if (tls_is_partially_sent_record(tls_ctx)) { |
| 374 | rec = list_first_entry(&ctx->tx_list, |
| 375 | struct tls_rec, list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 376 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 377 | if (flags == -1) |
| 378 | tx_flags = rec->tx_flags; |
| 379 | else |
| 380 | tx_flags = flags; |
| 381 | |
| 382 | rc = tls_push_partial_record(sk, tls_ctx, tx_flags); |
| 383 | if (rc) |
| 384 | goto tx_err; |
| 385 | |
| 386 | /* Full record has been transmitted. |
| 387 | * Remove the head of tx_list |
| 388 | */ |
| 389 | list_del(&rec->list); |
| 390 | sk_msg_free(sk, &rec->msg_plaintext); |
| 391 | kfree(rec); |
| 392 | } |
| 393 | |
| 394 | /* Tx all ready records */ |
| 395 | list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) { |
| 396 | if (READ_ONCE(rec->tx_ready)) { |
| 397 | if (flags == -1) |
| 398 | tx_flags = rec->tx_flags; |
| 399 | else |
| 400 | tx_flags = flags; |
| 401 | |
| 402 | msg_en = &rec->msg_encrypted; |
| 403 | rc = tls_push_sg(sk, tls_ctx, |
| 404 | &msg_en->sg.data[msg_en->sg.curr], |
| 405 | 0, tx_flags); |
| 406 | if (rc) |
| 407 | goto tx_err; |
| 408 | |
| 409 | list_del(&rec->list); |
| 410 | sk_msg_free(sk, &rec->msg_plaintext); |
| 411 | kfree(rec); |
| 412 | } else { |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | tx_err: |
| 418 | if (rc < 0 && rc != -EAGAIN) |
| 419 | tls_err_abort(sk, EBADMSG); |
| 420 | |
| 421 | return rc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 422 | } |
| 423 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 424 | static void tls_encrypt_done(struct crypto_async_request *req, int err) |
| 425 | { |
| 426 | struct aead_request *aead_req = (struct aead_request *)req; |
| 427 | struct sock *sk = req->data; |
| 428 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 429 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 430 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 431 | struct scatterlist *sge; |
| 432 | struct sk_msg *msg_en; |
| 433 | struct tls_rec *rec; |
| 434 | bool ready = false; |
| 435 | int pending; |
| 436 | |
| 437 | rec = container_of(aead_req, struct tls_rec, aead_req); |
| 438 | msg_en = &rec->msg_encrypted; |
| 439 | |
| 440 | sge = sk_msg_elem(msg_en, msg_en->sg.curr); |
| 441 | sge->offset -= prot->prepend_size; |
| 442 | sge->length += prot->prepend_size; |
| 443 | |
| 444 | /* Check if error is previously set on socket */ |
| 445 | if (err || sk->sk_err) { |
| 446 | rec = NULL; |
| 447 | |
| 448 | /* If err is already set on socket, return the same code */ |
| 449 | if (sk->sk_err) { |
| 450 | ctx->async_wait.err = sk->sk_err; |
| 451 | } else { |
| 452 | ctx->async_wait.err = err; |
| 453 | tls_err_abort(sk, err); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | if (rec) { |
| 458 | struct tls_rec *first_rec; |
| 459 | |
| 460 | /* Mark the record as ready for transmission */ |
| 461 | smp_store_mb(rec->tx_ready, true); |
| 462 | |
| 463 | /* If received record is at head of tx_list, schedule tx */ |
| 464 | first_rec = list_first_entry(&ctx->tx_list, |
| 465 | struct tls_rec, list); |
| 466 | if (rec == first_rec) |
| 467 | ready = true; |
| 468 | } |
| 469 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 470 | spin_lock_bh(&ctx->encrypt_compl_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 471 | pending = atomic_dec_return(&ctx->encrypt_pending); |
| 472 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 473 | if (!pending && ctx->async_notify) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 474 | complete(&ctx->async_wait.completion); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 475 | spin_unlock_bh(&ctx->encrypt_compl_lock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 476 | |
| 477 | if (!ready) |
| 478 | return; |
| 479 | |
| 480 | /* Schedule the transmission */ |
| 481 | if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) |
| 482 | schedule_delayed_work(&ctx->tx_work.work, 1); |
| 483 | } |
| 484 | |
| 485 | static int tls_do_encryption(struct sock *sk, |
| 486 | struct tls_context *tls_ctx, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 487 | struct tls_sw_context_tx *ctx, |
| 488 | struct aead_request *aead_req, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 489 | size_t data_len, u32 start) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 490 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 491 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 492 | struct tls_rec *rec = ctx->open_rec; |
| 493 | struct sk_msg *msg_en = &rec->msg_encrypted; |
| 494 | struct scatterlist *sge = sk_msg_elem(msg_en, start); |
| 495 | int rc, iv_offset = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 496 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 497 | /* For CCM based ciphers, first byte of IV is a constant */ |
| 498 | if (prot->cipher_type == TLS_CIPHER_AES_CCM_128) { |
| 499 | rec->iv_data[0] = TLS_AES_CCM_IV_B0_BYTE; |
| 500 | iv_offset = 1; |
| 501 | } |
| 502 | |
| 503 | memcpy(&rec->iv_data[iv_offset], tls_ctx->tx.iv, |
| 504 | prot->iv_size + prot->salt_size); |
| 505 | |
| 506 | xor_iv_with_seq(prot->version, rec->iv_data, tls_ctx->tx.rec_seq); |
| 507 | |
| 508 | sge->offset += prot->prepend_size; |
| 509 | sge->length -= prot->prepend_size; |
| 510 | |
| 511 | msg_en->sg.curr = start; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 512 | |
| 513 | aead_request_set_tfm(aead_req, ctx->aead_send); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 514 | aead_request_set_ad(aead_req, prot->aad_size); |
| 515 | aead_request_set_crypt(aead_req, rec->sg_aead_in, |
| 516 | rec->sg_aead_out, |
| 517 | data_len, rec->iv_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 518 | |
| 519 | aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 520 | tls_encrypt_done, sk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 521 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 522 | /* Add the record in tx_list */ |
| 523 | list_add_tail((struct list_head *)&rec->list, &ctx->tx_list); |
| 524 | atomic_inc(&ctx->encrypt_pending); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 525 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 526 | rc = crypto_aead_encrypt(aead_req); |
| 527 | if (!rc || rc != -EINPROGRESS) { |
| 528 | atomic_dec(&ctx->encrypt_pending); |
| 529 | sge->offset -= prot->prepend_size; |
| 530 | sge->length += prot->prepend_size; |
| 531 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 532 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 533 | if (!rc) { |
| 534 | WRITE_ONCE(rec->tx_ready, true); |
| 535 | } else if (rc != -EINPROGRESS) { |
| 536 | list_del(&rec->list); |
| 537 | return rc; |
| 538 | } |
| 539 | |
| 540 | /* Unhook the record from context if encryption is not failure */ |
| 541 | ctx->open_rec = NULL; |
| 542 | tls_advance_record_sn(sk, prot, &tls_ctx->tx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 543 | return rc; |
| 544 | } |
| 545 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 546 | static int tls_split_open_record(struct sock *sk, struct tls_rec *from, |
| 547 | struct tls_rec **to, struct sk_msg *msg_opl, |
| 548 | struct sk_msg *msg_oen, u32 split_point, |
| 549 | u32 tx_overhead_size, u32 *orig_end) |
| 550 | { |
| 551 | u32 i, j, bytes = 0, apply = msg_opl->apply_bytes; |
| 552 | struct scatterlist *sge, *osge, *nsge; |
| 553 | u32 orig_size = msg_opl->sg.size; |
| 554 | struct scatterlist tmp = { }; |
| 555 | struct sk_msg *msg_npl; |
| 556 | struct tls_rec *new; |
| 557 | int ret; |
| 558 | |
| 559 | new = tls_get_rec(sk); |
| 560 | if (!new) |
| 561 | return -ENOMEM; |
| 562 | ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size + |
| 563 | tx_overhead_size, 0); |
| 564 | if (ret < 0) { |
| 565 | tls_free_rec(sk, new); |
| 566 | return ret; |
| 567 | } |
| 568 | |
| 569 | *orig_end = msg_opl->sg.end; |
| 570 | i = msg_opl->sg.start; |
| 571 | sge = sk_msg_elem(msg_opl, i); |
| 572 | while (apply && sge->length) { |
| 573 | if (sge->length > apply) { |
| 574 | u32 len = sge->length - apply; |
| 575 | |
| 576 | get_page(sg_page(sge)); |
| 577 | sg_set_page(&tmp, sg_page(sge), len, |
| 578 | sge->offset + apply); |
| 579 | sge->length = apply; |
| 580 | bytes += apply; |
| 581 | apply = 0; |
| 582 | } else { |
| 583 | apply -= sge->length; |
| 584 | bytes += sge->length; |
| 585 | } |
| 586 | |
| 587 | sk_msg_iter_var_next(i); |
| 588 | if (i == msg_opl->sg.end) |
| 589 | break; |
| 590 | sge = sk_msg_elem(msg_opl, i); |
| 591 | } |
| 592 | |
| 593 | msg_opl->sg.end = i; |
| 594 | msg_opl->sg.curr = i; |
| 595 | msg_opl->sg.copybreak = 0; |
| 596 | msg_opl->apply_bytes = 0; |
| 597 | msg_opl->sg.size = bytes; |
| 598 | |
| 599 | msg_npl = &new->msg_plaintext; |
| 600 | msg_npl->apply_bytes = apply; |
| 601 | msg_npl->sg.size = orig_size - bytes; |
| 602 | |
| 603 | j = msg_npl->sg.start; |
| 604 | nsge = sk_msg_elem(msg_npl, j); |
| 605 | if (tmp.length) { |
| 606 | memcpy(nsge, &tmp, sizeof(*nsge)); |
| 607 | sk_msg_iter_var_next(j); |
| 608 | nsge = sk_msg_elem(msg_npl, j); |
| 609 | } |
| 610 | |
| 611 | osge = sk_msg_elem(msg_opl, i); |
| 612 | while (osge->length) { |
| 613 | memcpy(nsge, osge, sizeof(*nsge)); |
| 614 | sg_unmark_end(nsge); |
| 615 | sk_msg_iter_var_next(i); |
| 616 | sk_msg_iter_var_next(j); |
| 617 | if (i == *orig_end) |
| 618 | break; |
| 619 | osge = sk_msg_elem(msg_opl, i); |
| 620 | nsge = sk_msg_elem(msg_npl, j); |
| 621 | } |
| 622 | |
| 623 | msg_npl->sg.end = j; |
| 624 | msg_npl->sg.curr = j; |
| 625 | msg_npl->sg.copybreak = 0; |
| 626 | |
| 627 | *to = new; |
| 628 | return 0; |
| 629 | } |
| 630 | |
| 631 | static void tls_merge_open_record(struct sock *sk, struct tls_rec *to, |
| 632 | struct tls_rec *from, u32 orig_end) |
| 633 | { |
| 634 | struct sk_msg *msg_npl = &from->msg_plaintext; |
| 635 | struct sk_msg *msg_opl = &to->msg_plaintext; |
| 636 | struct scatterlist *osge, *nsge; |
| 637 | u32 i, j; |
| 638 | |
| 639 | i = msg_opl->sg.end; |
| 640 | sk_msg_iter_var_prev(i); |
| 641 | j = msg_npl->sg.start; |
| 642 | |
| 643 | osge = sk_msg_elem(msg_opl, i); |
| 644 | nsge = sk_msg_elem(msg_npl, j); |
| 645 | |
| 646 | if (sg_page(osge) == sg_page(nsge) && |
| 647 | osge->offset + osge->length == nsge->offset) { |
| 648 | osge->length += nsge->length; |
| 649 | put_page(sg_page(nsge)); |
| 650 | } |
| 651 | |
| 652 | msg_opl->sg.end = orig_end; |
| 653 | msg_opl->sg.curr = orig_end; |
| 654 | msg_opl->sg.copybreak = 0; |
| 655 | msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size; |
| 656 | msg_opl->sg.size += msg_npl->sg.size; |
| 657 | |
| 658 | sk_msg_free(sk, &to->msg_encrypted); |
| 659 | sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted); |
| 660 | |
| 661 | kfree(from); |
| 662 | } |
| 663 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 664 | static int tls_push_record(struct sock *sk, int flags, |
| 665 | unsigned char record_type) |
| 666 | { |
| 667 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 668 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 669 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 670 | struct tls_rec *rec = ctx->open_rec, *tmp = NULL; |
| 671 | u32 i, split_point, uninitialized_var(orig_end); |
| 672 | struct sk_msg *msg_pl, *msg_en; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 673 | struct aead_request *req; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 674 | bool split; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 675 | int rc; |
| 676 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 677 | if (!rec) |
| 678 | return 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 679 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 680 | msg_pl = &rec->msg_plaintext; |
| 681 | msg_en = &rec->msg_encrypted; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 682 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 683 | split_point = msg_pl->apply_bytes; |
| 684 | split = split_point && split_point < msg_pl->sg.size; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 685 | if (unlikely((!split && |
| 686 | msg_pl->sg.size + |
| 687 | prot->overhead_size > msg_en->sg.size) || |
| 688 | (split && |
| 689 | split_point + |
| 690 | prot->overhead_size > msg_en->sg.size))) { |
| 691 | split = true; |
| 692 | split_point = msg_en->sg.size; |
| 693 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 694 | if (split) { |
| 695 | rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en, |
| 696 | split_point, prot->overhead_size, |
| 697 | &orig_end); |
| 698 | if (rc < 0) |
| 699 | return rc; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 700 | /* This can happen if above tls_split_open_record allocates |
| 701 | * a single large encryption buffer instead of two smaller |
| 702 | * ones. In this case adjust pointers and continue without |
| 703 | * split. |
| 704 | */ |
| 705 | if (!msg_pl->sg.size) { |
| 706 | tls_merge_open_record(sk, rec, tmp, orig_end); |
| 707 | msg_pl = &rec->msg_plaintext; |
| 708 | msg_en = &rec->msg_encrypted; |
| 709 | split = false; |
| 710 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 711 | sk_msg_trim(sk, msg_en, msg_pl->sg.size + |
| 712 | prot->overhead_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 713 | } |
| 714 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 715 | rec->tx_flags = flags; |
| 716 | req = &rec->aead_req; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 717 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 718 | i = msg_pl->sg.end; |
| 719 | sk_msg_iter_var_prev(i); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 720 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 721 | rec->content_type = record_type; |
| 722 | if (prot->version == TLS_1_3_VERSION) { |
| 723 | /* Add content type to end of message. No padding added */ |
| 724 | sg_set_buf(&rec->sg_content_type, &rec->content_type, 1); |
| 725 | sg_mark_end(&rec->sg_content_type); |
| 726 | sg_chain(msg_pl->sg.data, msg_pl->sg.end + 1, |
| 727 | &rec->sg_content_type); |
| 728 | } else { |
| 729 | sg_mark_end(sk_msg_elem(msg_pl, i)); |
| 730 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 731 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 732 | if (msg_pl->sg.end < msg_pl->sg.start) { |
| 733 | sg_chain(&msg_pl->sg.data[msg_pl->sg.start], |
| 734 | MAX_SKB_FRAGS - msg_pl->sg.start + 1, |
| 735 | msg_pl->sg.data); |
| 736 | } |
| 737 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 738 | i = msg_pl->sg.start; |
| 739 | sg_chain(rec->sg_aead_in, 2, &msg_pl->sg.data[i]); |
| 740 | |
| 741 | i = msg_en->sg.end; |
| 742 | sk_msg_iter_var_prev(i); |
| 743 | sg_mark_end(sk_msg_elem(msg_en, i)); |
| 744 | |
| 745 | i = msg_en->sg.start; |
| 746 | sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]); |
| 747 | |
| 748 | tls_make_aad(rec->aad_space, msg_pl->sg.size + prot->tail_size, |
| 749 | tls_ctx->tx.rec_seq, prot->rec_seq_size, |
| 750 | record_type, prot->version); |
| 751 | |
| 752 | tls_fill_prepend(tls_ctx, |
| 753 | page_address(sg_page(&msg_en->sg.data[i])) + |
| 754 | msg_en->sg.data[i].offset, |
| 755 | msg_pl->sg.size + prot->tail_size, |
| 756 | record_type, prot->version); |
| 757 | |
| 758 | tls_ctx->pending_open_record_frags = false; |
| 759 | |
| 760 | rc = tls_do_encryption(sk, tls_ctx, ctx, req, |
| 761 | msg_pl->sg.size + prot->tail_size, i); |
| 762 | if (rc < 0) { |
| 763 | if (rc != -EINPROGRESS) { |
| 764 | tls_err_abort(sk, EBADMSG); |
| 765 | if (split) { |
| 766 | tls_ctx->pending_open_record_frags = true; |
| 767 | tls_merge_open_record(sk, rec, tmp, orig_end); |
| 768 | } |
| 769 | } |
| 770 | ctx->async_capable = 1; |
| 771 | return rc; |
| 772 | } else if (split) { |
| 773 | msg_pl = &tmp->msg_plaintext; |
| 774 | msg_en = &tmp->msg_encrypted; |
| 775 | sk_msg_trim(sk, msg_en, msg_pl->sg.size + prot->overhead_size); |
| 776 | tls_ctx->pending_open_record_frags = true; |
| 777 | ctx->open_rec = tmp; |
| 778 | } |
| 779 | |
| 780 | return tls_tx_records(sk, flags); |
| 781 | } |
| 782 | |
| 783 | static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk, |
| 784 | bool full_record, u8 record_type, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 785 | ssize_t *copied, int flags) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 786 | { |
| 787 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 788 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 789 | struct sk_msg msg_redir = { }; |
| 790 | struct sk_psock *psock; |
| 791 | struct sock *sk_redir; |
| 792 | struct tls_rec *rec; |
| 793 | bool enospc, policy; |
| 794 | int err = 0, send; |
| 795 | u32 delta = 0; |
| 796 | |
| 797 | policy = !(flags & MSG_SENDPAGE_NOPOLICY); |
| 798 | psock = sk_psock_get(sk); |
| 799 | if (!psock || !policy) { |
| 800 | err = tls_push_record(sk, flags, record_type); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 801 | if (err && sk->sk_err == EBADMSG) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 802 | *copied -= sk_msg_free(sk, msg); |
| 803 | tls_free_open_rec(sk); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 804 | err = -sk->sk_err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 805 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 806 | if (psock) |
| 807 | sk_psock_put(sk, psock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 808 | return err; |
| 809 | } |
| 810 | more_data: |
| 811 | enospc = sk_msg_full(msg); |
| 812 | if (psock->eval == __SK_NONE) { |
| 813 | delta = msg->sg.size; |
| 814 | psock->eval = sk_psock_msg_verdict(sk, psock, msg); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 815 | delta -= msg->sg.size; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 816 | } |
| 817 | if (msg->cork_bytes && msg->cork_bytes > msg->sg.size && |
| 818 | !enospc && !full_record) { |
| 819 | err = -ENOSPC; |
| 820 | goto out_err; |
| 821 | } |
| 822 | msg->cork_bytes = 0; |
| 823 | send = msg->sg.size; |
| 824 | if (msg->apply_bytes && msg->apply_bytes < send) |
| 825 | send = msg->apply_bytes; |
| 826 | |
| 827 | switch (psock->eval) { |
| 828 | case __SK_PASS: |
| 829 | err = tls_push_record(sk, flags, record_type); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 830 | if (err && sk->sk_err == EBADMSG) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 831 | *copied -= sk_msg_free(sk, msg); |
| 832 | tls_free_open_rec(sk); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 833 | err = -sk->sk_err; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 834 | goto out_err; |
| 835 | } |
| 836 | break; |
| 837 | case __SK_REDIRECT: |
| 838 | sk_redir = psock->sk_redir; |
| 839 | memcpy(&msg_redir, msg, sizeof(*msg)); |
| 840 | if (msg->apply_bytes < send) |
| 841 | msg->apply_bytes = 0; |
| 842 | else |
| 843 | msg->apply_bytes -= send; |
| 844 | sk_msg_return_zero(sk, msg, send); |
| 845 | msg->sg.size -= send; |
| 846 | release_sock(sk); |
| 847 | err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags); |
| 848 | lock_sock(sk); |
| 849 | if (err < 0) { |
| 850 | *copied -= sk_msg_free_nocharge(sk, &msg_redir); |
| 851 | msg->sg.size = 0; |
| 852 | } |
| 853 | if (msg->sg.size == 0) |
| 854 | tls_free_open_rec(sk); |
| 855 | break; |
| 856 | case __SK_DROP: |
| 857 | default: |
| 858 | sk_msg_free_partial(sk, msg, send); |
| 859 | if (msg->apply_bytes < send) |
| 860 | msg->apply_bytes = 0; |
| 861 | else |
| 862 | msg->apply_bytes -= send; |
| 863 | if (msg->sg.size == 0) |
| 864 | tls_free_open_rec(sk); |
| 865 | *copied -= (send + delta); |
| 866 | err = -EACCES; |
| 867 | } |
| 868 | |
| 869 | if (likely(!err)) { |
| 870 | bool reset_eval = !ctx->open_rec; |
| 871 | |
| 872 | rec = ctx->open_rec; |
| 873 | if (rec) { |
| 874 | msg = &rec->msg_plaintext; |
| 875 | if (!msg->apply_bytes) |
| 876 | reset_eval = true; |
| 877 | } |
| 878 | if (reset_eval) { |
| 879 | psock->eval = __SK_NONE; |
| 880 | if (psock->sk_redir) { |
| 881 | sock_put(psock->sk_redir); |
| 882 | psock->sk_redir = NULL; |
| 883 | } |
| 884 | } |
| 885 | if (rec) |
| 886 | goto more_data; |
| 887 | } |
| 888 | out_err: |
| 889 | sk_psock_put(sk, psock); |
| 890 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | static int tls_sw_push_pending_record(struct sock *sk, int flags) |
| 894 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 895 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 896 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 897 | struct tls_rec *rec = ctx->open_rec; |
| 898 | struct sk_msg *msg_pl; |
| 899 | size_t copied; |
| 900 | |
| 901 | if (!rec) |
| 902 | return 0; |
| 903 | |
| 904 | msg_pl = &rec->msg_plaintext; |
| 905 | copied = msg_pl->sg.size; |
| 906 | if (!copied) |
| 907 | return 0; |
| 908 | |
| 909 | return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA, |
| 910 | &copied, flags); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 911 | } |
| 912 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 913 | int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 914 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 915 | long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
| 916 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 917 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 918 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 919 | bool async_capable = ctx->async_capable; |
| 920 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
| 921 | bool is_kvec = iov_iter_is_kvec(&msg->msg_iter); |
| 922 | bool eor = !(msg->msg_flags & MSG_MORE); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 923 | size_t try_to_copy; |
| 924 | ssize_t copied = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 925 | struct sk_msg *msg_pl, *msg_en; |
| 926 | struct tls_rec *rec; |
| 927 | int required_size; |
| 928 | int num_async = 0; |
| 929 | bool full_record; |
| 930 | int record_room; |
| 931 | int num_zc = 0; |
| 932 | int orig_size; |
| 933 | int ret = 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 934 | int pending; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 935 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 936 | if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 937 | return -EOPNOTSUPP; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 938 | |
| 939 | mutex_lock(&tls_ctx->tx_lock); |
| 940 | lock_sock(sk); |
| 941 | |
| 942 | if (unlikely(msg->msg_controllen)) { |
| 943 | ret = tls_proccess_cmsg(sk, msg, &record_type); |
| 944 | if (ret) { |
| 945 | if (ret == -EINPROGRESS) |
| 946 | num_async++; |
| 947 | else if (ret != -EAGAIN) |
| 948 | goto send_end; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | while (msg_data_left(msg)) { |
| 953 | if (sk->sk_err) { |
| 954 | ret = -sk->sk_err; |
| 955 | goto send_end; |
| 956 | } |
| 957 | |
| 958 | if (ctx->open_rec) |
| 959 | rec = ctx->open_rec; |
| 960 | else |
| 961 | rec = ctx->open_rec = tls_get_rec(sk); |
| 962 | if (!rec) { |
| 963 | ret = -ENOMEM; |
| 964 | goto send_end; |
| 965 | } |
| 966 | |
| 967 | msg_pl = &rec->msg_plaintext; |
| 968 | msg_en = &rec->msg_encrypted; |
| 969 | |
| 970 | orig_size = msg_pl->sg.size; |
| 971 | full_record = false; |
| 972 | try_to_copy = msg_data_left(msg); |
| 973 | record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size; |
| 974 | if (try_to_copy >= record_room) { |
| 975 | try_to_copy = record_room; |
| 976 | full_record = true; |
| 977 | } |
| 978 | |
| 979 | required_size = msg_pl->sg.size + try_to_copy + |
| 980 | prot->overhead_size; |
| 981 | |
| 982 | if (!sk_stream_memory_free(sk)) |
| 983 | goto wait_for_sndbuf; |
| 984 | |
| 985 | alloc_encrypted: |
| 986 | ret = tls_alloc_encrypted_msg(sk, required_size); |
| 987 | if (ret) { |
| 988 | if (ret != -ENOSPC) |
| 989 | goto wait_for_memory; |
| 990 | |
| 991 | /* Adjust try_to_copy according to the amount that was |
| 992 | * actually allocated. The difference is due |
| 993 | * to max sg elements limit |
| 994 | */ |
| 995 | try_to_copy -= required_size - msg_en->sg.size; |
| 996 | full_record = true; |
| 997 | } |
| 998 | |
| 999 | if (!is_kvec && (full_record || eor) && !async_capable) { |
| 1000 | u32 first = msg_pl->sg.end; |
| 1001 | |
| 1002 | ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter, |
| 1003 | msg_pl, try_to_copy); |
| 1004 | if (ret) |
| 1005 | goto fallback_to_reg_send; |
| 1006 | |
| 1007 | num_zc++; |
| 1008 | copied += try_to_copy; |
| 1009 | |
| 1010 | sk_msg_sg_copy_set(msg_pl, first); |
| 1011 | ret = bpf_exec_tx_verdict(msg_pl, sk, full_record, |
| 1012 | record_type, &copied, |
| 1013 | msg->msg_flags); |
| 1014 | if (ret) { |
| 1015 | if (ret == -EINPROGRESS) |
| 1016 | num_async++; |
| 1017 | else if (ret == -ENOMEM) |
| 1018 | goto wait_for_memory; |
| 1019 | else if (ctx->open_rec && ret == -ENOSPC) |
| 1020 | goto rollback_iter; |
| 1021 | else if (ret != -EAGAIN) |
| 1022 | goto send_end; |
| 1023 | } |
| 1024 | continue; |
| 1025 | rollback_iter: |
| 1026 | copied -= try_to_copy; |
| 1027 | sk_msg_sg_copy_clear(msg_pl, first); |
| 1028 | iov_iter_revert(&msg->msg_iter, |
| 1029 | msg_pl->sg.size - orig_size); |
| 1030 | fallback_to_reg_send: |
| 1031 | sk_msg_trim(sk, msg_pl, orig_size); |
| 1032 | } |
| 1033 | |
| 1034 | required_size = msg_pl->sg.size + try_to_copy; |
| 1035 | |
| 1036 | ret = tls_clone_plaintext_msg(sk, required_size); |
| 1037 | if (ret) { |
| 1038 | if (ret != -ENOSPC) |
| 1039 | goto send_end; |
| 1040 | |
| 1041 | /* Adjust try_to_copy according to the amount that was |
| 1042 | * actually allocated. The difference is due |
| 1043 | * to max sg elements limit |
| 1044 | */ |
| 1045 | try_to_copy -= required_size - msg_pl->sg.size; |
| 1046 | full_record = true; |
| 1047 | sk_msg_trim(sk, msg_en, |
| 1048 | msg_pl->sg.size + prot->overhead_size); |
| 1049 | } |
| 1050 | |
| 1051 | if (try_to_copy) { |
| 1052 | ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, |
| 1053 | msg_pl, try_to_copy); |
| 1054 | if (ret < 0) |
| 1055 | goto trim_sgl; |
| 1056 | } |
| 1057 | |
| 1058 | /* Open records defined only if successfully copied, otherwise |
| 1059 | * we would trim the sg but not reset the open record frags. |
| 1060 | */ |
| 1061 | tls_ctx->pending_open_record_frags = true; |
| 1062 | copied += try_to_copy; |
| 1063 | if (full_record || eor) { |
| 1064 | ret = bpf_exec_tx_verdict(msg_pl, sk, full_record, |
| 1065 | record_type, &copied, |
| 1066 | msg->msg_flags); |
| 1067 | if (ret) { |
| 1068 | if (ret == -EINPROGRESS) |
| 1069 | num_async++; |
| 1070 | else if (ret == -ENOMEM) |
| 1071 | goto wait_for_memory; |
| 1072 | else if (ret != -EAGAIN) { |
| 1073 | if (ret == -ENOSPC) |
| 1074 | ret = 0; |
| 1075 | goto send_end; |
| 1076 | } |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | continue; |
| 1081 | |
| 1082 | wait_for_sndbuf: |
| 1083 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1084 | wait_for_memory: |
| 1085 | ret = sk_stream_wait_memory(sk, &timeo); |
| 1086 | if (ret) { |
| 1087 | trim_sgl: |
| 1088 | if (ctx->open_rec) |
| 1089 | tls_trim_both_msgs(sk, orig_size); |
| 1090 | goto send_end; |
| 1091 | } |
| 1092 | |
| 1093 | if (ctx->open_rec && msg_en->sg.size < required_size) |
| 1094 | goto alloc_encrypted; |
| 1095 | } |
| 1096 | |
| 1097 | if (!num_async) { |
| 1098 | goto send_end; |
| 1099 | } else if (num_zc) { |
| 1100 | /* Wait for pending encryptions to get completed */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1101 | spin_lock_bh(&ctx->encrypt_compl_lock); |
| 1102 | ctx->async_notify = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1103 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1104 | pending = atomic_read(&ctx->encrypt_pending); |
| 1105 | spin_unlock_bh(&ctx->encrypt_compl_lock); |
| 1106 | if (pending) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1107 | crypto_wait_req(-EINPROGRESS, &ctx->async_wait); |
| 1108 | else |
| 1109 | reinit_completion(&ctx->async_wait.completion); |
| 1110 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1111 | /* There can be no concurrent accesses, since we have no |
| 1112 | * pending encrypt operations |
| 1113 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1114 | WRITE_ONCE(ctx->async_notify, false); |
| 1115 | |
| 1116 | if (ctx->async_wait.err) { |
| 1117 | ret = ctx->async_wait.err; |
| 1118 | copied = 0; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* Transmit if any encryptions have completed */ |
| 1123 | if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) { |
| 1124 | cancel_delayed_work(&ctx->tx_work.work); |
| 1125 | tls_tx_records(sk, msg->msg_flags); |
| 1126 | } |
| 1127 | |
| 1128 | send_end: |
| 1129 | ret = sk_stream_error(sk, msg->msg_flags, ret); |
| 1130 | |
| 1131 | release_sock(sk); |
| 1132 | mutex_unlock(&tls_ctx->tx_lock); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1133 | return copied > 0 ? copied : ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | static int tls_sw_do_sendpage(struct sock *sk, struct page *page, |
| 1137 | int offset, size_t size, int flags) |
| 1138 | { |
| 1139 | long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
| 1140 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1141 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 1142 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 1143 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
| 1144 | struct sk_msg *msg_pl; |
| 1145 | struct tls_rec *rec; |
| 1146 | int num_async = 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1147 | ssize_t copied = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1148 | bool full_record; |
| 1149 | int record_room; |
| 1150 | int ret = 0; |
| 1151 | bool eor; |
| 1152 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1153 | eor = !(flags & MSG_SENDPAGE_NOTLAST); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1154 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 1155 | |
| 1156 | /* Call the sk_stream functions to manage the sndbuf mem. */ |
| 1157 | while (size > 0) { |
| 1158 | size_t copy, required_size; |
| 1159 | |
| 1160 | if (sk->sk_err) { |
| 1161 | ret = -sk->sk_err; |
| 1162 | goto sendpage_end; |
| 1163 | } |
| 1164 | |
| 1165 | if (ctx->open_rec) |
| 1166 | rec = ctx->open_rec; |
| 1167 | else |
| 1168 | rec = ctx->open_rec = tls_get_rec(sk); |
| 1169 | if (!rec) { |
| 1170 | ret = -ENOMEM; |
| 1171 | goto sendpage_end; |
| 1172 | } |
| 1173 | |
| 1174 | msg_pl = &rec->msg_plaintext; |
| 1175 | |
| 1176 | full_record = false; |
| 1177 | record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size; |
| 1178 | copy = size; |
| 1179 | if (copy >= record_room) { |
| 1180 | copy = record_room; |
| 1181 | full_record = true; |
| 1182 | } |
| 1183 | |
| 1184 | required_size = msg_pl->sg.size + copy + prot->overhead_size; |
| 1185 | |
| 1186 | if (!sk_stream_memory_free(sk)) |
| 1187 | goto wait_for_sndbuf; |
| 1188 | alloc_payload: |
| 1189 | ret = tls_alloc_encrypted_msg(sk, required_size); |
| 1190 | if (ret) { |
| 1191 | if (ret != -ENOSPC) |
| 1192 | goto wait_for_memory; |
| 1193 | |
| 1194 | /* Adjust copy according to the amount that was |
| 1195 | * actually allocated. The difference is due |
| 1196 | * to max sg elements limit |
| 1197 | */ |
| 1198 | copy -= required_size - msg_pl->sg.size; |
| 1199 | full_record = true; |
| 1200 | } |
| 1201 | |
| 1202 | sk_msg_page_add(msg_pl, page, copy, offset); |
| 1203 | sk_mem_charge(sk, copy); |
| 1204 | |
| 1205 | offset += copy; |
| 1206 | size -= copy; |
| 1207 | copied += copy; |
| 1208 | |
| 1209 | tls_ctx->pending_open_record_frags = true; |
| 1210 | if (full_record || eor || sk_msg_full(msg_pl)) { |
| 1211 | ret = bpf_exec_tx_verdict(msg_pl, sk, full_record, |
| 1212 | record_type, &copied, flags); |
| 1213 | if (ret) { |
| 1214 | if (ret == -EINPROGRESS) |
| 1215 | num_async++; |
| 1216 | else if (ret == -ENOMEM) |
| 1217 | goto wait_for_memory; |
| 1218 | else if (ret != -EAGAIN) { |
| 1219 | if (ret == -ENOSPC) |
| 1220 | ret = 0; |
| 1221 | goto sendpage_end; |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | continue; |
| 1226 | wait_for_sndbuf: |
| 1227 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1228 | wait_for_memory: |
| 1229 | ret = sk_stream_wait_memory(sk, &timeo); |
| 1230 | if (ret) { |
| 1231 | if (ctx->open_rec) |
| 1232 | tls_trim_both_msgs(sk, msg_pl->sg.size); |
| 1233 | goto sendpage_end; |
| 1234 | } |
| 1235 | |
| 1236 | if (ctx->open_rec) |
| 1237 | goto alloc_payload; |
| 1238 | } |
| 1239 | |
| 1240 | if (num_async) { |
| 1241 | /* Transmit if any encryptions have completed */ |
| 1242 | if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) { |
| 1243 | cancel_delayed_work(&ctx->tx_work.work); |
| 1244 | tls_tx_records(sk, flags); |
| 1245 | } |
| 1246 | } |
| 1247 | sendpage_end: |
| 1248 | ret = sk_stream_error(sk, flags, ret); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1249 | return copied > 0 ? copied : ret; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
| 1252 | int tls_sw_sendpage_locked(struct sock *sk, struct page *page, |
| 1253 | int offset, size_t size, int flags) |
| 1254 | { |
| 1255 | if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | |
| 1256 | MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY | |
| 1257 | MSG_NO_SHARED_FRAGS)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1258 | return -EOPNOTSUPP; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1259 | |
| 1260 | return tls_sw_do_sendpage(sk, page, offset, size, flags); |
| 1261 | } |
| 1262 | |
| 1263 | int tls_sw_sendpage(struct sock *sk, struct page *page, |
| 1264 | int offset, size_t size, int flags) |
| 1265 | { |
| 1266 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1267 | int ret; |
| 1268 | |
| 1269 | if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | |
| 1270 | MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY)) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1271 | return -EOPNOTSUPP; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1272 | |
| 1273 | mutex_lock(&tls_ctx->tx_lock); |
| 1274 | lock_sock(sk); |
| 1275 | ret = tls_sw_do_sendpage(sk, page, offset, size, flags); |
| 1276 | release_sock(sk); |
| 1277 | mutex_unlock(&tls_ctx->tx_lock); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | |
| 1281 | static struct sk_buff *tls_wait_data(struct sock *sk, struct sk_psock *psock, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1282 | bool nonblock, long timeo, int *err) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1283 | { |
| 1284 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1285 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1286 | struct sk_buff *skb; |
| 1287 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 1288 | |
| 1289 | while (!(skb = ctx->recv_pkt) && sk_psock_queue_empty(psock)) { |
| 1290 | if (sk->sk_err) { |
| 1291 | *err = sock_error(sk); |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1295 | if (!skb_queue_empty(&sk->sk_receive_queue)) { |
| 1296 | __strp_unpause(&ctx->strp); |
| 1297 | if (ctx->recv_pkt) |
| 1298 | return ctx->recv_pkt; |
| 1299 | } |
| 1300 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1301 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1302 | return NULL; |
| 1303 | |
| 1304 | if (sock_flag(sk, SOCK_DONE)) |
| 1305 | return NULL; |
| 1306 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1307 | if (nonblock || !timeo) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1308 | *err = -EAGAIN; |
| 1309 | return NULL; |
| 1310 | } |
| 1311 | |
| 1312 | add_wait_queue(sk_sleep(sk), &wait); |
| 1313 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 1314 | sk_wait_event(sk, &timeo, |
| 1315 | ctx->recv_pkt != skb || |
| 1316 | !sk_psock_queue_empty(psock), |
| 1317 | &wait); |
| 1318 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 1319 | remove_wait_queue(sk_sleep(sk), &wait); |
| 1320 | |
| 1321 | /* Handle signals */ |
| 1322 | if (signal_pending(current)) { |
| 1323 | *err = sock_intr_errno(timeo); |
| 1324 | return NULL; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | return skb; |
| 1329 | } |
| 1330 | |
| 1331 | static int tls_setup_from_iter(struct sock *sk, struct iov_iter *from, |
| 1332 | int length, int *pages_used, |
| 1333 | unsigned int *size_used, |
| 1334 | struct scatterlist *to, |
| 1335 | int to_max_pages) |
| 1336 | { |
| 1337 | int rc = 0, i = 0, num_elem = *pages_used, maxpages; |
| 1338 | struct page *pages[MAX_SKB_FRAGS]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1339 | unsigned int size = *size_used; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1340 | ssize_t copied, use; |
| 1341 | size_t offset; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1342 | |
| 1343 | while (length > 0) { |
| 1344 | i = 0; |
| 1345 | maxpages = to_max_pages - num_elem; |
| 1346 | if (maxpages == 0) { |
| 1347 | rc = -EFAULT; |
| 1348 | goto out; |
| 1349 | } |
| 1350 | copied = iov_iter_get_pages(from, pages, |
| 1351 | length, |
| 1352 | maxpages, &offset); |
| 1353 | if (copied <= 0) { |
| 1354 | rc = -EFAULT; |
| 1355 | goto out; |
| 1356 | } |
| 1357 | |
| 1358 | iov_iter_advance(from, copied); |
| 1359 | |
| 1360 | length -= copied; |
| 1361 | size += copied; |
| 1362 | while (copied) { |
| 1363 | use = min_t(int, copied, PAGE_SIZE - offset); |
| 1364 | |
| 1365 | sg_set_page(&to[num_elem], |
| 1366 | pages[i], use, offset); |
| 1367 | sg_unmark_end(&to[num_elem]); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1368 | /* We do not uncharge memory from this API */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1369 | |
| 1370 | offset = 0; |
| 1371 | copied -= use; |
| 1372 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1373 | i++; |
| 1374 | num_elem++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1375 | } |
| 1376 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1377 | /* Mark the end in the last sg entry if newly added */ |
| 1378 | if (num_elem > *pages_used) |
| 1379 | sg_mark_end(&to[num_elem - 1]); |
| 1380 | out: |
| 1381 | if (rc) |
| 1382 | iov_iter_revert(from, size - *size_used); |
| 1383 | *size_used = size; |
| 1384 | *pages_used = num_elem; |
| 1385 | |
| 1386 | return rc; |
| 1387 | } |
| 1388 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1389 | /* This function decrypts the input skb into either out_iov or in out_sg |
| 1390 | * or in skb buffers itself. The input parameter 'zc' indicates if |
| 1391 | * zero-copy mode needs to be tried or not. With zero-copy mode, either |
| 1392 | * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are |
| 1393 | * NULL, then the decryption happens inside skb buffers itself, i.e. |
| 1394 | * zero-copy gets disabled and 'zc' is updated. |
| 1395 | */ |
| 1396 | |
| 1397 | static int decrypt_internal(struct sock *sk, struct sk_buff *skb, |
| 1398 | struct iov_iter *out_iov, |
| 1399 | struct scatterlist *out_sg, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1400 | int *chunk, bool *zc, bool async) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1401 | { |
| 1402 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1403 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1404 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1405 | struct strp_msg *rxm = strp_msg(skb); |
| 1406 | int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0; |
| 1407 | struct aead_request *aead_req; |
| 1408 | struct sk_buff *unused; |
| 1409 | u8 *aad, *iv, *mem = NULL; |
| 1410 | struct scatterlist *sgin = NULL; |
| 1411 | struct scatterlist *sgout = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1412 | const int data_len = rxm->full_len - prot->overhead_size + |
| 1413 | prot->tail_size; |
| 1414 | int iv_offset = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1415 | |
| 1416 | if (*zc && (out_iov || out_sg)) { |
| 1417 | if (out_iov) |
| 1418 | n_sgout = iov_iter_npages(out_iov, INT_MAX) + 1; |
| 1419 | else |
| 1420 | n_sgout = sg_nents(out_sg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1421 | n_sgin = skb_nsg(skb, rxm->offset + prot->prepend_size, |
| 1422 | rxm->full_len - prot->prepend_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1423 | } else { |
| 1424 | n_sgout = 0; |
| 1425 | *zc = false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1426 | n_sgin = skb_cow_data(skb, 0, &unused); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1429 | if (n_sgin < 1) |
| 1430 | return -EBADMSG; |
| 1431 | |
| 1432 | /* Increment to accommodate AAD */ |
| 1433 | n_sgin = n_sgin + 1; |
| 1434 | |
| 1435 | nsg = n_sgin + n_sgout; |
| 1436 | |
| 1437 | aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv); |
| 1438 | mem_size = aead_size + (nsg * sizeof(struct scatterlist)); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1439 | mem_size = mem_size + prot->aad_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1440 | mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv); |
| 1441 | |
| 1442 | /* Allocate a single block of memory which contains |
| 1443 | * aead_req || sgin[] || sgout[] || aad || iv. |
| 1444 | * This order achieves correct alignment for aead_req, sgin, sgout. |
| 1445 | */ |
| 1446 | mem = kmalloc(mem_size, sk->sk_allocation); |
| 1447 | if (!mem) |
| 1448 | return -ENOMEM; |
| 1449 | |
| 1450 | /* Segment the allocated memory */ |
| 1451 | aead_req = (struct aead_request *)mem; |
| 1452 | sgin = (struct scatterlist *)(mem + aead_size); |
| 1453 | sgout = sgin + n_sgin; |
| 1454 | aad = (u8 *)(sgout + n_sgout); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1455 | iv = aad + prot->aad_size; |
| 1456 | |
| 1457 | /* For CCM based ciphers, first byte of nonce+iv is always '2' */ |
| 1458 | if (prot->cipher_type == TLS_CIPHER_AES_CCM_128) { |
| 1459 | iv[0] = 2; |
| 1460 | iv_offset = 1; |
| 1461 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1462 | |
| 1463 | /* Prepare IV */ |
| 1464 | err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1465 | iv + iv_offset + prot->salt_size, |
| 1466 | prot->iv_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1467 | if (err < 0) { |
| 1468 | kfree(mem); |
| 1469 | return err; |
| 1470 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1471 | if (prot->version == TLS_1_3_VERSION) |
| 1472 | memcpy(iv + iv_offset, tls_ctx->rx.iv, |
| 1473 | crypto_aead_ivsize(ctx->aead_recv)); |
| 1474 | else |
| 1475 | memcpy(iv + iv_offset, tls_ctx->rx.iv, prot->salt_size); |
| 1476 | |
| 1477 | xor_iv_with_seq(prot->version, iv, tls_ctx->rx.rec_seq); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1478 | |
| 1479 | /* Prepare AAD */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1480 | tls_make_aad(aad, rxm->full_len - prot->overhead_size + |
| 1481 | prot->tail_size, |
| 1482 | tls_ctx->rx.rec_seq, prot->rec_seq_size, |
| 1483 | ctx->control, prot->version); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1484 | |
| 1485 | /* Prepare sgin */ |
| 1486 | sg_init_table(sgin, n_sgin); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1487 | sg_set_buf(&sgin[0], aad, prot->aad_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1488 | err = skb_to_sgvec(skb, &sgin[1], |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1489 | rxm->offset + prot->prepend_size, |
| 1490 | rxm->full_len - prot->prepend_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1491 | if (err < 0) { |
| 1492 | kfree(mem); |
| 1493 | return err; |
| 1494 | } |
| 1495 | |
| 1496 | if (n_sgout) { |
| 1497 | if (out_iov) { |
| 1498 | sg_init_table(sgout, n_sgout); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1499 | sg_set_buf(&sgout[0], aad, prot->aad_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1500 | |
| 1501 | *chunk = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1502 | err = tls_setup_from_iter(sk, out_iov, data_len, |
| 1503 | &pages, chunk, &sgout[1], |
| 1504 | (n_sgout - 1)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1505 | if (err < 0) |
| 1506 | goto fallback_to_reg_recv; |
| 1507 | } else if (out_sg) { |
| 1508 | memcpy(sgout, out_sg, n_sgout * sizeof(*sgout)); |
| 1509 | } else { |
| 1510 | goto fallback_to_reg_recv; |
| 1511 | } |
| 1512 | } else { |
| 1513 | fallback_to_reg_recv: |
| 1514 | sgout = sgin; |
| 1515 | pages = 0; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1516 | *chunk = data_len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1517 | *zc = false; |
| 1518 | } |
| 1519 | |
| 1520 | /* Prepare and submit AEAD request */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1521 | err = tls_do_decryption(sk, skb, sgin, sgout, iv, |
| 1522 | data_len, aead_req, async); |
| 1523 | if (err == -EINPROGRESS) |
| 1524 | return err; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1525 | |
| 1526 | /* Release the pages in case iov was mapped to pages */ |
| 1527 | for (; pages > 0; pages--) |
| 1528 | put_page(sg_page(&sgout[pages])); |
| 1529 | |
| 1530 | kfree(mem); |
| 1531 | return err; |
| 1532 | } |
| 1533 | |
| 1534 | static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1535 | struct iov_iter *dest, int *chunk, bool *zc, |
| 1536 | bool async) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1537 | { |
| 1538 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1539 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1540 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1541 | struct strp_msg *rxm = strp_msg(skb); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1542 | int pad, err = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1543 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1544 | if (!ctx->decrypted) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1545 | if (tls_ctx->rx_conf == TLS_HW) { |
| 1546 | err = tls_device_decrypted(sk, skb); |
| 1547 | if (err < 0) |
| 1548 | return err; |
| 1549 | } |
| 1550 | |
| 1551 | /* Still not decrypted after tls_device */ |
| 1552 | if (!ctx->decrypted) { |
| 1553 | err = decrypt_internal(sk, skb, dest, NULL, chunk, zc, |
| 1554 | async); |
| 1555 | if (err < 0) { |
| 1556 | if (err == -EINPROGRESS) |
| 1557 | tls_advance_record_sn(sk, prot, |
| 1558 | &tls_ctx->rx); |
| 1559 | |
| 1560 | return err; |
| 1561 | } |
| 1562 | } else { |
| 1563 | *zc = false; |
| 1564 | } |
| 1565 | |
| 1566 | pad = padding_length(ctx, prot, skb); |
| 1567 | if (pad < 0) |
| 1568 | return pad; |
| 1569 | |
| 1570 | rxm->full_len -= pad; |
| 1571 | rxm->offset += prot->prepend_size; |
| 1572 | rxm->full_len -= prot->overhead_size; |
| 1573 | tls_advance_record_sn(sk, prot, &tls_ctx->rx); |
| 1574 | ctx->decrypted = true; |
| 1575 | ctx->saved_data_ready(sk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1576 | } else { |
| 1577 | *zc = false; |
| 1578 | } |
| 1579 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1580 | return err; |
| 1581 | } |
| 1582 | |
| 1583 | int decrypt_skb(struct sock *sk, struct sk_buff *skb, |
| 1584 | struct scatterlist *sgout) |
| 1585 | { |
| 1586 | bool zc = true; |
| 1587 | int chunk; |
| 1588 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1589 | return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc, false); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb, |
| 1593 | unsigned int len) |
| 1594 | { |
| 1595 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1596 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1597 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1598 | if (skb) { |
| 1599 | struct strp_msg *rxm = strp_msg(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1600 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1601 | if (len < rxm->full_len) { |
| 1602 | rxm->offset += len; |
| 1603 | rxm->full_len -= len; |
| 1604 | return false; |
| 1605 | } |
| 1606 | consume_skb(skb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | /* Finished with message */ |
| 1610 | ctx->recv_pkt = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1611 | __strp_unpause(&ctx->strp); |
| 1612 | |
| 1613 | return true; |
| 1614 | } |
| 1615 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1616 | /* This function traverses the rx_list in tls receive context to copies the |
| 1617 | * decrypted records into the buffer provided by caller zero copy is not |
| 1618 | * true. Further, the records are removed from the rx_list if it is not a peek |
| 1619 | * case and the record has been consumed completely. |
| 1620 | */ |
| 1621 | static int process_rx_list(struct tls_sw_context_rx *ctx, |
| 1622 | struct msghdr *msg, |
| 1623 | u8 *control, |
| 1624 | bool *cmsg, |
| 1625 | size_t skip, |
| 1626 | size_t len, |
| 1627 | bool zc, |
| 1628 | bool is_peek) |
| 1629 | { |
| 1630 | struct sk_buff *skb = skb_peek(&ctx->rx_list); |
| 1631 | u8 ctrl = *control; |
| 1632 | u8 msgc = *cmsg; |
| 1633 | struct tls_msg *tlm; |
| 1634 | ssize_t copied = 0; |
| 1635 | |
| 1636 | /* Set the record type in 'control' if caller didn't pass it */ |
| 1637 | if (!ctrl && skb) { |
| 1638 | tlm = tls_msg(skb); |
| 1639 | ctrl = tlm->control; |
| 1640 | } |
| 1641 | |
| 1642 | while (skip && skb) { |
| 1643 | struct strp_msg *rxm = strp_msg(skb); |
| 1644 | tlm = tls_msg(skb); |
| 1645 | |
| 1646 | /* Cannot process a record of different type */ |
| 1647 | if (ctrl != tlm->control) |
| 1648 | return 0; |
| 1649 | |
| 1650 | if (skip < rxm->full_len) |
| 1651 | break; |
| 1652 | |
| 1653 | skip = skip - rxm->full_len; |
| 1654 | skb = skb_peek_next(skb, &ctx->rx_list); |
| 1655 | } |
| 1656 | |
| 1657 | while (len && skb) { |
| 1658 | struct sk_buff *next_skb; |
| 1659 | struct strp_msg *rxm = strp_msg(skb); |
| 1660 | int chunk = min_t(unsigned int, rxm->full_len - skip, len); |
| 1661 | |
| 1662 | tlm = tls_msg(skb); |
| 1663 | |
| 1664 | /* Cannot process a record of different type */ |
| 1665 | if (ctrl != tlm->control) |
| 1666 | return 0; |
| 1667 | |
| 1668 | /* Set record type if not already done. For a non-data record, |
| 1669 | * do not proceed if record type could not be copied. |
| 1670 | */ |
| 1671 | if (!msgc) { |
| 1672 | int cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, |
| 1673 | sizeof(ctrl), &ctrl); |
| 1674 | msgc = true; |
| 1675 | if (ctrl != TLS_RECORD_TYPE_DATA) { |
| 1676 | if (cerr || msg->msg_flags & MSG_CTRUNC) |
| 1677 | return -EIO; |
| 1678 | |
| 1679 | *cmsg = msgc; |
| 1680 | } |
| 1681 | } |
| 1682 | |
| 1683 | if (!zc || (rxm->full_len - skip) > len) { |
| 1684 | int err = skb_copy_datagram_msg(skb, rxm->offset + skip, |
| 1685 | msg, chunk); |
| 1686 | if (err < 0) |
| 1687 | return err; |
| 1688 | } |
| 1689 | |
| 1690 | len = len - chunk; |
| 1691 | copied = copied + chunk; |
| 1692 | |
| 1693 | /* Consume the data from record if it is non-peek case*/ |
| 1694 | if (!is_peek) { |
| 1695 | rxm->offset = rxm->offset + chunk; |
| 1696 | rxm->full_len = rxm->full_len - chunk; |
| 1697 | |
| 1698 | /* Return if there is unconsumed data in the record */ |
| 1699 | if (rxm->full_len - skip) |
| 1700 | break; |
| 1701 | } |
| 1702 | |
| 1703 | /* The remaining skip-bytes must lie in 1st record in rx_list. |
| 1704 | * So from the 2nd record, 'skip' should be 0. |
| 1705 | */ |
| 1706 | skip = 0; |
| 1707 | |
| 1708 | if (msg) |
| 1709 | msg->msg_flags |= MSG_EOR; |
| 1710 | |
| 1711 | next_skb = skb_peek_next(skb, &ctx->rx_list); |
| 1712 | |
| 1713 | if (!is_peek) { |
| 1714 | skb_unlink(skb, &ctx->rx_list); |
| 1715 | consume_skb(skb); |
| 1716 | } |
| 1717 | |
| 1718 | skb = next_skb; |
| 1719 | } |
| 1720 | |
| 1721 | *control = ctrl; |
| 1722 | return copied; |
| 1723 | } |
| 1724 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1725 | int tls_sw_recvmsg(struct sock *sk, |
| 1726 | struct msghdr *msg, |
| 1727 | size_t len, |
| 1728 | int nonblock, |
| 1729 | int flags, |
| 1730 | int *addr_len) |
| 1731 | { |
| 1732 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1733 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1734 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
| 1735 | struct sk_psock *psock; |
| 1736 | unsigned char control = 0; |
| 1737 | ssize_t decrypted = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1738 | struct strp_msg *rxm; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1739 | struct tls_msg *tlm; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1740 | struct sk_buff *skb; |
| 1741 | ssize_t copied = 0; |
| 1742 | bool cmsg = false; |
| 1743 | int target, err = 0; |
| 1744 | long timeo; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1745 | bool is_kvec = iov_iter_is_kvec(&msg->msg_iter); |
| 1746 | bool is_peek = flags & MSG_PEEK; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1747 | bool bpf_strp_enabled; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1748 | int num_async = 0; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1749 | int pending; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1750 | |
| 1751 | flags |= nonblock; |
| 1752 | |
| 1753 | if (unlikely(flags & MSG_ERRQUEUE)) |
| 1754 | return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR); |
| 1755 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1756 | psock = sk_psock_get(sk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1757 | lock_sock(sk); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1758 | bpf_strp_enabled = sk_psock_strp_enabled(psock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1759 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1760 | /* Process pending decrypted records. It must be non-zero-copy */ |
| 1761 | err = process_rx_list(ctx, msg, &control, &cmsg, 0, len, false, |
| 1762 | is_peek); |
| 1763 | if (err < 0) { |
| 1764 | tls_err_abort(sk, err); |
| 1765 | goto end; |
| 1766 | } else { |
| 1767 | copied = err; |
| 1768 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1769 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1770 | if (len <= copied) |
| 1771 | goto recv_end; |
| 1772 | |
| 1773 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
| 1774 | len = len - copied; |
| 1775 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 1776 | |
| 1777 | while (len && (decrypted + copied < target || ctx->recv_pkt)) { |
| 1778 | bool retain_skb = false; |
| 1779 | bool zc = false; |
| 1780 | int to_decrypt; |
| 1781 | int chunk = 0; |
| 1782 | bool async_capable; |
| 1783 | bool async = false; |
| 1784 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1785 | skb = tls_wait_data(sk, psock, flags & MSG_DONTWAIT, timeo, &err); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1786 | if (!skb) { |
| 1787 | if (psock) { |
| 1788 | int ret = __tcp_bpf_recvmsg(sk, psock, |
| 1789 | msg, len, flags); |
| 1790 | |
| 1791 | if (ret > 0) { |
| 1792 | decrypted += ret; |
| 1793 | len -= ret; |
| 1794 | continue; |
| 1795 | } |
| 1796 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1797 | goto recv_end; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1798 | } else { |
| 1799 | tlm = tls_msg(skb); |
| 1800 | if (prot->version == TLS_1_3_VERSION) |
| 1801 | tlm->control = 0; |
| 1802 | else |
| 1803 | tlm->control = ctx->control; |
| 1804 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1805 | |
| 1806 | rxm = strp_msg(skb); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1807 | |
| 1808 | to_decrypt = rxm->full_len - prot->overhead_size; |
| 1809 | |
| 1810 | if (to_decrypt <= len && !is_kvec && !is_peek && |
| 1811 | ctx->control == TLS_RECORD_TYPE_DATA && |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1812 | prot->version != TLS_1_3_VERSION && |
| 1813 | !bpf_strp_enabled) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1814 | zc = true; |
| 1815 | |
| 1816 | /* Do not use async mode if record is non-data */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1817 | if (ctx->control == TLS_RECORD_TYPE_DATA && !bpf_strp_enabled) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1818 | async_capable = ctx->async_capable; |
| 1819 | else |
| 1820 | async_capable = false; |
| 1821 | |
| 1822 | err = decrypt_skb_update(sk, skb, &msg->msg_iter, |
| 1823 | &chunk, &zc, async_capable); |
| 1824 | if (err < 0 && err != -EINPROGRESS) { |
| 1825 | tls_err_abort(sk, EBADMSG); |
| 1826 | goto recv_end; |
| 1827 | } |
| 1828 | |
| 1829 | if (err == -EINPROGRESS) { |
| 1830 | async = true; |
| 1831 | num_async++; |
| 1832 | } else if (prot->version == TLS_1_3_VERSION) { |
| 1833 | tlm->control = ctx->control; |
| 1834 | } |
| 1835 | |
| 1836 | /* If the type of records being processed is not known yet, |
| 1837 | * set it to record type just dequeued. If it is already known, |
| 1838 | * but does not match the record type just dequeued, go to end. |
| 1839 | * We always get record type here since for tls1.2, record type |
| 1840 | * is known just after record is dequeued from stream parser. |
| 1841 | * For tls1.3, we disable async. |
| 1842 | */ |
| 1843 | |
| 1844 | if (!control) |
| 1845 | control = tlm->control; |
| 1846 | else if (control != tlm->control) |
| 1847 | goto recv_end; |
| 1848 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1849 | if (!cmsg) { |
| 1850 | int cerr; |
| 1851 | |
| 1852 | cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1853 | sizeof(control), &control); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1854 | cmsg = true; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1855 | if (control != TLS_RECORD_TYPE_DATA) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1856 | if (cerr || msg->msg_flags & MSG_CTRUNC) { |
| 1857 | err = -EIO; |
| 1858 | goto recv_end; |
| 1859 | } |
| 1860 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1863 | if (async) |
| 1864 | goto pick_next_record; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1865 | |
| 1866 | if (!zc) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1867 | if (bpf_strp_enabled) { |
| 1868 | err = sk_psock_tls_strp_read(psock, skb); |
| 1869 | if (err != __SK_PASS) { |
| 1870 | rxm->offset = rxm->offset + rxm->full_len; |
| 1871 | rxm->full_len = 0; |
| 1872 | if (err == __SK_DROP) |
| 1873 | consume_skb(skb); |
| 1874 | ctx->recv_pkt = NULL; |
| 1875 | __strp_unpause(&ctx->strp); |
| 1876 | continue; |
| 1877 | } |
| 1878 | } |
| 1879 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1880 | if (rxm->full_len > len) { |
| 1881 | retain_skb = true; |
| 1882 | chunk = len; |
| 1883 | } else { |
| 1884 | chunk = rxm->full_len; |
| 1885 | } |
| 1886 | |
| 1887 | err = skb_copy_datagram_msg(skb, rxm->offset, |
| 1888 | msg, chunk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1889 | if (err < 0) |
| 1890 | goto recv_end; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1891 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1892 | if (!is_peek) { |
| 1893 | rxm->offset = rxm->offset + chunk; |
| 1894 | rxm->full_len = rxm->full_len - chunk; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1895 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1898 | pick_next_record: |
| 1899 | if (chunk > len) |
| 1900 | chunk = len; |
| 1901 | |
| 1902 | decrypted += chunk; |
| 1903 | len -= chunk; |
| 1904 | |
| 1905 | /* For async or peek case, queue the current skb */ |
| 1906 | if (async || is_peek || retain_skb) { |
| 1907 | skb_queue_tail(&ctx->rx_list, skb); |
| 1908 | skb = NULL; |
| 1909 | } |
| 1910 | |
| 1911 | if (tls_sw_advance_skb(sk, skb, chunk)) { |
| 1912 | /* Return full control message to |
| 1913 | * userspace before trying to parse |
| 1914 | * another message type |
| 1915 | */ |
| 1916 | msg->msg_flags |= MSG_EOR; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1917 | if (control != TLS_RECORD_TYPE_DATA) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1918 | goto recv_end; |
| 1919 | } else { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1920 | break; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1921 | } |
| 1922 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1923 | |
| 1924 | recv_end: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1925 | if (num_async) { |
| 1926 | /* Wait for all previously submitted records to be decrypted */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1927 | spin_lock_bh(&ctx->decrypt_compl_lock); |
| 1928 | ctx->async_notify = true; |
| 1929 | pending = atomic_read(&ctx->decrypt_pending); |
| 1930 | spin_unlock_bh(&ctx->decrypt_compl_lock); |
| 1931 | if (pending) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1932 | err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait); |
| 1933 | if (err) { |
| 1934 | /* one of async decrypt failed */ |
| 1935 | tls_err_abort(sk, err); |
| 1936 | copied = 0; |
| 1937 | decrypted = 0; |
| 1938 | goto end; |
| 1939 | } |
| 1940 | } else { |
| 1941 | reinit_completion(&ctx->async_wait.completion); |
| 1942 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1943 | |
| 1944 | /* There can be no concurrent accesses, since we have no |
| 1945 | * pending decrypt operations |
| 1946 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1947 | WRITE_ONCE(ctx->async_notify, false); |
| 1948 | |
| 1949 | /* Drain records from the rx_list & copy if required */ |
| 1950 | if (is_peek || is_kvec) |
| 1951 | err = process_rx_list(ctx, msg, &control, &cmsg, copied, |
| 1952 | decrypted, false, is_peek); |
| 1953 | else |
| 1954 | err = process_rx_list(ctx, msg, &control, &cmsg, 0, |
| 1955 | decrypted, true, is_peek); |
| 1956 | if (err < 0) { |
| 1957 | tls_err_abort(sk, err); |
| 1958 | copied = 0; |
| 1959 | goto end; |
| 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | copied += decrypted; |
| 1964 | |
| 1965 | end: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1966 | release_sock(sk); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1967 | if (psock) |
| 1968 | sk_psock_put(sk, psock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1969 | return copied ? : err; |
| 1970 | } |
| 1971 | |
| 1972 | ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, |
| 1973 | struct pipe_inode_info *pipe, |
| 1974 | size_t len, unsigned int flags) |
| 1975 | { |
| 1976 | struct tls_context *tls_ctx = tls_get_ctx(sock->sk); |
| 1977 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1978 | struct strp_msg *rxm = NULL; |
| 1979 | struct sock *sk = sock->sk; |
| 1980 | struct sk_buff *skb; |
| 1981 | ssize_t copied = 0; |
| 1982 | int err = 0; |
| 1983 | long timeo; |
| 1984 | int chunk; |
| 1985 | bool zc = false; |
| 1986 | |
| 1987 | lock_sock(sk); |
| 1988 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1989 | timeo = sock_rcvtimeo(sk, flags & SPLICE_F_NONBLOCK); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1990 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 1991 | skb = tls_wait_data(sk, NULL, flags & SPLICE_F_NONBLOCK, timeo, &err); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1992 | if (!skb) |
| 1993 | goto splice_read_end; |
| 1994 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1995 | if (!ctx->decrypted) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1996 | err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc, false); |
| 1997 | |
| 1998 | /* splice does not support reading control messages */ |
| 1999 | if (ctx->control != TLS_RECORD_TYPE_DATA) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2000 | err = -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2001 | goto splice_read_end; |
| 2002 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2003 | |
| 2004 | if (err < 0) { |
| 2005 | tls_err_abort(sk, EBADMSG); |
| 2006 | goto splice_read_end; |
| 2007 | } |
| 2008 | ctx->decrypted = true; |
| 2009 | } |
| 2010 | rxm = strp_msg(skb); |
| 2011 | |
| 2012 | chunk = min_t(unsigned int, rxm->full_len, len); |
| 2013 | copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags); |
| 2014 | if (copied < 0) |
| 2015 | goto splice_read_end; |
| 2016 | |
| 2017 | if (likely(!(flags & MSG_PEEK))) |
| 2018 | tls_sw_advance_skb(sk, skb, copied); |
| 2019 | |
| 2020 | splice_read_end: |
| 2021 | release_sock(sk); |
| 2022 | return copied ? : err; |
| 2023 | } |
| 2024 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2025 | bool tls_sw_stream_read(const struct sock *sk) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2026 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2027 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2028 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2029 | bool ingress_empty = true; |
| 2030 | struct sk_psock *psock; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2031 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2032 | rcu_read_lock(); |
| 2033 | psock = sk_psock(sk); |
| 2034 | if (psock) |
| 2035 | ingress_empty = list_empty(&psock->ingress_msg); |
| 2036 | rcu_read_unlock(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2037 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2038 | return !ingress_empty || ctx->recv_pkt || |
| 2039 | !skb_queue_empty(&ctx->rx_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
| 2042 | static int tls_read_size(struct strparser *strp, struct sk_buff *skb) |
| 2043 | { |
| 2044 | struct tls_context *tls_ctx = tls_get_ctx(strp->sk); |
| 2045 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2046 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2047 | char header[TLS_HEADER_SIZE + MAX_IV_SIZE]; |
| 2048 | struct strp_msg *rxm = strp_msg(skb); |
| 2049 | size_t cipher_overhead; |
| 2050 | size_t data_len = 0; |
| 2051 | int ret; |
| 2052 | |
| 2053 | /* Verify that we have a full TLS header, or wait for more data */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2054 | if (rxm->offset + prot->prepend_size > skb->len) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2055 | return 0; |
| 2056 | |
| 2057 | /* Sanity-check size of on-stack buffer. */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2058 | if (WARN_ON(prot->prepend_size > sizeof(header))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2059 | ret = -EINVAL; |
| 2060 | goto read_failure; |
| 2061 | } |
| 2062 | |
| 2063 | /* Linearize header to local buffer */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2064 | ret = skb_copy_bits(skb, rxm->offset, header, prot->prepend_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2065 | |
| 2066 | if (ret < 0) |
| 2067 | goto read_failure; |
| 2068 | |
| 2069 | ctx->control = header[0]; |
| 2070 | |
| 2071 | data_len = ((header[4] & 0xFF) | (header[3] << 8)); |
| 2072 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2073 | cipher_overhead = prot->tag_size; |
| 2074 | if (prot->version != TLS_1_3_VERSION) |
| 2075 | cipher_overhead += prot->iv_size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2076 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2077 | if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead + |
| 2078 | prot->tail_size) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2079 | ret = -EMSGSIZE; |
| 2080 | goto read_failure; |
| 2081 | } |
| 2082 | if (data_len < cipher_overhead) { |
| 2083 | ret = -EBADMSG; |
| 2084 | goto read_failure; |
| 2085 | } |
| 2086 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2087 | /* Note that both TLS1.3 and TLS1.2 use TLS_1_2 version here */ |
| 2088 | if (header[1] != TLS_1_2_VERSION_MINOR || |
| 2089 | header[2] != TLS_1_2_VERSION_MAJOR) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2090 | ret = -EINVAL; |
| 2091 | goto read_failure; |
| 2092 | } |
| 2093 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2094 | tls_device_rx_resync_new_rec(strp->sk, data_len + TLS_HEADER_SIZE, |
| 2095 | TCP_SKB_CB(skb)->seq + rxm->offset); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2096 | return data_len + TLS_HEADER_SIZE; |
| 2097 | |
| 2098 | read_failure: |
| 2099 | tls_err_abort(strp->sk, ret); |
| 2100 | |
| 2101 | return ret; |
| 2102 | } |
| 2103 | |
| 2104 | static void tls_queue(struct strparser *strp, struct sk_buff *skb) |
| 2105 | { |
| 2106 | struct tls_context *tls_ctx = tls_get_ctx(strp->sk); |
| 2107 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2108 | |
| 2109 | ctx->decrypted = false; |
| 2110 | |
| 2111 | ctx->recv_pkt = skb; |
| 2112 | strp_pause(strp); |
| 2113 | |
| 2114 | ctx->saved_data_ready(strp->sk); |
| 2115 | } |
| 2116 | |
| 2117 | static void tls_data_ready(struct sock *sk) |
| 2118 | { |
| 2119 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2120 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2121 | struct sk_psock *psock; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2122 | |
| 2123 | strp_data_ready(&ctx->strp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2124 | |
| 2125 | psock = sk_psock_get(sk); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2126 | if (psock) { |
| 2127 | if (!list_empty(&psock->ingress_msg)) |
| 2128 | ctx->saved_data_ready(sk); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2129 | sk_psock_put(sk, psock); |
| 2130 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2133 | void tls_sw_cancel_work_tx(struct tls_context *tls_ctx) |
| 2134 | { |
| 2135 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 2136 | |
| 2137 | set_bit(BIT_TX_CLOSING, &ctx->tx_bitmask); |
| 2138 | set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask); |
| 2139 | cancel_delayed_work_sync(&ctx->tx_work.work); |
| 2140 | } |
| 2141 | |
| 2142 | void tls_sw_release_resources_tx(struct sock *sk) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2143 | { |
| 2144 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2145 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2146 | struct tls_rec *rec, *tmp; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2147 | int pending; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2148 | |
| 2149 | /* Wait for any pending async encryptions to complete */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2150 | spin_lock_bh(&ctx->encrypt_compl_lock); |
| 2151 | ctx->async_notify = true; |
| 2152 | pending = atomic_read(&ctx->encrypt_pending); |
| 2153 | spin_unlock_bh(&ctx->encrypt_compl_lock); |
| 2154 | |
| 2155 | if (pending) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2156 | crypto_wait_req(-EINPROGRESS, &ctx->async_wait); |
| 2157 | |
| 2158 | tls_tx_records(sk, -1); |
| 2159 | |
| 2160 | /* Free up un-sent records in tx_list. First, free |
| 2161 | * the partially sent record if any at head of tx_list. |
| 2162 | */ |
| 2163 | if (tls_ctx->partially_sent_record) { |
| 2164 | tls_free_partial_record(sk, tls_ctx); |
| 2165 | rec = list_first_entry(&ctx->tx_list, |
| 2166 | struct tls_rec, list); |
| 2167 | list_del(&rec->list); |
| 2168 | sk_msg_free(sk, &rec->msg_plaintext); |
| 2169 | kfree(rec); |
| 2170 | } |
| 2171 | |
| 2172 | list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) { |
| 2173 | list_del(&rec->list); |
| 2174 | sk_msg_free(sk, &rec->msg_encrypted); |
| 2175 | sk_msg_free(sk, &rec->msg_plaintext); |
| 2176 | kfree(rec); |
| 2177 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2178 | |
| 2179 | crypto_free_aead(ctx->aead_send); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2180 | tls_free_open_rec(sk); |
| 2181 | } |
| 2182 | |
| 2183 | void tls_sw_free_ctx_tx(struct tls_context *tls_ctx) |
| 2184 | { |
| 2185 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2186 | |
| 2187 | kfree(ctx); |
| 2188 | } |
| 2189 | |
| 2190 | void tls_sw_release_resources_rx(struct sock *sk) |
| 2191 | { |
| 2192 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2193 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2194 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2195 | kfree(tls_ctx->rx.rec_seq); |
| 2196 | kfree(tls_ctx->rx.iv); |
| 2197 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2198 | if (ctx->aead_recv) { |
| 2199 | kfree_skb(ctx->recv_pkt); |
| 2200 | ctx->recv_pkt = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2201 | skb_queue_purge(&ctx->rx_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2202 | crypto_free_aead(ctx->aead_recv); |
| 2203 | strp_stop(&ctx->strp); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2204 | /* If tls_sw_strparser_arm() was not called (cleanup paths) |
| 2205 | * we still want to strp_stop(), but sk->sk_data_ready was |
| 2206 | * never swapped. |
| 2207 | */ |
| 2208 | if (ctx->saved_data_ready) { |
| 2209 | write_lock_bh(&sk->sk_callback_lock); |
| 2210 | sk->sk_data_ready = ctx->saved_data_ready; |
| 2211 | write_unlock_bh(&sk->sk_callback_lock); |
| 2212 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2213 | } |
| 2214 | } |
| 2215 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2216 | void tls_sw_strparser_done(struct tls_context *tls_ctx) |
| 2217 | { |
| 2218 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2219 | |
| 2220 | strp_done(&ctx->strp); |
| 2221 | } |
| 2222 | |
| 2223 | void tls_sw_free_ctx_rx(struct tls_context *tls_ctx) |
| 2224 | { |
| 2225 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 2226 | |
| 2227 | kfree(ctx); |
| 2228 | } |
| 2229 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2230 | void tls_sw_free_resources_rx(struct sock *sk) |
| 2231 | { |
| 2232 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2233 | |
| 2234 | tls_sw_release_resources_rx(sk); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2235 | tls_sw_free_ctx_rx(tls_ctx); |
| 2236 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2237 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2238 | /* The work handler to transmitt the encrypted records in tx_list */ |
| 2239 | static void tx_work_handler(struct work_struct *work) |
| 2240 | { |
| 2241 | struct delayed_work *delayed_work = to_delayed_work(work); |
| 2242 | struct tx_work *tx_work = container_of(delayed_work, |
| 2243 | struct tx_work, work); |
| 2244 | struct sock *sk = tx_work->sk; |
| 2245 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2246 | struct tls_sw_context_tx *ctx; |
| 2247 | |
| 2248 | if (unlikely(!tls_ctx)) |
| 2249 | return; |
| 2250 | |
| 2251 | ctx = tls_sw_ctx_tx(tls_ctx); |
| 2252 | if (test_bit(BIT_TX_CLOSING, &ctx->tx_bitmask)) |
| 2253 | return; |
| 2254 | |
| 2255 | if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) |
| 2256 | return; |
| 2257 | mutex_lock(&tls_ctx->tx_lock); |
| 2258 | lock_sock(sk); |
| 2259 | tls_tx_records(sk, -1); |
| 2260 | release_sock(sk); |
| 2261 | mutex_unlock(&tls_ctx->tx_lock); |
| 2262 | } |
| 2263 | |
| 2264 | void tls_sw_write_space(struct sock *sk, struct tls_context *ctx) |
| 2265 | { |
| 2266 | struct tls_sw_context_tx *tx_ctx = tls_sw_ctx_tx(ctx); |
| 2267 | |
| 2268 | /* Schedule the transmission if tx list is ready */ |
| 2269 | if (is_tx_ready(tx_ctx) && |
| 2270 | !test_and_set_bit(BIT_TX_SCHEDULED, &tx_ctx->tx_bitmask)) |
| 2271 | schedule_delayed_work(&tx_ctx->tx_work.work, 0); |
| 2272 | } |
| 2273 | |
| 2274 | void tls_sw_strparser_arm(struct sock *sk, struct tls_context *tls_ctx) |
| 2275 | { |
| 2276 | struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(tls_ctx); |
| 2277 | |
| 2278 | write_lock_bh(&sk->sk_callback_lock); |
| 2279 | rx_ctx->saved_data_ready = sk->sk_data_ready; |
| 2280 | sk->sk_data_ready = tls_data_ready; |
| 2281 | write_unlock_bh(&sk->sk_callback_lock); |
| 2282 | |
| 2283 | strp_check_rcv(&rx_ctx->strp); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
| 2286 | int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) |
| 2287 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2288 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 2289 | struct tls_prot_info *prot = &tls_ctx->prot_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2290 | struct tls_crypto_info *crypto_info; |
| 2291 | struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2292 | struct tls12_crypto_info_aes_gcm_256 *gcm_256_info; |
| 2293 | struct tls12_crypto_info_aes_ccm_128 *ccm_128_info; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2294 | struct tls_sw_context_tx *sw_ctx_tx = NULL; |
| 2295 | struct tls_sw_context_rx *sw_ctx_rx = NULL; |
| 2296 | struct cipher_context *cctx; |
| 2297 | struct crypto_aead **aead; |
| 2298 | struct strp_callbacks cb; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2299 | u16 nonce_size, tag_size, iv_size, rec_seq_size, salt_size; |
| 2300 | struct crypto_tfm *tfm; |
| 2301 | char *iv, *rec_seq, *key, *salt, *cipher_name; |
| 2302 | size_t keysize; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2303 | int rc = 0; |
| 2304 | |
| 2305 | if (!ctx) { |
| 2306 | rc = -EINVAL; |
| 2307 | goto out; |
| 2308 | } |
| 2309 | |
| 2310 | if (tx) { |
| 2311 | if (!ctx->priv_ctx_tx) { |
| 2312 | sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL); |
| 2313 | if (!sw_ctx_tx) { |
| 2314 | rc = -ENOMEM; |
| 2315 | goto out; |
| 2316 | } |
| 2317 | ctx->priv_ctx_tx = sw_ctx_tx; |
| 2318 | } else { |
| 2319 | sw_ctx_tx = |
| 2320 | (struct tls_sw_context_tx *)ctx->priv_ctx_tx; |
| 2321 | } |
| 2322 | } else { |
| 2323 | if (!ctx->priv_ctx_rx) { |
| 2324 | sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL); |
| 2325 | if (!sw_ctx_rx) { |
| 2326 | rc = -ENOMEM; |
| 2327 | goto out; |
| 2328 | } |
| 2329 | ctx->priv_ctx_rx = sw_ctx_rx; |
| 2330 | } else { |
| 2331 | sw_ctx_rx = |
| 2332 | (struct tls_sw_context_rx *)ctx->priv_ctx_rx; |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | if (tx) { |
| 2337 | crypto_init_wait(&sw_ctx_tx->async_wait); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2338 | spin_lock_init(&sw_ctx_tx->encrypt_compl_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2339 | crypto_info = &ctx->crypto_send.info; |
| 2340 | cctx = &ctx->tx; |
| 2341 | aead = &sw_ctx_tx->aead_send; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2342 | INIT_LIST_HEAD(&sw_ctx_tx->tx_list); |
| 2343 | INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler); |
| 2344 | sw_ctx_tx->tx_work.sk = sk; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2345 | } else { |
| 2346 | crypto_init_wait(&sw_ctx_rx->async_wait); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 2347 | spin_lock_init(&sw_ctx_rx->decrypt_compl_lock); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2348 | crypto_info = &ctx->crypto_recv.info; |
| 2349 | cctx = &ctx->rx; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2350 | skb_queue_head_init(&sw_ctx_rx->rx_list); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2351 | aead = &sw_ctx_rx->aead_recv; |
| 2352 | } |
| 2353 | |
| 2354 | switch (crypto_info->cipher_type) { |
| 2355 | case TLS_CIPHER_AES_GCM_128: { |
| 2356 | nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 2357 | tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE; |
| 2358 | iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 2359 | iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; |
| 2360 | rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE; |
| 2361 | rec_seq = |
| 2362 | ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; |
| 2363 | gcm_128_info = |
| 2364 | (struct tls12_crypto_info_aes_gcm_128 *)crypto_info; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2365 | keysize = TLS_CIPHER_AES_GCM_128_KEY_SIZE; |
| 2366 | key = gcm_128_info->key; |
| 2367 | salt = gcm_128_info->salt; |
| 2368 | salt_size = TLS_CIPHER_AES_GCM_128_SALT_SIZE; |
| 2369 | cipher_name = "gcm(aes)"; |
| 2370 | break; |
| 2371 | } |
| 2372 | case TLS_CIPHER_AES_GCM_256: { |
| 2373 | nonce_size = TLS_CIPHER_AES_GCM_256_IV_SIZE; |
| 2374 | tag_size = TLS_CIPHER_AES_GCM_256_TAG_SIZE; |
| 2375 | iv_size = TLS_CIPHER_AES_GCM_256_IV_SIZE; |
| 2376 | iv = ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->iv; |
| 2377 | rec_seq_size = TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE; |
| 2378 | rec_seq = |
| 2379 | ((struct tls12_crypto_info_aes_gcm_256 *)crypto_info)->rec_seq; |
| 2380 | gcm_256_info = |
| 2381 | (struct tls12_crypto_info_aes_gcm_256 *)crypto_info; |
| 2382 | keysize = TLS_CIPHER_AES_GCM_256_KEY_SIZE; |
| 2383 | key = gcm_256_info->key; |
| 2384 | salt = gcm_256_info->salt; |
| 2385 | salt_size = TLS_CIPHER_AES_GCM_256_SALT_SIZE; |
| 2386 | cipher_name = "gcm(aes)"; |
| 2387 | break; |
| 2388 | } |
| 2389 | case TLS_CIPHER_AES_CCM_128: { |
| 2390 | nonce_size = TLS_CIPHER_AES_CCM_128_IV_SIZE; |
| 2391 | tag_size = TLS_CIPHER_AES_CCM_128_TAG_SIZE; |
| 2392 | iv_size = TLS_CIPHER_AES_CCM_128_IV_SIZE; |
| 2393 | iv = ((struct tls12_crypto_info_aes_ccm_128 *)crypto_info)->iv; |
| 2394 | rec_seq_size = TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE; |
| 2395 | rec_seq = |
| 2396 | ((struct tls12_crypto_info_aes_ccm_128 *)crypto_info)->rec_seq; |
| 2397 | ccm_128_info = |
| 2398 | (struct tls12_crypto_info_aes_ccm_128 *)crypto_info; |
| 2399 | keysize = TLS_CIPHER_AES_CCM_128_KEY_SIZE; |
| 2400 | key = ccm_128_info->key; |
| 2401 | salt = ccm_128_info->salt; |
| 2402 | salt_size = TLS_CIPHER_AES_CCM_128_SALT_SIZE; |
| 2403 | cipher_name = "ccm(aes)"; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2404 | break; |
| 2405 | } |
| 2406 | default: |
| 2407 | rc = -EINVAL; |
| 2408 | goto free_priv; |
| 2409 | } |
| 2410 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2411 | /* Sanity-check the sizes for stack allocations. */ |
| 2412 | if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE || |
| 2413 | rec_seq_size > TLS_MAX_REC_SEQ_SIZE) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2414 | rc = -EINVAL; |
| 2415 | goto free_priv; |
| 2416 | } |
| 2417 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2418 | if (crypto_info->version == TLS_1_3_VERSION) { |
| 2419 | nonce_size = 0; |
| 2420 | prot->aad_size = TLS_HEADER_SIZE; |
| 2421 | prot->tail_size = 1; |
| 2422 | } else { |
| 2423 | prot->aad_size = TLS_AAD_SPACE_SIZE; |
| 2424 | prot->tail_size = 0; |
| 2425 | } |
| 2426 | |
| 2427 | prot->version = crypto_info->version; |
| 2428 | prot->cipher_type = crypto_info->cipher_type; |
| 2429 | prot->prepend_size = TLS_HEADER_SIZE + nonce_size; |
| 2430 | prot->tag_size = tag_size; |
| 2431 | prot->overhead_size = prot->prepend_size + |
| 2432 | prot->tag_size + prot->tail_size; |
| 2433 | prot->iv_size = iv_size; |
| 2434 | prot->salt_size = salt_size; |
| 2435 | cctx->iv = kmalloc(iv_size + salt_size, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2436 | if (!cctx->iv) { |
| 2437 | rc = -ENOMEM; |
| 2438 | goto free_priv; |
| 2439 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2440 | /* Note: 128 & 256 bit salt are the same size */ |
| 2441 | prot->rec_seq_size = rec_seq_size; |
| 2442 | memcpy(cctx->iv, salt, salt_size); |
| 2443 | memcpy(cctx->iv + salt_size, iv, iv_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2444 | cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL); |
| 2445 | if (!cctx->rec_seq) { |
| 2446 | rc = -ENOMEM; |
| 2447 | goto free_iv; |
| 2448 | } |
| 2449 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2450 | if (!*aead) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2451 | *aead = crypto_alloc_aead(cipher_name, 0, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2452 | if (IS_ERR(*aead)) { |
| 2453 | rc = PTR_ERR(*aead); |
| 2454 | *aead = NULL; |
| 2455 | goto free_rec_seq; |
| 2456 | } |
| 2457 | } |
| 2458 | |
| 2459 | ctx->push_pending_record = tls_sw_push_pending_record; |
| 2460 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2461 | rc = crypto_aead_setkey(*aead, key, keysize); |
| 2462 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2463 | if (rc) |
| 2464 | goto free_aead; |
| 2465 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2466 | rc = crypto_aead_setauthsize(*aead, prot->tag_size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2467 | if (rc) |
| 2468 | goto free_aead; |
| 2469 | |
| 2470 | if (sw_ctx_rx) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2471 | tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv); |
| 2472 | |
| 2473 | if (crypto_info->version == TLS_1_3_VERSION) |
| 2474 | sw_ctx_rx->async_capable = false; |
| 2475 | else |
| 2476 | sw_ctx_rx->async_capable = |
| 2477 | tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC; |
| 2478 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2479 | /* Set up strparser */ |
| 2480 | memset(&cb, 0, sizeof(cb)); |
| 2481 | cb.rcv_msg = tls_queue; |
| 2482 | cb.parse_msg = tls_read_size; |
| 2483 | |
| 2484 | strp_init(&sw_ctx_rx->strp, sk, &cb); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
| 2487 | goto out; |
| 2488 | |
| 2489 | free_aead: |
| 2490 | crypto_free_aead(*aead); |
| 2491 | *aead = NULL; |
| 2492 | free_rec_seq: |
| 2493 | kfree(cctx->rec_seq); |
| 2494 | cctx->rec_seq = NULL; |
| 2495 | free_iv: |
| 2496 | kfree(cctx->iv); |
| 2497 | cctx->iv = NULL; |
| 2498 | free_priv: |
| 2499 | if (tx) { |
| 2500 | kfree(ctx->priv_ctx_tx); |
| 2501 | ctx->priv_ctx_tx = NULL; |
| 2502 | } else { |
| 2503 | kfree(ctx->priv_ctx_rx); |
| 2504 | ctx->priv_ctx_rx = NULL; |
| 2505 | } |
| 2506 | out: |
| 2507 | return rc; |
| 2508 | } |