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