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