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