blob: 86b9527c4826b04ad9c0853e90556e38d5ea8291 [file] [log] [blame]
Dave Watson3c4d7552017-06-14 11:37:39 -07001/*
2 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4 * Copyright (c) 2016-2017, Lance Chao <lancerchao@fb.com>. All rights reserved.
5 * Copyright (c) 2016, Fridolin Pokorny <fridolin.pokorny@gmail.com>. All rights reserved.
6 * Copyright (c) 2016, Nikos Mavrogiannopoulos <nmav@gnutls.org>. All rights reserved.
John Fastabendd3b18ad32018-10-13 02:46:01 +02007 * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io
Dave Watson3c4d7552017-06-14 11:37:39 -07008 *
9 * This software is available to you under a choice of one of two
10 * licenses. You may choose to be licensed under the terms of the GNU
11 * General Public License (GPL) Version 2, available from the file
12 * COPYING in the main directory of this source tree, or the
13 * OpenIB.org BSD license below:
14 *
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
17 * conditions are met:
18 *
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer.
22 *
23 * - Redistributions in binary form must reproduce the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer in the documentation and/or other materials
26 * provided with the distribution.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * SOFTWARE.
36 */
37
Dave Watsonc46234e2018-03-22 10:10:35 -070038#include <linux/sched/signal.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070039#include <linux/module.h>
40#include <crypto/aead.h>
41
Dave Watsonc46234e2018-03-22 10:10:35 -070042#include <net/strparser.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070043#include <net/tls.h>
44
Kees Cookb16520f2018-04-10 17:52:34 -070045#define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE
46
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -070047static int __skb_nsg(struct sk_buff *skb, int offset, int len,
48 unsigned int recursion_level)
49{
50 int start = skb_headlen(skb);
51 int i, chunk = start - offset;
52 struct sk_buff *frag_iter;
53 int elt = 0;
54
55 if (unlikely(recursion_level >= 24))
56 return -EMSGSIZE;
57
58 if (chunk > 0) {
59 if (chunk > len)
60 chunk = len;
61 elt++;
62 len -= chunk;
63 if (len == 0)
64 return elt;
65 offset += chunk;
66 }
67
68 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
69 int end;
70
71 WARN_ON(start > offset + len);
72
73 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
74 chunk = end - offset;
75 if (chunk > 0) {
76 if (chunk > len)
77 chunk = len;
78 elt++;
79 len -= chunk;
80 if (len == 0)
81 return elt;
82 offset += chunk;
83 }
84 start = end;
85 }
86
87 if (unlikely(skb_has_frag_list(skb))) {
88 skb_walk_frags(skb, frag_iter) {
89 int end, ret;
90
91 WARN_ON(start > offset + len);
92
93 end = start + frag_iter->len;
94 chunk = end - offset;
95 if (chunk > 0) {
96 if (chunk > len)
97 chunk = len;
98 ret = __skb_nsg(frag_iter, offset - start, chunk,
99 recursion_level + 1);
100 if (unlikely(ret < 0))
101 return ret;
102 elt += ret;
103 len -= chunk;
104 if (len == 0)
105 return elt;
106 offset += chunk;
107 }
108 start = end;
109 }
110 }
111 BUG_ON(len);
112 return elt;
113}
114
115/* Return the number of scatterlist elements required to completely map the
116 * skb, or -EMSGSIZE if the recursion depth is exceeded.
117 */
118static int skb_nsg(struct sk_buff *skb, int offset, int len)
119{
120 return __skb_nsg(skb, offset, len, 0);
121}
122
Vakul Garg94524d82018-08-29 15:26:55 +0530123static void tls_decrypt_done(struct crypto_async_request *req, int err)
124{
125 struct aead_request *aead_req = (struct aead_request *)req;
Vakul Garg94524d82018-08-29 15:26:55 +0530126 struct scatterlist *sgout = aead_req->dst;
Vakul Garg692d7b52019-01-16 10:40:16 +0000127 struct scatterlist *sgin = aead_req->src;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700128 struct tls_sw_context_rx *ctx;
129 struct tls_context *tls_ctx;
Vakul Garg94524d82018-08-29 15:26:55 +0530130 struct scatterlist *sg;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700131 struct sk_buff *skb;
Vakul Garg94524d82018-08-29 15:26:55 +0530132 unsigned int pages;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700133 int pending;
134
135 skb = (struct sk_buff *)req->data;
136 tls_ctx = tls_get_ctx(skb->sk);
137 ctx = tls_sw_ctx_rx(tls_ctx);
Vakul Garg94524d82018-08-29 15:26:55 +0530138
139 /* Propagate if there was an err */
140 if (err) {
141 ctx->async_wait.err = err;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700142 tls_err_abort(skb->sk, err);
Vakul Garg692d7b52019-01-16 10:40:16 +0000143 } else {
144 struct strp_msg *rxm = strp_msg(skb);
145
146 rxm->offset += tls_ctx->rx.prepend_size;
147 rxm->full_len -= tls_ctx->rx.overhead_size;
Vakul Garg94524d82018-08-29 15:26:55 +0530148 }
149
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700150 /* After using skb->sk to propagate sk through crypto async callback
151 * we need to NULL it again.
152 */
153 skb->sk = NULL;
154
Vakul Garg94524d82018-08-29 15:26:55 +0530155
Vakul Garg692d7b52019-01-16 10:40:16 +0000156 /* Free the destination pages if skb was not decrypted inplace */
157 if (sgout != sgin) {
158 /* Skip the first S/G entry as it points to AAD */
159 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
160 if (!sg)
161 break;
162 put_page(sg_page(sg));
163 }
Vakul Garg94524d82018-08-29 15:26:55 +0530164 }
165
166 kfree(aead_req);
167
Vakul Garg692d7b52019-01-16 10:40:16 +0000168 pending = atomic_dec_return(&ctx->decrypt_pending);
169
Vakul Garg94524d82018-08-29 15:26:55 +0530170 if (!pending && READ_ONCE(ctx->async_notify))
171 complete(&ctx->async_wait.completion);
172}
173
Dave Watsonc46234e2018-03-22 10:10:35 -0700174static int tls_do_decryption(struct sock *sk,
Vakul Garg94524d82018-08-29 15:26:55 +0530175 struct sk_buff *skb,
Dave Watsonc46234e2018-03-22 10:10:35 -0700176 struct scatterlist *sgin,
177 struct scatterlist *sgout,
178 char *iv_recv,
179 size_t data_len,
Vakul Garg94524d82018-08-29 15:26:55 +0530180 struct aead_request *aead_req,
181 bool async)
Dave Watsonc46234e2018-03-22 10:10:35 -0700182{
183 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300184 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -0700185 int ret;
Dave Watsonc46234e2018-03-22 10:10:35 -0700186
Vakul Garg0b243d02018-08-10 20:46:41 +0530187 aead_request_set_tfm(aead_req, ctx->aead_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700188 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
189 aead_request_set_crypt(aead_req, sgin, sgout,
190 data_len + tls_ctx->rx.tag_size,
191 (u8 *)iv_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700192
Vakul Garg94524d82018-08-29 15:26:55 +0530193 if (async) {
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700194 /* Using skb->sk to push sk through to crypto async callback
195 * handler. This allows propagating errors up to the socket
196 * if needed. It _must_ be cleared in the async handler
197 * before kfree_skb is called. We _know_ skb->sk is NULL
198 * because it is a clone from strparser.
199 */
200 skb->sk = sk;
Vakul Garg94524d82018-08-29 15:26:55 +0530201 aead_request_set_callback(aead_req,
202 CRYPTO_TFM_REQ_MAY_BACKLOG,
203 tls_decrypt_done, skb);
204 atomic_inc(&ctx->decrypt_pending);
205 } else {
206 aead_request_set_callback(aead_req,
207 CRYPTO_TFM_REQ_MAY_BACKLOG,
208 crypto_req_done, &ctx->async_wait);
209 }
210
211 ret = crypto_aead_decrypt(aead_req);
212 if (ret == -EINPROGRESS) {
213 if (async)
214 return ret;
215
216 ret = crypto_wait_req(ret, &ctx->async_wait);
217 }
218
219 if (async)
220 atomic_dec(&ctx->decrypt_pending);
221
Dave Watsonc46234e2018-03-22 10:10:35 -0700222 return ret;
223}
224
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200225static void tls_trim_both_msgs(struct sock *sk, int target_size)
Dave Watson3c4d7552017-06-14 11:37:39 -0700226{
227 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300228 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530229 struct tls_rec *rec = ctx->open_rec;
Dave Watson3c4d7552017-06-14 11:37:39 -0700230
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200231 sk_msg_trim(sk, &rec->msg_plaintext, target_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700232 if (target_size > 0)
Dave Watsondbe42552018-03-22 10:10:06 -0700233 target_size += tls_ctx->tx.overhead_size;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200234 sk_msg_trim(sk, &rec->msg_encrypted, target_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700235}
236
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200237static int tls_alloc_encrypted_msg(struct sock *sk, int len)
Dave Watson3c4d7552017-06-14 11:37:39 -0700238{
239 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300240 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530241 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200242 struct sk_msg *msg_en = &rec->msg_encrypted;
Dave Watson3c4d7552017-06-14 11:37:39 -0700243
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200244 return sk_msg_alloc(sk, msg_en, len, 0);
Dave Watson3c4d7552017-06-14 11:37:39 -0700245}
246
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200247static int tls_clone_plaintext_msg(struct sock *sk, int required)
Dave Watson3c4d7552017-06-14 11:37:39 -0700248{
249 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300250 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530251 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200252 struct sk_msg *msg_pl = &rec->msg_plaintext;
253 struct sk_msg *msg_en = &rec->msg_encrypted;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530254 int skip, len;
Dave Watson3c4d7552017-06-14 11:37:39 -0700255
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200256 /* We add page references worth len bytes from encrypted sg
257 * at the end of plaintext sg. It is guaranteed that msg_en
Vakul Garg4e6d4722018-09-30 08:04:35 +0530258 * has enough required room (ensured by caller).
259 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200260 len = required - msg_pl->sg.size;
Vakul Garg52ea9922018-09-06 21:41:40 +0530261
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200262 /* Skip initial bytes in msg_en's data to be able to use
263 * same offset of both plain and encrypted data.
Vakul Garg4e6d4722018-09-30 08:04:35 +0530264 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200265 skip = tls_ctx->tx.prepend_size + msg_pl->sg.size;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530266
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200267 return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
Dave Watson3c4d7552017-06-14 11:37:39 -0700268}
269
John Fastabendd3b18ad32018-10-13 02:46:01 +0200270static struct tls_rec *tls_get_rec(struct sock *sk)
271{
272 struct tls_context *tls_ctx = tls_get_ctx(sk);
273 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
274 struct sk_msg *msg_pl, *msg_en;
275 struct tls_rec *rec;
276 int mem_size;
277
278 mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
279
280 rec = kzalloc(mem_size, sk->sk_allocation);
281 if (!rec)
282 return NULL;
283
284 msg_pl = &rec->msg_plaintext;
285 msg_en = &rec->msg_encrypted;
286
287 sk_msg_init(msg_pl);
288 sk_msg_init(msg_en);
289
290 sg_init_table(rec->sg_aead_in, 2);
291 sg_set_buf(&rec->sg_aead_in[0], rec->aad_space,
292 sizeof(rec->aad_space));
293 sg_unmark_end(&rec->sg_aead_in[1]);
294
295 sg_init_table(rec->sg_aead_out, 2);
296 sg_set_buf(&rec->sg_aead_out[0], rec->aad_space,
297 sizeof(rec->aad_space));
298 sg_unmark_end(&rec->sg_aead_out[1]);
299
300 return rec;
301}
302
303static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
304{
305 sk_msg_free(sk, &rec->msg_encrypted);
306 sk_msg_free(sk, &rec->msg_plaintext);
307 kfree(rec);
308}
309
Vakul Gargc7749732018-09-25 20:21:51 +0530310static void tls_free_open_rec(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -0700311{
312 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300313 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530314 struct tls_rec *rec = ctx->open_rec;
Dave Watson3c4d7552017-06-14 11:37:39 -0700315
John Fastabendd3b18ad32018-10-13 02:46:01 +0200316 if (rec) {
317 tls_free_rec(sk, rec);
318 ctx->open_rec = NULL;
319 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700320}
321
Vakul Garga42055e2018-09-21 09:46:13 +0530322int tls_tx_records(struct sock *sk, int flags)
323{
324 struct tls_context *tls_ctx = tls_get_ctx(sk);
325 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
326 struct tls_rec *rec, *tmp;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200327 struct sk_msg *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530328 int tx_flags, rc = 0;
329
330 if (tls_is_partially_sent_record(tls_ctx)) {
Vakul Garg9932a292018-09-24 15:35:56 +0530331 rec = list_first_entry(&ctx->tx_list,
Vakul Garga42055e2018-09-21 09:46:13 +0530332 struct tls_rec, list);
333
334 if (flags == -1)
335 tx_flags = rec->tx_flags;
336 else
337 tx_flags = flags;
338
339 rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
340 if (rc)
341 goto tx_err;
342
343 /* Full record has been transmitted.
Vakul Garg9932a292018-09-24 15:35:56 +0530344 * Remove the head of tx_list
Vakul Garga42055e2018-09-21 09:46:13 +0530345 */
Vakul Garga42055e2018-09-21 09:46:13 +0530346 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200347 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +0530348 kfree(rec);
349 }
350
Vakul Garg9932a292018-09-24 15:35:56 +0530351 /* Tx all ready records */
352 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
353 if (READ_ONCE(rec->tx_ready)) {
Vakul Garga42055e2018-09-21 09:46:13 +0530354 if (flags == -1)
355 tx_flags = rec->tx_flags;
356 else
357 tx_flags = flags;
358
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200359 msg_en = &rec->msg_encrypted;
Vakul Garga42055e2018-09-21 09:46:13 +0530360 rc = tls_push_sg(sk, tls_ctx,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200361 &msg_en->sg.data[msg_en->sg.curr],
Vakul Garga42055e2018-09-21 09:46:13 +0530362 0, tx_flags);
363 if (rc)
364 goto tx_err;
365
Vakul Garga42055e2018-09-21 09:46:13 +0530366 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200367 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +0530368 kfree(rec);
369 } else {
370 break;
371 }
372 }
373
374tx_err:
375 if (rc < 0 && rc != -EAGAIN)
376 tls_err_abort(sk, EBADMSG);
377
378 return rc;
379}
380
381static void tls_encrypt_done(struct crypto_async_request *req, int err)
382{
383 struct aead_request *aead_req = (struct aead_request *)req;
384 struct sock *sk = req->data;
385 struct tls_context *tls_ctx = tls_get_ctx(sk);
386 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200387 struct scatterlist *sge;
388 struct sk_msg *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530389 struct tls_rec *rec;
390 bool ready = false;
391 int pending;
392
393 rec = container_of(aead_req, struct tls_rec, aead_req);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200394 msg_en = &rec->msg_encrypted;
Vakul Garga42055e2018-09-21 09:46:13 +0530395
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200396 sge = sk_msg_elem(msg_en, msg_en->sg.curr);
397 sge->offset -= tls_ctx->tx.prepend_size;
398 sge->length += tls_ctx->tx.prepend_size;
Vakul Garga42055e2018-09-21 09:46:13 +0530399
Vakul Garg80ece6a2018-09-26 16:22:08 +0530400 /* Check if error is previously set on socket */
Vakul Garga42055e2018-09-21 09:46:13 +0530401 if (err || sk->sk_err) {
Vakul Garga42055e2018-09-21 09:46:13 +0530402 rec = NULL;
403
404 /* If err is already set on socket, return the same code */
405 if (sk->sk_err) {
406 ctx->async_wait.err = sk->sk_err;
407 } else {
408 ctx->async_wait.err = err;
409 tls_err_abort(sk, err);
410 }
411 }
412
Vakul Garg9932a292018-09-24 15:35:56 +0530413 if (rec) {
414 struct tls_rec *first_rec;
415
416 /* Mark the record as ready for transmission */
417 smp_store_mb(rec->tx_ready, true);
418
419 /* If received record is at head of tx_list, schedule tx */
420 first_rec = list_first_entry(&ctx->tx_list,
421 struct tls_rec, list);
422 if (rec == first_rec)
423 ready = true;
424 }
Vakul Garga42055e2018-09-21 09:46:13 +0530425
426 pending = atomic_dec_return(&ctx->encrypt_pending);
427
428 if (!pending && READ_ONCE(ctx->async_notify))
429 complete(&ctx->async_wait.completion);
430
431 if (!ready)
432 return;
433
434 /* Schedule the transmission */
435 if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200436 schedule_delayed_work(&ctx->tx_work.work, 1);
Vakul Garga42055e2018-09-21 09:46:13 +0530437}
438
439static int tls_do_encryption(struct sock *sk,
440 struct tls_context *tls_ctx,
Daniel Borkmanna447da72018-06-15 03:07:45 +0200441 struct tls_sw_context_tx *ctx,
442 struct aead_request *aead_req,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200443 size_t data_len, u32 start)
Dave Watson3c4d7552017-06-14 11:37:39 -0700444{
Vakul Garga42055e2018-09-21 09:46:13 +0530445 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200446 struct sk_msg *msg_en = &rec->msg_encrypted;
447 struct scatterlist *sge = sk_msg_elem(msg_en, start);
Dave Watson3c4d7552017-06-14 11:37:39 -0700448 int rc;
449
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200450 sge->offset += tls_ctx->tx.prepend_size;
451 sge->length -= tls_ctx->tx.prepend_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700452
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200453 msg_en->sg.curr = start;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530454
Dave Watson3c4d7552017-06-14 11:37:39 -0700455 aead_request_set_tfm(aead_req, ctx->aead_send);
456 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200457 aead_request_set_crypt(aead_req, rec->sg_aead_in,
458 rec->sg_aead_out,
Dave Watsondbe42552018-03-22 10:10:06 -0700459 data_len, tls_ctx->tx.iv);
Vakul Garga54667f2018-01-31 21:34:37 +0530460
461 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Vakul Garga42055e2018-09-21 09:46:13 +0530462 tls_encrypt_done, sk);
Vakul Garga54667f2018-01-31 21:34:37 +0530463
Vakul Garg9932a292018-09-24 15:35:56 +0530464 /* Add the record in tx_list */
465 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +0530466 atomic_inc(&ctx->encrypt_pending);
Dave Watson3c4d7552017-06-14 11:37:39 -0700467
Vakul Garga42055e2018-09-21 09:46:13 +0530468 rc = crypto_aead_encrypt(aead_req);
469 if (!rc || rc != -EINPROGRESS) {
470 atomic_dec(&ctx->encrypt_pending);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200471 sge->offset -= tls_ctx->tx.prepend_size;
472 sge->length += tls_ctx->tx.prepend_size;
Vakul Garga42055e2018-09-21 09:46:13 +0530473 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700474
Vakul Garg9932a292018-09-24 15:35:56 +0530475 if (!rc) {
476 WRITE_ONCE(rec->tx_ready, true);
477 } else if (rc != -EINPROGRESS) {
478 list_del(&rec->list);
Vakul Garga42055e2018-09-21 09:46:13 +0530479 return rc;
Vakul Garg9932a292018-09-24 15:35:56 +0530480 }
Vakul Garga42055e2018-09-21 09:46:13 +0530481
482 /* Unhook the record from context if encryption is not failure */
483 ctx->open_rec = NULL;
484 tls_advance_record_sn(sk, &tls_ctx->tx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700485 return rc;
486}
487
John Fastabendd3b18ad32018-10-13 02:46:01 +0200488static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
489 struct tls_rec **to, struct sk_msg *msg_opl,
490 struct sk_msg *msg_oen, u32 split_point,
491 u32 tx_overhead_size, u32 *orig_end)
492{
493 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
494 struct scatterlist *sge, *osge, *nsge;
495 u32 orig_size = msg_opl->sg.size;
496 struct scatterlist tmp = { };
497 struct sk_msg *msg_npl;
498 struct tls_rec *new;
499 int ret;
500
501 new = tls_get_rec(sk);
502 if (!new)
503 return -ENOMEM;
504 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
505 tx_overhead_size, 0);
506 if (ret < 0) {
507 tls_free_rec(sk, new);
508 return ret;
509 }
510
511 *orig_end = msg_opl->sg.end;
512 i = msg_opl->sg.start;
513 sge = sk_msg_elem(msg_opl, i);
514 while (apply && sge->length) {
515 if (sge->length > apply) {
516 u32 len = sge->length - apply;
517
518 get_page(sg_page(sge));
519 sg_set_page(&tmp, sg_page(sge), len,
520 sge->offset + apply);
521 sge->length = apply;
522 bytes += apply;
523 apply = 0;
524 } else {
525 apply -= sge->length;
526 bytes += sge->length;
527 }
528
529 sk_msg_iter_var_next(i);
530 if (i == msg_opl->sg.end)
531 break;
532 sge = sk_msg_elem(msg_opl, i);
533 }
534
535 msg_opl->sg.end = i;
536 msg_opl->sg.curr = i;
537 msg_opl->sg.copybreak = 0;
538 msg_opl->apply_bytes = 0;
539 msg_opl->sg.size = bytes;
540
541 msg_npl = &new->msg_plaintext;
542 msg_npl->apply_bytes = apply;
543 msg_npl->sg.size = orig_size - bytes;
544
545 j = msg_npl->sg.start;
546 nsge = sk_msg_elem(msg_npl, j);
547 if (tmp.length) {
548 memcpy(nsge, &tmp, sizeof(*nsge));
549 sk_msg_iter_var_next(j);
550 nsge = sk_msg_elem(msg_npl, j);
551 }
552
553 osge = sk_msg_elem(msg_opl, i);
554 while (osge->length) {
555 memcpy(nsge, osge, sizeof(*nsge));
556 sg_unmark_end(nsge);
557 sk_msg_iter_var_next(i);
558 sk_msg_iter_var_next(j);
559 if (i == *orig_end)
560 break;
561 osge = sk_msg_elem(msg_opl, i);
562 nsge = sk_msg_elem(msg_npl, j);
563 }
564
565 msg_npl->sg.end = j;
566 msg_npl->sg.curr = j;
567 msg_npl->sg.copybreak = 0;
568
569 *to = new;
570 return 0;
571}
572
573static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
574 struct tls_rec *from, u32 orig_end)
575{
576 struct sk_msg *msg_npl = &from->msg_plaintext;
577 struct sk_msg *msg_opl = &to->msg_plaintext;
578 struct scatterlist *osge, *nsge;
579 u32 i, j;
580
581 i = msg_opl->sg.end;
582 sk_msg_iter_var_prev(i);
583 j = msg_npl->sg.start;
584
585 osge = sk_msg_elem(msg_opl, i);
586 nsge = sk_msg_elem(msg_npl, j);
587
588 if (sg_page(osge) == sg_page(nsge) &&
589 osge->offset + osge->length == nsge->offset) {
590 osge->length += nsge->length;
591 put_page(sg_page(nsge));
592 }
593
594 msg_opl->sg.end = orig_end;
595 msg_opl->sg.curr = orig_end;
596 msg_opl->sg.copybreak = 0;
597 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
598 msg_opl->sg.size += msg_npl->sg.size;
599
600 sk_msg_free(sk, &to->msg_encrypted);
601 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
602
603 kfree(from);
604}
605
Dave Watson3c4d7552017-06-14 11:37:39 -0700606static int tls_push_record(struct sock *sk, int flags,
607 unsigned char record_type)
608{
609 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300610 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200611 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
612 u32 i, split_point, uninitialized_var(orig_end);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200613 struct sk_msg *msg_pl, *msg_en;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200614 struct aead_request *req;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200615 bool split;
Dave Watson3c4d7552017-06-14 11:37:39 -0700616 int rc;
617
Vakul Garga42055e2018-09-21 09:46:13 +0530618 if (!rec)
619 return 0;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200620
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200621 msg_pl = &rec->msg_plaintext;
622 msg_en = &rec->msg_encrypted;
623
John Fastabendd3b18ad32018-10-13 02:46:01 +0200624 split_point = msg_pl->apply_bytes;
625 split = split_point && split_point < msg_pl->sg.size;
626 if (split) {
627 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
628 split_point, tls_ctx->tx.overhead_size,
629 &orig_end);
630 if (rc < 0)
631 return rc;
632 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
633 tls_ctx->tx.overhead_size);
634 }
635
Vakul Garga42055e2018-09-21 09:46:13 +0530636 rec->tx_flags = flags;
637 req = &rec->aead_req;
Dave Watson3c4d7552017-06-14 11:37:39 -0700638
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200639 i = msg_pl->sg.end;
640 sk_msg_iter_var_prev(i);
641 sg_mark_end(sk_msg_elem(msg_pl, i));
Vakul Garga42055e2018-09-21 09:46:13 +0530642
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200643 i = msg_pl->sg.start;
644 sg_chain(rec->sg_aead_in, 2, rec->inplace_crypto ?
645 &msg_en->sg.data[i] : &msg_pl->sg.data[i]);
646
647 i = msg_en->sg.end;
648 sk_msg_iter_var_prev(i);
649 sg_mark_end(sk_msg_elem(msg_en, i));
650
651 i = msg_en->sg.start;
652 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
653
654 tls_make_aad(rec->aad_space, msg_pl->sg.size,
Dave Watsondbe42552018-03-22 10:10:06 -0700655 tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size,
Dave Watson3c4d7552017-06-14 11:37:39 -0700656 record_type);
657
658 tls_fill_prepend(tls_ctx,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200659 page_address(sg_page(&msg_en->sg.data[i])) +
660 msg_en->sg.data[i].offset, msg_pl->sg.size,
661 record_type);
Dave Watson3c4d7552017-06-14 11:37:39 -0700662
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200663 tls_ctx->pending_open_record_frags = false;
Dave Watson3c4d7552017-06-14 11:37:39 -0700664
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200665 rc = tls_do_encryption(sk, tls_ctx, ctx, req, msg_pl->sg.size, i);
Dave Watson3c4d7552017-06-14 11:37:39 -0700666 if (rc < 0) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200667 if (rc != -EINPROGRESS) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200668 tls_err_abort(sk, EBADMSG);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200669 if (split) {
670 tls_ctx->pending_open_record_frags = true;
671 tls_merge_open_record(sk, rec, tmp, orig_end);
672 }
673 }
Vakul Garga42055e2018-09-21 09:46:13 +0530674 return rc;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200675 } else if (split) {
676 msg_pl = &tmp->msg_plaintext;
677 msg_en = &tmp->msg_encrypted;
678 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
679 tls_ctx->tx.overhead_size);
680 tls_ctx->pending_open_record_frags = true;
681 ctx->open_rec = tmp;
Dave Watson3c4d7552017-06-14 11:37:39 -0700682 }
683
Vakul Garg9932a292018-09-24 15:35:56 +0530684 return tls_tx_records(sk, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700685}
686
John Fastabendd3b18ad32018-10-13 02:46:01 +0200687static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
688 bool full_record, u8 record_type,
689 size_t *copied, int flags)
Dave Watson3c4d7552017-06-14 11:37:39 -0700690{
691 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300692 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200693 struct sk_msg msg_redir = { };
694 struct sk_psock *psock;
695 struct sock *sk_redir;
Vakul Garga42055e2018-09-21 09:46:13 +0530696 struct tls_rec *rec;
John Fastabend0608c692018-12-20 11:35:35 -0800697 bool enospc, policy;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200698 int err = 0, send;
John Fastabend7246d8e2018-11-26 14:16:17 -0800699 u32 delta = 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530700
John Fastabend0608c692018-12-20 11:35:35 -0800701 policy = !(flags & MSG_SENDPAGE_NOPOLICY);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200702 psock = sk_psock_get(sk);
John Fastabend0608c692018-12-20 11:35:35 -0800703 if (!psock || !policy)
John Fastabendd3b18ad32018-10-13 02:46:01 +0200704 return tls_push_record(sk, flags, record_type);
705more_data:
706 enospc = sk_msg_full(msg);
John Fastabend7246d8e2018-11-26 14:16:17 -0800707 if (psock->eval == __SK_NONE) {
708 delta = msg->sg.size;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200709 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
John Fastabend7246d8e2018-11-26 14:16:17 -0800710 if (delta < msg->sg.size)
711 delta -= msg->sg.size;
712 else
713 delta = 0;
714 }
John Fastabendd3b18ad32018-10-13 02:46:01 +0200715 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
716 !enospc && !full_record) {
717 err = -ENOSPC;
718 goto out_err;
719 }
720 msg->cork_bytes = 0;
721 send = msg->sg.size;
722 if (msg->apply_bytes && msg->apply_bytes < send)
723 send = msg->apply_bytes;
Vakul Garga42055e2018-09-21 09:46:13 +0530724
John Fastabendd3b18ad32018-10-13 02:46:01 +0200725 switch (psock->eval) {
726 case __SK_PASS:
727 err = tls_push_record(sk, flags, record_type);
728 if (err < 0) {
729 *copied -= sk_msg_free(sk, msg);
730 tls_free_open_rec(sk);
731 goto out_err;
732 }
733 break;
734 case __SK_REDIRECT:
735 sk_redir = psock->sk_redir;
736 memcpy(&msg_redir, msg, sizeof(*msg));
737 if (msg->apply_bytes < send)
738 msg->apply_bytes = 0;
739 else
740 msg->apply_bytes -= send;
741 sk_msg_return_zero(sk, msg, send);
742 msg->sg.size -= send;
743 release_sock(sk);
744 err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
745 lock_sock(sk);
746 if (err < 0) {
747 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
748 msg->sg.size = 0;
749 }
750 if (msg->sg.size == 0)
751 tls_free_open_rec(sk);
752 break;
753 case __SK_DROP:
754 default:
755 sk_msg_free_partial(sk, msg, send);
756 if (msg->apply_bytes < send)
757 msg->apply_bytes = 0;
758 else
759 msg->apply_bytes -= send;
760 if (msg->sg.size == 0)
761 tls_free_open_rec(sk);
John Fastabend7246d8e2018-11-26 14:16:17 -0800762 *copied -= (send + delta);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200763 err = -EACCES;
764 }
Vakul Garga42055e2018-09-21 09:46:13 +0530765
John Fastabendd3b18ad32018-10-13 02:46:01 +0200766 if (likely(!err)) {
767 bool reset_eval = !ctx->open_rec;
768
769 rec = ctx->open_rec;
770 if (rec) {
771 msg = &rec->msg_plaintext;
772 if (!msg->apply_bytes)
773 reset_eval = true;
774 }
775 if (reset_eval) {
776 psock->eval = __SK_NONE;
777 if (psock->sk_redir) {
778 sock_put(psock->sk_redir);
779 psock->sk_redir = NULL;
780 }
781 }
782 if (rec)
783 goto more_data;
784 }
785 out_err:
786 sk_psock_put(sk, psock);
787 return err;
788}
789
790static int tls_sw_push_pending_record(struct sock *sk, int flags)
791{
792 struct tls_context *tls_ctx = tls_get_ctx(sk);
793 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
794 struct tls_rec *rec = ctx->open_rec;
795 struct sk_msg *msg_pl;
796 size_t copied;
797
Vakul Garga42055e2018-09-21 09:46:13 +0530798 if (!rec)
John Fastabendd3b18ad32018-10-13 02:46:01 +0200799 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530800
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200801 msg_pl = &rec->msg_plaintext;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200802 copied = msg_pl->sg.size;
803 if (!copied)
804 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530805
John Fastabendd3b18ad32018-10-13 02:46:01 +0200806 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
807 &copied, flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530808}
809
810int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
811{
Dave Watson3c4d7552017-06-14 11:37:39 -0700812 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
Vakul Garga42055e2018-09-21 09:46:13 +0530813 struct tls_context *tls_ctx = tls_get_ctx(sk);
814 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
815 struct crypto_tfm *tfm = crypto_aead_tfm(ctx->aead_send);
816 bool async_capable = tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
817 unsigned char record_type = TLS_RECORD_TYPE_DATA;
David Howells00e23702018-10-22 13:07:28 +0100818 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Dave Watson3c4d7552017-06-14 11:37:39 -0700819 bool eor = !(msg->msg_flags & MSG_MORE);
820 size_t try_to_copy, copied = 0;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200821 struct sk_msg *msg_pl, *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530822 struct tls_rec *rec;
823 int required_size;
824 int num_async = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700825 bool full_record;
Vakul Garga42055e2018-09-21 09:46:13 +0530826 int record_room;
827 int num_zc = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700828 int orig_size;
Vakul Garg4128c0c2018-09-24 16:09:49 +0530829 int ret = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700830
831 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
832 return -ENOTSUPP;
833
834 lock_sock(sk);
835
Vakul Garga42055e2018-09-21 09:46:13 +0530836 /* Wait till there is any pending write on socket */
837 if (unlikely(sk->sk_write_pending)) {
838 ret = wait_on_pending_writer(sk, &timeo);
839 if (unlikely(ret))
840 goto send_end;
841 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700842
843 if (unlikely(msg->msg_controllen)) {
844 ret = tls_proccess_cmsg(sk, msg, &record_type);
Vakul Garga42055e2018-09-21 09:46:13 +0530845 if (ret) {
846 if (ret == -EINPROGRESS)
847 num_async++;
848 else if (ret != -EAGAIN)
849 goto send_end;
850 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700851 }
852
853 while (msg_data_left(msg)) {
854 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +0100855 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -0700856 goto send_end;
857 }
858
John Fastabendd3b18ad32018-10-13 02:46:01 +0200859 if (ctx->open_rec)
860 rec = ctx->open_rec;
861 else
862 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +0530863 if (!rec) {
864 ret = -ENOMEM;
865 goto send_end;
866 }
867
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200868 msg_pl = &rec->msg_plaintext;
869 msg_en = &rec->msg_encrypted;
870
871 orig_size = msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700872 full_record = false;
873 try_to_copy = msg_data_left(msg);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200874 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700875 if (try_to_copy >= record_room) {
876 try_to_copy = record_room;
877 full_record = true;
878 }
879
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200880 required_size = msg_pl->sg.size + try_to_copy +
Dave Watsondbe42552018-03-22 10:10:06 -0700881 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700882
883 if (!sk_stream_memory_free(sk))
884 goto wait_for_sndbuf;
Vakul Garga42055e2018-09-21 09:46:13 +0530885
Dave Watson3c4d7552017-06-14 11:37:39 -0700886alloc_encrypted:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200887 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700888 if (ret) {
889 if (ret != -ENOSPC)
890 goto wait_for_memory;
891
892 /* Adjust try_to_copy according to the amount that was
893 * actually allocated. The difference is due
894 * to max sg elements limit
895 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200896 try_to_copy -= required_size - msg_en->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700897 full_record = true;
898 }
Vakul Garga42055e2018-09-21 09:46:13 +0530899
900 if (!is_kvec && (full_record || eor) && !async_capable) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200901 u32 first = msg_pl->sg.end;
902
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200903 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
904 msg_pl, try_to_copy);
Dave Watson3c4d7552017-06-14 11:37:39 -0700905 if (ret)
906 goto fallback_to_reg_send;
907
Vakul Garg4e6d4722018-09-30 08:04:35 +0530908 rec->inplace_crypto = 0;
909
Vakul Garga42055e2018-09-21 09:46:13 +0530910 num_zc++;
Dave Watson3c4d7552017-06-14 11:37:39 -0700911 copied += try_to_copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200912
913 sk_msg_sg_copy_set(msg_pl, first);
914 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
915 record_type, &copied,
916 msg->msg_flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530917 if (ret) {
918 if (ret == -EINPROGRESS)
919 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200920 else if (ret == -ENOMEM)
921 goto wait_for_memory;
922 else if (ret == -ENOSPC)
923 goto rollback_iter;
Vakul Garga42055e2018-09-21 09:46:13 +0530924 else if (ret != -EAGAIN)
925 goto send_end;
926 }
Doron Roberts-Kedes5a3611e2018-07-26 07:59:35 -0700927 continue;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200928rollback_iter:
929 copied -= try_to_copy;
930 sk_msg_sg_copy_clear(msg_pl, first);
931 iov_iter_revert(&msg->msg_iter,
932 msg_pl->sg.size - orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700933fallback_to_reg_send:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200934 sk_msg_trim(sk, msg_pl, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700935 }
936
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200937 required_size = msg_pl->sg.size + try_to_copy;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530938
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200939 ret = tls_clone_plaintext_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700940 if (ret) {
941 if (ret != -ENOSPC)
Vakul Garg4e6d4722018-09-30 08:04:35 +0530942 goto send_end;
Dave Watson3c4d7552017-06-14 11:37:39 -0700943
944 /* Adjust try_to_copy according to the amount that was
945 * actually allocated. The difference is due
946 * to max sg elements limit
947 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200948 try_to_copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700949 full_record = true;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200950 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
951 tls_ctx->tx.overhead_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700952 }
953
Vakul Garg65a10e22018-12-21 15:16:52 +0000954 if (try_to_copy) {
955 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
956 msg_pl, try_to_copy);
957 if (ret < 0)
958 goto trim_sgl;
959 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700960
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200961 /* Open records defined only if successfully copied, otherwise
962 * we would trim the sg but not reset the open record frags.
963 */
964 tls_ctx->pending_open_record_frags = true;
Dave Watson3c4d7552017-06-14 11:37:39 -0700965 copied += try_to_copy;
966 if (full_record || eor) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200967 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
968 record_type, &copied,
969 msg->msg_flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700970 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +0530971 if (ret == -EINPROGRESS)
972 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200973 else if (ret == -ENOMEM)
974 goto wait_for_memory;
975 else if (ret != -EAGAIN) {
976 if (ret == -ENOSPC)
977 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530978 goto send_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200979 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700980 }
981 }
982
983 continue;
984
985wait_for_sndbuf:
986 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
987wait_for_memory:
988 ret = sk_stream_wait_memory(sk, &timeo);
989 if (ret) {
990trim_sgl:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200991 tls_trim_both_msgs(sk, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700992 goto send_end;
993 }
994
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200995 if (msg_en->sg.size < required_size)
Dave Watson3c4d7552017-06-14 11:37:39 -0700996 goto alloc_encrypted;
Dave Watson3c4d7552017-06-14 11:37:39 -0700997 }
998
Vakul Garga42055e2018-09-21 09:46:13 +0530999 if (!num_async) {
1000 goto send_end;
1001 } else if (num_zc) {
1002 /* Wait for pending encryptions to get completed */
1003 smp_store_mb(ctx->async_notify, true);
1004
1005 if (atomic_read(&ctx->encrypt_pending))
1006 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1007 else
1008 reinit_completion(&ctx->async_wait.completion);
1009
1010 WRITE_ONCE(ctx->async_notify, false);
1011
1012 if (ctx->async_wait.err) {
1013 ret = ctx->async_wait.err;
1014 copied = 0;
1015 }
1016 }
1017
1018 /* Transmit if any encryptions have completed */
1019 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1020 cancel_delayed_work(&ctx->tx_work.work);
1021 tls_tx_records(sk, msg->msg_flags);
1022 }
1023
Dave Watson3c4d7552017-06-14 11:37:39 -07001024send_end:
1025 ret = sk_stream_error(sk, msg->msg_flags, ret);
1026
1027 release_sock(sk);
1028 return copied ? copied : ret;
1029}
1030
YueHaibing01cb8a12019-01-16 10:39:28 +08001031static int tls_sw_do_sendpage(struct sock *sk, struct page *page,
1032 int offset, size_t size, int flags)
Dave Watson3c4d7552017-06-14 11:37:39 -07001033{
Vakul Garga42055e2018-09-21 09:46:13 +05301034 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
Dave Watson3c4d7552017-06-14 11:37:39 -07001035 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001036 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -07001037 unsigned char record_type = TLS_RECORD_TYPE_DATA;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001038 struct sk_msg *msg_pl;
Vakul Garga42055e2018-09-21 09:46:13 +05301039 struct tls_rec *rec;
1040 int num_async = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001041 size_t copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001042 bool full_record;
1043 int record_room;
Vakul Garg4128c0c2018-09-24 16:09:49 +05301044 int ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301045 bool eor;
Dave Watson3c4d7552017-06-14 11:37:39 -07001046
Dave Watson3c4d7552017-06-14 11:37:39 -07001047 eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
Dave Watson3c4d7552017-06-14 11:37:39 -07001048 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1049
Vakul Garga42055e2018-09-21 09:46:13 +05301050 /* Wait till there is any pending write on socket */
1051 if (unlikely(sk->sk_write_pending)) {
1052 ret = wait_on_pending_writer(sk, &timeo);
1053 if (unlikely(ret))
1054 goto sendpage_end;
1055 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001056
1057 /* Call the sk_stream functions to manage the sndbuf mem. */
1058 while (size > 0) {
1059 size_t copy, required_size;
1060
1061 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +01001062 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -07001063 goto sendpage_end;
1064 }
1065
John Fastabendd3b18ad32018-10-13 02:46:01 +02001066 if (ctx->open_rec)
1067 rec = ctx->open_rec;
1068 else
1069 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +05301070 if (!rec) {
1071 ret = -ENOMEM;
1072 goto sendpage_end;
1073 }
1074
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001075 msg_pl = &rec->msg_plaintext;
1076
Dave Watson3c4d7552017-06-14 11:37:39 -07001077 full_record = false;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001078 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001079 copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001080 copy = size;
1081 if (copy >= record_room) {
1082 copy = record_room;
1083 full_record = true;
1084 }
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001085
1086 required_size = msg_pl->sg.size + copy +
1087 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001088
1089 if (!sk_stream_memory_free(sk))
1090 goto wait_for_sndbuf;
1091alloc_payload:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001092 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001093 if (ret) {
1094 if (ret != -ENOSPC)
1095 goto wait_for_memory;
1096
1097 /* Adjust copy according to the amount that was
1098 * actually allocated. The difference is due
1099 * to max sg elements limit
1100 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001101 copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001102 full_record = true;
1103 }
1104
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001105 sk_msg_page_add(msg_pl, page, copy, offset);
Dave Watson3c4d7552017-06-14 11:37:39 -07001106 sk_mem_charge(sk, copy);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001107
Dave Watson3c4d7552017-06-14 11:37:39 -07001108 offset += copy;
1109 size -= copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001110 copied += copy;
Dave Watson3c4d7552017-06-14 11:37:39 -07001111
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001112 tls_ctx->pending_open_record_frags = true;
1113 if (full_record || eor || sk_msg_full(msg_pl)) {
Vakul Garg4e6d4722018-09-30 08:04:35 +05301114 rec->inplace_crypto = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001115 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1116 record_type, &copied, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -07001117 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +05301118 if (ret == -EINPROGRESS)
1119 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001120 else if (ret == -ENOMEM)
1121 goto wait_for_memory;
1122 else if (ret != -EAGAIN) {
1123 if (ret == -ENOSPC)
1124 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301125 goto sendpage_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001126 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001127 }
1128 }
1129 continue;
1130wait_for_sndbuf:
1131 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1132wait_for_memory:
1133 ret = sk_stream_wait_memory(sk, &timeo);
1134 if (ret) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001135 tls_trim_both_msgs(sk, msg_pl->sg.size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001136 goto sendpage_end;
1137 }
1138
Dave Watson3c4d7552017-06-14 11:37:39 -07001139 goto alloc_payload;
1140 }
1141
Vakul Garga42055e2018-09-21 09:46:13 +05301142 if (num_async) {
1143 /* Transmit if any encryptions have completed */
1144 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1145 cancel_delayed_work(&ctx->tx_work.work);
1146 tls_tx_records(sk, flags);
1147 }
1148 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001149sendpage_end:
John Fastabendd3b18ad32018-10-13 02:46:01 +02001150 ret = sk_stream_error(sk, flags, ret);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001151 return copied ? copied : ret;
Dave Watson3c4d7552017-06-14 11:37:39 -07001152}
1153
John Fastabend0608c692018-12-20 11:35:35 -08001154int tls_sw_sendpage(struct sock *sk, struct page *page,
1155 int offset, size_t size, int flags)
1156{
1157 int ret;
1158
1159 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1160 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1161 return -ENOTSUPP;
1162
1163 lock_sock(sk);
1164 ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
1165 release_sock(sk);
1166 return ret;
1167}
1168
John Fastabendd3b18ad32018-10-13 02:46:01 +02001169static struct sk_buff *tls_wait_data(struct sock *sk, struct sk_psock *psock,
1170 int flags, long timeo, int *err)
Dave Watsonc46234e2018-03-22 10:10:35 -07001171{
1172 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001173 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001174 struct sk_buff *skb;
1175 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1176
John Fastabendd3b18ad32018-10-13 02:46:01 +02001177 while (!(skb = ctx->recv_pkt) && sk_psock_queue_empty(psock)) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001178 if (sk->sk_err) {
1179 *err = sock_error(sk);
1180 return NULL;
1181 }
1182
Doron Roberts-Kedesfcf47932018-07-18 16:22:27 -07001183 if (sk->sk_shutdown & RCV_SHUTDOWN)
1184 return NULL;
1185
Dave Watsonc46234e2018-03-22 10:10:35 -07001186 if (sock_flag(sk, SOCK_DONE))
1187 return NULL;
1188
1189 if ((flags & MSG_DONTWAIT) || !timeo) {
1190 *err = -EAGAIN;
1191 return NULL;
1192 }
1193
1194 add_wait_queue(sk_sleep(sk), &wait);
1195 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001196 sk_wait_event(sk, &timeo,
1197 ctx->recv_pkt != skb ||
1198 !sk_psock_queue_empty(psock),
1199 &wait);
Dave Watsonc46234e2018-03-22 10:10:35 -07001200 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1201 remove_wait_queue(sk_sleep(sk), &wait);
1202
1203 /* Handle signals */
1204 if (signal_pending(current)) {
1205 *err = sock_intr_errno(timeo);
1206 return NULL;
1207 }
1208 }
1209
1210 return skb;
1211}
1212
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001213static int tls_setup_from_iter(struct sock *sk, struct iov_iter *from,
1214 int length, int *pages_used,
1215 unsigned int *size_used,
1216 struct scatterlist *to,
1217 int to_max_pages)
1218{
1219 int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1220 struct page *pages[MAX_SKB_FRAGS];
1221 unsigned int size = *size_used;
1222 ssize_t copied, use;
1223 size_t offset;
1224
1225 while (length > 0) {
1226 i = 0;
1227 maxpages = to_max_pages - num_elem;
1228 if (maxpages == 0) {
1229 rc = -EFAULT;
1230 goto out;
1231 }
1232 copied = iov_iter_get_pages(from, pages,
1233 length,
1234 maxpages, &offset);
1235 if (copied <= 0) {
1236 rc = -EFAULT;
1237 goto out;
1238 }
1239
1240 iov_iter_advance(from, copied);
1241
1242 length -= copied;
1243 size += copied;
1244 while (copied) {
1245 use = min_t(int, copied, PAGE_SIZE - offset);
1246
1247 sg_set_page(&to[num_elem],
1248 pages[i], use, offset);
1249 sg_unmark_end(&to[num_elem]);
1250 /* We do not uncharge memory from this API */
1251
1252 offset = 0;
1253 copied -= use;
1254
1255 i++;
1256 num_elem++;
1257 }
1258 }
1259 /* Mark the end in the last sg entry if newly added */
1260 if (num_elem > *pages_used)
1261 sg_mark_end(&to[num_elem - 1]);
1262out:
1263 if (rc)
1264 iov_iter_revert(from, size - *size_used);
1265 *size_used = size;
1266 *pages_used = num_elem;
1267
1268 return rc;
1269}
1270
Vakul Garg0b243d02018-08-10 20:46:41 +05301271/* This function decrypts the input skb into either out_iov or in out_sg
1272 * or in skb buffers itself. The input parameter 'zc' indicates if
1273 * zero-copy mode needs to be tried or not. With zero-copy mode, either
1274 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
1275 * NULL, then the decryption happens inside skb buffers itself, i.e.
1276 * zero-copy gets disabled and 'zc' is updated.
1277 */
1278
1279static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1280 struct iov_iter *out_iov,
1281 struct scatterlist *out_sg,
Vakul Garg692d7b52019-01-16 10:40:16 +00001282 int *chunk, bool *zc, bool async)
Vakul Garg0b243d02018-08-10 20:46:41 +05301283{
1284 struct tls_context *tls_ctx = tls_get_ctx(sk);
1285 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1286 struct strp_msg *rxm = strp_msg(skb);
1287 int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0;
1288 struct aead_request *aead_req;
1289 struct sk_buff *unused;
1290 u8 *aad, *iv, *mem = NULL;
1291 struct scatterlist *sgin = NULL;
1292 struct scatterlist *sgout = NULL;
1293 const int data_len = rxm->full_len - tls_ctx->rx.overhead_size;
1294
1295 if (*zc && (out_iov || out_sg)) {
1296 if (out_iov)
1297 n_sgout = iov_iter_npages(out_iov, INT_MAX) + 1;
1298 else
1299 n_sgout = sg_nents(out_sg);
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -07001300 n_sgin = skb_nsg(skb, rxm->offset + tls_ctx->rx.prepend_size,
1301 rxm->full_len - tls_ctx->rx.prepend_size);
Vakul Garg0b243d02018-08-10 20:46:41 +05301302 } else {
1303 n_sgout = 0;
1304 *zc = false;
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -07001305 n_sgin = skb_cow_data(skb, 0, &unused);
Vakul Garg0b243d02018-08-10 20:46:41 +05301306 }
1307
Vakul Garg0b243d02018-08-10 20:46:41 +05301308 if (n_sgin < 1)
1309 return -EBADMSG;
1310
1311 /* Increment to accommodate AAD */
1312 n_sgin = n_sgin + 1;
1313
1314 nsg = n_sgin + n_sgout;
1315
1316 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
1317 mem_size = aead_size + (nsg * sizeof(struct scatterlist));
1318 mem_size = mem_size + TLS_AAD_SPACE_SIZE;
1319 mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv);
1320
1321 /* Allocate a single block of memory which contains
1322 * aead_req || sgin[] || sgout[] || aad || iv.
1323 * This order achieves correct alignment for aead_req, sgin, sgout.
1324 */
1325 mem = kmalloc(mem_size, sk->sk_allocation);
1326 if (!mem)
1327 return -ENOMEM;
1328
1329 /* Segment the allocated memory */
1330 aead_req = (struct aead_request *)mem;
1331 sgin = (struct scatterlist *)(mem + aead_size);
1332 sgout = sgin + n_sgin;
1333 aad = (u8 *)(sgout + n_sgout);
1334 iv = aad + TLS_AAD_SPACE_SIZE;
1335
1336 /* Prepare IV */
1337 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1338 iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1339 tls_ctx->rx.iv_size);
1340 if (err < 0) {
1341 kfree(mem);
1342 return err;
1343 }
1344 memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
1345
1346 /* Prepare AAD */
1347 tls_make_aad(aad, rxm->full_len - tls_ctx->rx.overhead_size,
1348 tls_ctx->rx.rec_seq, tls_ctx->rx.rec_seq_size,
1349 ctx->control);
1350
1351 /* Prepare sgin */
1352 sg_init_table(sgin, n_sgin);
1353 sg_set_buf(&sgin[0], aad, TLS_AAD_SPACE_SIZE);
1354 err = skb_to_sgvec(skb, &sgin[1],
1355 rxm->offset + tls_ctx->rx.prepend_size,
1356 rxm->full_len - tls_ctx->rx.prepend_size);
1357 if (err < 0) {
1358 kfree(mem);
1359 return err;
1360 }
1361
1362 if (n_sgout) {
1363 if (out_iov) {
1364 sg_init_table(sgout, n_sgout);
1365 sg_set_buf(&sgout[0], aad, TLS_AAD_SPACE_SIZE);
1366
1367 *chunk = 0;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001368 err = tls_setup_from_iter(sk, out_iov, data_len,
1369 &pages, chunk, &sgout[1],
1370 (n_sgout - 1));
Vakul Garg0b243d02018-08-10 20:46:41 +05301371 if (err < 0)
1372 goto fallback_to_reg_recv;
1373 } else if (out_sg) {
1374 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
1375 } else {
1376 goto fallback_to_reg_recv;
1377 }
1378 } else {
1379fallback_to_reg_recv:
1380 sgout = sgin;
1381 pages = 0;
Vakul Garg692d7b52019-01-16 10:40:16 +00001382 *chunk = data_len;
Vakul Garg0b243d02018-08-10 20:46:41 +05301383 *zc = false;
1384 }
1385
1386 /* Prepare and submit AEAD request */
Vakul Garg94524d82018-08-29 15:26:55 +05301387 err = tls_do_decryption(sk, skb, sgin, sgout, iv,
Vakul Garg692d7b52019-01-16 10:40:16 +00001388 data_len, aead_req, async);
Vakul Garg94524d82018-08-29 15:26:55 +05301389 if (err == -EINPROGRESS)
1390 return err;
Vakul Garg0b243d02018-08-10 20:46:41 +05301391
1392 /* Release the pages in case iov was mapped to pages */
1393 for (; pages > 0; pages--)
1394 put_page(sg_page(&sgout[pages]));
1395
1396 kfree(mem);
1397 return err;
1398}
1399
Boris Pismennydafb67f2018-07-13 14:33:40 +03001400static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
Vakul Garg692d7b52019-01-16 10:40:16 +00001401 struct iov_iter *dest, int *chunk, bool *zc,
1402 bool async)
Boris Pismennydafb67f2018-07-13 14:33:40 +03001403{
1404 struct tls_context *tls_ctx = tls_get_ctx(sk);
1405 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1406 struct strp_msg *rxm = strp_msg(skb);
1407 int err = 0;
1408
Boris Pismenny4799ac82018-07-13 14:33:43 +03001409#ifdef CONFIG_TLS_DEVICE
1410 err = tls_device_decrypted(sk, skb);
Boris Pismennydafb67f2018-07-13 14:33:40 +03001411 if (err < 0)
1412 return err;
Boris Pismenny4799ac82018-07-13 14:33:43 +03001413#endif
1414 if (!ctx->decrypted) {
Vakul Garg692d7b52019-01-16 10:40:16 +00001415 err = decrypt_internal(sk, skb, dest, NULL, chunk, zc, async);
Vakul Garg94524d82018-08-29 15:26:55 +05301416 if (err < 0) {
1417 if (err == -EINPROGRESS)
1418 tls_advance_record_sn(sk, &tls_ctx->rx);
1419
Boris Pismenny4799ac82018-07-13 14:33:43 +03001420 return err;
Vakul Garg94524d82018-08-29 15:26:55 +05301421 }
Boris Pismenny4799ac82018-07-13 14:33:43 +03001422 } else {
1423 *zc = false;
1424 }
Boris Pismennydafb67f2018-07-13 14:33:40 +03001425
1426 rxm->offset += tls_ctx->rx.prepend_size;
1427 rxm->full_len -= tls_ctx->rx.overhead_size;
1428 tls_advance_record_sn(sk, &tls_ctx->rx);
1429 ctx->decrypted = true;
1430 ctx->saved_data_ready(sk);
1431
1432 return err;
1433}
1434
1435int decrypt_skb(struct sock *sk, struct sk_buff *skb,
1436 struct scatterlist *sgout)
Dave Watsonc46234e2018-03-22 10:10:35 -07001437{
Vakul Garg0b243d02018-08-10 20:46:41 +05301438 bool zc = true;
1439 int chunk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001440
Vakul Garg692d7b52019-01-16 10:40:16 +00001441 return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc, false);
Dave Watsonc46234e2018-03-22 10:10:35 -07001442}
1443
1444static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb,
1445 unsigned int len)
1446{
1447 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001448 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001449
Vakul Garg94524d82018-08-29 15:26:55 +05301450 if (skb) {
1451 struct strp_msg *rxm = strp_msg(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001452
Vakul Garg94524d82018-08-29 15:26:55 +05301453 if (len < rxm->full_len) {
1454 rxm->offset += len;
1455 rxm->full_len -= len;
1456 return false;
1457 }
1458 kfree_skb(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001459 }
1460
1461 /* Finished with message */
1462 ctx->recv_pkt = NULL;
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -07001463 __strp_unpause(&ctx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07001464
1465 return true;
1466}
1467
Vakul Garg692d7b52019-01-16 10:40:16 +00001468/* This function traverses the rx_list in tls receive context to copies the
1469 * decrypted data records into the buffer provided by caller zero copy is not
1470 * true. Further, the records are removed from the rx_list if it is not a peek
1471 * case and the record has been consumed completely.
1472 */
1473static int process_rx_list(struct tls_sw_context_rx *ctx,
1474 struct msghdr *msg,
1475 size_t skip,
1476 size_t len,
1477 bool zc,
1478 bool is_peek)
1479{
1480 struct sk_buff *skb = skb_peek(&ctx->rx_list);
1481 ssize_t copied = 0;
1482
1483 while (skip && skb) {
1484 struct strp_msg *rxm = strp_msg(skb);
1485
1486 if (skip < rxm->full_len)
1487 break;
1488
1489 skip = skip - rxm->full_len;
1490 skb = skb_peek_next(skb, &ctx->rx_list);
1491 }
1492
1493 while (len && skb) {
1494 struct sk_buff *next_skb;
1495 struct strp_msg *rxm = strp_msg(skb);
1496 int chunk = min_t(unsigned int, rxm->full_len - skip, len);
1497
1498 if (!zc || (rxm->full_len - skip) > len) {
1499 int err = skb_copy_datagram_msg(skb, rxm->offset + skip,
1500 msg, chunk);
1501 if (err < 0)
1502 return err;
1503 }
1504
1505 len = len - chunk;
1506 copied = copied + chunk;
1507
1508 /* Consume the data from record if it is non-peek case*/
1509 if (!is_peek) {
1510 rxm->offset = rxm->offset + chunk;
1511 rxm->full_len = rxm->full_len - chunk;
1512
1513 /* Return if there is unconsumed data in the record */
1514 if (rxm->full_len - skip)
1515 break;
1516 }
1517
1518 /* The remaining skip-bytes must lie in 1st record in rx_list.
1519 * So from the 2nd record, 'skip' should be 0.
1520 */
1521 skip = 0;
1522
1523 if (msg)
1524 msg->msg_flags |= MSG_EOR;
1525
1526 next_skb = skb_peek_next(skb, &ctx->rx_list);
1527
1528 if (!is_peek) {
1529 skb_unlink(skb, &ctx->rx_list);
1530 kfree_skb(skb);
1531 }
1532
1533 skb = next_skb;
1534 }
1535
1536 return copied;
1537}
1538
Dave Watsonc46234e2018-03-22 10:10:35 -07001539int tls_sw_recvmsg(struct sock *sk,
1540 struct msghdr *msg,
1541 size_t len,
1542 int nonblock,
1543 int flags,
1544 int *addr_len)
1545{
1546 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001547 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001548 struct sk_psock *psock;
Vakul Garg692d7b52019-01-16 10:40:16 +00001549 unsigned char control = 0;
1550 ssize_t decrypted = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001551 struct strp_msg *rxm;
1552 struct sk_buff *skb;
1553 ssize_t copied = 0;
1554 bool cmsg = false;
Daniel Borkmann06030db2018-06-15 03:07:46 +02001555 int target, err = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001556 long timeo;
David Howells00e23702018-10-22 13:07:28 +01001557 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Vakul Garg692d7b52019-01-16 10:40:16 +00001558 bool is_peek = flags & MSG_PEEK;
Vakul Garg94524d82018-08-29 15:26:55 +05301559 int num_async = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001560
1561 flags |= nonblock;
1562
1563 if (unlikely(flags & MSG_ERRQUEUE))
1564 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1565
John Fastabendd3b18ad32018-10-13 02:46:01 +02001566 psock = sk_psock_get(sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001567 lock_sock(sk);
1568
Vakul Garg692d7b52019-01-16 10:40:16 +00001569 /* Process pending decrypted records. It must be non-zero-copy */
1570 err = process_rx_list(ctx, msg, 0, len, false, is_peek);
1571 if (err < 0) {
1572 tls_err_abort(sk, err);
1573 goto end;
1574 } else {
1575 copied = err;
1576 }
1577
1578 len = len - copied;
1579 if (len) {
1580 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
1581 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1582 } else {
1583 goto recv_end;
1584 }
1585
Dave Watsonc46234e2018-03-22 10:10:35 -07001586 do {
Vakul Garg692d7b52019-01-16 10:40:16 +00001587 bool retain_skb = false;
Vakul Garg94524d82018-08-29 15:26:55 +05301588 bool async = false;
Vakul Garg692d7b52019-01-16 10:40:16 +00001589 bool zc = false;
1590 int to_decrypt;
Dave Watsonc46234e2018-03-22 10:10:35 -07001591 int chunk = 0;
1592
John Fastabendd3b18ad32018-10-13 02:46:01 +02001593 skb = tls_wait_data(sk, psock, flags, timeo, &err);
1594 if (!skb) {
1595 if (psock) {
John Fastabend02c558b2018-10-16 11:08:04 -07001596 int ret = __tcp_bpf_recvmsg(sk, psock,
1597 msg, len, flags);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001598
1599 if (ret > 0) {
Vakul Garg692d7b52019-01-16 10:40:16 +00001600 decrypted += ret;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001601 len -= ret;
1602 continue;
1603 }
1604 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001605 goto recv_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001606 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001607
1608 rxm = strp_msg(skb);
Vakul Garg94524d82018-08-29 15:26:55 +05301609
Dave Watsonc46234e2018-03-22 10:10:35 -07001610 if (!cmsg) {
1611 int cerr;
1612
1613 cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1614 sizeof(ctx->control), &ctx->control);
1615 cmsg = true;
1616 control = ctx->control;
1617 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1618 if (cerr || msg->msg_flags & MSG_CTRUNC) {
1619 err = -EIO;
1620 goto recv_end;
1621 }
1622 }
1623 } else if (control != ctx->control) {
1624 goto recv_end;
1625 }
1626
Vakul Garg692d7b52019-01-16 10:40:16 +00001627 to_decrypt = rxm->full_len - tls_ctx->rx.overhead_size;
Dave Watsonc46234e2018-03-22 10:10:35 -07001628
Vakul Garg692d7b52019-01-16 10:40:16 +00001629 if (to_decrypt <= len && !is_kvec && !is_peek)
1630 zc = true;
Dave Watsonc46234e2018-03-22 10:10:35 -07001631
Vakul Garg692d7b52019-01-16 10:40:16 +00001632 err = decrypt_skb_update(sk, skb, &msg->msg_iter,
1633 &chunk, &zc, ctx->async_capable);
1634 if (err < 0 && err != -EINPROGRESS) {
1635 tls_err_abort(sk, EBADMSG);
1636 goto recv_end;
Dave Watsonc46234e2018-03-22 10:10:35 -07001637 }
1638
Vakul Garg692d7b52019-01-16 10:40:16 +00001639 if (err == -EINPROGRESS) {
1640 async = true;
1641 num_async++;
1642 goto pick_next_record;
1643 } else {
1644 if (!zc) {
1645 if (rxm->full_len > len) {
1646 retain_skb = true;
1647 chunk = len;
1648 } else {
1649 chunk = rxm->full_len;
1650 }
Vakul Garg94524d82018-08-29 15:26:55 +05301651
Vakul Garg692d7b52019-01-16 10:40:16 +00001652 err = skb_copy_datagram_msg(skb, rxm->offset,
1653 msg, chunk);
1654 if (err < 0)
1655 goto recv_end;
1656
1657 if (!is_peek) {
1658 rxm->offset = rxm->offset + chunk;
1659 rxm->full_len = rxm->full_len - chunk;
1660 }
1661 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001662 }
1663
Vakul Garg94524d82018-08-29 15:26:55 +05301664pick_next_record:
Vakul Garg692d7b52019-01-16 10:40:16 +00001665 if (chunk > len)
1666 chunk = len;
1667
1668 decrypted += chunk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001669 len -= chunk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001670
Vakul Garg692d7b52019-01-16 10:40:16 +00001671 /* For async or peek case, queue the current skb */
1672 if (async || is_peek || retain_skb) {
1673 skb_queue_tail(&ctx->rx_list, skb);
1674 skb = NULL;
1675 }
Vakul Garg94524d82018-08-29 15:26:55 +05301676
Vakul Garg692d7b52019-01-16 10:40:16 +00001677 if (tls_sw_advance_skb(sk, skb, chunk)) {
1678 /* Return full control message to
1679 * userspace before trying to parse
1680 * another message type
Daniel Borkmann50c6b582018-09-14 23:00:55 +02001681 */
Vakul Garg692d7b52019-01-16 10:40:16 +00001682 msg->msg_flags |= MSG_EOR;
1683 if (ctx->control != TLS_RECORD_TYPE_DATA)
1684 goto recv_end;
1685 } else {
Daniel Borkmann50c6b582018-09-14 23:00:55 +02001686 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001687 }
Vakul Garg94524d82018-08-29 15:26:55 +05301688
Daniel Borkmann06030db2018-06-15 03:07:46 +02001689 /* If we have a new message from strparser, continue now. */
Vakul Garg692d7b52019-01-16 10:40:16 +00001690 if (decrypted >= target && !ctx->recv_pkt)
Daniel Borkmann06030db2018-06-15 03:07:46 +02001691 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001692 } while (len);
1693
1694recv_end:
Vakul Garg94524d82018-08-29 15:26:55 +05301695 if (num_async) {
1696 /* Wait for all previously submitted records to be decrypted */
1697 smp_store_mb(ctx->async_notify, true);
1698 if (atomic_read(&ctx->decrypt_pending)) {
1699 err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1700 if (err) {
1701 /* one of async decrypt failed */
1702 tls_err_abort(sk, err);
1703 copied = 0;
Vakul Garg692d7b52019-01-16 10:40:16 +00001704 decrypted = 0;
1705 goto end;
Vakul Garg94524d82018-08-29 15:26:55 +05301706 }
1707 } else {
1708 reinit_completion(&ctx->async_wait.completion);
1709 }
1710 WRITE_ONCE(ctx->async_notify, false);
Vakul Garg692d7b52019-01-16 10:40:16 +00001711
1712 /* Drain records from the rx_list & copy if required */
1713 if (is_peek || is_kvec)
1714 err = process_rx_list(ctx, msg, copied,
1715 decrypted, false, is_peek);
1716 else
1717 err = process_rx_list(ctx, msg, 0,
1718 decrypted, true, is_peek);
1719 if (err < 0) {
1720 tls_err_abort(sk, err);
1721 copied = 0;
1722 goto end;
1723 }
1724
1725 WARN_ON(decrypted != err);
Vakul Garg94524d82018-08-29 15:26:55 +05301726 }
1727
Vakul Garg692d7b52019-01-16 10:40:16 +00001728 copied += decrypted;
1729
1730end:
Dave Watsonc46234e2018-03-22 10:10:35 -07001731 release_sock(sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001732 if (psock)
1733 sk_psock_put(sk, psock);
Dave Watsonc46234e2018-03-22 10:10:35 -07001734 return copied ? : err;
1735}
1736
1737ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
1738 struct pipe_inode_info *pipe,
1739 size_t len, unsigned int flags)
1740{
1741 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001742 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001743 struct strp_msg *rxm = NULL;
1744 struct sock *sk = sock->sk;
1745 struct sk_buff *skb;
1746 ssize_t copied = 0;
1747 int err = 0;
1748 long timeo;
1749 int chunk;
Vakul Garg0b243d02018-08-10 20:46:41 +05301750 bool zc = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001751
1752 lock_sock(sk);
1753
1754 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1755
John Fastabendd3b18ad32018-10-13 02:46:01 +02001756 skb = tls_wait_data(sk, NULL, flags, timeo, &err);
Dave Watsonc46234e2018-03-22 10:10:35 -07001757 if (!skb)
1758 goto splice_read_end;
1759
1760 /* splice does not support reading control messages */
1761 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1762 err = -ENOTSUPP;
1763 goto splice_read_end;
1764 }
1765
1766 if (!ctx->decrypted) {
Vakul Garg692d7b52019-01-16 10:40:16 +00001767 err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc, false);
Dave Watsonc46234e2018-03-22 10:10:35 -07001768
1769 if (err < 0) {
1770 tls_err_abort(sk, EBADMSG);
1771 goto splice_read_end;
1772 }
1773 ctx->decrypted = true;
1774 }
1775 rxm = strp_msg(skb);
1776
1777 chunk = min_t(unsigned int, rxm->full_len, len);
1778 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
1779 if (copied < 0)
1780 goto splice_read_end;
1781
1782 if (likely(!(flags & MSG_PEEK)))
1783 tls_sw_advance_skb(sk, skb, copied);
1784
1785splice_read_end:
1786 release_sock(sk);
1787 return copied ? : err;
1788}
1789
John Fastabend924ad652018-10-13 02:46:00 +02001790bool tls_sw_stream_read(const struct sock *sk)
Dave Watsonc46234e2018-03-22 10:10:35 -07001791{
Dave Watsonc46234e2018-03-22 10:10:35 -07001792 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001793 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001794 bool ingress_empty = true;
1795 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001796
John Fastabendd3b18ad32018-10-13 02:46:01 +02001797 rcu_read_lock();
1798 psock = sk_psock(sk);
1799 if (psock)
1800 ingress_empty = list_empty(&psock->ingress_msg);
1801 rcu_read_unlock();
Dave Watsonc46234e2018-03-22 10:10:35 -07001802
John Fastabendd3b18ad32018-10-13 02:46:01 +02001803 return !ingress_empty || ctx->recv_pkt;
Dave Watsonc46234e2018-03-22 10:10:35 -07001804}
1805
1806static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1807{
1808 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001809 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Kees Cook3463e512018-06-25 16:55:05 -07001810 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
Dave Watsonc46234e2018-03-22 10:10:35 -07001811 struct strp_msg *rxm = strp_msg(skb);
1812 size_t cipher_overhead;
1813 size_t data_len = 0;
1814 int ret;
1815
1816 /* Verify that we have a full TLS header, or wait for more data */
1817 if (rxm->offset + tls_ctx->rx.prepend_size > skb->len)
1818 return 0;
1819
Kees Cook3463e512018-06-25 16:55:05 -07001820 /* Sanity-check size of on-stack buffer. */
1821 if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) {
1822 ret = -EINVAL;
1823 goto read_failure;
1824 }
1825
Dave Watsonc46234e2018-03-22 10:10:35 -07001826 /* Linearize header to local buffer */
1827 ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size);
1828
1829 if (ret < 0)
1830 goto read_failure;
1831
1832 ctx->control = header[0];
1833
1834 data_len = ((header[4] & 0xFF) | (header[3] << 8));
1835
1836 cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size;
1837
1838 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) {
1839 ret = -EMSGSIZE;
1840 goto read_failure;
1841 }
1842 if (data_len < cipher_overhead) {
1843 ret = -EBADMSG;
1844 goto read_failure;
1845 }
1846
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001847 if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.info.version) ||
1848 header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.info.version)) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001849 ret = -EINVAL;
1850 goto read_failure;
1851 }
1852
Boris Pismenny4799ac82018-07-13 14:33:43 +03001853#ifdef CONFIG_TLS_DEVICE
1854 handle_device_resync(strp->sk, TCP_SKB_CB(skb)->seq + rxm->offset,
1855 *(u64*)tls_ctx->rx.rec_seq);
1856#endif
Dave Watsonc46234e2018-03-22 10:10:35 -07001857 return data_len + TLS_HEADER_SIZE;
1858
1859read_failure:
1860 tls_err_abort(strp->sk, ret);
1861
1862 return ret;
1863}
1864
1865static void tls_queue(struct strparser *strp, struct sk_buff *skb)
1866{
1867 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001868 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001869
1870 ctx->decrypted = false;
1871
1872 ctx->recv_pkt = skb;
1873 strp_pause(strp);
1874
Vakul Gargad13acc2018-07-30 16:08:33 +05301875 ctx->saved_data_ready(strp->sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001876}
1877
1878static void tls_data_ready(struct sock *sk)
1879{
1880 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001881 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001882 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001883
1884 strp_data_ready(&ctx->strp);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001885
1886 psock = sk_psock_get(sk);
1887 if (psock && !list_empty(&psock->ingress_msg)) {
1888 ctx->saved_data_ready(sk);
1889 sk_psock_put(sk, psock);
1890 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001891}
1892
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001893void tls_sw_free_resources_tx(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -07001894{
1895 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001896 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +05301897 struct tls_rec *rec, *tmp;
1898
1899 /* Wait for any pending async encryptions to complete */
1900 smp_store_mb(ctx->async_notify, true);
1901 if (atomic_read(&ctx->encrypt_pending))
1902 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1903
1904 cancel_delayed_work_sync(&ctx->tx_work.work);
1905
1906 /* Tx whatever records we can transmit and abandon the rest */
1907 tls_tx_records(sk, -1);
1908
Vakul Garg9932a292018-09-24 15:35:56 +05301909 /* Free up un-sent records in tx_list. First, free
Vakul Garga42055e2018-09-21 09:46:13 +05301910 * the partially sent record if any at head of tx_list.
1911 */
1912 if (tls_ctx->partially_sent_record) {
1913 struct scatterlist *sg = tls_ctx->partially_sent_record;
1914
1915 while (1) {
1916 put_page(sg_page(sg));
1917 sk_mem_uncharge(sk, sg->length);
1918
1919 if (sg_is_last(sg))
1920 break;
1921 sg++;
1922 }
1923
1924 tls_ctx->partially_sent_record = NULL;
1925
Vakul Garg9932a292018-09-24 15:35:56 +05301926 rec = list_first_entry(&ctx->tx_list,
Vakul Garga42055e2018-09-21 09:46:13 +05301927 struct tls_rec, list);
1928 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001929 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301930 kfree(rec);
1931 }
1932
Vakul Garg9932a292018-09-24 15:35:56 +05301933 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
Vakul Garga42055e2018-09-21 09:46:13 +05301934 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001935 sk_msg_free(sk, &rec->msg_encrypted);
1936 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301937 kfree(rec);
1938 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001939
Vakul Garg201876b2018-07-24 16:54:27 +05301940 crypto_free_aead(ctx->aead_send);
Vakul Gargc7749732018-09-25 20:21:51 +05301941 tls_free_open_rec(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001942
1943 kfree(ctx);
1944}
1945
Boris Pismenny39f56e12018-07-13 14:33:41 +03001946void tls_sw_release_resources_rx(struct sock *sk)
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001947{
1948 struct tls_context *tls_ctx = tls_get_ctx(sk);
1949 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1950
Dave Watsonc46234e2018-03-22 10:10:35 -07001951 if (ctx->aead_recv) {
Vakul Garg201876b2018-07-24 16:54:27 +05301952 kfree_skb(ctx->recv_pkt);
1953 ctx->recv_pkt = NULL;
Vakul Garg692d7b52019-01-16 10:40:16 +00001954 skb_queue_purge(&ctx->rx_list);
Dave Watsonc46234e2018-03-22 10:10:35 -07001955 crypto_free_aead(ctx->aead_recv);
1956 strp_stop(&ctx->strp);
1957 write_lock_bh(&sk->sk_callback_lock);
1958 sk->sk_data_ready = ctx->saved_data_ready;
1959 write_unlock_bh(&sk->sk_callback_lock);
1960 release_sock(sk);
1961 strp_done(&ctx->strp);
1962 lock_sock(sk);
1963 }
Boris Pismenny39f56e12018-07-13 14:33:41 +03001964}
1965
1966void tls_sw_free_resources_rx(struct sock *sk)
1967{
1968 struct tls_context *tls_ctx = tls_get_ctx(sk);
1969 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1970
1971 tls_sw_release_resources_rx(sk);
Dave Watson3c4d7552017-06-14 11:37:39 -07001972
Dave Watson3c4d7552017-06-14 11:37:39 -07001973 kfree(ctx);
1974}
1975
Vakul Garg9932a292018-09-24 15:35:56 +05301976/* The work handler to transmitt the encrypted records in tx_list */
Vakul Garga42055e2018-09-21 09:46:13 +05301977static void tx_work_handler(struct work_struct *work)
1978{
1979 struct delayed_work *delayed_work = to_delayed_work(work);
1980 struct tx_work *tx_work = container_of(delayed_work,
1981 struct tx_work, work);
1982 struct sock *sk = tx_work->sk;
1983 struct tls_context *tls_ctx = tls_get_ctx(sk);
1984 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1985
1986 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
1987 return;
1988
1989 lock_sock(sk);
1990 tls_tx_records(sk, -1);
1991 release_sock(sk);
1992}
1993
Dave Watsonc46234e2018-03-22 10:10:35 -07001994int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
Dave Watson3c4d7552017-06-14 11:37:39 -07001995{
Dave Watson3c4d7552017-06-14 11:37:39 -07001996 struct tls_crypto_info *crypto_info;
1997 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001998 struct tls_sw_context_tx *sw_ctx_tx = NULL;
1999 struct tls_sw_context_rx *sw_ctx_rx = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07002000 struct cipher_context *cctx;
2001 struct crypto_aead **aead;
2002 struct strp_callbacks cb;
Dave Watson3c4d7552017-06-14 11:37:39 -07002003 u16 nonce_size, tag_size, iv_size, rec_seq_size;
Vakul Garg692d7b52019-01-16 10:40:16 +00002004 struct crypto_tfm *tfm;
Dave Watson3c4d7552017-06-14 11:37:39 -07002005 char *iv, *rec_seq;
2006 int rc = 0;
2007
2008 if (!ctx) {
2009 rc = -EINVAL;
2010 goto out;
2011 }
2012
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002013 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03002014 if (!ctx->priv_ctx_tx) {
2015 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
2016 if (!sw_ctx_tx) {
2017 rc = -ENOMEM;
2018 goto out;
2019 }
2020 ctx->priv_ctx_tx = sw_ctx_tx;
2021 } else {
2022 sw_ctx_tx =
2023 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
Dave Watsonc46234e2018-03-22 10:10:35 -07002024 }
Dave Watsonc46234e2018-03-22 10:10:35 -07002025 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03002026 if (!ctx->priv_ctx_rx) {
2027 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
2028 if (!sw_ctx_rx) {
2029 rc = -ENOMEM;
2030 goto out;
2031 }
2032 ctx->priv_ctx_rx = sw_ctx_rx;
2033 } else {
2034 sw_ctx_rx =
2035 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002036 }
Dave Watson3c4d7552017-06-14 11:37:39 -07002037 }
2038
Dave Watsonc46234e2018-03-22 10:10:35 -07002039 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03002040 crypto_init_wait(&sw_ctx_tx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02002041 crypto_info = &ctx->crypto_send.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07002042 cctx = &ctx->tx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002043 aead = &sw_ctx_tx->aead_send;
Vakul Garg9932a292018-09-24 15:35:56 +05302044 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +05302045 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
2046 sw_ctx_tx->tx_work.sk = sk;
Dave Watsonc46234e2018-03-22 10:10:35 -07002047 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03002048 crypto_init_wait(&sw_ctx_rx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02002049 crypto_info = &ctx->crypto_recv.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07002050 cctx = &ctx->rx;
Vakul Garg692d7b52019-01-16 10:40:16 +00002051 skb_queue_head_init(&sw_ctx_rx->rx_list);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002052 aead = &sw_ctx_rx->aead_recv;
Dave Watsonc46234e2018-03-22 10:10:35 -07002053 }
2054
Dave Watson3c4d7552017-06-14 11:37:39 -07002055 switch (crypto_info->cipher_type) {
2056 case TLS_CIPHER_AES_GCM_128: {
2057 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2058 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
2059 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
2060 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
2061 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
2062 rec_seq =
2063 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
2064 gcm_128_info =
2065 (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
2066 break;
2067 }
2068 default:
2069 rc = -EINVAL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01002070 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07002071 }
2072
Kees Cookb16520f2018-04-10 17:52:34 -07002073 /* Sanity-check the IV size for stack allocations. */
Kees Cook3463e512018-06-25 16:55:05 -07002074 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) {
Kees Cookb16520f2018-04-10 17:52:34 -07002075 rc = -EINVAL;
2076 goto free_priv;
2077 }
2078
Dave Watsonc46234e2018-03-22 10:10:35 -07002079 cctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
2080 cctx->tag_size = tag_size;
2081 cctx->overhead_size = cctx->prepend_size + cctx->tag_size;
2082 cctx->iv_size = iv_size;
2083 cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
2084 GFP_KERNEL);
2085 if (!cctx->iv) {
Dave Watson3c4d7552017-06-14 11:37:39 -07002086 rc = -ENOMEM;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01002087 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07002088 }
Dave Watsonc46234e2018-03-22 10:10:35 -07002089 memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
2090 memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
2091 cctx->rec_seq_size = rec_seq_size;
zhong jiang969d5092018-08-01 00:50:24 +08002092 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
Dave Watsonc46234e2018-03-22 10:10:35 -07002093 if (!cctx->rec_seq) {
Dave Watson3c4d7552017-06-14 11:37:39 -07002094 rc = -ENOMEM;
2095 goto free_iv;
2096 }
Dave Watson3c4d7552017-06-14 11:37:39 -07002097
Dave Watsonc46234e2018-03-22 10:10:35 -07002098 if (!*aead) {
2099 *aead = crypto_alloc_aead("gcm(aes)", 0, 0);
2100 if (IS_ERR(*aead)) {
2101 rc = PTR_ERR(*aead);
2102 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002103 goto free_rec_seq;
2104 }
2105 }
2106
2107 ctx->push_pending_record = tls_sw_push_pending_record;
2108
Sabrina Dubroca7cba09c2018-09-12 17:44:41 +02002109 rc = crypto_aead_setkey(*aead, gcm_128_info->key,
Dave Watson3c4d7552017-06-14 11:37:39 -07002110 TLS_CIPHER_AES_GCM_128_KEY_SIZE);
2111 if (rc)
2112 goto free_aead;
2113
Dave Watsonc46234e2018-03-22 10:10:35 -07002114 rc = crypto_aead_setauthsize(*aead, cctx->tag_size);
2115 if (rc)
2116 goto free_aead;
2117
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002118 if (sw_ctx_rx) {
Vakul Garg692d7b52019-01-16 10:40:16 +00002119 tfm = crypto_aead_tfm(sw_ctx_rx->aead_recv);
2120 sw_ctx_rx->async_capable =
2121 tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
2122
Dave Watsonc46234e2018-03-22 10:10:35 -07002123 /* Set up strparser */
2124 memset(&cb, 0, sizeof(cb));
2125 cb.rcv_msg = tls_queue;
2126 cb.parse_msg = tls_read_size;
2127
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002128 strp_init(&sw_ctx_rx->strp, sk, &cb);
Dave Watsonc46234e2018-03-22 10:10:35 -07002129
2130 write_lock_bh(&sk->sk_callback_lock);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002131 sw_ctx_rx->saved_data_ready = sk->sk_data_ready;
Dave Watsonc46234e2018-03-22 10:10:35 -07002132 sk->sk_data_ready = tls_data_ready;
2133 write_unlock_bh(&sk->sk_callback_lock);
2134
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002135 strp_check_rcv(&sw_ctx_rx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07002136 }
2137
2138 goto out;
Dave Watson3c4d7552017-06-14 11:37:39 -07002139
2140free_aead:
Dave Watsonc46234e2018-03-22 10:10:35 -07002141 crypto_free_aead(*aead);
2142 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002143free_rec_seq:
Dave Watsonc46234e2018-03-22 10:10:35 -07002144 kfree(cctx->rec_seq);
2145 cctx->rec_seq = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002146free_iv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002147 kfree(cctx->iv);
2148 cctx->iv = NULL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01002149free_priv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002150 if (tx) {
2151 kfree(ctx->priv_ctx_tx);
2152 ctx->priv_ctx_tx = NULL;
2153 } else {
2154 kfree(ctx->priv_ctx_rx);
2155 ctx->priv_ctx_rx = NULL;
2156 }
Dave Watson3c4d7552017-06-14 11:37:39 -07002157out:
2158 return rc;
2159}