blob: ad64b9c8b6006593815bb366589b780f65e92ab2 [file] [log] [blame]
Dave Watson3c4d7552017-06-14 11:37:39 -07001/*
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 Fastabendd3b18ad32018-10-13 02:46:01 +02007 * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io
Dave Watson3c4d7552017-06-14 11:37:39 -07008 *
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 Watsonc46234e2018-03-22 10:10:35 -070038#include <linux/sched/signal.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070039#include <linux/module.h>
40#include <crypto/aead.h>
41
Dave Watsonc46234e2018-03-22 10:10:35 -070042#include <net/strparser.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070043#include <net/tls.h>
44
Kees Cookb16520f2018-04-10 17:52:34 -070045#define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE
46
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -070047static 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 */
118static int skb_nsg(struct sk_buff *skb, int offset, int len)
119{
120 return __skb_nsg(skb, offset, len, 0);
121}
122
Vakul Garg94524d82018-08-29 15:26:55 +0530123static void tls_decrypt_done(struct crypto_async_request *req, int err)
124{
125 struct aead_request *aead_req = (struct aead_request *)req;
Vakul Garg94524d82018-08-29 15:26:55 +0530126 struct scatterlist *sgout = aead_req->dst;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700127 struct tls_sw_context_rx *ctx;
128 struct tls_context *tls_ctx;
Vakul Garg94524d82018-08-29 15:26:55 +0530129 struct scatterlist *sg;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700130 struct sk_buff *skb;
Vakul Garg94524d82018-08-29 15:26:55 +0530131 unsigned int pages;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700132 int pending;
133
134 skb = (struct sk_buff *)req->data;
135 tls_ctx = tls_get_ctx(skb->sk);
136 ctx = tls_sw_ctx_rx(tls_ctx);
137 pending = atomic_dec_return(&ctx->decrypt_pending);
Vakul Garg94524d82018-08-29 15:26:55 +0530138
139 /* Propagate if there was an err */
140 if (err) {
141 ctx->async_wait.err = err;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700142 tls_err_abort(skb->sk, err);
Vakul Garg94524d82018-08-29 15:26:55 +0530143 }
144
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700145 /* After using skb->sk to propagate sk through crypto async callback
146 * we need to NULL it again.
147 */
148 skb->sk = NULL;
149
Vakul Garg94524d82018-08-29 15:26:55 +0530150 /* Release the skb, pages and memory allocated for crypto req */
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700151 kfree_skb(skb);
Vakul Garg94524d82018-08-29 15:26:55 +0530152
153 /* Skip the first S/G entry as it points to AAD */
154 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
155 if (!sg)
156 break;
157 put_page(sg_page(sg));
158 }
159
160 kfree(aead_req);
161
162 if (!pending && READ_ONCE(ctx->async_notify))
163 complete(&ctx->async_wait.completion);
164}
165
Dave Watsonc46234e2018-03-22 10:10:35 -0700166static int tls_do_decryption(struct sock *sk,
Vakul Garg94524d82018-08-29 15:26:55 +0530167 struct sk_buff *skb,
Dave Watsonc46234e2018-03-22 10:10:35 -0700168 struct scatterlist *sgin,
169 struct scatterlist *sgout,
170 char *iv_recv,
171 size_t data_len,
Vakul Garg94524d82018-08-29 15:26:55 +0530172 struct aead_request *aead_req,
173 bool async)
Dave Watsonc46234e2018-03-22 10:10:35 -0700174{
175 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300176 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -0700177 int ret;
Dave Watsonc46234e2018-03-22 10:10:35 -0700178
Vakul Garg0b243d02018-08-10 20:46:41 +0530179 aead_request_set_tfm(aead_req, ctx->aead_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700180 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
181 aead_request_set_crypt(aead_req, sgin, sgout,
182 data_len + tls_ctx->rx.tag_size,
183 (u8 *)iv_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700184
Vakul Garg94524d82018-08-29 15:26:55 +0530185 if (async) {
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700186 /* Using skb->sk to push sk through to crypto async callback
187 * handler. This allows propagating errors up to the socket
188 * if needed. It _must_ be cleared in the async handler
189 * before kfree_skb is called. We _know_ skb->sk is NULL
190 * because it is a clone from strparser.
191 */
192 skb->sk = sk;
Vakul Garg94524d82018-08-29 15:26:55 +0530193 aead_request_set_callback(aead_req,
194 CRYPTO_TFM_REQ_MAY_BACKLOG,
195 tls_decrypt_done, skb);
196 atomic_inc(&ctx->decrypt_pending);
197 } else {
198 aead_request_set_callback(aead_req,
199 CRYPTO_TFM_REQ_MAY_BACKLOG,
200 crypto_req_done, &ctx->async_wait);
201 }
202
203 ret = crypto_aead_decrypt(aead_req);
204 if (ret == -EINPROGRESS) {
205 if (async)
206 return ret;
207
208 ret = crypto_wait_req(ret, &ctx->async_wait);
209 }
210
211 if (async)
212 atomic_dec(&ctx->decrypt_pending);
213
Dave Watsonc46234e2018-03-22 10:10:35 -0700214 return ret;
215}
216
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200217static void tls_trim_both_msgs(struct sock *sk, int target_size)
Dave Watson3c4d7552017-06-14 11:37:39 -0700218{
219 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300220 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530221 struct tls_rec *rec = ctx->open_rec;
Dave Watson3c4d7552017-06-14 11:37:39 -0700222
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200223 sk_msg_trim(sk, &rec->msg_plaintext, target_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700224 if (target_size > 0)
Dave Watsondbe42552018-03-22 10:10:06 -0700225 target_size += tls_ctx->tx.overhead_size;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200226 sk_msg_trim(sk, &rec->msg_encrypted, target_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700227}
228
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200229static int tls_alloc_encrypted_msg(struct sock *sk, int len)
Dave Watson3c4d7552017-06-14 11:37:39 -0700230{
231 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300232 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530233 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200234 struct sk_msg *msg_en = &rec->msg_encrypted;
Dave Watson3c4d7552017-06-14 11:37:39 -0700235
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200236 return sk_msg_alloc(sk, msg_en, len, 0);
Dave Watson3c4d7552017-06-14 11:37:39 -0700237}
238
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200239static int tls_clone_plaintext_msg(struct sock *sk, int required)
Dave Watson3c4d7552017-06-14 11:37:39 -0700240{
241 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300242 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530243 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200244 struct sk_msg *msg_pl = &rec->msg_plaintext;
245 struct sk_msg *msg_en = &rec->msg_encrypted;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530246 int skip, len;
Dave Watson3c4d7552017-06-14 11:37:39 -0700247
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200248 /* We add page references worth len bytes from encrypted sg
249 * at the end of plaintext sg. It is guaranteed that msg_en
Vakul Garg4e6d4722018-09-30 08:04:35 +0530250 * has enough required room (ensured by caller).
251 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200252 len = required - msg_pl->sg.size;
Vakul Garg52ea9922018-09-06 21:41:40 +0530253
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200254 /* Skip initial bytes in msg_en's data to be able to use
255 * same offset of both plain and encrypted data.
Vakul Garg4e6d4722018-09-30 08:04:35 +0530256 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200257 skip = tls_ctx->tx.prepend_size + msg_pl->sg.size;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530258
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200259 return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
Dave Watson3c4d7552017-06-14 11:37:39 -0700260}
261
John Fastabendd3b18ad32018-10-13 02:46:01 +0200262static struct tls_rec *tls_get_rec(struct sock *sk)
263{
264 struct tls_context *tls_ctx = tls_get_ctx(sk);
265 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
266 struct sk_msg *msg_pl, *msg_en;
267 struct tls_rec *rec;
268 int mem_size;
269
270 mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
271
272 rec = kzalloc(mem_size, sk->sk_allocation);
273 if (!rec)
274 return NULL;
275
276 msg_pl = &rec->msg_plaintext;
277 msg_en = &rec->msg_encrypted;
278
279 sk_msg_init(msg_pl);
280 sk_msg_init(msg_en);
281
282 sg_init_table(rec->sg_aead_in, 2);
283 sg_set_buf(&rec->sg_aead_in[0], rec->aad_space,
284 sizeof(rec->aad_space));
285 sg_unmark_end(&rec->sg_aead_in[1]);
286
287 sg_init_table(rec->sg_aead_out, 2);
288 sg_set_buf(&rec->sg_aead_out[0], rec->aad_space,
289 sizeof(rec->aad_space));
290 sg_unmark_end(&rec->sg_aead_out[1]);
291
292 return rec;
293}
294
295static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
296{
297 sk_msg_free(sk, &rec->msg_encrypted);
298 sk_msg_free(sk, &rec->msg_plaintext);
299 kfree(rec);
300}
301
Vakul Gargc7749732018-09-25 20:21:51 +0530302static void tls_free_open_rec(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -0700303{
304 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300305 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530306 struct tls_rec *rec = ctx->open_rec;
Dave Watson3c4d7552017-06-14 11:37:39 -0700307
John Fastabendd3b18ad32018-10-13 02:46:01 +0200308 if (rec) {
309 tls_free_rec(sk, rec);
310 ctx->open_rec = NULL;
311 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700312}
313
Vakul Garga42055e2018-09-21 09:46:13 +0530314int tls_tx_records(struct sock *sk, int flags)
315{
316 struct tls_context *tls_ctx = tls_get_ctx(sk);
317 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
318 struct tls_rec *rec, *tmp;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200319 struct sk_msg *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530320 int tx_flags, rc = 0;
321
322 if (tls_is_partially_sent_record(tls_ctx)) {
Vakul Garg9932a292018-09-24 15:35:56 +0530323 rec = list_first_entry(&ctx->tx_list,
Vakul Garga42055e2018-09-21 09:46:13 +0530324 struct tls_rec, list);
325
326 if (flags == -1)
327 tx_flags = rec->tx_flags;
328 else
329 tx_flags = flags;
330
331 rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
332 if (rc)
333 goto tx_err;
334
335 /* Full record has been transmitted.
Vakul Garg9932a292018-09-24 15:35:56 +0530336 * Remove the head of tx_list
Vakul Garga42055e2018-09-21 09:46:13 +0530337 */
Vakul Garga42055e2018-09-21 09:46:13 +0530338 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200339 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +0530340 kfree(rec);
341 }
342
Vakul Garg9932a292018-09-24 15:35:56 +0530343 /* Tx all ready records */
344 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
345 if (READ_ONCE(rec->tx_ready)) {
Vakul Garga42055e2018-09-21 09:46:13 +0530346 if (flags == -1)
347 tx_flags = rec->tx_flags;
348 else
349 tx_flags = flags;
350
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200351 msg_en = &rec->msg_encrypted;
Vakul Garga42055e2018-09-21 09:46:13 +0530352 rc = tls_push_sg(sk, tls_ctx,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200353 &msg_en->sg.data[msg_en->sg.curr],
Vakul Garga42055e2018-09-21 09:46:13 +0530354 0, tx_flags);
355 if (rc)
356 goto tx_err;
357
Vakul Garga42055e2018-09-21 09:46:13 +0530358 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200359 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +0530360 kfree(rec);
361 } else {
362 break;
363 }
364 }
365
366tx_err:
367 if (rc < 0 && rc != -EAGAIN)
368 tls_err_abort(sk, EBADMSG);
369
370 return rc;
371}
372
373static void tls_encrypt_done(struct crypto_async_request *req, int err)
374{
375 struct aead_request *aead_req = (struct aead_request *)req;
376 struct sock *sk = req->data;
377 struct tls_context *tls_ctx = tls_get_ctx(sk);
378 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200379 struct scatterlist *sge;
380 struct sk_msg *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530381 struct tls_rec *rec;
382 bool ready = false;
383 int pending;
384
385 rec = container_of(aead_req, struct tls_rec, aead_req);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200386 msg_en = &rec->msg_encrypted;
Vakul Garga42055e2018-09-21 09:46:13 +0530387
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200388 sge = sk_msg_elem(msg_en, msg_en->sg.curr);
389 sge->offset -= tls_ctx->tx.prepend_size;
390 sge->length += tls_ctx->tx.prepend_size;
Vakul Garga42055e2018-09-21 09:46:13 +0530391
Vakul Garg80ece6a2018-09-26 16:22:08 +0530392 /* Check if error is previously set on socket */
Vakul Garga42055e2018-09-21 09:46:13 +0530393 if (err || sk->sk_err) {
Vakul Garga42055e2018-09-21 09:46:13 +0530394 rec = NULL;
395
396 /* If err is already set on socket, return the same code */
397 if (sk->sk_err) {
398 ctx->async_wait.err = sk->sk_err;
399 } else {
400 ctx->async_wait.err = err;
401 tls_err_abort(sk, err);
402 }
403 }
404
Vakul Garg9932a292018-09-24 15:35:56 +0530405 if (rec) {
406 struct tls_rec *first_rec;
407
408 /* Mark the record as ready for transmission */
409 smp_store_mb(rec->tx_ready, true);
410
411 /* If received record is at head of tx_list, schedule tx */
412 first_rec = list_first_entry(&ctx->tx_list,
413 struct tls_rec, list);
414 if (rec == first_rec)
415 ready = true;
416 }
Vakul Garga42055e2018-09-21 09:46:13 +0530417
418 pending = atomic_dec_return(&ctx->encrypt_pending);
419
420 if (!pending && READ_ONCE(ctx->async_notify))
421 complete(&ctx->async_wait.completion);
422
423 if (!ready)
424 return;
425
426 /* Schedule the transmission */
427 if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200428 schedule_delayed_work(&ctx->tx_work.work, 1);
Vakul Garga42055e2018-09-21 09:46:13 +0530429}
430
431static int tls_do_encryption(struct sock *sk,
432 struct tls_context *tls_ctx,
Daniel Borkmanna447da72018-06-15 03:07:45 +0200433 struct tls_sw_context_tx *ctx,
434 struct aead_request *aead_req,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200435 size_t data_len, u32 start)
Dave Watson3c4d7552017-06-14 11:37:39 -0700436{
Vakul Garga42055e2018-09-21 09:46:13 +0530437 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200438 struct sk_msg *msg_en = &rec->msg_encrypted;
439 struct scatterlist *sge = sk_msg_elem(msg_en, start);
Dave Watson3c4d7552017-06-14 11:37:39 -0700440 int rc;
441
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200442 sge->offset += tls_ctx->tx.prepend_size;
443 sge->length -= tls_ctx->tx.prepend_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700444
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200445 msg_en->sg.curr = start;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530446
Dave Watson3c4d7552017-06-14 11:37:39 -0700447 aead_request_set_tfm(aead_req, ctx->aead_send);
448 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200449 aead_request_set_crypt(aead_req, rec->sg_aead_in,
450 rec->sg_aead_out,
Dave Watsondbe42552018-03-22 10:10:06 -0700451 data_len, tls_ctx->tx.iv);
Vakul Garga54667f2018-01-31 21:34:37 +0530452
453 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Vakul Garga42055e2018-09-21 09:46:13 +0530454 tls_encrypt_done, sk);
Vakul Garga54667f2018-01-31 21:34:37 +0530455
Vakul Garg9932a292018-09-24 15:35:56 +0530456 /* Add the record in tx_list */
457 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +0530458 atomic_inc(&ctx->encrypt_pending);
Dave Watson3c4d7552017-06-14 11:37:39 -0700459
Vakul Garga42055e2018-09-21 09:46:13 +0530460 rc = crypto_aead_encrypt(aead_req);
461 if (!rc || rc != -EINPROGRESS) {
462 atomic_dec(&ctx->encrypt_pending);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200463 sge->offset -= tls_ctx->tx.prepend_size;
464 sge->length += tls_ctx->tx.prepend_size;
Vakul Garga42055e2018-09-21 09:46:13 +0530465 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700466
Vakul Garg9932a292018-09-24 15:35:56 +0530467 if (!rc) {
468 WRITE_ONCE(rec->tx_ready, true);
469 } else if (rc != -EINPROGRESS) {
470 list_del(&rec->list);
Vakul Garga42055e2018-09-21 09:46:13 +0530471 return rc;
Vakul Garg9932a292018-09-24 15:35:56 +0530472 }
Vakul Garga42055e2018-09-21 09:46:13 +0530473
474 /* Unhook the record from context if encryption is not failure */
475 ctx->open_rec = NULL;
476 tls_advance_record_sn(sk, &tls_ctx->tx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700477 return rc;
478}
479
John Fastabendd3b18ad32018-10-13 02:46:01 +0200480static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
481 struct tls_rec **to, struct sk_msg *msg_opl,
482 struct sk_msg *msg_oen, u32 split_point,
483 u32 tx_overhead_size, u32 *orig_end)
484{
485 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
486 struct scatterlist *sge, *osge, *nsge;
487 u32 orig_size = msg_opl->sg.size;
488 struct scatterlist tmp = { };
489 struct sk_msg *msg_npl;
490 struct tls_rec *new;
491 int ret;
492
493 new = tls_get_rec(sk);
494 if (!new)
495 return -ENOMEM;
496 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
497 tx_overhead_size, 0);
498 if (ret < 0) {
499 tls_free_rec(sk, new);
500 return ret;
501 }
502
503 *orig_end = msg_opl->sg.end;
504 i = msg_opl->sg.start;
505 sge = sk_msg_elem(msg_opl, i);
506 while (apply && sge->length) {
507 if (sge->length > apply) {
508 u32 len = sge->length - apply;
509
510 get_page(sg_page(sge));
511 sg_set_page(&tmp, sg_page(sge), len,
512 sge->offset + apply);
513 sge->length = apply;
514 bytes += apply;
515 apply = 0;
516 } else {
517 apply -= sge->length;
518 bytes += sge->length;
519 }
520
521 sk_msg_iter_var_next(i);
522 if (i == msg_opl->sg.end)
523 break;
524 sge = sk_msg_elem(msg_opl, i);
525 }
526
527 msg_opl->sg.end = i;
528 msg_opl->sg.curr = i;
529 msg_opl->sg.copybreak = 0;
530 msg_opl->apply_bytes = 0;
531 msg_opl->sg.size = bytes;
532
533 msg_npl = &new->msg_plaintext;
534 msg_npl->apply_bytes = apply;
535 msg_npl->sg.size = orig_size - bytes;
536
537 j = msg_npl->sg.start;
538 nsge = sk_msg_elem(msg_npl, j);
539 if (tmp.length) {
540 memcpy(nsge, &tmp, sizeof(*nsge));
541 sk_msg_iter_var_next(j);
542 nsge = sk_msg_elem(msg_npl, j);
543 }
544
545 osge = sk_msg_elem(msg_opl, i);
546 while (osge->length) {
547 memcpy(nsge, osge, sizeof(*nsge));
548 sg_unmark_end(nsge);
549 sk_msg_iter_var_next(i);
550 sk_msg_iter_var_next(j);
551 if (i == *orig_end)
552 break;
553 osge = sk_msg_elem(msg_opl, i);
554 nsge = sk_msg_elem(msg_npl, j);
555 }
556
557 msg_npl->sg.end = j;
558 msg_npl->sg.curr = j;
559 msg_npl->sg.copybreak = 0;
560
561 *to = new;
562 return 0;
563}
564
565static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
566 struct tls_rec *from, u32 orig_end)
567{
568 struct sk_msg *msg_npl = &from->msg_plaintext;
569 struct sk_msg *msg_opl = &to->msg_plaintext;
570 struct scatterlist *osge, *nsge;
571 u32 i, j;
572
573 i = msg_opl->sg.end;
574 sk_msg_iter_var_prev(i);
575 j = msg_npl->sg.start;
576
577 osge = sk_msg_elem(msg_opl, i);
578 nsge = sk_msg_elem(msg_npl, j);
579
580 if (sg_page(osge) == sg_page(nsge) &&
581 osge->offset + osge->length == nsge->offset) {
582 osge->length += nsge->length;
583 put_page(sg_page(nsge));
584 }
585
586 msg_opl->sg.end = orig_end;
587 msg_opl->sg.curr = orig_end;
588 msg_opl->sg.copybreak = 0;
589 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
590 msg_opl->sg.size += msg_npl->sg.size;
591
592 sk_msg_free(sk, &to->msg_encrypted);
593 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
594
595 kfree(from);
596}
597
Dave Watson3c4d7552017-06-14 11:37:39 -0700598static int tls_push_record(struct sock *sk, int flags,
599 unsigned char record_type)
600{
601 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300602 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200603 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
604 u32 i, split_point, uninitialized_var(orig_end);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200605 struct sk_msg *msg_pl, *msg_en;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200606 struct aead_request *req;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200607 bool split;
Dave Watson3c4d7552017-06-14 11:37:39 -0700608 int rc;
609
Vakul Garga42055e2018-09-21 09:46:13 +0530610 if (!rec)
611 return 0;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200612
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200613 msg_pl = &rec->msg_plaintext;
614 msg_en = &rec->msg_encrypted;
615
John Fastabendd3b18ad32018-10-13 02:46:01 +0200616 split_point = msg_pl->apply_bytes;
617 split = split_point && split_point < msg_pl->sg.size;
618 if (split) {
619 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
620 split_point, tls_ctx->tx.overhead_size,
621 &orig_end);
622 if (rc < 0)
623 return rc;
624 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
625 tls_ctx->tx.overhead_size);
626 }
627
Vakul Garga42055e2018-09-21 09:46:13 +0530628 rec->tx_flags = flags;
629 req = &rec->aead_req;
Dave Watson3c4d7552017-06-14 11:37:39 -0700630
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200631 i = msg_pl->sg.end;
632 sk_msg_iter_var_prev(i);
633 sg_mark_end(sk_msg_elem(msg_pl, i));
Vakul Garga42055e2018-09-21 09:46:13 +0530634
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200635 i = msg_pl->sg.start;
636 sg_chain(rec->sg_aead_in, 2, rec->inplace_crypto ?
637 &msg_en->sg.data[i] : &msg_pl->sg.data[i]);
638
639 i = msg_en->sg.end;
640 sk_msg_iter_var_prev(i);
641 sg_mark_end(sk_msg_elem(msg_en, i));
642
643 i = msg_en->sg.start;
644 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
645
646 tls_make_aad(rec->aad_space, msg_pl->sg.size,
Dave Watsondbe42552018-03-22 10:10:06 -0700647 tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size,
Dave Watson3c4d7552017-06-14 11:37:39 -0700648 record_type);
649
650 tls_fill_prepend(tls_ctx,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200651 page_address(sg_page(&msg_en->sg.data[i])) +
652 msg_en->sg.data[i].offset, msg_pl->sg.size,
653 record_type);
Dave Watson3c4d7552017-06-14 11:37:39 -0700654
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200655 tls_ctx->pending_open_record_frags = false;
Dave Watson3c4d7552017-06-14 11:37:39 -0700656
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200657 rc = tls_do_encryption(sk, tls_ctx, ctx, req, msg_pl->sg.size, i);
Dave Watson3c4d7552017-06-14 11:37:39 -0700658 if (rc < 0) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200659 if (rc != -EINPROGRESS) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200660 tls_err_abort(sk, EBADMSG);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200661 if (split) {
662 tls_ctx->pending_open_record_frags = true;
663 tls_merge_open_record(sk, rec, tmp, orig_end);
664 }
665 }
Vakul Garga42055e2018-09-21 09:46:13 +0530666 return rc;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200667 } else if (split) {
668 msg_pl = &tmp->msg_plaintext;
669 msg_en = &tmp->msg_encrypted;
670 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
671 tls_ctx->tx.overhead_size);
672 tls_ctx->pending_open_record_frags = true;
673 ctx->open_rec = tmp;
Dave Watson3c4d7552017-06-14 11:37:39 -0700674 }
675
Vakul Garg9932a292018-09-24 15:35:56 +0530676 return tls_tx_records(sk, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700677}
678
John Fastabendd3b18ad32018-10-13 02:46:01 +0200679static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
680 bool full_record, u8 record_type,
681 size_t *copied, int flags)
Dave Watson3c4d7552017-06-14 11:37:39 -0700682{
683 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300684 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200685 struct sk_msg msg_redir = { };
686 struct sk_psock *psock;
687 struct sock *sk_redir;
Vakul Garga42055e2018-09-21 09:46:13 +0530688 struct tls_rec *rec;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200689 int err = 0, send;
690 bool enospc;
Vakul Garga42055e2018-09-21 09:46:13 +0530691
John Fastabendd3b18ad32018-10-13 02:46:01 +0200692 psock = sk_psock_get(sk);
693 if (!psock)
694 return tls_push_record(sk, flags, record_type);
695more_data:
696 enospc = sk_msg_full(msg);
697 if (psock->eval == __SK_NONE)
698 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
699 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
700 !enospc && !full_record) {
701 err = -ENOSPC;
702 goto out_err;
703 }
704 msg->cork_bytes = 0;
705 send = msg->sg.size;
706 if (msg->apply_bytes && msg->apply_bytes < send)
707 send = msg->apply_bytes;
Vakul Garga42055e2018-09-21 09:46:13 +0530708
John Fastabendd3b18ad32018-10-13 02:46:01 +0200709 switch (psock->eval) {
710 case __SK_PASS:
711 err = tls_push_record(sk, flags, record_type);
712 if (err < 0) {
713 *copied -= sk_msg_free(sk, msg);
714 tls_free_open_rec(sk);
715 goto out_err;
716 }
717 break;
718 case __SK_REDIRECT:
719 sk_redir = psock->sk_redir;
720 memcpy(&msg_redir, msg, sizeof(*msg));
721 if (msg->apply_bytes < send)
722 msg->apply_bytes = 0;
723 else
724 msg->apply_bytes -= send;
725 sk_msg_return_zero(sk, msg, send);
726 msg->sg.size -= send;
727 release_sock(sk);
728 err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
729 lock_sock(sk);
730 if (err < 0) {
731 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
732 msg->sg.size = 0;
733 }
734 if (msg->sg.size == 0)
735 tls_free_open_rec(sk);
736 break;
737 case __SK_DROP:
738 default:
739 sk_msg_free_partial(sk, msg, send);
740 if (msg->apply_bytes < send)
741 msg->apply_bytes = 0;
742 else
743 msg->apply_bytes -= send;
744 if (msg->sg.size == 0)
745 tls_free_open_rec(sk);
746 *copied -= send;
747 err = -EACCES;
748 }
Vakul Garga42055e2018-09-21 09:46:13 +0530749
John Fastabendd3b18ad32018-10-13 02:46:01 +0200750 if (likely(!err)) {
751 bool reset_eval = !ctx->open_rec;
752
753 rec = ctx->open_rec;
754 if (rec) {
755 msg = &rec->msg_plaintext;
756 if (!msg->apply_bytes)
757 reset_eval = true;
758 }
759 if (reset_eval) {
760 psock->eval = __SK_NONE;
761 if (psock->sk_redir) {
762 sock_put(psock->sk_redir);
763 psock->sk_redir = NULL;
764 }
765 }
766 if (rec)
767 goto more_data;
768 }
769 out_err:
770 sk_psock_put(sk, psock);
771 return err;
772}
773
774static int tls_sw_push_pending_record(struct sock *sk, int flags)
775{
776 struct tls_context *tls_ctx = tls_get_ctx(sk);
777 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
778 struct tls_rec *rec = ctx->open_rec;
779 struct sk_msg *msg_pl;
780 size_t copied;
781
Vakul Garga42055e2018-09-21 09:46:13 +0530782 if (!rec)
John Fastabendd3b18ad32018-10-13 02:46:01 +0200783 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530784
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200785 msg_pl = &rec->msg_plaintext;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200786 copied = msg_pl->sg.size;
787 if (!copied)
788 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530789
John Fastabendd3b18ad32018-10-13 02:46:01 +0200790 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
791 &copied, flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530792}
793
794int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
795{
Dave Watson3c4d7552017-06-14 11:37:39 -0700796 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
Vakul Garga42055e2018-09-21 09:46:13 +0530797 struct tls_context *tls_ctx = tls_get_ctx(sk);
798 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
799 struct crypto_tfm *tfm = crypto_aead_tfm(ctx->aead_send);
800 bool async_capable = tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
801 unsigned char record_type = TLS_RECORD_TYPE_DATA;
David Howells00e23702018-10-22 13:07:28 +0100802 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Dave Watson3c4d7552017-06-14 11:37:39 -0700803 bool eor = !(msg->msg_flags & MSG_MORE);
804 size_t try_to_copy, copied = 0;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200805 struct sk_msg *msg_pl, *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530806 struct tls_rec *rec;
807 int required_size;
808 int num_async = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700809 bool full_record;
Vakul Garga42055e2018-09-21 09:46:13 +0530810 int record_room;
811 int num_zc = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700812 int orig_size;
Vakul Garg4128c0c2018-09-24 16:09:49 +0530813 int ret = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700814
815 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
816 return -ENOTSUPP;
817
818 lock_sock(sk);
819
Vakul Garga42055e2018-09-21 09:46:13 +0530820 /* Wait till there is any pending write on socket */
821 if (unlikely(sk->sk_write_pending)) {
822 ret = wait_on_pending_writer(sk, &timeo);
823 if (unlikely(ret))
824 goto send_end;
825 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700826
827 if (unlikely(msg->msg_controllen)) {
828 ret = tls_proccess_cmsg(sk, msg, &record_type);
Vakul Garga42055e2018-09-21 09:46:13 +0530829 if (ret) {
830 if (ret == -EINPROGRESS)
831 num_async++;
832 else if (ret != -EAGAIN)
833 goto send_end;
834 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700835 }
836
837 while (msg_data_left(msg)) {
838 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +0100839 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -0700840 goto send_end;
841 }
842
John Fastabendd3b18ad32018-10-13 02:46:01 +0200843 if (ctx->open_rec)
844 rec = ctx->open_rec;
845 else
846 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +0530847 if (!rec) {
848 ret = -ENOMEM;
849 goto send_end;
850 }
851
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200852 msg_pl = &rec->msg_plaintext;
853 msg_en = &rec->msg_encrypted;
854
855 orig_size = msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700856 full_record = false;
857 try_to_copy = msg_data_left(msg);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200858 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700859 if (try_to_copy >= record_room) {
860 try_to_copy = record_room;
861 full_record = true;
862 }
863
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200864 required_size = msg_pl->sg.size + try_to_copy +
Dave Watsondbe42552018-03-22 10:10:06 -0700865 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700866
867 if (!sk_stream_memory_free(sk))
868 goto wait_for_sndbuf;
Vakul Garga42055e2018-09-21 09:46:13 +0530869
Dave Watson3c4d7552017-06-14 11:37:39 -0700870alloc_encrypted:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200871 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700872 if (ret) {
873 if (ret != -ENOSPC)
874 goto wait_for_memory;
875
876 /* Adjust try_to_copy according to the amount that was
877 * actually allocated. The difference is due
878 * to max sg elements limit
879 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200880 try_to_copy -= required_size - msg_en->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700881 full_record = true;
882 }
Vakul Garga42055e2018-09-21 09:46:13 +0530883
884 if (!is_kvec && (full_record || eor) && !async_capable) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200885 u32 first = msg_pl->sg.end;
886
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200887 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
888 msg_pl, try_to_copy);
Dave Watson3c4d7552017-06-14 11:37:39 -0700889 if (ret)
890 goto fallback_to_reg_send;
891
Vakul Garg4e6d4722018-09-30 08:04:35 +0530892 rec->inplace_crypto = 0;
893
Vakul Garga42055e2018-09-21 09:46:13 +0530894 num_zc++;
Dave Watson3c4d7552017-06-14 11:37:39 -0700895 copied += try_to_copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200896
897 sk_msg_sg_copy_set(msg_pl, first);
898 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
899 record_type, &copied,
900 msg->msg_flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530901 if (ret) {
902 if (ret == -EINPROGRESS)
903 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200904 else if (ret == -ENOMEM)
905 goto wait_for_memory;
906 else if (ret == -ENOSPC)
907 goto rollback_iter;
Vakul Garga42055e2018-09-21 09:46:13 +0530908 else if (ret != -EAGAIN)
909 goto send_end;
910 }
Doron Roberts-Kedes5a3611e2018-07-26 07:59:35 -0700911 continue;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200912rollback_iter:
913 copied -= try_to_copy;
914 sk_msg_sg_copy_clear(msg_pl, first);
915 iov_iter_revert(&msg->msg_iter,
916 msg_pl->sg.size - orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700917fallback_to_reg_send:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200918 sk_msg_trim(sk, msg_pl, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700919 }
920
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200921 required_size = msg_pl->sg.size + try_to_copy;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530922
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200923 ret = tls_clone_plaintext_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700924 if (ret) {
925 if (ret != -ENOSPC)
Vakul Garg4e6d4722018-09-30 08:04:35 +0530926 goto send_end;
Dave Watson3c4d7552017-06-14 11:37:39 -0700927
928 /* Adjust try_to_copy according to the amount that was
929 * actually allocated. The difference is due
930 * to max sg elements limit
931 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200932 try_to_copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700933 full_record = true;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200934 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
935 tls_ctx->tx.overhead_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700936 }
937
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200938 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_pl,
939 try_to_copy);
940 if (ret < 0)
Dave Watson3c4d7552017-06-14 11:37:39 -0700941 goto trim_sgl;
942
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200943 /* Open records defined only if successfully copied, otherwise
944 * we would trim the sg but not reset the open record frags.
945 */
946 tls_ctx->pending_open_record_frags = true;
Dave Watson3c4d7552017-06-14 11:37:39 -0700947 copied += try_to_copy;
948 if (full_record || eor) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200949 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
950 record_type, &copied,
951 msg->msg_flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700952 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +0530953 if (ret == -EINPROGRESS)
954 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200955 else if (ret == -ENOMEM)
956 goto wait_for_memory;
957 else if (ret != -EAGAIN) {
958 if (ret == -ENOSPC)
959 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530960 goto send_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200961 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700962 }
963 }
964
965 continue;
966
967wait_for_sndbuf:
968 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
969wait_for_memory:
970 ret = sk_stream_wait_memory(sk, &timeo);
971 if (ret) {
972trim_sgl:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200973 tls_trim_both_msgs(sk, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700974 goto send_end;
975 }
976
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200977 if (msg_en->sg.size < required_size)
Dave Watson3c4d7552017-06-14 11:37:39 -0700978 goto alloc_encrypted;
Dave Watson3c4d7552017-06-14 11:37:39 -0700979 }
980
Vakul Garga42055e2018-09-21 09:46:13 +0530981 if (!num_async) {
982 goto send_end;
983 } else if (num_zc) {
984 /* Wait for pending encryptions to get completed */
985 smp_store_mb(ctx->async_notify, true);
986
987 if (atomic_read(&ctx->encrypt_pending))
988 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
989 else
990 reinit_completion(&ctx->async_wait.completion);
991
992 WRITE_ONCE(ctx->async_notify, false);
993
994 if (ctx->async_wait.err) {
995 ret = ctx->async_wait.err;
996 copied = 0;
997 }
998 }
999
1000 /* Transmit if any encryptions have completed */
1001 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1002 cancel_delayed_work(&ctx->tx_work.work);
1003 tls_tx_records(sk, msg->msg_flags);
1004 }
1005
Dave Watson3c4d7552017-06-14 11:37:39 -07001006send_end:
1007 ret = sk_stream_error(sk, msg->msg_flags, ret);
1008
1009 release_sock(sk);
1010 return copied ? copied : ret;
1011}
1012
1013int tls_sw_sendpage(struct sock *sk, struct page *page,
1014 int offset, size_t size, int flags)
1015{
Vakul Garga42055e2018-09-21 09:46:13 +05301016 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
Dave Watson3c4d7552017-06-14 11:37:39 -07001017 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001018 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -07001019 unsigned char record_type = TLS_RECORD_TYPE_DATA;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001020 struct sk_msg *msg_pl;
Vakul Garga42055e2018-09-21 09:46:13 +05301021 struct tls_rec *rec;
1022 int num_async = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001023 size_t copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001024 bool full_record;
1025 int record_room;
Vakul Garg4128c0c2018-09-24 16:09:49 +05301026 int ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301027 bool eor;
Dave Watson3c4d7552017-06-14 11:37:39 -07001028
1029 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1030 MSG_SENDPAGE_NOTLAST))
1031 return -ENOTSUPP;
1032
1033 /* No MSG_EOR from splice, only look at MSG_MORE */
1034 eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
1035
1036 lock_sock(sk);
1037
1038 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1039
Vakul Garga42055e2018-09-21 09:46:13 +05301040 /* Wait till there is any pending write on socket */
1041 if (unlikely(sk->sk_write_pending)) {
1042 ret = wait_on_pending_writer(sk, &timeo);
1043 if (unlikely(ret))
1044 goto sendpage_end;
1045 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001046
1047 /* Call the sk_stream functions to manage the sndbuf mem. */
1048 while (size > 0) {
1049 size_t copy, required_size;
1050
1051 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +01001052 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -07001053 goto sendpage_end;
1054 }
1055
John Fastabendd3b18ad32018-10-13 02:46:01 +02001056 if (ctx->open_rec)
1057 rec = ctx->open_rec;
1058 else
1059 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +05301060 if (!rec) {
1061 ret = -ENOMEM;
1062 goto sendpage_end;
1063 }
1064
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001065 msg_pl = &rec->msg_plaintext;
1066
Dave Watson3c4d7552017-06-14 11:37:39 -07001067 full_record = false;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001068 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001069 copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001070 copy = size;
1071 if (copy >= record_room) {
1072 copy = record_room;
1073 full_record = true;
1074 }
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001075
1076 required_size = msg_pl->sg.size + copy +
1077 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001078
1079 if (!sk_stream_memory_free(sk))
1080 goto wait_for_sndbuf;
1081alloc_payload:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001082 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001083 if (ret) {
1084 if (ret != -ENOSPC)
1085 goto wait_for_memory;
1086
1087 /* Adjust copy according to the amount that was
1088 * actually allocated. The difference is due
1089 * to max sg elements limit
1090 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001091 copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001092 full_record = true;
1093 }
1094
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001095 sk_msg_page_add(msg_pl, page, copy, offset);
Dave Watson3c4d7552017-06-14 11:37:39 -07001096 sk_mem_charge(sk, copy);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001097
Dave Watson3c4d7552017-06-14 11:37:39 -07001098 offset += copy;
1099 size -= copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001100 copied += copy;
Dave Watson3c4d7552017-06-14 11:37:39 -07001101
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001102 tls_ctx->pending_open_record_frags = true;
1103 if (full_record || eor || sk_msg_full(msg_pl)) {
Vakul Garg4e6d4722018-09-30 08:04:35 +05301104 rec->inplace_crypto = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001105 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1106 record_type, &copied, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -07001107 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +05301108 if (ret == -EINPROGRESS)
1109 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001110 else if (ret == -ENOMEM)
1111 goto wait_for_memory;
1112 else if (ret != -EAGAIN) {
1113 if (ret == -ENOSPC)
1114 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301115 goto sendpage_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001116 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001117 }
1118 }
1119 continue;
1120wait_for_sndbuf:
1121 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1122wait_for_memory:
1123 ret = sk_stream_wait_memory(sk, &timeo);
1124 if (ret) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001125 tls_trim_both_msgs(sk, msg_pl->sg.size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001126 goto sendpage_end;
1127 }
1128
Dave Watson3c4d7552017-06-14 11:37:39 -07001129 goto alloc_payload;
1130 }
1131
Vakul Garga42055e2018-09-21 09:46:13 +05301132 if (num_async) {
1133 /* Transmit if any encryptions have completed */
1134 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1135 cancel_delayed_work(&ctx->tx_work.work);
1136 tls_tx_records(sk, flags);
1137 }
1138 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001139sendpage_end:
John Fastabendd3b18ad32018-10-13 02:46:01 +02001140 ret = sk_stream_error(sk, flags, ret);
Dave Watson3c4d7552017-06-14 11:37:39 -07001141 release_sock(sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001142 return copied ? copied : ret;
Dave Watson3c4d7552017-06-14 11:37:39 -07001143}
1144
John Fastabendd3b18ad32018-10-13 02:46:01 +02001145static struct sk_buff *tls_wait_data(struct sock *sk, struct sk_psock *psock,
1146 int flags, long timeo, int *err)
Dave Watsonc46234e2018-03-22 10:10:35 -07001147{
1148 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001149 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001150 struct sk_buff *skb;
1151 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1152
John Fastabendd3b18ad32018-10-13 02:46:01 +02001153 while (!(skb = ctx->recv_pkt) && sk_psock_queue_empty(psock)) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001154 if (sk->sk_err) {
1155 *err = sock_error(sk);
1156 return NULL;
1157 }
1158
Doron Roberts-Kedesfcf47932018-07-18 16:22:27 -07001159 if (sk->sk_shutdown & RCV_SHUTDOWN)
1160 return NULL;
1161
Dave Watsonc46234e2018-03-22 10:10:35 -07001162 if (sock_flag(sk, SOCK_DONE))
1163 return NULL;
1164
1165 if ((flags & MSG_DONTWAIT) || !timeo) {
1166 *err = -EAGAIN;
1167 return NULL;
1168 }
1169
1170 add_wait_queue(sk_sleep(sk), &wait);
1171 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001172 sk_wait_event(sk, &timeo,
1173 ctx->recv_pkt != skb ||
1174 !sk_psock_queue_empty(psock),
1175 &wait);
Dave Watsonc46234e2018-03-22 10:10:35 -07001176 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1177 remove_wait_queue(sk_sleep(sk), &wait);
1178
1179 /* Handle signals */
1180 if (signal_pending(current)) {
1181 *err = sock_intr_errno(timeo);
1182 return NULL;
1183 }
1184 }
1185
1186 return skb;
1187}
1188
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001189static int tls_setup_from_iter(struct sock *sk, struct iov_iter *from,
1190 int length, int *pages_used,
1191 unsigned int *size_used,
1192 struct scatterlist *to,
1193 int to_max_pages)
1194{
1195 int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1196 struct page *pages[MAX_SKB_FRAGS];
1197 unsigned int size = *size_used;
1198 ssize_t copied, use;
1199 size_t offset;
1200
1201 while (length > 0) {
1202 i = 0;
1203 maxpages = to_max_pages - num_elem;
1204 if (maxpages == 0) {
1205 rc = -EFAULT;
1206 goto out;
1207 }
1208 copied = iov_iter_get_pages(from, pages,
1209 length,
1210 maxpages, &offset);
1211 if (copied <= 0) {
1212 rc = -EFAULT;
1213 goto out;
1214 }
1215
1216 iov_iter_advance(from, copied);
1217
1218 length -= copied;
1219 size += copied;
1220 while (copied) {
1221 use = min_t(int, copied, PAGE_SIZE - offset);
1222
1223 sg_set_page(&to[num_elem],
1224 pages[i], use, offset);
1225 sg_unmark_end(&to[num_elem]);
1226 /* We do not uncharge memory from this API */
1227
1228 offset = 0;
1229 copied -= use;
1230
1231 i++;
1232 num_elem++;
1233 }
1234 }
1235 /* Mark the end in the last sg entry if newly added */
1236 if (num_elem > *pages_used)
1237 sg_mark_end(&to[num_elem - 1]);
1238out:
1239 if (rc)
1240 iov_iter_revert(from, size - *size_used);
1241 *size_used = size;
1242 *pages_used = num_elem;
1243
1244 return rc;
1245}
1246
Vakul Garg0b243d02018-08-10 20:46:41 +05301247/* This function decrypts the input skb into either out_iov or in out_sg
1248 * or in skb buffers itself. The input parameter 'zc' indicates if
1249 * zero-copy mode needs to be tried or not. With zero-copy mode, either
1250 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
1251 * NULL, then the decryption happens inside skb buffers itself, i.e.
1252 * zero-copy gets disabled and 'zc' is updated.
1253 */
1254
1255static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1256 struct iov_iter *out_iov,
1257 struct scatterlist *out_sg,
1258 int *chunk, bool *zc)
1259{
1260 struct tls_context *tls_ctx = tls_get_ctx(sk);
1261 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1262 struct strp_msg *rxm = strp_msg(skb);
1263 int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0;
1264 struct aead_request *aead_req;
1265 struct sk_buff *unused;
1266 u8 *aad, *iv, *mem = NULL;
1267 struct scatterlist *sgin = NULL;
1268 struct scatterlist *sgout = NULL;
1269 const int data_len = rxm->full_len - tls_ctx->rx.overhead_size;
1270
1271 if (*zc && (out_iov || out_sg)) {
1272 if (out_iov)
1273 n_sgout = iov_iter_npages(out_iov, INT_MAX) + 1;
1274 else
1275 n_sgout = sg_nents(out_sg);
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -07001276 n_sgin = skb_nsg(skb, rxm->offset + tls_ctx->rx.prepend_size,
1277 rxm->full_len - tls_ctx->rx.prepend_size);
Vakul Garg0b243d02018-08-10 20:46:41 +05301278 } else {
1279 n_sgout = 0;
1280 *zc = false;
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -07001281 n_sgin = skb_cow_data(skb, 0, &unused);
Vakul Garg0b243d02018-08-10 20:46:41 +05301282 }
1283
Vakul Garg0b243d02018-08-10 20:46:41 +05301284 if (n_sgin < 1)
1285 return -EBADMSG;
1286
1287 /* Increment to accommodate AAD */
1288 n_sgin = n_sgin + 1;
1289
1290 nsg = n_sgin + n_sgout;
1291
1292 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
1293 mem_size = aead_size + (nsg * sizeof(struct scatterlist));
1294 mem_size = mem_size + TLS_AAD_SPACE_SIZE;
1295 mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv);
1296
1297 /* Allocate a single block of memory which contains
1298 * aead_req || sgin[] || sgout[] || aad || iv.
1299 * This order achieves correct alignment for aead_req, sgin, sgout.
1300 */
1301 mem = kmalloc(mem_size, sk->sk_allocation);
1302 if (!mem)
1303 return -ENOMEM;
1304
1305 /* Segment the allocated memory */
1306 aead_req = (struct aead_request *)mem;
1307 sgin = (struct scatterlist *)(mem + aead_size);
1308 sgout = sgin + n_sgin;
1309 aad = (u8 *)(sgout + n_sgout);
1310 iv = aad + TLS_AAD_SPACE_SIZE;
1311
1312 /* Prepare IV */
1313 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1314 iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1315 tls_ctx->rx.iv_size);
1316 if (err < 0) {
1317 kfree(mem);
1318 return err;
1319 }
1320 memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
1321
1322 /* Prepare AAD */
1323 tls_make_aad(aad, rxm->full_len - tls_ctx->rx.overhead_size,
1324 tls_ctx->rx.rec_seq, tls_ctx->rx.rec_seq_size,
1325 ctx->control);
1326
1327 /* Prepare sgin */
1328 sg_init_table(sgin, n_sgin);
1329 sg_set_buf(&sgin[0], aad, TLS_AAD_SPACE_SIZE);
1330 err = skb_to_sgvec(skb, &sgin[1],
1331 rxm->offset + tls_ctx->rx.prepend_size,
1332 rxm->full_len - tls_ctx->rx.prepend_size);
1333 if (err < 0) {
1334 kfree(mem);
1335 return err;
1336 }
1337
1338 if (n_sgout) {
1339 if (out_iov) {
1340 sg_init_table(sgout, n_sgout);
1341 sg_set_buf(&sgout[0], aad, TLS_AAD_SPACE_SIZE);
1342
1343 *chunk = 0;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001344 err = tls_setup_from_iter(sk, out_iov, data_len,
1345 &pages, chunk, &sgout[1],
1346 (n_sgout - 1));
Vakul Garg0b243d02018-08-10 20:46:41 +05301347 if (err < 0)
1348 goto fallback_to_reg_recv;
1349 } else if (out_sg) {
1350 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
1351 } else {
1352 goto fallback_to_reg_recv;
1353 }
1354 } else {
1355fallback_to_reg_recv:
1356 sgout = sgin;
1357 pages = 0;
1358 *chunk = 0;
1359 *zc = false;
1360 }
1361
1362 /* Prepare and submit AEAD request */
Vakul Garg94524d82018-08-29 15:26:55 +05301363 err = tls_do_decryption(sk, skb, sgin, sgout, iv,
1364 data_len, aead_req, *zc);
1365 if (err == -EINPROGRESS)
1366 return err;
Vakul Garg0b243d02018-08-10 20:46:41 +05301367
1368 /* Release the pages in case iov was mapped to pages */
1369 for (; pages > 0; pages--)
1370 put_page(sg_page(&sgout[pages]));
1371
1372 kfree(mem);
1373 return err;
1374}
1375
Boris Pismennydafb67f2018-07-13 14:33:40 +03001376static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
Vakul Garg0b243d02018-08-10 20:46:41 +05301377 struct iov_iter *dest, int *chunk, bool *zc)
Boris Pismennydafb67f2018-07-13 14:33:40 +03001378{
1379 struct tls_context *tls_ctx = tls_get_ctx(sk);
1380 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1381 struct strp_msg *rxm = strp_msg(skb);
1382 int err = 0;
1383
Boris Pismenny4799ac82018-07-13 14:33:43 +03001384#ifdef CONFIG_TLS_DEVICE
1385 err = tls_device_decrypted(sk, skb);
Boris Pismennydafb67f2018-07-13 14:33:40 +03001386 if (err < 0)
1387 return err;
Boris Pismenny4799ac82018-07-13 14:33:43 +03001388#endif
1389 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301390 err = decrypt_internal(sk, skb, dest, NULL, chunk, zc);
Vakul Garg94524d82018-08-29 15:26:55 +05301391 if (err < 0) {
1392 if (err == -EINPROGRESS)
1393 tls_advance_record_sn(sk, &tls_ctx->rx);
1394
Boris Pismenny4799ac82018-07-13 14:33:43 +03001395 return err;
Vakul Garg94524d82018-08-29 15:26:55 +05301396 }
Boris Pismenny4799ac82018-07-13 14:33:43 +03001397 } else {
1398 *zc = false;
1399 }
Boris Pismennydafb67f2018-07-13 14:33:40 +03001400
1401 rxm->offset += tls_ctx->rx.prepend_size;
1402 rxm->full_len -= tls_ctx->rx.overhead_size;
1403 tls_advance_record_sn(sk, &tls_ctx->rx);
1404 ctx->decrypted = true;
1405 ctx->saved_data_ready(sk);
1406
1407 return err;
1408}
1409
1410int decrypt_skb(struct sock *sk, struct sk_buff *skb,
1411 struct scatterlist *sgout)
Dave Watsonc46234e2018-03-22 10:10:35 -07001412{
Vakul Garg0b243d02018-08-10 20:46:41 +05301413 bool zc = true;
1414 int chunk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001415
Vakul Garg0b243d02018-08-10 20:46:41 +05301416 return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -07001417}
1418
1419static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb,
1420 unsigned int len)
1421{
1422 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001423 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001424
Vakul Garg94524d82018-08-29 15:26:55 +05301425 if (skb) {
1426 struct strp_msg *rxm = strp_msg(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001427
Vakul Garg94524d82018-08-29 15:26:55 +05301428 if (len < rxm->full_len) {
1429 rxm->offset += len;
1430 rxm->full_len -= len;
1431 return false;
1432 }
1433 kfree_skb(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001434 }
1435
1436 /* Finished with message */
1437 ctx->recv_pkt = NULL;
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -07001438 __strp_unpause(&ctx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07001439
1440 return true;
1441}
1442
1443int tls_sw_recvmsg(struct sock *sk,
1444 struct msghdr *msg,
1445 size_t len,
1446 int nonblock,
1447 int flags,
1448 int *addr_len)
1449{
1450 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001451 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001452 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001453 unsigned char control;
1454 struct strp_msg *rxm;
1455 struct sk_buff *skb;
1456 ssize_t copied = 0;
1457 bool cmsg = false;
Daniel Borkmann06030db2018-06-15 03:07:46 +02001458 int target, err = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001459 long timeo;
David Howells00e23702018-10-22 13:07:28 +01001460 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Vakul Garg94524d82018-08-29 15:26:55 +05301461 int num_async = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001462
1463 flags |= nonblock;
1464
1465 if (unlikely(flags & MSG_ERRQUEUE))
1466 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1467
John Fastabendd3b18ad32018-10-13 02:46:01 +02001468 psock = sk_psock_get(sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001469 lock_sock(sk);
1470
Daniel Borkmann06030db2018-06-15 03:07:46 +02001471 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
Dave Watsonc46234e2018-03-22 10:10:35 -07001472 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1473 do {
1474 bool zc = false;
Vakul Garg94524d82018-08-29 15:26:55 +05301475 bool async = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001476 int chunk = 0;
1477
John Fastabendd3b18ad32018-10-13 02:46:01 +02001478 skb = tls_wait_data(sk, psock, flags, timeo, &err);
1479 if (!skb) {
1480 if (psock) {
1481 int ret = __tcp_bpf_recvmsg(sk, psock, msg, len);
1482
1483 if (ret > 0) {
1484 copied += ret;
1485 len -= ret;
1486 continue;
1487 }
1488 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001489 goto recv_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001490 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001491
1492 rxm = strp_msg(skb);
Vakul Garg94524d82018-08-29 15:26:55 +05301493
Dave Watsonc46234e2018-03-22 10:10:35 -07001494 if (!cmsg) {
1495 int cerr;
1496
1497 cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1498 sizeof(ctx->control), &ctx->control);
1499 cmsg = true;
1500 control = ctx->control;
1501 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1502 if (cerr || msg->msg_flags & MSG_CTRUNC) {
1503 err = -EIO;
1504 goto recv_end;
1505 }
1506 }
1507 } else if (control != ctx->control) {
1508 goto recv_end;
1509 }
1510
1511 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301512 int to_copy = rxm->full_len - tls_ctx->rx.overhead_size;
Dave Watsonc46234e2018-03-22 10:10:35 -07001513
Vakul Garg0b243d02018-08-10 20:46:41 +05301514 if (!is_kvec && to_copy <= len &&
1515 likely(!(flags & MSG_PEEK)))
Dave Watsonc46234e2018-03-22 10:10:35 -07001516 zc = true;
Dave Watsonc46234e2018-03-22 10:10:35 -07001517
Vakul Garg0b243d02018-08-10 20:46:41 +05301518 err = decrypt_skb_update(sk, skb, &msg->msg_iter,
1519 &chunk, &zc);
Vakul Garg94524d82018-08-29 15:26:55 +05301520 if (err < 0 && err != -EINPROGRESS) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301521 tls_err_abort(sk, EBADMSG);
1522 goto recv_end;
Dave Watsonc46234e2018-03-22 10:10:35 -07001523 }
Vakul Garg94524d82018-08-29 15:26:55 +05301524
1525 if (err == -EINPROGRESS) {
1526 async = true;
1527 num_async++;
1528 goto pick_next_record;
1529 }
1530
Dave Watsonc46234e2018-03-22 10:10:35 -07001531 ctx->decrypted = true;
1532 }
1533
1534 if (!zc) {
1535 chunk = min_t(unsigned int, rxm->full_len, len);
Vakul Garg94524d82018-08-29 15:26:55 +05301536
Dave Watsonc46234e2018-03-22 10:10:35 -07001537 err = skb_copy_datagram_msg(skb, rxm->offset, msg,
1538 chunk);
1539 if (err < 0)
1540 goto recv_end;
1541 }
1542
Vakul Garg94524d82018-08-29 15:26:55 +05301543pick_next_record:
Dave Watsonc46234e2018-03-22 10:10:35 -07001544 copied += chunk;
1545 len -= chunk;
1546 if (likely(!(flags & MSG_PEEK))) {
1547 u8 control = ctx->control;
1548
Vakul Garg94524d82018-08-29 15:26:55 +05301549 /* For async, drop current skb reference */
1550 if (async)
1551 skb = NULL;
1552
Dave Watsonc46234e2018-03-22 10:10:35 -07001553 if (tls_sw_advance_skb(sk, skb, chunk)) {
1554 /* Return full control message to
1555 * userspace before trying to parse
1556 * another message type
1557 */
1558 msg->msg_flags |= MSG_EOR;
1559 if (control != TLS_RECORD_TYPE_DATA)
1560 goto recv_end;
Vakul Garg94524d82018-08-29 15:26:55 +05301561 } else {
1562 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001563 }
Daniel Borkmann50c6b582018-09-14 23:00:55 +02001564 } else {
1565 /* MSG_PEEK right now cannot look beyond current skb
1566 * from strparser, meaning we cannot advance skb here
1567 * and thus unpause strparser since we'd loose original
1568 * one.
1569 */
1570 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001571 }
Vakul Garg94524d82018-08-29 15:26:55 +05301572
Daniel Borkmann06030db2018-06-15 03:07:46 +02001573 /* If we have a new message from strparser, continue now. */
1574 if (copied >= target && !ctx->recv_pkt)
1575 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001576 } while (len);
1577
1578recv_end:
Vakul Garg94524d82018-08-29 15:26:55 +05301579 if (num_async) {
1580 /* Wait for all previously submitted records to be decrypted */
1581 smp_store_mb(ctx->async_notify, true);
1582 if (atomic_read(&ctx->decrypt_pending)) {
1583 err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1584 if (err) {
1585 /* one of async decrypt failed */
1586 tls_err_abort(sk, err);
1587 copied = 0;
1588 }
1589 } else {
1590 reinit_completion(&ctx->async_wait.completion);
1591 }
1592 WRITE_ONCE(ctx->async_notify, false);
1593 }
1594
Dave Watsonc46234e2018-03-22 10:10:35 -07001595 release_sock(sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001596 if (psock)
1597 sk_psock_put(sk, psock);
Dave Watsonc46234e2018-03-22 10:10:35 -07001598 return copied ? : err;
1599}
1600
1601ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
1602 struct pipe_inode_info *pipe,
1603 size_t len, unsigned int flags)
1604{
1605 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001606 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001607 struct strp_msg *rxm = NULL;
1608 struct sock *sk = sock->sk;
1609 struct sk_buff *skb;
1610 ssize_t copied = 0;
1611 int err = 0;
1612 long timeo;
1613 int chunk;
Vakul Garg0b243d02018-08-10 20:46:41 +05301614 bool zc = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001615
1616 lock_sock(sk);
1617
1618 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1619
John Fastabendd3b18ad32018-10-13 02:46:01 +02001620 skb = tls_wait_data(sk, NULL, flags, timeo, &err);
Dave Watsonc46234e2018-03-22 10:10:35 -07001621 if (!skb)
1622 goto splice_read_end;
1623
1624 /* splice does not support reading control messages */
1625 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1626 err = -ENOTSUPP;
1627 goto splice_read_end;
1628 }
1629
1630 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301631 err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -07001632
1633 if (err < 0) {
1634 tls_err_abort(sk, EBADMSG);
1635 goto splice_read_end;
1636 }
1637 ctx->decrypted = true;
1638 }
1639 rxm = strp_msg(skb);
1640
1641 chunk = min_t(unsigned int, rxm->full_len, len);
1642 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
1643 if (copied < 0)
1644 goto splice_read_end;
1645
1646 if (likely(!(flags & MSG_PEEK)))
1647 tls_sw_advance_skb(sk, skb, copied);
1648
1649splice_read_end:
1650 release_sock(sk);
1651 return copied ? : err;
1652}
1653
John Fastabend924ad652018-10-13 02:46:00 +02001654bool tls_sw_stream_read(const struct sock *sk)
Dave Watsonc46234e2018-03-22 10:10:35 -07001655{
Dave Watsonc46234e2018-03-22 10:10:35 -07001656 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001657 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001658 bool ingress_empty = true;
1659 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001660
John Fastabendd3b18ad32018-10-13 02:46:01 +02001661 rcu_read_lock();
1662 psock = sk_psock(sk);
1663 if (psock)
1664 ingress_empty = list_empty(&psock->ingress_msg);
1665 rcu_read_unlock();
Dave Watsonc46234e2018-03-22 10:10:35 -07001666
John Fastabendd3b18ad32018-10-13 02:46:01 +02001667 return !ingress_empty || ctx->recv_pkt;
Dave Watsonc46234e2018-03-22 10:10:35 -07001668}
1669
1670static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1671{
1672 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001673 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Kees Cook3463e512018-06-25 16:55:05 -07001674 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
Dave Watsonc46234e2018-03-22 10:10:35 -07001675 struct strp_msg *rxm = strp_msg(skb);
1676 size_t cipher_overhead;
1677 size_t data_len = 0;
1678 int ret;
1679
1680 /* Verify that we have a full TLS header, or wait for more data */
1681 if (rxm->offset + tls_ctx->rx.prepend_size > skb->len)
1682 return 0;
1683
Kees Cook3463e512018-06-25 16:55:05 -07001684 /* Sanity-check size of on-stack buffer. */
1685 if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) {
1686 ret = -EINVAL;
1687 goto read_failure;
1688 }
1689
Dave Watsonc46234e2018-03-22 10:10:35 -07001690 /* Linearize header to local buffer */
1691 ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size);
1692
1693 if (ret < 0)
1694 goto read_failure;
1695
1696 ctx->control = header[0];
1697
1698 data_len = ((header[4] & 0xFF) | (header[3] << 8));
1699
1700 cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size;
1701
1702 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) {
1703 ret = -EMSGSIZE;
1704 goto read_failure;
1705 }
1706 if (data_len < cipher_overhead) {
1707 ret = -EBADMSG;
1708 goto read_failure;
1709 }
1710
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001711 if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.info.version) ||
1712 header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.info.version)) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001713 ret = -EINVAL;
1714 goto read_failure;
1715 }
1716
Boris Pismenny4799ac82018-07-13 14:33:43 +03001717#ifdef CONFIG_TLS_DEVICE
1718 handle_device_resync(strp->sk, TCP_SKB_CB(skb)->seq + rxm->offset,
1719 *(u64*)tls_ctx->rx.rec_seq);
1720#endif
Dave Watsonc46234e2018-03-22 10:10:35 -07001721 return data_len + TLS_HEADER_SIZE;
1722
1723read_failure:
1724 tls_err_abort(strp->sk, ret);
1725
1726 return ret;
1727}
1728
1729static void tls_queue(struct strparser *strp, struct sk_buff *skb)
1730{
1731 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001732 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001733
1734 ctx->decrypted = false;
1735
1736 ctx->recv_pkt = skb;
1737 strp_pause(strp);
1738
Vakul Gargad13acc2018-07-30 16:08:33 +05301739 ctx->saved_data_ready(strp->sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001740}
1741
1742static void tls_data_ready(struct sock *sk)
1743{
1744 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001745 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001746 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001747
1748 strp_data_ready(&ctx->strp);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001749
1750 psock = sk_psock_get(sk);
1751 if (psock && !list_empty(&psock->ingress_msg)) {
1752 ctx->saved_data_ready(sk);
1753 sk_psock_put(sk, psock);
1754 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001755}
1756
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001757void tls_sw_free_resources_tx(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -07001758{
1759 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001760 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +05301761 struct tls_rec *rec, *tmp;
1762
1763 /* Wait for any pending async encryptions to complete */
1764 smp_store_mb(ctx->async_notify, true);
1765 if (atomic_read(&ctx->encrypt_pending))
1766 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1767
1768 cancel_delayed_work_sync(&ctx->tx_work.work);
1769
1770 /* Tx whatever records we can transmit and abandon the rest */
1771 tls_tx_records(sk, -1);
1772
Vakul Garg9932a292018-09-24 15:35:56 +05301773 /* Free up un-sent records in tx_list. First, free
Vakul Garga42055e2018-09-21 09:46:13 +05301774 * the partially sent record if any at head of tx_list.
1775 */
1776 if (tls_ctx->partially_sent_record) {
1777 struct scatterlist *sg = tls_ctx->partially_sent_record;
1778
1779 while (1) {
1780 put_page(sg_page(sg));
1781 sk_mem_uncharge(sk, sg->length);
1782
1783 if (sg_is_last(sg))
1784 break;
1785 sg++;
1786 }
1787
1788 tls_ctx->partially_sent_record = NULL;
1789
Vakul Garg9932a292018-09-24 15:35:56 +05301790 rec = list_first_entry(&ctx->tx_list,
Vakul Garga42055e2018-09-21 09:46:13 +05301791 struct tls_rec, list);
1792 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001793 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301794 kfree(rec);
1795 }
1796
Vakul Garg9932a292018-09-24 15:35:56 +05301797 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
Vakul Garga42055e2018-09-21 09:46:13 +05301798 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001799 sk_msg_free(sk, &rec->msg_encrypted);
1800 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301801 kfree(rec);
1802 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001803
Vakul Garg201876b2018-07-24 16:54:27 +05301804 crypto_free_aead(ctx->aead_send);
Vakul Gargc7749732018-09-25 20:21:51 +05301805 tls_free_open_rec(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001806
1807 kfree(ctx);
1808}
1809
Boris Pismenny39f56e12018-07-13 14:33:41 +03001810void tls_sw_release_resources_rx(struct sock *sk)
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001811{
1812 struct tls_context *tls_ctx = tls_get_ctx(sk);
1813 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1814
Dave Watsonc46234e2018-03-22 10:10:35 -07001815 if (ctx->aead_recv) {
Vakul Garg201876b2018-07-24 16:54:27 +05301816 kfree_skb(ctx->recv_pkt);
1817 ctx->recv_pkt = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001818 crypto_free_aead(ctx->aead_recv);
1819 strp_stop(&ctx->strp);
1820 write_lock_bh(&sk->sk_callback_lock);
1821 sk->sk_data_ready = ctx->saved_data_ready;
1822 write_unlock_bh(&sk->sk_callback_lock);
1823 release_sock(sk);
1824 strp_done(&ctx->strp);
1825 lock_sock(sk);
1826 }
Boris Pismenny39f56e12018-07-13 14:33:41 +03001827}
1828
1829void tls_sw_free_resources_rx(struct sock *sk)
1830{
1831 struct tls_context *tls_ctx = tls_get_ctx(sk);
1832 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1833
1834 tls_sw_release_resources_rx(sk);
Dave Watson3c4d7552017-06-14 11:37:39 -07001835
Dave Watson3c4d7552017-06-14 11:37:39 -07001836 kfree(ctx);
1837}
1838
Vakul Garg9932a292018-09-24 15:35:56 +05301839/* The work handler to transmitt the encrypted records in tx_list */
Vakul Garga42055e2018-09-21 09:46:13 +05301840static void tx_work_handler(struct work_struct *work)
1841{
1842 struct delayed_work *delayed_work = to_delayed_work(work);
1843 struct tx_work *tx_work = container_of(delayed_work,
1844 struct tx_work, work);
1845 struct sock *sk = tx_work->sk;
1846 struct tls_context *tls_ctx = tls_get_ctx(sk);
1847 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1848
1849 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
1850 return;
1851
1852 lock_sock(sk);
1853 tls_tx_records(sk, -1);
1854 release_sock(sk);
1855}
1856
Dave Watsonc46234e2018-03-22 10:10:35 -07001857int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
Dave Watson3c4d7552017-06-14 11:37:39 -07001858{
Dave Watson3c4d7552017-06-14 11:37:39 -07001859 struct tls_crypto_info *crypto_info;
1860 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001861 struct tls_sw_context_tx *sw_ctx_tx = NULL;
1862 struct tls_sw_context_rx *sw_ctx_rx = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001863 struct cipher_context *cctx;
1864 struct crypto_aead **aead;
1865 struct strp_callbacks cb;
Dave Watson3c4d7552017-06-14 11:37:39 -07001866 u16 nonce_size, tag_size, iv_size, rec_seq_size;
1867 char *iv, *rec_seq;
1868 int rc = 0;
1869
1870 if (!ctx) {
1871 rc = -EINVAL;
1872 goto out;
1873 }
1874
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001875 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001876 if (!ctx->priv_ctx_tx) {
1877 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
1878 if (!sw_ctx_tx) {
1879 rc = -ENOMEM;
1880 goto out;
1881 }
1882 ctx->priv_ctx_tx = sw_ctx_tx;
1883 } else {
1884 sw_ctx_tx =
1885 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
Dave Watsonc46234e2018-03-22 10:10:35 -07001886 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001887 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001888 if (!ctx->priv_ctx_rx) {
1889 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
1890 if (!sw_ctx_rx) {
1891 rc = -ENOMEM;
1892 goto out;
1893 }
1894 ctx->priv_ctx_rx = sw_ctx_rx;
1895 } else {
1896 sw_ctx_rx =
1897 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001898 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001899 }
1900
Dave Watsonc46234e2018-03-22 10:10:35 -07001901 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001902 crypto_init_wait(&sw_ctx_tx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001903 crypto_info = &ctx->crypto_send.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07001904 cctx = &ctx->tx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001905 aead = &sw_ctx_tx->aead_send;
Vakul Garg9932a292018-09-24 15:35:56 +05301906 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +05301907 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
1908 sw_ctx_tx->tx_work.sk = sk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001909 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001910 crypto_init_wait(&sw_ctx_rx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001911 crypto_info = &ctx->crypto_recv.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07001912 cctx = &ctx->rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001913 aead = &sw_ctx_rx->aead_recv;
Dave Watsonc46234e2018-03-22 10:10:35 -07001914 }
1915
Dave Watson3c4d7552017-06-14 11:37:39 -07001916 switch (crypto_info->cipher_type) {
1917 case TLS_CIPHER_AES_GCM_128: {
1918 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1919 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
1920 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1921 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1922 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
1923 rec_seq =
1924 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1925 gcm_128_info =
1926 (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
1927 break;
1928 }
1929 default:
1930 rc = -EINVAL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001931 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001932 }
1933
Kees Cookb16520f2018-04-10 17:52:34 -07001934 /* Sanity-check the IV size for stack allocations. */
Kees Cook3463e512018-06-25 16:55:05 -07001935 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) {
Kees Cookb16520f2018-04-10 17:52:34 -07001936 rc = -EINVAL;
1937 goto free_priv;
1938 }
1939
Dave Watsonc46234e2018-03-22 10:10:35 -07001940 cctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
1941 cctx->tag_size = tag_size;
1942 cctx->overhead_size = cctx->prepend_size + cctx->tag_size;
1943 cctx->iv_size = iv_size;
1944 cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1945 GFP_KERNEL);
1946 if (!cctx->iv) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001947 rc = -ENOMEM;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001948 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001949 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001950 memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
1951 memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
1952 cctx->rec_seq_size = rec_seq_size;
zhong jiang969d5092018-08-01 00:50:24 +08001953 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
Dave Watsonc46234e2018-03-22 10:10:35 -07001954 if (!cctx->rec_seq) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001955 rc = -ENOMEM;
1956 goto free_iv;
1957 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001958
Dave Watsonc46234e2018-03-22 10:10:35 -07001959 if (!*aead) {
1960 *aead = crypto_alloc_aead("gcm(aes)", 0, 0);
1961 if (IS_ERR(*aead)) {
1962 rc = PTR_ERR(*aead);
1963 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07001964 goto free_rec_seq;
1965 }
1966 }
1967
1968 ctx->push_pending_record = tls_sw_push_pending_record;
1969
Sabrina Dubroca7cba09c2018-09-12 17:44:41 +02001970 rc = crypto_aead_setkey(*aead, gcm_128_info->key,
Dave Watson3c4d7552017-06-14 11:37:39 -07001971 TLS_CIPHER_AES_GCM_128_KEY_SIZE);
1972 if (rc)
1973 goto free_aead;
1974
Dave Watsonc46234e2018-03-22 10:10:35 -07001975 rc = crypto_aead_setauthsize(*aead, cctx->tag_size);
1976 if (rc)
1977 goto free_aead;
1978
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001979 if (sw_ctx_rx) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001980 /* Set up strparser */
1981 memset(&cb, 0, sizeof(cb));
1982 cb.rcv_msg = tls_queue;
1983 cb.parse_msg = tls_read_size;
1984
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001985 strp_init(&sw_ctx_rx->strp, sk, &cb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001986
1987 write_lock_bh(&sk->sk_callback_lock);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001988 sw_ctx_rx->saved_data_ready = sk->sk_data_ready;
Dave Watsonc46234e2018-03-22 10:10:35 -07001989 sk->sk_data_ready = tls_data_ready;
1990 write_unlock_bh(&sk->sk_callback_lock);
1991
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001992 strp_check_rcv(&sw_ctx_rx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07001993 }
1994
1995 goto out;
Dave Watson3c4d7552017-06-14 11:37:39 -07001996
1997free_aead:
Dave Watsonc46234e2018-03-22 10:10:35 -07001998 crypto_free_aead(*aead);
1999 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002000free_rec_seq:
Dave Watsonc46234e2018-03-22 10:10:35 -07002001 kfree(cctx->rec_seq);
2002 cctx->rec_seq = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002003free_iv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002004 kfree(cctx->iv);
2005 cctx->iv = NULL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01002006free_priv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002007 if (tx) {
2008 kfree(ctx->priv_ctx_tx);
2009 ctx->priv_ctx_tx = NULL;
2010 } else {
2011 kfree(ctx->priv_ctx_rx);
2012 ctx->priv_ctx_rx = NULL;
2013 }
Dave Watson3c4d7552017-06-14 11:37:39 -07002014out:
2015 return rc;
2016}