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