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