Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [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. |
| 7 | * |
| 8 | * This software is available to you under a choice of one of two |
| 9 | * licenses. You may choose to be licensed under the terms of the GNU |
| 10 | * General Public License (GPL) Version 2, available from the file |
| 11 | * COPYING in the main directory of this source tree, or the |
| 12 | * OpenIB.org BSD license below: |
| 13 | * |
| 14 | * Redistribution and use in source and binary forms, with or |
| 15 | * without modification, are permitted provided that the following |
| 16 | * conditions are met: |
| 17 | * |
| 18 | * - Redistributions of source code must retain the above |
| 19 | * copyright notice, this list of conditions and the following |
| 20 | * disclaimer. |
| 21 | * |
| 22 | * - Redistributions in binary form must reproduce the above |
| 23 | * copyright notice, this list of conditions and the following |
| 24 | * disclaimer in the documentation and/or other materials |
| 25 | * provided with the distribution. |
| 26 | * |
| 27 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 28 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 29 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 30 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 31 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 32 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 33 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 34 | * SOFTWARE. |
| 35 | */ |
| 36 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 37 | #include <linux/sched/signal.h> |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 38 | #include <linux/module.h> |
| 39 | #include <crypto/aead.h> |
| 40 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 41 | #include <net/strparser.h> |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 42 | #include <net/tls.h> |
| 43 | |
Kees Cook | b16520f | 2018-04-10 17:52:34 -0700 | [diff] [blame] | 44 | #define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE |
| 45 | |
Doron Roberts-Kedes | 0927f71 | 2018-08-28 16:33:57 -0700 | [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 | |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 122 | static void tls_decrypt_done(struct crypto_async_request *req, int err) |
| 123 | { |
| 124 | struct aead_request *aead_req = (struct aead_request *)req; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 125 | struct scatterlist *sgout = aead_req->dst; |
John Fastabend | 7a3dd8c | 2018-09-14 13:01:46 -0700 | [diff] [blame] | 126 | struct tls_sw_context_rx *ctx; |
| 127 | struct tls_context *tls_ctx; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 128 | struct scatterlist *sg; |
John Fastabend | 7a3dd8c | 2018-09-14 13:01:46 -0700 | [diff] [blame] | 129 | struct sk_buff *skb; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 130 | unsigned int pages; |
John Fastabend | 7a3dd8c | 2018-09-14 13:01:46 -0700 | [diff] [blame] | 131 | int pending; |
| 132 | |
| 133 | skb = (struct sk_buff *)req->data; |
| 134 | tls_ctx = tls_get_ctx(skb->sk); |
| 135 | ctx = tls_sw_ctx_rx(tls_ctx); |
| 136 | pending = atomic_dec_return(&ctx->decrypt_pending); |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 137 | |
| 138 | /* Propagate if there was an err */ |
| 139 | if (err) { |
| 140 | ctx->async_wait.err = err; |
John Fastabend | 7a3dd8c | 2018-09-14 13:01:46 -0700 | [diff] [blame] | 141 | tls_err_abort(skb->sk, err); |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 142 | } |
| 143 | |
John Fastabend | 7a3dd8c | 2018-09-14 13:01:46 -0700 | [diff] [blame] | 144 | /* After using skb->sk to propagate sk through crypto async callback |
| 145 | * we need to NULL it again. |
| 146 | */ |
| 147 | skb->sk = NULL; |
| 148 | |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 149 | /* Release the skb, pages and memory allocated for crypto req */ |
John Fastabend | 7a3dd8c | 2018-09-14 13:01:46 -0700 | [diff] [blame] | 150 | kfree_skb(skb); |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 151 | |
| 152 | /* Skip the first S/G entry as it points to AAD */ |
| 153 | for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) { |
| 154 | if (!sg) |
| 155 | break; |
| 156 | put_page(sg_page(sg)); |
| 157 | } |
| 158 | |
| 159 | kfree(aead_req); |
| 160 | |
| 161 | if (!pending && READ_ONCE(ctx->async_notify)) |
| 162 | complete(&ctx->async_wait.completion); |
| 163 | } |
| 164 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 165 | static int tls_do_decryption(struct sock *sk, |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 166 | struct sk_buff *skb, |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 167 | struct scatterlist *sgin, |
| 168 | struct scatterlist *sgout, |
| 169 | char *iv_recv, |
| 170 | size_t data_len, |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 171 | struct aead_request *aead_req, |
| 172 | bool async) |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 173 | { |
| 174 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 175 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 176 | int ret; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 177 | |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 178 | aead_request_set_tfm(aead_req, ctx->aead_recv); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 179 | aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); |
| 180 | aead_request_set_crypt(aead_req, sgin, sgout, |
| 181 | data_len + tls_ctx->rx.tag_size, |
| 182 | (u8 *)iv_recv); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 183 | |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 184 | if (async) { |
John Fastabend | 7a3dd8c | 2018-09-14 13:01:46 -0700 | [diff] [blame] | 185 | /* Using skb->sk to push sk through to crypto async callback |
| 186 | * handler. This allows propagating errors up to the socket |
| 187 | * if needed. It _must_ be cleared in the async handler |
| 188 | * before kfree_skb is called. We _know_ skb->sk is NULL |
| 189 | * because it is a clone from strparser. |
| 190 | */ |
| 191 | skb->sk = sk; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 192 | aead_request_set_callback(aead_req, |
| 193 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 194 | tls_decrypt_done, skb); |
| 195 | atomic_inc(&ctx->decrypt_pending); |
| 196 | } else { |
| 197 | aead_request_set_callback(aead_req, |
| 198 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 199 | crypto_req_done, &ctx->async_wait); |
| 200 | } |
| 201 | |
| 202 | ret = crypto_aead_decrypt(aead_req); |
| 203 | if (ret == -EINPROGRESS) { |
| 204 | if (async) |
| 205 | return ret; |
| 206 | |
| 207 | ret = crypto_wait_req(ret, &ctx->async_wait); |
| 208 | } |
| 209 | |
| 210 | if (async) |
| 211 | atomic_dec(&ctx->decrypt_pending); |
| 212 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 216 | static void trim_sg(struct sock *sk, struct scatterlist *sg, |
| 217 | int *sg_num_elem, unsigned int *sg_size, int target_size) |
| 218 | { |
| 219 | int i = *sg_num_elem - 1; |
| 220 | int trim = *sg_size - target_size; |
| 221 | |
| 222 | if (trim <= 0) { |
| 223 | WARN_ON(trim < 0); |
| 224 | return; |
| 225 | } |
| 226 | |
| 227 | *sg_size = target_size; |
| 228 | while (trim >= sg[i].length) { |
| 229 | trim -= sg[i].length; |
| 230 | sk_mem_uncharge(sk, sg[i].length); |
| 231 | put_page(sg_page(&sg[i])); |
| 232 | i--; |
| 233 | |
| 234 | if (i < 0) |
| 235 | goto out; |
| 236 | } |
| 237 | |
| 238 | sg[i].length -= trim; |
| 239 | sk_mem_uncharge(sk, trim); |
| 240 | |
| 241 | out: |
| 242 | *sg_num_elem = i + 1; |
| 243 | } |
| 244 | |
| 245 | static void trim_both_sgl(struct sock *sk, int target_size) |
| 246 | { |
| 247 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 248 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 249 | struct tls_rec *rec = ctx->open_rec; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 250 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 251 | trim_sg(sk, &rec->sg_plaintext_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 252 | &rec->sg_plaintext_num_elem, |
| 253 | &rec->sg_plaintext_size, |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 254 | target_size); |
| 255 | |
| 256 | if (target_size > 0) |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 257 | target_size += tls_ctx->tx.overhead_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 258 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 259 | trim_sg(sk, &rec->sg_encrypted_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 260 | &rec->sg_encrypted_num_elem, |
| 261 | &rec->sg_encrypted_size, |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 262 | target_size); |
| 263 | } |
| 264 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 265 | static int alloc_encrypted_sg(struct sock *sk, int len) |
| 266 | { |
| 267 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 268 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 269 | struct tls_rec *rec = ctx->open_rec; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 270 | int rc = 0; |
| 271 | |
John Fastabend | 2c3682f | 2018-03-18 12:56:49 -0700 | [diff] [blame] | 272 | rc = sk_alloc_sg(sk, len, |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 273 | &rec->sg_encrypted_data[1], 0, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 274 | &rec->sg_encrypted_num_elem, |
| 275 | &rec->sg_encrypted_size, 0); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 276 | |
Vakul Garg | 52ea992 | 2018-09-06 21:41:40 +0530 | [diff] [blame] | 277 | if (rc == -ENOSPC) |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 278 | rec->sg_encrypted_num_elem = |
| 279 | ARRAY_SIZE(rec->sg_encrypted_data) - 1; |
Vakul Garg | 52ea992 | 2018-09-06 21:41:40 +0530 | [diff] [blame] | 280 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 281 | return rc; |
| 282 | } |
| 283 | |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 284 | static int move_to_plaintext_sg(struct sock *sk, int required_size) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 285 | { |
| 286 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 287 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 288 | struct tls_rec *rec = ctx->open_rec; |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 289 | struct scatterlist *plain_sg = &rec->sg_plaintext_data[1]; |
| 290 | struct scatterlist *enc_sg = &rec->sg_encrypted_data[1]; |
| 291 | int enc_sg_idx = 0; |
| 292 | int skip, len; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 293 | |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 294 | if (rec->sg_plaintext_num_elem == MAX_SKB_FRAGS) |
| 295 | return -ENOSPC; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 296 | |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 297 | /* We add page references worth len bytes from enc_sg at the |
| 298 | * end of plain_sg. It is guaranteed that sg_encrypted_data |
| 299 | * has enough required room (ensured by caller). |
| 300 | */ |
| 301 | len = required_size - rec->sg_plaintext_size; |
Vakul Garg | 52ea992 | 2018-09-06 21:41:40 +0530 | [diff] [blame] | 302 | |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 303 | /* Skip initial bytes in sg_encrypted_data to be able |
| 304 | * to use same offset of both plain and encrypted data. |
| 305 | */ |
| 306 | skip = tls_ctx->tx.prepend_size + rec->sg_plaintext_size; |
| 307 | |
| 308 | while (enc_sg_idx < rec->sg_encrypted_num_elem) { |
| 309 | if (enc_sg[enc_sg_idx].length > skip) |
| 310 | break; |
| 311 | |
| 312 | skip -= enc_sg[enc_sg_idx].length; |
| 313 | enc_sg_idx++; |
| 314 | } |
| 315 | |
| 316 | /* unmark the end of plain_sg*/ |
| 317 | sg_unmark_end(plain_sg + rec->sg_plaintext_num_elem - 1); |
| 318 | |
| 319 | while (len) { |
| 320 | struct page *page = sg_page(&enc_sg[enc_sg_idx]); |
| 321 | int bytes = enc_sg[enc_sg_idx].length - skip; |
| 322 | int offset = enc_sg[enc_sg_idx].offset + skip; |
| 323 | |
| 324 | if (bytes > len) |
| 325 | bytes = len; |
| 326 | else |
| 327 | enc_sg_idx++; |
| 328 | |
| 329 | /* Skipping is required only one time */ |
| 330 | skip = 0; |
| 331 | |
| 332 | /* Increment page reference */ |
| 333 | get_page(page); |
| 334 | |
| 335 | sg_set_page(&plain_sg[rec->sg_plaintext_num_elem], page, |
| 336 | bytes, offset); |
| 337 | |
| 338 | sk_mem_charge(sk, bytes); |
| 339 | |
| 340 | len -= bytes; |
| 341 | rec->sg_plaintext_size += bytes; |
| 342 | |
| 343 | rec->sg_plaintext_num_elem++; |
| 344 | |
| 345 | if (rec->sg_plaintext_num_elem == MAX_SKB_FRAGS) |
| 346 | return -ENOSPC; |
| 347 | } |
| 348 | |
| 349 | return 0; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static void free_sg(struct sock *sk, struct scatterlist *sg, |
| 353 | int *sg_num_elem, unsigned int *sg_size) |
| 354 | { |
| 355 | int i, n = *sg_num_elem; |
| 356 | |
| 357 | for (i = 0; i < n; ++i) { |
| 358 | sk_mem_uncharge(sk, sg[i].length); |
| 359 | put_page(sg_page(&sg[i])); |
| 360 | } |
| 361 | *sg_num_elem = 0; |
| 362 | *sg_size = 0; |
| 363 | } |
| 364 | |
Vakul Garg | c774973 | 2018-09-25 20:21:51 +0530 | [diff] [blame] | 365 | static void tls_free_open_rec(struct sock *sk) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 366 | { |
| 367 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 368 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 369 | struct tls_rec *rec = ctx->open_rec; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 370 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 371 | /* Return if there is no open record */ |
| 372 | if (!rec) |
| 373 | return; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 374 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 375 | free_sg(sk, &rec->sg_encrypted_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 376 | &rec->sg_encrypted_num_elem, |
| 377 | &rec->sg_encrypted_size); |
| 378 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 379 | free_sg(sk, &rec->sg_plaintext_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 380 | &rec->sg_plaintext_num_elem, |
| 381 | &rec->sg_plaintext_size); |
Vakul Garg | c774973 | 2018-09-25 20:21:51 +0530 | [diff] [blame] | 382 | |
| 383 | kfree(rec); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 386 | int tls_tx_records(struct sock *sk, int flags) |
| 387 | { |
| 388 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 389 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 390 | struct tls_rec *rec, *tmp; |
| 391 | int tx_flags, rc = 0; |
| 392 | |
| 393 | if (tls_is_partially_sent_record(tls_ctx)) { |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 394 | rec = list_first_entry(&ctx->tx_list, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 395 | struct tls_rec, list); |
| 396 | |
| 397 | if (flags == -1) |
| 398 | tx_flags = rec->tx_flags; |
| 399 | else |
| 400 | tx_flags = flags; |
| 401 | |
| 402 | rc = tls_push_partial_record(sk, tls_ctx, tx_flags); |
| 403 | if (rc) |
| 404 | goto tx_err; |
| 405 | |
| 406 | /* Full record has been transmitted. |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 407 | * Remove the head of tx_list |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 408 | */ |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 409 | list_del(&rec->list); |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 410 | free_sg(sk, &rec->sg_plaintext_data[1], |
Vakul Garg | b85135b | 2018-09-25 16:26:17 +0530 | [diff] [blame] | 411 | &rec->sg_plaintext_num_elem, &rec->sg_plaintext_size); |
| 412 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 413 | kfree(rec); |
| 414 | } |
| 415 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 416 | /* Tx all ready records */ |
| 417 | list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) { |
| 418 | if (READ_ONCE(rec->tx_ready)) { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 419 | if (flags == -1) |
| 420 | tx_flags = rec->tx_flags; |
| 421 | else |
| 422 | tx_flags = flags; |
| 423 | |
| 424 | rc = tls_push_sg(sk, tls_ctx, |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 425 | &rec->sg_encrypted_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 426 | 0, tx_flags); |
| 427 | if (rc) |
| 428 | goto tx_err; |
| 429 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 430 | list_del(&rec->list); |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 431 | free_sg(sk, &rec->sg_plaintext_data[1], |
Vakul Garg | b85135b | 2018-09-25 16:26:17 +0530 | [diff] [blame] | 432 | &rec->sg_plaintext_num_elem, |
| 433 | &rec->sg_plaintext_size); |
| 434 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 435 | kfree(rec); |
| 436 | } else { |
| 437 | break; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | tx_err: |
| 442 | if (rc < 0 && rc != -EAGAIN) |
| 443 | tls_err_abort(sk, EBADMSG); |
| 444 | |
| 445 | return rc; |
| 446 | } |
| 447 | |
| 448 | static void tls_encrypt_done(struct crypto_async_request *req, int err) |
| 449 | { |
| 450 | struct aead_request *aead_req = (struct aead_request *)req; |
| 451 | struct sock *sk = req->data; |
| 452 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 453 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 454 | struct tls_rec *rec; |
| 455 | bool ready = false; |
| 456 | int pending; |
| 457 | |
| 458 | rec = container_of(aead_req, struct tls_rec, aead_req); |
| 459 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 460 | rec->sg_encrypted_data[1].offset -= tls_ctx->tx.prepend_size; |
| 461 | rec->sg_encrypted_data[1].length += tls_ctx->tx.prepend_size; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 462 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 463 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 464 | /* Check if error is previously set on socket */ |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 465 | if (err || sk->sk_err) { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 466 | rec = NULL; |
| 467 | |
| 468 | /* If err is already set on socket, return the same code */ |
| 469 | if (sk->sk_err) { |
| 470 | ctx->async_wait.err = sk->sk_err; |
| 471 | } else { |
| 472 | ctx->async_wait.err = err; |
| 473 | tls_err_abort(sk, err); |
| 474 | } |
| 475 | } |
| 476 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 477 | if (rec) { |
| 478 | struct tls_rec *first_rec; |
| 479 | |
| 480 | /* Mark the record as ready for transmission */ |
| 481 | smp_store_mb(rec->tx_ready, true); |
| 482 | |
| 483 | /* If received record is at head of tx_list, schedule tx */ |
| 484 | first_rec = list_first_entry(&ctx->tx_list, |
| 485 | struct tls_rec, list); |
| 486 | if (rec == first_rec) |
| 487 | ready = true; |
| 488 | } |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 489 | |
| 490 | pending = atomic_dec_return(&ctx->encrypt_pending); |
| 491 | |
| 492 | if (!pending && READ_ONCE(ctx->async_notify)) |
| 493 | complete(&ctx->async_wait.completion); |
| 494 | |
| 495 | if (!ready) |
| 496 | return; |
| 497 | |
| 498 | /* Schedule the transmission */ |
| 499 | if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 500 | schedule_delayed_work(&ctx->tx_work.work, 2); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static int tls_do_encryption(struct sock *sk, |
| 504 | struct tls_context *tls_ctx, |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 505 | struct tls_sw_context_tx *ctx, |
| 506 | struct aead_request *aead_req, |
| 507 | size_t data_len) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 508 | { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 509 | struct tls_rec *rec = ctx->open_rec; |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 510 | struct scatterlist *plain_sg = rec->sg_plaintext_data; |
| 511 | struct scatterlist *enc_sg = rec->sg_encrypted_data; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 512 | int rc; |
| 513 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 514 | /* Skip the first index as it contains AAD data */ |
| 515 | rec->sg_encrypted_data[1].offset += tls_ctx->tx.prepend_size; |
| 516 | rec->sg_encrypted_data[1].length -= tls_ctx->tx.prepend_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 517 | |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 518 | /* If it is inplace crypto, then pass same SG list as both src, dst */ |
| 519 | if (rec->inplace_crypto) |
| 520 | plain_sg = enc_sg; |
| 521 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 522 | aead_request_set_tfm(aead_req, ctx->aead_send); |
| 523 | aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE); |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 524 | aead_request_set_crypt(aead_req, plain_sg, enc_sg, |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 525 | data_len, tls_ctx->tx.iv); |
Vakul Garg | a54667f | 2018-01-31 21:34:37 +0530 | [diff] [blame] | 526 | |
| 527 | aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 528 | tls_encrypt_done, sk); |
Vakul Garg | a54667f | 2018-01-31 21:34:37 +0530 | [diff] [blame] | 529 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 530 | /* Add the record in tx_list */ |
| 531 | list_add_tail((struct list_head *)&rec->list, &ctx->tx_list); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 532 | atomic_inc(&ctx->encrypt_pending); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 533 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 534 | rc = crypto_aead_encrypt(aead_req); |
| 535 | if (!rc || rc != -EINPROGRESS) { |
| 536 | atomic_dec(&ctx->encrypt_pending); |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 537 | rec->sg_encrypted_data[1].offset -= tls_ctx->tx.prepend_size; |
| 538 | rec->sg_encrypted_data[1].length += tls_ctx->tx.prepend_size; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 539 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 540 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 541 | if (!rc) { |
| 542 | WRITE_ONCE(rec->tx_ready, true); |
| 543 | } else if (rc != -EINPROGRESS) { |
| 544 | list_del(&rec->list); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 545 | return rc; |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 546 | } |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 547 | |
| 548 | /* Unhook the record from context if encryption is not failure */ |
| 549 | ctx->open_rec = NULL; |
| 550 | tls_advance_record_sn(sk, &tls_ctx->tx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 551 | return rc; |
| 552 | } |
| 553 | |
| 554 | static int tls_push_record(struct sock *sk, int flags, |
| 555 | unsigned char record_type) |
| 556 | { |
| 557 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 558 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 559 | struct tls_rec *rec = ctx->open_rec; |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 560 | struct aead_request *req; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 561 | int rc; |
| 562 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 563 | if (!rec) |
| 564 | return 0; |
Daniel Borkmann | a447da7 | 2018-06-15 03:07:45 +0200 | [diff] [blame] | 565 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 566 | rec->tx_flags = flags; |
| 567 | req = &rec->aead_req; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 568 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 569 | sg_mark_end(rec->sg_plaintext_data + rec->sg_plaintext_num_elem); |
| 570 | sg_mark_end(rec->sg_encrypted_data + rec->sg_encrypted_num_elem); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 571 | |
| 572 | tls_make_aad(rec->aad_space, rec->sg_plaintext_size, |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 573 | tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size, |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 574 | record_type); |
| 575 | |
| 576 | tls_fill_prepend(tls_ctx, |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 577 | page_address(sg_page(&rec->sg_encrypted_data[1])) + |
| 578 | rec->sg_encrypted_data[1].offset, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 579 | rec->sg_plaintext_size, record_type); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 580 | |
| 581 | tls_ctx->pending_open_record_frags = 0; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 582 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 583 | rc = tls_do_encryption(sk, tls_ctx, ctx, req, rec->sg_plaintext_size); |
| 584 | if (rc == -EINPROGRESS) |
| 585 | return -EINPROGRESS; |
| 586 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 587 | if (rc < 0) { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 588 | tls_err_abort(sk, EBADMSG); |
| 589 | return rc; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 590 | } |
| 591 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 592 | return tls_tx_records(sk, flags); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static int tls_sw_push_pending_record(struct sock *sk, int flags) |
| 596 | { |
| 597 | return tls_push_record(sk, flags, TLS_RECORD_TYPE_DATA); |
| 598 | } |
| 599 | |
| 600 | static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from, |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 601 | int length, int *pages_used, |
| 602 | unsigned int *size_used, |
| 603 | struct scatterlist *to, int to_max_pages, |
Doron Roberts-Kedes | 2da19ed | 2018-07-26 07:59:36 -0700 | [diff] [blame] | 604 | bool charge) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 605 | { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 606 | struct page *pages[MAX_SKB_FRAGS]; |
| 607 | |
| 608 | size_t offset; |
| 609 | ssize_t copied, use; |
| 610 | int i = 0; |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 611 | unsigned int size = *size_used; |
| 612 | int num_elem = *pages_used; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 613 | int rc = 0; |
| 614 | int maxpages; |
| 615 | |
| 616 | while (length > 0) { |
| 617 | i = 0; |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 618 | maxpages = to_max_pages - num_elem; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 619 | if (maxpages == 0) { |
| 620 | rc = -EFAULT; |
| 621 | goto out; |
| 622 | } |
| 623 | copied = iov_iter_get_pages(from, pages, |
| 624 | length, |
| 625 | maxpages, &offset); |
| 626 | if (copied <= 0) { |
| 627 | rc = -EFAULT; |
| 628 | goto out; |
| 629 | } |
| 630 | |
| 631 | iov_iter_advance(from, copied); |
| 632 | |
| 633 | length -= copied; |
| 634 | size += copied; |
| 635 | while (copied) { |
| 636 | use = min_t(int, copied, PAGE_SIZE - offset); |
| 637 | |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 638 | sg_set_page(&to[num_elem], |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 639 | pages[i], use, offset); |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 640 | sg_unmark_end(&to[num_elem]); |
| 641 | if (charge) |
| 642 | sk_mem_charge(sk, use); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 643 | |
| 644 | offset = 0; |
| 645 | copied -= use; |
| 646 | |
| 647 | ++i; |
| 648 | ++num_elem; |
| 649 | } |
| 650 | } |
| 651 | |
Vakul Garg | cfb4099 | 2018-08-02 20:43:10 +0530 | [diff] [blame] | 652 | /* Mark the end in the last sg entry if newly added */ |
| 653 | if (num_elem > *pages_used) |
| 654 | sg_mark_end(&to[num_elem - 1]); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 655 | out: |
Doron Roberts-Kedes | 2da19ed | 2018-07-26 07:59:36 -0700 | [diff] [blame] | 656 | if (rc) |
| 657 | iov_iter_revert(from, size - *size_used); |
Dave Watson | 69ca929 | 2018-03-22 10:09:53 -0700 | [diff] [blame] | 658 | *size_used = size; |
| 659 | *pages_used = num_elem; |
| 660 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 661 | return rc; |
| 662 | } |
| 663 | |
| 664 | static int memcopy_from_iter(struct sock *sk, struct iov_iter *from, |
| 665 | int bytes) |
| 666 | { |
| 667 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 668 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 669 | struct tls_rec *rec = ctx->open_rec; |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 670 | struct scatterlist *sg = &rec->sg_plaintext_data[1]; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 671 | int copy, i, rc = 0; |
| 672 | |
| 673 | for (i = tls_ctx->pending_open_record_frags; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 674 | i < rec->sg_plaintext_num_elem; ++i) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 675 | copy = sg[i].length; |
| 676 | if (copy_from_iter( |
| 677 | page_address(sg_page(&sg[i])) + sg[i].offset, |
| 678 | copy, from) != copy) { |
| 679 | rc = -EFAULT; |
| 680 | goto out; |
| 681 | } |
| 682 | bytes -= copy; |
| 683 | |
| 684 | ++tls_ctx->pending_open_record_frags; |
| 685 | |
| 686 | if (!bytes) |
| 687 | break; |
| 688 | } |
| 689 | |
| 690 | out: |
| 691 | return rc; |
| 692 | } |
| 693 | |
Wei Yongjun | bf17b67 | 2018-09-26 12:10:48 +0000 | [diff] [blame] | 694 | static struct tls_rec *get_rec(struct sock *sk) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 695 | { |
| 696 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 697 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 698 | struct tls_rec *rec; |
| 699 | int mem_size; |
| 700 | |
| 701 | /* Return if we already have an open record */ |
| 702 | if (ctx->open_rec) |
| 703 | return ctx->open_rec; |
| 704 | |
| 705 | mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send); |
| 706 | |
| 707 | rec = kzalloc(mem_size, sk->sk_allocation); |
| 708 | if (!rec) |
| 709 | return NULL; |
| 710 | |
| 711 | sg_init_table(&rec->sg_plaintext_data[0], |
| 712 | ARRAY_SIZE(rec->sg_plaintext_data)); |
| 713 | sg_init_table(&rec->sg_encrypted_data[0], |
| 714 | ARRAY_SIZE(rec->sg_encrypted_data)); |
| 715 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 716 | sg_set_buf(&rec->sg_plaintext_data[0], rec->aad_space, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 717 | sizeof(rec->aad_space)); |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 718 | sg_set_buf(&rec->sg_encrypted_data[0], rec->aad_space, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 719 | sizeof(rec->aad_space)); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 720 | |
| 721 | ctx->open_rec = rec; |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 722 | rec->inplace_crypto = 1; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 723 | |
| 724 | return rec; |
| 725 | } |
| 726 | |
| 727 | int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) |
| 728 | { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 729 | long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 730 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 731 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 732 | struct crypto_tfm *tfm = crypto_aead_tfm(ctx->aead_send); |
| 733 | bool async_capable = tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC; |
| 734 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
| 735 | bool is_kvec = msg->msg_iter.type & ITER_KVEC; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 736 | bool eor = !(msg->msg_flags & MSG_MORE); |
| 737 | size_t try_to_copy, copied = 0; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 738 | struct tls_rec *rec; |
| 739 | int required_size; |
| 740 | int num_async = 0; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 741 | bool full_record; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 742 | int record_room; |
| 743 | int num_zc = 0; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 744 | int orig_size; |
Vakul Garg | 4128c0c | 2018-09-24 16:09:49 +0530 | [diff] [blame] | 745 | int ret = 0; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 746 | |
| 747 | if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL)) |
| 748 | return -ENOTSUPP; |
| 749 | |
| 750 | lock_sock(sk); |
| 751 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 752 | /* Wait till there is any pending write on socket */ |
| 753 | if (unlikely(sk->sk_write_pending)) { |
| 754 | ret = wait_on_pending_writer(sk, &timeo); |
| 755 | if (unlikely(ret)) |
| 756 | goto send_end; |
| 757 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 758 | |
| 759 | if (unlikely(msg->msg_controllen)) { |
| 760 | ret = tls_proccess_cmsg(sk, msg, &record_type); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 761 | if (ret) { |
| 762 | if (ret == -EINPROGRESS) |
| 763 | num_async++; |
| 764 | else if (ret != -EAGAIN) |
| 765 | goto send_end; |
| 766 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | while (msg_data_left(msg)) { |
| 770 | if (sk->sk_err) { |
r.hering@avm.de | 30be8f8 | 2018-01-12 15:42:06 +0100 | [diff] [blame] | 771 | ret = -sk->sk_err; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 772 | goto send_end; |
| 773 | } |
| 774 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 775 | rec = get_rec(sk); |
| 776 | if (!rec) { |
| 777 | ret = -ENOMEM; |
| 778 | goto send_end; |
| 779 | } |
| 780 | |
| 781 | orig_size = rec->sg_plaintext_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 782 | full_record = false; |
| 783 | try_to_copy = msg_data_left(msg); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 784 | record_room = TLS_MAX_PAYLOAD_SIZE - rec->sg_plaintext_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 785 | if (try_to_copy >= record_room) { |
| 786 | try_to_copy = record_room; |
| 787 | full_record = true; |
| 788 | } |
| 789 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 790 | required_size = rec->sg_plaintext_size + try_to_copy + |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 791 | tls_ctx->tx.overhead_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 792 | |
| 793 | if (!sk_stream_memory_free(sk)) |
| 794 | goto wait_for_sndbuf; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 795 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 796 | alloc_encrypted: |
| 797 | ret = alloc_encrypted_sg(sk, required_size); |
| 798 | if (ret) { |
| 799 | if (ret != -ENOSPC) |
| 800 | goto wait_for_memory; |
| 801 | |
| 802 | /* Adjust try_to_copy according to the amount that was |
| 803 | * actually allocated. The difference is due |
| 804 | * to max sg elements limit |
| 805 | */ |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 806 | try_to_copy -= required_size - rec->sg_encrypted_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 807 | full_record = true; |
| 808 | } |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 809 | |
| 810 | if (!is_kvec && (full_record || eor) && !async_capable) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 811 | ret = zerocopy_from_iter(sk, &msg->msg_iter, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 812 | try_to_copy, &rec->sg_plaintext_num_elem, |
| 813 | &rec->sg_plaintext_size, |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 814 | &rec->sg_plaintext_data[1], |
| 815 | ARRAY_SIZE(rec->sg_plaintext_data) - 1, |
Doron Roberts-Kedes | 2da19ed | 2018-07-26 07:59:36 -0700 | [diff] [blame] | 816 | true); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 817 | if (ret) |
| 818 | goto fallback_to_reg_send; |
| 819 | |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 820 | rec->inplace_crypto = 0; |
| 821 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 822 | num_zc++; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 823 | copied += try_to_copy; |
| 824 | ret = tls_push_record(sk, msg->msg_flags, record_type); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 825 | if (ret) { |
| 826 | if (ret == -EINPROGRESS) |
| 827 | num_async++; |
| 828 | else if (ret != -EAGAIN) |
| 829 | goto send_end; |
| 830 | } |
Doron Roberts-Kedes | 5a3611e | 2018-07-26 07:59:35 -0700 | [diff] [blame] | 831 | continue; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 832 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 833 | fallback_to_reg_send: |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 834 | trim_sg(sk, &rec->sg_plaintext_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 835 | &rec->sg_plaintext_num_elem, |
| 836 | &rec->sg_plaintext_size, |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 837 | orig_size); |
| 838 | } |
| 839 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 840 | required_size = rec->sg_plaintext_size + try_to_copy; |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 841 | |
| 842 | ret = move_to_plaintext_sg(sk, required_size); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 843 | if (ret) { |
| 844 | if (ret != -ENOSPC) |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 845 | goto send_end; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 846 | |
| 847 | /* Adjust try_to_copy according to the amount that was |
| 848 | * actually allocated. The difference is due |
| 849 | * to max sg elements limit |
| 850 | */ |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 851 | try_to_copy -= required_size - rec->sg_plaintext_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 852 | full_record = true; |
| 853 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 854 | trim_sg(sk, &rec->sg_encrypted_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 855 | &rec->sg_encrypted_num_elem, |
| 856 | &rec->sg_encrypted_size, |
| 857 | rec->sg_plaintext_size + |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 858 | tls_ctx->tx.overhead_size); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | ret = memcopy_from_iter(sk, &msg->msg_iter, try_to_copy); |
| 862 | if (ret) |
| 863 | goto trim_sgl; |
| 864 | |
| 865 | copied += try_to_copy; |
| 866 | if (full_record || eor) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 867 | ret = tls_push_record(sk, msg->msg_flags, record_type); |
| 868 | if (ret) { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 869 | if (ret == -EINPROGRESS) |
| 870 | num_async++; |
| 871 | else if (ret != -EAGAIN) |
| 872 | goto send_end; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | |
| 876 | continue; |
| 877 | |
| 878 | wait_for_sndbuf: |
| 879 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 880 | wait_for_memory: |
| 881 | ret = sk_stream_wait_memory(sk, &timeo); |
| 882 | if (ret) { |
| 883 | trim_sgl: |
| 884 | trim_both_sgl(sk, orig_size); |
| 885 | goto send_end; |
| 886 | } |
| 887 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 888 | if (rec->sg_encrypted_size < required_size) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 889 | goto alloc_encrypted; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 890 | } |
| 891 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 892 | if (!num_async) { |
| 893 | goto send_end; |
| 894 | } else if (num_zc) { |
| 895 | /* Wait for pending encryptions to get completed */ |
| 896 | smp_store_mb(ctx->async_notify, true); |
| 897 | |
| 898 | if (atomic_read(&ctx->encrypt_pending)) |
| 899 | crypto_wait_req(-EINPROGRESS, &ctx->async_wait); |
| 900 | else |
| 901 | reinit_completion(&ctx->async_wait.completion); |
| 902 | |
| 903 | WRITE_ONCE(ctx->async_notify, false); |
| 904 | |
| 905 | if (ctx->async_wait.err) { |
| 906 | ret = ctx->async_wait.err; |
| 907 | copied = 0; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /* Transmit if any encryptions have completed */ |
| 912 | if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) { |
| 913 | cancel_delayed_work(&ctx->tx_work.work); |
| 914 | tls_tx_records(sk, msg->msg_flags); |
| 915 | } |
| 916 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 917 | send_end: |
| 918 | ret = sk_stream_error(sk, msg->msg_flags, ret); |
| 919 | |
| 920 | release_sock(sk); |
| 921 | return copied ? copied : ret; |
| 922 | } |
| 923 | |
| 924 | int tls_sw_sendpage(struct sock *sk, struct page *page, |
| 925 | int offset, size_t size, int flags) |
| 926 | { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 927 | long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 928 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 929 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 930 | unsigned char record_type = TLS_RECORD_TYPE_DATA; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 931 | size_t orig_size = size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 932 | struct scatterlist *sg; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 933 | struct tls_rec *rec; |
| 934 | int num_async = 0; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 935 | bool full_record; |
| 936 | int record_room; |
Vakul Garg | 4128c0c | 2018-09-24 16:09:49 +0530 | [diff] [blame] | 937 | int ret = 0; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 938 | bool eor; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 939 | |
| 940 | if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | |
| 941 | MSG_SENDPAGE_NOTLAST)) |
| 942 | return -ENOTSUPP; |
| 943 | |
| 944 | /* No MSG_EOR from splice, only look at MSG_MORE */ |
| 945 | eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST)); |
| 946 | |
| 947 | lock_sock(sk); |
| 948 | |
| 949 | sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk); |
| 950 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 951 | /* Wait till there is any pending write on socket */ |
| 952 | if (unlikely(sk->sk_write_pending)) { |
| 953 | ret = wait_on_pending_writer(sk, &timeo); |
| 954 | if (unlikely(ret)) |
| 955 | goto sendpage_end; |
| 956 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 957 | |
| 958 | /* Call the sk_stream functions to manage the sndbuf mem. */ |
| 959 | while (size > 0) { |
| 960 | size_t copy, required_size; |
| 961 | |
| 962 | if (sk->sk_err) { |
r.hering@avm.de | 30be8f8 | 2018-01-12 15:42:06 +0100 | [diff] [blame] | 963 | ret = -sk->sk_err; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 964 | goto sendpage_end; |
| 965 | } |
| 966 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 967 | rec = get_rec(sk); |
| 968 | if (!rec) { |
| 969 | ret = -ENOMEM; |
| 970 | goto sendpage_end; |
| 971 | } |
| 972 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 973 | full_record = false; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 974 | record_room = TLS_MAX_PAYLOAD_SIZE - rec->sg_plaintext_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 975 | copy = size; |
| 976 | if (copy >= record_room) { |
| 977 | copy = record_room; |
| 978 | full_record = true; |
| 979 | } |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 980 | required_size = rec->sg_plaintext_size + copy + |
Dave Watson | dbe4255 | 2018-03-22 10:10:06 -0700 | [diff] [blame] | 981 | tls_ctx->tx.overhead_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 982 | |
| 983 | if (!sk_stream_memory_free(sk)) |
| 984 | goto wait_for_sndbuf; |
| 985 | alloc_payload: |
| 986 | ret = alloc_encrypted_sg(sk, required_size); |
| 987 | if (ret) { |
| 988 | if (ret != -ENOSPC) |
| 989 | goto wait_for_memory; |
| 990 | |
| 991 | /* Adjust copy according to the amount that was |
| 992 | * actually allocated. The difference is due |
| 993 | * to max sg elements limit |
| 994 | */ |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 995 | copy -= required_size - rec->sg_plaintext_size; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 996 | full_record = true; |
| 997 | } |
| 998 | |
| 999 | get_page(page); |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 1000 | sg = &rec->sg_plaintext_data[1] + rec->sg_plaintext_num_elem; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1001 | sg_set_page(sg, page, copy, offset); |
Dave Watson | 7a8c4dd | 2018-01-19 12:30:13 -0800 | [diff] [blame] | 1002 | sg_unmark_end(sg); |
| 1003 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1004 | rec->sg_plaintext_num_elem++; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1005 | |
| 1006 | sk_mem_charge(sk, copy); |
| 1007 | offset += copy; |
| 1008 | size -= copy; |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1009 | rec->sg_plaintext_size += copy; |
| 1010 | tls_ctx->pending_open_record_frags = rec->sg_plaintext_num_elem; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1011 | |
| 1012 | if (full_record || eor || |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1013 | rec->sg_plaintext_num_elem == |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 1014 | ARRAY_SIZE(rec->sg_plaintext_data) - 1) { |
Vakul Garg | 4e6d472 | 2018-09-30 08:04:35 +0530 | [diff] [blame^] | 1015 | rec->inplace_crypto = 0; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1016 | ret = tls_push_record(sk, flags, record_type); |
| 1017 | if (ret) { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1018 | if (ret == -EINPROGRESS) |
| 1019 | num_async++; |
| 1020 | else if (ret != -EAGAIN) |
| 1021 | goto sendpage_end; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1022 | } |
| 1023 | } |
| 1024 | continue; |
| 1025 | wait_for_sndbuf: |
| 1026 | set_bit(SOCK_NOSPACE, &sk->sk_socket->flags); |
| 1027 | wait_for_memory: |
| 1028 | ret = sk_stream_wait_memory(sk, &timeo); |
| 1029 | if (ret) { |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1030 | trim_both_sgl(sk, rec->sg_plaintext_size); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1031 | goto sendpage_end; |
| 1032 | } |
| 1033 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1034 | goto alloc_payload; |
| 1035 | } |
| 1036 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1037 | if (num_async) { |
| 1038 | /* Transmit if any encryptions have completed */ |
| 1039 | if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) { |
| 1040 | cancel_delayed_work(&ctx->tx_work.work); |
| 1041 | tls_tx_records(sk, flags); |
| 1042 | } |
| 1043 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1044 | sendpage_end: |
| 1045 | if (orig_size > size) |
| 1046 | ret = orig_size - size; |
| 1047 | else |
| 1048 | ret = sk_stream_error(sk, flags, ret); |
| 1049 | |
| 1050 | release_sock(sk); |
| 1051 | return ret; |
| 1052 | } |
| 1053 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1054 | static struct sk_buff *tls_wait_data(struct sock *sk, int flags, |
| 1055 | long timeo, int *err) |
| 1056 | { |
| 1057 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1058 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1059 | struct sk_buff *skb; |
| 1060 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
| 1061 | |
| 1062 | while (!(skb = ctx->recv_pkt)) { |
| 1063 | if (sk->sk_err) { |
| 1064 | *err = sock_error(sk); |
| 1065 | return NULL; |
| 1066 | } |
| 1067 | |
Doron Roberts-Kedes | fcf4793 | 2018-07-18 16:22:27 -0700 | [diff] [blame] | 1068 | if (sk->sk_shutdown & RCV_SHUTDOWN) |
| 1069 | return NULL; |
| 1070 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1071 | if (sock_flag(sk, SOCK_DONE)) |
| 1072 | return NULL; |
| 1073 | |
| 1074 | if ((flags & MSG_DONTWAIT) || !timeo) { |
| 1075 | *err = -EAGAIN; |
| 1076 | return NULL; |
| 1077 | } |
| 1078 | |
| 1079 | add_wait_queue(sk_sleep(sk), &wait); |
| 1080 | sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 1081 | sk_wait_event(sk, &timeo, ctx->recv_pkt != skb, &wait); |
| 1082 | sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk); |
| 1083 | remove_wait_queue(sk_sleep(sk), &wait); |
| 1084 | |
| 1085 | /* Handle signals */ |
| 1086 | if (signal_pending(current)) { |
| 1087 | *err = sock_intr_errno(timeo); |
| 1088 | return NULL; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | return skb; |
| 1093 | } |
| 1094 | |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1095 | /* This function decrypts the input skb into either out_iov or in out_sg |
| 1096 | * or in skb buffers itself. The input parameter 'zc' indicates if |
| 1097 | * zero-copy mode needs to be tried or not. With zero-copy mode, either |
| 1098 | * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are |
| 1099 | * NULL, then the decryption happens inside skb buffers itself, i.e. |
| 1100 | * zero-copy gets disabled and 'zc' is updated. |
| 1101 | */ |
| 1102 | |
| 1103 | static int decrypt_internal(struct sock *sk, struct sk_buff *skb, |
| 1104 | struct iov_iter *out_iov, |
| 1105 | struct scatterlist *out_sg, |
| 1106 | int *chunk, bool *zc) |
| 1107 | { |
| 1108 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1109 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1110 | struct strp_msg *rxm = strp_msg(skb); |
| 1111 | int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0; |
| 1112 | struct aead_request *aead_req; |
| 1113 | struct sk_buff *unused; |
| 1114 | u8 *aad, *iv, *mem = NULL; |
| 1115 | struct scatterlist *sgin = NULL; |
| 1116 | struct scatterlist *sgout = NULL; |
| 1117 | const int data_len = rxm->full_len - tls_ctx->rx.overhead_size; |
| 1118 | |
| 1119 | if (*zc && (out_iov || out_sg)) { |
| 1120 | if (out_iov) |
| 1121 | n_sgout = iov_iter_npages(out_iov, INT_MAX) + 1; |
| 1122 | else |
| 1123 | n_sgout = sg_nents(out_sg); |
Doron Roberts-Kedes | 0927f71 | 2018-08-28 16:33:57 -0700 | [diff] [blame] | 1124 | n_sgin = skb_nsg(skb, rxm->offset + tls_ctx->rx.prepend_size, |
| 1125 | rxm->full_len - tls_ctx->rx.prepend_size); |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1126 | } else { |
| 1127 | n_sgout = 0; |
| 1128 | *zc = false; |
Doron Roberts-Kedes | 0927f71 | 2018-08-28 16:33:57 -0700 | [diff] [blame] | 1129 | n_sgin = skb_cow_data(skb, 0, &unused); |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1130 | } |
| 1131 | |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1132 | if (n_sgin < 1) |
| 1133 | return -EBADMSG; |
| 1134 | |
| 1135 | /* Increment to accommodate AAD */ |
| 1136 | n_sgin = n_sgin + 1; |
| 1137 | |
| 1138 | nsg = n_sgin + n_sgout; |
| 1139 | |
| 1140 | aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv); |
| 1141 | mem_size = aead_size + (nsg * sizeof(struct scatterlist)); |
| 1142 | mem_size = mem_size + TLS_AAD_SPACE_SIZE; |
| 1143 | mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv); |
| 1144 | |
| 1145 | /* Allocate a single block of memory which contains |
| 1146 | * aead_req || sgin[] || sgout[] || aad || iv. |
| 1147 | * This order achieves correct alignment for aead_req, sgin, sgout. |
| 1148 | */ |
| 1149 | mem = kmalloc(mem_size, sk->sk_allocation); |
| 1150 | if (!mem) |
| 1151 | return -ENOMEM; |
| 1152 | |
| 1153 | /* Segment the allocated memory */ |
| 1154 | aead_req = (struct aead_request *)mem; |
| 1155 | sgin = (struct scatterlist *)(mem + aead_size); |
| 1156 | sgout = sgin + n_sgin; |
| 1157 | aad = (u8 *)(sgout + n_sgout); |
| 1158 | iv = aad + TLS_AAD_SPACE_SIZE; |
| 1159 | |
| 1160 | /* Prepare IV */ |
| 1161 | err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE, |
| 1162 | iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, |
| 1163 | tls_ctx->rx.iv_size); |
| 1164 | if (err < 0) { |
| 1165 | kfree(mem); |
| 1166 | return err; |
| 1167 | } |
| 1168 | memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE); |
| 1169 | |
| 1170 | /* Prepare AAD */ |
| 1171 | tls_make_aad(aad, rxm->full_len - tls_ctx->rx.overhead_size, |
| 1172 | tls_ctx->rx.rec_seq, tls_ctx->rx.rec_seq_size, |
| 1173 | ctx->control); |
| 1174 | |
| 1175 | /* Prepare sgin */ |
| 1176 | sg_init_table(sgin, n_sgin); |
| 1177 | sg_set_buf(&sgin[0], aad, TLS_AAD_SPACE_SIZE); |
| 1178 | err = skb_to_sgvec(skb, &sgin[1], |
| 1179 | rxm->offset + tls_ctx->rx.prepend_size, |
| 1180 | rxm->full_len - tls_ctx->rx.prepend_size); |
| 1181 | if (err < 0) { |
| 1182 | kfree(mem); |
| 1183 | return err; |
| 1184 | } |
| 1185 | |
| 1186 | if (n_sgout) { |
| 1187 | if (out_iov) { |
| 1188 | sg_init_table(sgout, n_sgout); |
| 1189 | sg_set_buf(&sgout[0], aad, TLS_AAD_SPACE_SIZE); |
| 1190 | |
| 1191 | *chunk = 0; |
| 1192 | err = zerocopy_from_iter(sk, out_iov, data_len, &pages, |
| 1193 | chunk, &sgout[1], |
| 1194 | (n_sgout - 1), false); |
| 1195 | if (err < 0) |
| 1196 | goto fallback_to_reg_recv; |
| 1197 | } else if (out_sg) { |
| 1198 | memcpy(sgout, out_sg, n_sgout * sizeof(*sgout)); |
| 1199 | } else { |
| 1200 | goto fallback_to_reg_recv; |
| 1201 | } |
| 1202 | } else { |
| 1203 | fallback_to_reg_recv: |
| 1204 | sgout = sgin; |
| 1205 | pages = 0; |
| 1206 | *chunk = 0; |
| 1207 | *zc = false; |
| 1208 | } |
| 1209 | |
| 1210 | /* Prepare and submit AEAD request */ |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1211 | err = tls_do_decryption(sk, skb, sgin, sgout, iv, |
| 1212 | data_len, aead_req, *zc); |
| 1213 | if (err == -EINPROGRESS) |
| 1214 | return err; |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1215 | |
| 1216 | /* Release the pages in case iov was mapped to pages */ |
| 1217 | for (; pages > 0; pages--) |
| 1218 | put_page(sg_page(&sgout[pages])); |
| 1219 | |
| 1220 | kfree(mem); |
| 1221 | return err; |
| 1222 | } |
| 1223 | |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 1224 | static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb, |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1225 | struct iov_iter *dest, int *chunk, bool *zc) |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 1226 | { |
| 1227 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1228 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1229 | struct strp_msg *rxm = strp_msg(skb); |
| 1230 | int err = 0; |
| 1231 | |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 1232 | #ifdef CONFIG_TLS_DEVICE |
| 1233 | err = tls_device_decrypted(sk, skb); |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 1234 | if (err < 0) |
| 1235 | return err; |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 1236 | #endif |
| 1237 | if (!ctx->decrypted) { |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1238 | err = decrypt_internal(sk, skb, dest, NULL, chunk, zc); |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1239 | if (err < 0) { |
| 1240 | if (err == -EINPROGRESS) |
| 1241 | tls_advance_record_sn(sk, &tls_ctx->rx); |
| 1242 | |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 1243 | return err; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1244 | } |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 1245 | } else { |
| 1246 | *zc = false; |
| 1247 | } |
Boris Pismenny | dafb67f | 2018-07-13 14:33:40 +0300 | [diff] [blame] | 1248 | |
| 1249 | rxm->offset += tls_ctx->rx.prepend_size; |
| 1250 | rxm->full_len -= tls_ctx->rx.overhead_size; |
| 1251 | tls_advance_record_sn(sk, &tls_ctx->rx); |
| 1252 | ctx->decrypted = true; |
| 1253 | ctx->saved_data_ready(sk); |
| 1254 | |
| 1255 | return err; |
| 1256 | } |
| 1257 | |
| 1258 | int decrypt_skb(struct sock *sk, struct sk_buff *skb, |
| 1259 | struct scatterlist *sgout) |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1260 | { |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1261 | bool zc = true; |
| 1262 | int chunk; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1263 | |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1264 | return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb, |
| 1268 | unsigned int len) |
| 1269 | { |
| 1270 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1271 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1272 | |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1273 | if (skb) { |
| 1274 | struct strp_msg *rxm = strp_msg(skb); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1275 | |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1276 | if (len < rxm->full_len) { |
| 1277 | rxm->offset += len; |
| 1278 | rxm->full_len -= len; |
| 1279 | return false; |
| 1280 | } |
| 1281 | kfree_skb(skb); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | /* Finished with message */ |
| 1285 | ctx->recv_pkt = NULL; |
Doron Roberts-Kedes | 7170e60 | 2018-06-06 09:33:28 -0700 | [diff] [blame] | 1286 | __strp_unpause(&ctx->strp); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1287 | |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | int tls_sw_recvmsg(struct sock *sk, |
| 1292 | struct msghdr *msg, |
| 1293 | size_t len, |
| 1294 | int nonblock, |
| 1295 | int flags, |
| 1296 | int *addr_len) |
| 1297 | { |
| 1298 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1299 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1300 | unsigned char control; |
| 1301 | struct strp_msg *rxm; |
| 1302 | struct sk_buff *skb; |
| 1303 | ssize_t copied = 0; |
| 1304 | bool cmsg = false; |
Daniel Borkmann | 06030db | 2018-06-15 03:07:46 +0200 | [diff] [blame] | 1305 | int target, err = 0; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1306 | long timeo; |
Doron Roberts-Kedes | 0a26cf3 | 2018-07-25 14:48:21 -0700 | [diff] [blame] | 1307 | bool is_kvec = msg->msg_iter.type & ITER_KVEC; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1308 | int num_async = 0; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1309 | |
| 1310 | flags |= nonblock; |
| 1311 | |
| 1312 | if (unlikely(flags & MSG_ERRQUEUE)) |
| 1313 | return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR); |
| 1314 | |
| 1315 | lock_sock(sk); |
| 1316 | |
Daniel Borkmann | 06030db | 2018-06-15 03:07:46 +0200 | [diff] [blame] | 1317 | target = sock_rcvlowat(sk, flags & MSG_WAITALL, len); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1318 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 1319 | do { |
| 1320 | bool zc = false; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1321 | bool async = false; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1322 | int chunk = 0; |
| 1323 | |
| 1324 | skb = tls_wait_data(sk, flags, timeo, &err); |
| 1325 | if (!skb) |
| 1326 | goto recv_end; |
| 1327 | |
| 1328 | rxm = strp_msg(skb); |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1329 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1330 | if (!cmsg) { |
| 1331 | int cerr; |
| 1332 | |
| 1333 | cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE, |
| 1334 | sizeof(ctx->control), &ctx->control); |
| 1335 | cmsg = true; |
| 1336 | control = ctx->control; |
| 1337 | if (ctx->control != TLS_RECORD_TYPE_DATA) { |
| 1338 | if (cerr || msg->msg_flags & MSG_CTRUNC) { |
| 1339 | err = -EIO; |
| 1340 | goto recv_end; |
| 1341 | } |
| 1342 | } |
| 1343 | } else if (control != ctx->control) { |
| 1344 | goto recv_end; |
| 1345 | } |
| 1346 | |
| 1347 | if (!ctx->decrypted) { |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1348 | int to_copy = rxm->full_len - tls_ctx->rx.overhead_size; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1349 | |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1350 | if (!is_kvec && to_copy <= len && |
| 1351 | likely(!(flags & MSG_PEEK))) |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1352 | zc = true; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1353 | |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1354 | err = decrypt_skb_update(sk, skb, &msg->msg_iter, |
| 1355 | &chunk, &zc); |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1356 | if (err < 0 && err != -EINPROGRESS) { |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1357 | tls_err_abort(sk, EBADMSG); |
| 1358 | goto recv_end; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1359 | } |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1360 | |
| 1361 | if (err == -EINPROGRESS) { |
| 1362 | async = true; |
| 1363 | num_async++; |
| 1364 | goto pick_next_record; |
| 1365 | } |
| 1366 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1367 | ctx->decrypted = true; |
| 1368 | } |
| 1369 | |
| 1370 | if (!zc) { |
| 1371 | chunk = min_t(unsigned int, rxm->full_len, len); |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1372 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1373 | err = skb_copy_datagram_msg(skb, rxm->offset, msg, |
| 1374 | chunk); |
| 1375 | if (err < 0) |
| 1376 | goto recv_end; |
| 1377 | } |
| 1378 | |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1379 | pick_next_record: |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1380 | copied += chunk; |
| 1381 | len -= chunk; |
| 1382 | if (likely(!(flags & MSG_PEEK))) { |
| 1383 | u8 control = ctx->control; |
| 1384 | |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1385 | /* For async, drop current skb reference */ |
| 1386 | if (async) |
| 1387 | skb = NULL; |
| 1388 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1389 | if (tls_sw_advance_skb(sk, skb, chunk)) { |
| 1390 | /* Return full control message to |
| 1391 | * userspace before trying to parse |
| 1392 | * another message type |
| 1393 | */ |
| 1394 | msg->msg_flags |= MSG_EOR; |
| 1395 | if (control != TLS_RECORD_TYPE_DATA) |
| 1396 | goto recv_end; |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1397 | } else { |
| 1398 | break; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1399 | } |
Daniel Borkmann | 50c6b58 | 2018-09-14 23:00:55 +0200 | [diff] [blame] | 1400 | } else { |
| 1401 | /* MSG_PEEK right now cannot look beyond current skb |
| 1402 | * from strparser, meaning we cannot advance skb here |
| 1403 | * and thus unpause strparser since we'd loose original |
| 1404 | * one. |
| 1405 | */ |
| 1406 | break; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1407 | } |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1408 | |
Daniel Borkmann | 06030db | 2018-06-15 03:07:46 +0200 | [diff] [blame] | 1409 | /* If we have a new message from strparser, continue now. */ |
| 1410 | if (copied >= target && !ctx->recv_pkt) |
| 1411 | break; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1412 | } while (len); |
| 1413 | |
| 1414 | recv_end: |
Vakul Garg | 94524d8 | 2018-08-29 15:26:55 +0530 | [diff] [blame] | 1415 | if (num_async) { |
| 1416 | /* Wait for all previously submitted records to be decrypted */ |
| 1417 | smp_store_mb(ctx->async_notify, true); |
| 1418 | if (atomic_read(&ctx->decrypt_pending)) { |
| 1419 | err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait); |
| 1420 | if (err) { |
| 1421 | /* one of async decrypt failed */ |
| 1422 | tls_err_abort(sk, err); |
| 1423 | copied = 0; |
| 1424 | } |
| 1425 | } else { |
| 1426 | reinit_completion(&ctx->async_wait.completion); |
| 1427 | } |
| 1428 | WRITE_ONCE(ctx->async_notify, false); |
| 1429 | } |
| 1430 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1431 | release_sock(sk); |
| 1432 | return copied ? : err; |
| 1433 | } |
| 1434 | |
| 1435 | ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, |
| 1436 | struct pipe_inode_info *pipe, |
| 1437 | size_t len, unsigned int flags) |
| 1438 | { |
| 1439 | struct tls_context *tls_ctx = tls_get_ctx(sock->sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1440 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1441 | struct strp_msg *rxm = NULL; |
| 1442 | struct sock *sk = sock->sk; |
| 1443 | struct sk_buff *skb; |
| 1444 | ssize_t copied = 0; |
| 1445 | int err = 0; |
| 1446 | long timeo; |
| 1447 | int chunk; |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1448 | bool zc = false; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1449 | |
| 1450 | lock_sock(sk); |
| 1451 | |
| 1452 | timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); |
| 1453 | |
| 1454 | skb = tls_wait_data(sk, flags, timeo, &err); |
| 1455 | if (!skb) |
| 1456 | goto splice_read_end; |
| 1457 | |
| 1458 | /* splice does not support reading control messages */ |
| 1459 | if (ctx->control != TLS_RECORD_TYPE_DATA) { |
| 1460 | err = -ENOTSUPP; |
| 1461 | goto splice_read_end; |
| 1462 | } |
| 1463 | |
| 1464 | if (!ctx->decrypted) { |
Vakul Garg | 0b243d0 | 2018-08-10 20:46:41 +0530 | [diff] [blame] | 1465 | err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1466 | |
| 1467 | if (err < 0) { |
| 1468 | tls_err_abort(sk, EBADMSG); |
| 1469 | goto splice_read_end; |
| 1470 | } |
| 1471 | ctx->decrypted = true; |
| 1472 | } |
| 1473 | rxm = strp_msg(skb); |
| 1474 | |
| 1475 | chunk = min_t(unsigned int, rxm->full_len, len); |
| 1476 | copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags); |
| 1477 | if (copied < 0) |
| 1478 | goto splice_read_end; |
| 1479 | |
| 1480 | if (likely(!(flags & MSG_PEEK))) |
| 1481 | tls_sw_advance_skb(sk, skb, copied); |
| 1482 | |
| 1483 | splice_read_end: |
| 1484 | release_sock(sk); |
| 1485 | return copied ? : err; |
| 1486 | } |
| 1487 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1488 | unsigned int tls_sw_poll(struct file *file, struct socket *sock, |
| 1489 | struct poll_table_struct *wait) |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1490 | { |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1491 | unsigned int ret; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1492 | struct sock *sk = sock->sk; |
| 1493 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1494 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1495 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1496 | /* Grab POLLOUT and POLLHUP from the underlying socket */ |
| 1497 | ret = ctx->sk_poll(file, sock, wait); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1498 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1499 | /* Clear POLLIN bits, and set based on recv_pkt */ |
| 1500 | ret &= ~(POLLIN | POLLRDNORM); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1501 | if (ctx->recv_pkt) |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1502 | ret |= POLLIN | POLLRDNORM; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1503 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1504 | return ret; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | static int tls_read_size(struct strparser *strp, struct sk_buff *skb) |
| 1508 | { |
| 1509 | struct tls_context *tls_ctx = tls_get_ctx(strp->sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1510 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Kees Cook | 3463e51 | 2018-06-25 16:55:05 -0700 | [diff] [blame] | 1511 | char header[TLS_HEADER_SIZE + MAX_IV_SIZE]; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1512 | struct strp_msg *rxm = strp_msg(skb); |
| 1513 | size_t cipher_overhead; |
| 1514 | size_t data_len = 0; |
| 1515 | int ret; |
| 1516 | |
| 1517 | /* Verify that we have a full TLS header, or wait for more data */ |
| 1518 | if (rxm->offset + tls_ctx->rx.prepend_size > skb->len) |
| 1519 | return 0; |
| 1520 | |
Kees Cook | 3463e51 | 2018-06-25 16:55:05 -0700 | [diff] [blame] | 1521 | /* Sanity-check size of on-stack buffer. */ |
| 1522 | if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) { |
| 1523 | ret = -EINVAL; |
| 1524 | goto read_failure; |
| 1525 | } |
| 1526 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1527 | /* Linearize header to local buffer */ |
| 1528 | ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size); |
| 1529 | |
| 1530 | if (ret < 0) |
| 1531 | goto read_failure; |
| 1532 | |
| 1533 | ctx->control = header[0]; |
| 1534 | |
| 1535 | data_len = ((header[4] & 0xFF) | (header[3] << 8)); |
| 1536 | |
| 1537 | cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size; |
| 1538 | |
| 1539 | if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) { |
| 1540 | ret = -EMSGSIZE; |
| 1541 | goto read_failure; |
| 1542 | } |
| 1543 | if (data_len < cipher_overhead) { |
| 1544 | ret = -EBADMSG; |
| 1545 | goto read_failure; |
| 1546 | } |
| 1547 | |
Sabrina Dubroca | 86029d1 | 2018-09-12 17:44:42 +0200 | [diff] [blame] | 1548 | if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.info.version) || |
| 1549 | header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.info.version)) { |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1550 | ret = -EINVAL; |
| 1551 | goto read_failure; |
| 1552 | } |
| 1553 | |
Boris Pismenny | 4799ac8 | 2018-07-13 14:33:43 +0300 | [diff] [blame] | 1554 | #ifdef CONFIG_TLS_DEVICE |
| 1555 | handle_device_resync(strp->sk, TCP_SKB_CB(skb)->seq + rxm->offset, |
| 1556 | *(u64*)tls_ctx->rx.rec_seq); |
| 1557 | #endif |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1558 | return data_len + TLS_HEADER_SIZE; |
| 1559 | |
| 1560 | read_failure: |
| 1561 | tls_err_abort(strp->sk, ret); |
| 1562 | |
| 1563 | return ret; |
| 1564 | } |
| 1565 | |
| 1566 | static void tls_queue(struct strparser *strp, struct sk_buff *skb) |
| 1567 | { |
| 1568 | struct tls_context *tls_ctx = tls_get_ctx(strp->sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1569 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1570 | |
| 1571 | ctx->decrypted = false; |
| 1572 | |
| 1573 | ctx->recv_pkt = skb; |
| 1574 | strp_pause(strp); |
| 1575 | |
Vakul Garg | ad13acc | 2018-07-30 16:08:33 +0530 | [diff] [blame] | 1576 | ctx->saved_data_ready(strp->sk); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | static void tls_data_ready(struct sock *sk) |
| 1580 | { |
| 1581 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1582 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1583 | |
| 1584 | strp_data_ready(&ctx->strp); |
| 1585 | } |
| 1586 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1587 | void tls_sw_free_resources_tx(struct sock *sk) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1588 | { |
| 1589 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1590 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1591 | struct tls_rec *rec, *tmp; |
| 1592 | |
| 1593 | /* Wait for any pending async encryptions to complete */ |
| 1594 | smp_store_mb(ctx->async_notify, true); |
| 1595 | if (atomic_read(&ctx->encrypt_pending)) |
| 1596 | crypto_wait_req(-EINPROGRESS, &ctx->async_wait); |
| 1597 | |
| 1598 | cancel_delayed_work_sync(&ctx->tx_work.work); |
| 1599 | |
| 1600 | /* Tx whatever records we can transmit and abandon the rest */ |
| 1601 | tls_tx_records(sk, -1); |
| 1602 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 1603 | /* Free up un-sent records in tx_list. First, free |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1604 | * the partially sent record if any at head of tx_list. |
| 1605 | */ |
| 1606 | if (tls_ctx->partially_sent_record) { |
| 1607 | struct scatterlist *sg = tls_ctx->partially_sent_record; |
| 1608 | |
| 1609 | while (1) { |
| 1610 | put_page(sg_page(sg)); |
| 1611 | sk_mem_uncharge(sk, sg->length); |
| 1612 | |
| 1613 | if (sg_is_last(sg)) |
| 1614 | break; |
| 1615 | sg++; |
| 1616 | } |
| 1617 | |
| 1618 | tls_ctx->partially_sent_record = NULL; |
| 1619 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 1620 | rec = list_first_entry(&ctx->tx_list, |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1621 | struct tls_rec, list); |
Vakul Garg | b85135b | 2018-09-25 16:26:17 +0530 | [diff] [blame] | 1622 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 1623 | free_sg(sk, &rec->sg_plaintext_data[1], |
Vakul Garg | b85135b | 2018-09-25 16:26:17 +0530 | [diff] [blame] | 1624 | &rec->sg_plaintext_num_elem, |
| 1625 | &rec->sg_plaintext_size); |
| 1626 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1627 | list_del(&rec->list); |
| 1628 | kfree(rec); |
| 1629 | } |
| 1630 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 1631 | list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) { |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 1632 | free_sg(sk, &rec->sg_encrypted_data[1], |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1633 | &rec->sg_encrypted_num_elem, |
| 1634 | &rec->sg_encrypted_size); |
| 1635 | |
Vakul Garg | 80ece6a | 2018-09-26 16:22:08 +0530 | [diff] [blame] | 1636 | free_sg(sk, &rec->sg_plaintext_data[1], |
Vakul Garg | b85135b | 2018-09-25 16:26:17 +0530 | [diff] [blame] | 1637 | &rec->sg_plaintext_num_elem, |
| 1638 | &rec->sg_plaintext_size); |
| 1639 | |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1640 | list_del(&rec->list); |
| 1641 | kfree(rec); |
| 1642 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1643 | |
Vakul Garg | 201876b | 2018-07-24 16:54:27 +0530 | [diff] [blame] | 1644 | crypto_free_aead(ctx->aead_send); |
Vakul Garg | c774973 | 2018-09-25 20:21:51 +0530 | [diff] [blame] | 1645 | tls_free_open_rec(sk); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1646 | |
| 1647 | kfree(ctx); |
| 1648 | } |
| 1649 | |
Boris Pismenny | 39f56e1 | 2018-07-13 14:33:41 +0300 | [diff] [blame] | 1650 | void tls_sw_release_resources_rx(struct sock *sk) |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1651 | { |
| 1652 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1653 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1654 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1655 | if (ctx->aead_recv) { |
Vakul Garg | 201876b | 2018-07-24 16:54:27 +0530 | [diff] [blame] | 1656 | kfree_skb(ctx->recv_pkt); |
| 1657 | ctx->recv_pkt = NULL; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1658 | crypto_free_aead(ctx->aead_recv); |
| 1659 | strp_stop(&ctx->strp); |
| 1660 | write_lock_bh(&sk->sk_callback_lock); |
| 1661 | sk->sk_data_ready = ctx->saved_data_ready; |
| 1662 | write_unlock_bh(&sk->sk_callback_lock); |
| 1663 | release_sock(sk); |
| 1664 | strp_done(&ctx->strp); |
| 1665 | lock_sock(sk); |
| 1666 | } |
Boris Pismenny | 39f56e1 | 2018-07-13 14:33:41 +0300 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | void tls_sw_free_resources_rx(struct sock *sk) |
| 1670 | { |
| 1671 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1672 | struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx); |
| 1673 | |
| 1674 | tls_sw_release_resources_rx(sk); |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1675 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1676 | kfree(ctx); |
| 1677 | } |
| 1678 | |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 1679 | /* The work handler to transmitt the encrypted records in tx_list */ |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1680 | static void tx_work_handler(struct work_struct *work) |
| 1681 | { |
| 1682 | struct delayed_work *delayed_work = to_delayed_work(work); |
| 1683 | struct tx_work *tx_work = container_of(delayed_work, |
| 1684 | struct tx_work, work); |
| 1685 | struct sock *sk = tx_work->sk; |
| 1686 | struct tls_context *tls_ctx = tls_get_ctx(sk); |
| 1687 | struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx); |
| 1688 | |
| 1689 | if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) |
| 1690 | return; |
| 1691 | |
| 1692 | lock_sock(sk); |
| 1693 | tls_tx_records(sk, -1); |
| 1694 | release_sock(sk); |
| 1695 | } |
| 1696 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1697 | int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx) |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1698 | { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1699 | struct tls_crypto_info *crypto_info; |
| 1700 | struct tls12_crypto_info_aes_gcm_128 *gcm_128_info; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1701 | struct tls_sw_context_tx *sw_ctx_tx = NULL; |
| 1702 | struct tls_sw_context_rx *sw_ctx_rx = NULL; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1703 | struct cipher_context *cctx; |
| 1704 | struct crypto_aead **aead; |
| 1705 | struct strp_callbacks cb; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1706 | u16 nonce_size, tag_size, iv_size, rec_seq_size; |
| 1707 | char *iv, *rec_seq; |
| 1708 | int rc = 0; |
| 1709 | |
| 1710 | if (!ctx) { |
| 1711 | rc = -EINVAL; |
| 1712 | goto out; |
| 1713 | } |
| 1714 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1715 | if (tx) { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1716 | if (!ctx->priv_ctx_tx) { |
| 1717 | sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL); |
| 1718 | if (!sw_ctx_tx) { |
| 1719 | rc = -ENOMEM; |
| 1720 | goto out; |
| 1721 | } |
| 1722 | ctx->priv_ctx_tx = sw_ctx_tx; |
| 1723 | } else { |
| 1724 | sw_ctx_tx = |
| 1725 | (struct tls_sw_context_tx *)ctx->priv_ctx_tx; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1726 | } |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1727 | } else { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1728 | if (!ctx->priv_ctx_rx) { |
| 1729 | sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL); |
| 1730 | if (!sw_ctx_rx) { |
| 1731 | rc = -ENOMEM; |
| 1732 | goto out; |
| 1733 | } |
| 1734 | ctx->priv_ctx_rx = sw_ctx_rx; |
| 1735 | } else { |
| 1736 | sw_ctx_rx = |
| 1737 | (struct tls_sw_context_rx *)ctx->priv_ctx_rx; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1738 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1739 | } |
| 1740 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1741 | if (tx) { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1742 | crypto_init_wait(&sw_ctx_tx->async_wait); |
Sabrina Dubroca | 86029d1 | 2018-09-12 17:44:42 +0200 | [diff] [blame] | 1743 | crypto_info = &ctx->crypto_send.info; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1744 | cctx = &ctx->tx; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1745 | aead = &sw_ctx_tx->aead_send; |
Vakul Garg | 9932a29 | 2018-09-24 15:35:56 +0530 | [diff] [blame] | 1746 | INIT_LIST_HEAD(&sw_ctx_tx->tx_list); |
Vakul Garg | a42055e | 2018-09-21 09:46:13 +0530 | [diff] [blame] | 1747 | INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler); |
| 1748 | sw_ctx_tx->tx_work.sk = sk; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1749 | } else { |
Boris Pismenny | b190a58 | 2018-07-13 14:33:42 +0300 | [diff] [blame] | 1750 | crypto_init_wait(&sw_ctx_rx->async_wait); |
Sabrina Dubroca | 86029d1 | 2018-09-12 17:44:42 +0200 | [diff] [blame] | 1751 | crypto_info = &ctx->crypto_recv.info; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1752 | cctx = &ctx->rx; |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1753 | aead = &sw_ctx_rx->aead_recv; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1756 | switch (crypto_info->cipher_type) { |
| 1757 | case TLS_CIPHER_AES_GCM_128: { |
| 1758 | nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 1759 | tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE; |
| 1760 | iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE; |
| 1761 | iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv; |
| 1762 | rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE; |
| 1763 | rec_seq = |
| 1764 | ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq; |
| 1765 | gcm_128_info = |
| 1766 | (struct tls12_crypto_info_aes_gcm_128 *)crypto_info; |
| 1767 | break; |
| 1768 | } |
| 1769 | default: |
| 1770 | rc = -EINVAL; |
Sabrina Dubroca | cf6d43e | 2018-01-16 16:04:26 +0100 | [diff] [blame] | 1771 | goto free_priv; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
Kees Cook | b16520f | 2018-04-10 17:52:34 -0700 | [diff] [blame] | 1774 | /* Sanity-check the IV size for stack allocations. */ |
Kees Cook | 3463e51 | 2018-06-25 16:55:05 -0700 | [diff] [blame] | 1775 | if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) { |
Kees Cook | b16520f | 2018-04-10 17:52:34 -0700 | [diff] [blame] | 1776 | rc = -EINVAL; |
| 1777 | goto free_priv; |
| 1778 | } |
| 1779 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1780 | cctx->prepend_size = TLS_HEADER_SIZE + nonce_size; |
| 1781 | cctx->tag_size = tag_size; |
| 1782 | cctx->overhead_size = cctx->prepend_size + cctx->tag_size; |
| 1783 | cctx->iv_size = iv_size; |
| 1784 | cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE, |
| 1785 | GFP_KERNEL); |
| 1786 | if (!cctx->iv) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1787 | rc = -ENOMEM; |
Sabrina Dubroca | cf6d43e | 2018-01-16 16:04:26 +0100 | [diff] [blame] | 1788 | goto free_priv; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1789 | } |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1790 | memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE); |
| 1791 | memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size); |
| 1792 | cctx->rec_seq_size = rec_seq_size; |
zhong jiang | 969d509 | 2018-08-01 00:50:24 +0800 | [diff] [blame] | 1793 | cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1794 | if (!cctx->rec_seq) { |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1795 | rc = -ENOMEM; |
| 1796 | goto free_iv; |
| 1797 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1798 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1799 | if (!*aead) { |
| 1800 | *aead = crypto_alloc_aead("gcm(aes)", 0, 0); |
| 1801 | if (IS_ERR(*aead)) { |
| 1802 | rc = PTR_ERR(*aead); |
| 1803 | *aead = NULL; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1804 | goto free_rec_seq; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | ctx->push_pending_record = tls_sw_push_pending_record; |
| 1809 | |
Sabrina Dubroca | 7cba09c | 2018-09-12 17:44:41 +0200 | [diff] [blame] | 1810 | rc = crypto_aead_setkey(*aead, gcm_128_info->key, |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1811 | TLS_CIPHER_AES_GCM_128_KEY_SIZE); |
| 1812 | if (rc) |
| 1813 | goto free_aead; |
| 1814 | |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1815 | rc = crypto_aead_setauthsize(*aead, cctx->tag_size); |
| 1816 | if (rc) |
| 1817 | goto free_aead; |
| 1818 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1819 | if (sw_ctx_rx) { |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1820 | /* Set up strparser */ |
| 1821 | memset(&cb, 0, sizeof(cb)); |
| 1822 | cb.rcv_msg = tls_queue; |
| 1823 | cb.parse_msg = tls_read_size; |
| 1824 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1825 | strp_init(&sw_ctx_rx->strp, sk, &cb); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1826 | |
| 1827 | write_lock_bh(&sk->sk_callback_lock); |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1828 | sw_ctx_rx->saved_data_ready = sk->sk_data_ready; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1829 | sk->sk_data_ready = tls_data_ready; |
| 1830 | write_unlock_bh(&sk->sk_callback_lock); |
| 1831 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1832 | sw_ctx_rx->sk_poll = sk->sk_socket->ops->poll; |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1833 | |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1834 | strp_check_rcv(&sw_ctx_rx->strp); |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | goto out; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1838 | |
| 1839 | free_aead: |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1840 | crypto_free_aead(*aead); |
| 1841 | *aead = NULL; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1842 | free_rec_seq: |
Dave Watson | c46234e | 2018-03-22 10:10:35 -0700 | [diff] [blame] | 1843 | kfree(cctx->rec_seq); |
| 1844 | cctx->rec_seq = NULL; |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1845 | free_iv: |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1846 | kfree(cctx->iv); |
| 1847 | cctx->iv = NULL; |
Sabrina Dubroca | cf6d43e | 2018-01-16 16:04:26 +0100 | [diff] [blame] | 1848 | free_priv: |
Boris Pismenny | f66de3e | 2018-04-30 10:16:15 +0300 | [diff] [blame] | 1849 | if (tx) { |
| 1850 | kfree(ctx->priv_ctx_tx); |
| 1851 | ctx->priv_ctx_tx = NULL; |
| 1852 | } else { |
| 1853 | kfree(ctx->priv_ctx_rx); |
| 1854 | ctx->priv_ctx_rx = NULL; |
| 1855 | } |
Dave Watson | 3c4d755 | 2017-06-14 11:37:39 -0700 | [diff] [blame] | 1856 | out: |
| 1857 | return rc; |
| 1858 | } |