blob: 5aee9ae5ca535669128299b6940182338b3680c3 [file] [log] [blame]
Dave Watson3c4d7552017-06-14 11:37:39 -07001/*
2 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4 * Copyright (c) 2016-2017, Lance Chao <lancerchao@fb.com>. All rights reserved.
5 * Copyright (c) 2016, Fridolin Pokorny <fridolin.pokorny@gmail.com>. All rights reserved.
6 * Copyright (c) 2016, Nikos Mavrogiannopoulos <nmav@gnutls.org>. All rights reserved.
John Fastabendd3b18ad32018-10-13 02:46:01 +02007 * Copyright (c) 2018, Covalent IO, Inc. http://covalent.io
Dave Watson3c4d7552017-06-14 11:37:39 -07008 *
9 * This software is available to you under a choice of one of two
10 * licenses. You may choose to be licensed under the terms of the GNU
11 * General Public License (GPL) Version 2, available from the file
12 * COPYING in the main directory of this source tree, or the
13 * OpenIB.org BSD license below:
14 *
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
17 * conditions are met:
18 *
19 * - Redistributions of source code must retain the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer.
22 *
23 * - Redistributions in binary form must reproduce the above
24 * copyright notice, this list of conditions and the following
25 * disclaimer in the documentation and/or other materials
26 * provided with the distribution.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
32 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
33 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 * SOFTWARE.
36 */
37
Dave Watsonc46234e2018-03-22 10:10:35 -070038#include <linux/sched/signal.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070039#include <linux/module.h>
40#include <crypto/aead.h>
41
Dave Watsonc46234e2018-03-22 10:10:35 -070042#include <net/strparser.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070043#include <net/tls.h>
44
Kees Cookb16520f2018-04-10 17:52:34 -070045#define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE
46
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -070047static int __skb_nsg(struct sk_buff *skb, int offset, int len,
48 unsigned int recursion_level)
49{
50 int start = skb_headlen(skb);
51 int i, chunk = start - offset;
52 struct sk_buff *frag_iter;
53 int elt = 0;
54
55 if (unlikely(recursion_level >= 24))
56 return -EMSGSIZE;
57
58 if (chunk > 0) {
59 if (chunk > len)
60 chunk = len;
61 elt++;
62 len -= chunk;
63 if (len == 0)
64 return elt;
65 offset += chunk;
66 }
67
68 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
69 int end;
70
71 WARN_ON(start > offset + len);
72
73 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
74 chunk = end - offset;
75 if (chunk > 0) {
76 if (chunk > len)
77 chunk = len;
78 elt++;
79 len -= chunk;
80 if (len == 0)
81 return elt;
82 offset += chunk;
83 }
84 start = end;
85 }
86
87 if (unlikely(skb_has_frag_list(skb))) {
88 skb_walk_frags(skb, frag_iter) {
89 int end, ret;
90
91 WARN_ON(start > offset + len);
92
93 end = start + frag_iter->len;
94 chunk = end - offset;
95 if (chunk > 0) {
96 if (chunk > len)
97 chunk = len;
98 ret = __skb_nsg(frag_iter, offset - start, chunk,
99 recursion_level + 1);
100 if (unlikely(ret < 0))
101 return ret;
102 elt += ret;
103 len -= chunk;
104 if (len == 0)
105 return elt;
106 offset += chunk;
107 }
108 start = end;
109 }
110 }
111 BUG_ON(len);
112 return elt;
113}
114
115/* Return the number of scatterlist elements required to completely map the
116 * skb, or -EMSGSIZE if the recursion depth is exceeded.
117 */
118static int skb_nsg(struct sk_buff *skb, int offset, int len)
119{
120 return __skb_nsg(skb, offset, len, 0);
121}
122
Vakul Garg94524d82018-08-29 15:26:55 +0530123static void tls_decrypt_done(struct crypto_async_request *req, int err)
124{
125 struct aead_request *aead_req = (struct aead_request *)req;
Vakul Garg94524d82018-08-29 15:26:55 +0530126 struct scatterlist *sgout = aead_req->dst;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700127 struct tls_sw_context_rx *ctx;
128 struct tls_context *tls_ctx;
Vakul Garg94524d82018-08-29 15:26:55 +0530129 struct scatterlist *sg;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700130 struct sk_buff *skb;
Vakul Garg94524d82018-08-29 15:26:55 +0530131 unsigned int pages;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700132 int pending;
133
134 skb = (struct sk_buff *)req->data;
135 tls_ctx = tls_get_ctx(skb->sk);
136 ctx = tls_sw_ctx_rx(tls_ctx);
137 pending = atomic_dec_return(&ctx->decrypt_pending);
Vakul Garg94524d82018-08-29 15:26:55 +0530138
139 /* Propagate if there was an err */
140 if (err) {
141 ctx->async_wait.err = err;
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700142 tls_err_abort(skb->sk, err);
Vakul Garg94524d82018-08-29 15:26:55 +0530143 }
144
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700145 /* After using skb->sk to propagate sk through crypto async callback
146 * we need to NULL it again.
147 */
148 skb->sk = NULL;
149
Vakul Garg94524d82018-08-29 15:26:55 +0530150 /* Release the skb, pages and memory allocated for crypto req */
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700151 kfree_skb(skb);
Vakul Garg94524d82018-08-29 15:26:55 +0530152
153 /* Skip the first S/G entry as it points to AAD */
154 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
155 if (!sg)
156 break;
157 put_page(sg_page(sg));
158 }
159
160 kfree(aead_req);
161
162 if (!pending && READ_ONCE(ctx->async_notify))
163 complete(&ctx->async_wait.completion);
164}
165
Dave Watsonc46234e2018-03-22 10:10:35 -0700166static int tls_do_decryption(struct sock *sk,
Vakul Garg94524d82018-08-29 15:26:55 +0530167 struct sk_buff *skb,
Dave Watsonc46234e2018-03-22 10:10:35 -0700168 struct scatterlist *sgin,
169 struct scatterlist *sgout,
170 char *iv_recv,
171 size_t data_len,
Vakul Garg94524d82018-08-29 15:26:55 +0530172 struct aead_request *aead_req,
173 bool async)
Dave Watsonc46234e2018-03-22 10:10:35 -0700174{
175 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300176 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -0700177 int ret;
Dave Watsonc46234e2018-03-22 10:10:35 -0700178
Vakul Garg0b243d02018-08-10 20:46:41 +0530179 aead_request_set_tfm(aead_req, ctx->aead_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700180 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
181 aead_request_set_crypt(aead_req, sgin, sgout,
182 data_len + tls_ctx->rx.tag_size,
183 (u8 *)iv_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700184
Vakul Garg94524d82018-08-29 15:26:55 +0530185 if (async) {
John Fastabend7a3dd8c2018-09-14 13:01:46 -0700186 /* Using skb->sk to push sk through to crypto async callback
187 * handler. This allows propagating errors up to the socket
188 * if needed. It _must_ be cleared in the async handler
189 * before kfree_skb is called. We _know_ skb->sk is NULL
190 * because it is a clone from strparser.
191 */
192 skb->sk = sk;
Vakul Garg94524d82018-08-29 15:26:55 +0530193 aead_request_set_callback(aead_req,
194 CRYPTO_TFM_REQ_MAY_BACKLOG,
195 tls_decrypt_done, skb);
196 atomic_inc(&ctx->decrypt_pending);
197 } else {
198 aead_request_set_callback(aead_req,
199 CRYPTO_TFM_REQ_MAY_BACKLOG,
200 crypto_req_done, &ctx->async_wait);
201 }
202
203 ret = crypto_aead_decrypt(aead_req);
204 if (ret == -EINPROGRESS) {
205 if (async)
206 return ret;
207
208 ret = crypto_wait_req(ret, &ctx->async_wait);
209 }
210
211 if (async)
212 atomic_dec(&ctx->decrypt_pending);
213
Dave Watsonc46234e2018-03-22 10:10:35 -0700214 return ret;
215}
216
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200217static void tls_trim_both_msgs(struct sock *sk, int target_size)
Dave Watson3c4d7552017-06-14 11:37:39 -0700218{
219 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300220 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530221 struct tls_rec *rec = ctx->open_rec;
Dave Watson3c4d7552017-06-14 11:37:39 -0700222
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200223 sk_msg_trim(sk, &rec->msg_plaintext, target_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700224 if (target_size > 0)
Dave Watsondbe42552018-03-22 10:10:06 -0700225 target_size += tls_ctx->tx.overhead_size;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200226 sk_msg_trim(sk, &rec->msg_encrypted, target_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700227}
228
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200229static int tls_alloc_encrypted_msg(struct sock *sk, int len)
Dave Watson3c4d7552017-06-14 11:37:39 -0700230{
231 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300232 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530233 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200234 struct sk_msg *msg_en = &rec->msg_encrypted;
Dave Watson3c4d7552017-06-14 11:37:39 -0700235
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200236 return sk_msg_alloc(sk, msg_en, len, 0);
Dave Watson3c4d7552017-06-14 11:37:39 -0700237}
238
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200239static int tls_clone_plaintext_msg(struct sock *sk, int required)
Dave Watson3c4d7552017-06-14 11:37:39 -0700240{
241 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300242 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530243 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200244 struct sk_msg *msg_pl = &rec->msg_plaintext;
245 struct sk_msg *msg_en = &rec->msg_encrypted;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530246 int skip, len;
Dave Watson3c4d7552017-06-14 11:37:39 -0700247
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200248 /* We add page references worth len bytes from encrypted sg
249 * at the end of plaintext sg. It is guaranteed that msg_en
Vakul Garg4e6d4722018-09-30 08:04:35 +0530250 * has enough required room (ensured by caller).
251 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200252 len = required - msg_pl->sg.size;
Vakul Garg52ea9922018-09-06 21:41:40 +0530253
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200254 /* Skip initial bytes in msg_en's data to be able to use
255 * same offset of both plain and encrypted data.
Vakul Garg4e6d4722018-09-30 08:04:35 +0530256 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200257 skip = tls_ctx->tx.prepend_size + msg_pl->sg.size;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530258
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200259 return sk_msg_clone(sk, msg_pl, msg_en, skip, len);
Dave Watson3c4d7552017-06-14 11:37:39 -0700260}
261
John Fastabendd3b18ad32018-10-13 02:46:01 +0200262static struct tls_rec *tls_get_rec(struct sock *sk)
263{
264 struct tls_context *tls_ctx = tls_get_ctx(sk);
265 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
266 struct sk_msg *msg_pl, *msg_en;
267 struct tls_rec *rec;
268 int mem_size;
269
270 mem_size = sizeof(struct tls_rec) + crypto_aead_reqsize(ctx->aead_send);
271
272 rec = kzalloc(mem_size, sk->sk_allocation);
273 if (!rec)
274 return NULL;
275
276 msg_pl = &rec->msg_plaintext;
277 msg_en = &rec->msg_encrypted;
278
279 sk_msg_init(msg_pl);
280 sk_msg_init(msg_en);
281
282 sg_init_table(rec->sg_aead_in, 2);
283 sg_set_buf(&rec->sg_aead_in[0], rec->aad_space,
284 sizeof(rec->aad_space));
285 sg_unmark_end(&rec->sg_aead_in[1]);
286
287 sg_init_table(rec->sg_aead_out, 2);
288 sg_set_buf(&rec->sg_aead_out[0], rec->aad_space,
289 sizeof(rec->aad_space));
290 sg_unmark_end(&rec->sg_aead_out[1]);
291
292 return rec;
293}
294
295static void tls_free_rec(struct sock *sk, struct tls_rec *rec)
296{
297 sk_msg_free(sk, &rec->msg_encrypted);
298 sk_msg_free(sk, &rec->msg_plaintext);
299 kfree(rec);
300}
301
Vakul Gargc7749732018-09-25 20:21:51 +0530302static void tls_free_open_rec(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -0700303{
304 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300305 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +0530306 struct tls_rec *rec = ctx->open_rec;
Dave Watson3c4d7552017-06-14 11:37:39 -0700307
John Fastabendd3b18ad32018-10-13 02:46:01 +0200308 if (rec) {
309 tls_free_rec(sk, rec);
310 ctx->open_rec = NULL;
311 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700312}
313
Vakul Garga42055e2018-09-21 09:46:13 +0530314int tls_tx_records(struct sock *sk, int flags)
315{
316 struct tls_context *tls_ctx = tls_get_ctx(sk);
317 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
318 struct tls_rec *rec, *tmp;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200319 struct sk_msg *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530320 int tx_flags, rc = 0;
321
322 if (tls_is_partially_sent_record(tls_ctx)) {
Vakul Garg9932a292018-09-24 15:35:56 +0530323 rec = list_first_entry(&ctx->tx_list,
Vakul Garga42055e2018-09-21 09:46:13 +0530324 struct tls_rec, list);
325
326 if (flags == -1)
327 tx_flags = rec->tx_flags;
328 else
329 tx_flags = flags;
330
331 rc = tls_push_partial_record(sk, tls_ctx, tx_flags);
332 if (rc)
333 goto tx_err;
334
335 /* Full record has been transmitted.
Vakul Garg9932a292018-09-24 15:35:56 +0530336 * Remove the head of tx_list
Vakul Garga42055e2018-09-21 09:46:13 +0530337 */
Vakul Garga42055e2018-09-21 09:46:13 +0530338 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200339 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +0530340 kfree(rec);
341 }
342
Vakul Garg9932a292018-09-24 15:35:56 +0530343 /* Tx all ready records */
344 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
345 if (READ_ONCE(rec->tx_ready)) {
Vakul Garga42055e2018-09-21 09:46:13 +0530346 if (flags == -1)
347 tx_flags = rec->tx_flags;
348 else
349 tx_flags = flags;
350
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200351 msg_en = &rec->msg_encrypted;
Vakul Garga42055e2018-09-21 09:46:13 +0530352 rc = tls_push_sg(sk, tls_ctx,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200353 &msg_en->sg.data[msg_en->sg.curr],
Vakul Garga42055e2018-09-21 09:46:13 +0530354 0, tx_flags);
355 if (rc)
356 goto tx_err;
357
Vakul Garga42055e2018-09-21 09:46:13 +0530358 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200359 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +0530360 kfree(rec);
361 } else {
362 break;
363 }
364 }
365
366tx_err:
367 if (rc < 0 && rc != -EAGAIN)
368 tls_err_abort(sk, EBADMSG);
369
370 return rc;
371}
372
373static void tls_encrypt_done(struct crypto_async_request *req, int err)
374{
375 struct aead_request *aead_req = (struct aead_request *)req;
376 struct sock *sk = req->data;
377 struct tls_context *tls_ctx = tls_get_ctx(sk);
378 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200379 struct scatterlist *sge;
380 struct sk_msg *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530381 struct tls_rec *rec;
382 bool ready = false;
383 int pending;
384
385 rec = container_of(aead_req, struct tls_rec, aead_req);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200386 msg_en = &rec->msg_encrypted;
Vakul Garga42055e2018-09-21 09:46:13 +0530387
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200388 sge = sk_msg_elem(msg_en, msg_en->sg.curr);
389 sge->offset -= tls_ctx->tx.prepend_size;
390 sge->length += tls_ctx->tx.prepend_size;
Vakul Garga42055e2018-09-21 09:46:13 +0530391
Vakul Garg80ece6a2018-09-26 16:22:08 +0530392 /* Check if error is previously set on socket */
Vakul Garga42055e2018-09-21 09:46:13 +0530393 if (err || sk->sk_err) {
Vakul Garga42055e2018-09-21 09:46:13 +0530394 rec = NULL;
395
396 /* If err is already set on socket, return the same code */
397 if (sk->sk_err) {
398 ctx->async_wait.err = sk->sk_err;
399 } else {
400 ctx->async_wait.err = err;
401 tls_err_abort(sk, err);
402 }
403 }
404
Vakul Garg9932a292018-09-24 15:35:56 +0530405 if (rec) {
406 struct tls_rec *first_rec;
407
408 /* Mark the record as ready for transmission */
409 smp_store_mb(rec->tx_ready, true);
410
411 /* If received record is at head of tx_list, schedule tx */
412 first_rec = list_first_entry(&ctx->tx_list,
413 struct tls_rec, list);
414 if (rec == first_rec)
415 ready = true;
416 }
Vakul Garga42055e2018-09-21 09:46:13 +0530417
418 pending = atomic_dec_return(&ctx->encrypt_pending);
419
420 if (!pending && READ_ONCE(ctx->async_notify))
421 complete(&ctx->async_wait.completion);
422
423 if (!ready)
424 return;
425
426 /* Schedule the transmission */
427 if (!test_and_set_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200428 schedule_delayed_work(&ctx->tx_work.work, 1);
Vakul Garga42055e2018-09-21 09:46:13 +0530429}
430
431static int tls_do_encryption(struct sock *sk,
432 struct tls_context *tls_ctx,
Daniel Borkmanna447da72018-06-15 03:07:45 +0200433 struct tls_sw_context_tx *ctx,
434 struct aead_request *aead_req,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200435 size_t data_len, u32 start)
Dave Watson3c4d7552017-06-14 11:37:39 -0700436{
Vakul Garga42055e2018-09-21 09:46:13 +0530437 struct tls_rec *rec = ctx->open_rec;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200438 struct sk_msg *msg_en = &rec->msg_encrypted;
439 struct scatterlist *sge = sk_msg_elem(msg_en, start);
Dave Watson3c4d7552017-06-14 11:37:39 -0700440 int rc;
441
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200442 sge->offset += tls_ctx->tx.prepend_size;
443 sge->length -= tls_ctx->tx.prepend_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700444
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200445 msg_en->sg.curr = start;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530446
Dave Watson3c4d7552017-06-14 11:37:39 -0700447 aead_request_set_tfm(aead_req, ctx->aead_send);
448 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200449 aead_request_set_crypt(aead_req, rec->sg_aead_in,
450 rec->sg_aead_out,
Dave Watsondbe42552018-03-22 10:10:06 -0700451 data_len, tls_ctx->tx.iv);
Vakul Garga54667f2018-01-31 21:34:37 +0530452
453 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Vakul Garga42055e2018-09-21 09:46:13 +0530454 tls_encrypt_done, sk);
Vakul Garga54667f2018-01-31 21:34:37 +0530455
Vakul Garg9932a292018-09-24 15:35:56 +0530456 /* Add the record in tx_list */
457 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +0530458 atomic_inc(&ctx->encrypt_pending);
Dave Watson3c4d7552017-06-14 11:37:39 -0700459
Vakul Garga42055e2018-09-21 09:46:13 +0530460 rc = crypto_aead_encrypt(aead_req);
461 if (!rc || rc != -EINPROGRESS) {
462 atomic_dec(&ctx->encrypt_pending);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200463 sge->offset -= tls_ctx->tx.prepend_size;
464 sge->length += tls_ctx->tx.prepend_size;
Vakul Garga42055e2018-09-21 09:46:13 +0530465 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700466
Vakul Garg9932a292018-09-24 15:35:56 +0530467 if (!rc) {
468 WRITE_ONCE(rec->tx_ready, true);
469 } else if (rc != -EINPROGRESS) {
470 list_del(&rec->list);
Vakul Garga42055e2018-09-21 09:46:13 +0530471 return rc;
Vakul Garg9932a292018-09-24 15:35:56 +0530472 }
Vakul Garga42055e2018-09-21 09:46:13 +0530473
474 /* Unhook the record from context if encryption is not failure */
475 ctx->open_rec = NULL;
476 tls_advance_record_sn(sk, &tls_ctx->tx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700477 return rc;
478}
479
John Fastabendd3b18ad32018-10-13 02:46:01 +0200480static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
481 struct tls_rec **to, struct sk_msg *msg_opl,
482 struct sk_msg *msg_oen, u32 split_point,
483 u32 tx_overhead_size, u32 *orig_end)
484{
485 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
486 struct scatterlist *sge, *osge, *nsge;
487 u32 orig_size = msg_opl->sg.size;
488 struct scatterlist tmp = { };
489 struct sk_msg *msg_npl;
490 struct tls_rec *new;
491 int ret;
492
493 new = tls_get_rec(sk);
494 if (!new)
495 return -ENOMEM;
496 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
497 tx_overhead_size, 0);
498 if (ret < 0) {
499 tls_free_rec(sk, new);
500 return ret;
501 }
502
503 *orig_end = msg_opl->sg.end;
504 i = msg_opl->sg.start;
505 sge = sk_msg_elem(msg_opl, i);
506 while (apply && sge->length) {
507 if (sge->length > apply) {
508 u32 len = sge->length - apply;
509
510 get_page(sg_page(sge));
511 sg_set_page(&tmp, sg_page(sge), len,
512 sge->offset + apply);
513 sge->length = apply;
514 bytes += apply;
515 apply = 0;
516 } else {
517 apply -= sge->length;
518 bytes += sge->length;
519 }
520
521 sk_msg_iter_var_next(i);
522 if (i == msg_opl->sg.end)
523 break;
524 sge = sk_msg_elem(msg_opl, i);
525 }
526
527 msg_opl->sg.end = i;
528 msg_opl->sg.curr = i;
529 msg_opl->sg.copybreak = 0;
530 msg_opl->apply_bytes = 0;
531 msg_opl->sg.size = bytes;
532
533 msg_npl = &new->msg_plaintext;
534 msg_npl->apply_bytes = apply;
535 msg_npl->sg.size = orig_size - bytes;
536
537 j = msg_npl->sg.start;
538 nsge = sk_msg_elem(msg_npl, j);
539 if (tmp.length) {
540 memcpy(nsge, &tmp, sizeof(*nsge));
541 sk_msg_iter_var_next(j);
542 nsge = sk_msg_elem(msg_npl, j);
543 }
544
545 osge = sk_msg_elem(msg_opl, i);
546 while (osge->length) {
547 memcpy(nsge, osge, sizeof(*nsge));
548 sg_unmark_end(nsge);
549 sk_msg_iter_var_next(i);
550 sk_msg_iter_var_next(j);
551 if (i == *orig_end)
552 break;
553 osge = sk_msg_elem(msg_opl, i);
554 nsge = sk_msg_elem(msg_npl, j);
555 }
556
557 msg_npl->sg.end = j;
558 msg_npl->sg.curr = j;
559 msg_npl->sg.copybreak = 0;
560
561 *to = new;
562 return 0;
563}
564
565static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
566 struct tls_rec *from, u32 orig_end)
567{
568 struct sk_msg *msg_npl = &from->msg_plaintext;
569 struct sk_msg *msg_opl = &to->msg_plaintext;
570 struct scatterlist *osge, *nsge;
571 u32 i, j;
572
573 i = msg_opl->sg.end;
574 sk_msg_iter_var_prev(i);
575 j = msg_npl->sg.start;
576
577 osge = sk_msg_elem(msg_opl, i);
578 nsge = sk_msg_elem(msg_npl, j);
579
580 if (sg_page(osge) == sg_page(nsge) &&
581 osge->offset + osge->length == nsge->offset) {
582 osge->length += nsge->length;
583 put_page(sg_page(nsge));
584 }
585
586 msg_opl->sg.end = orig_end;
587 msg_opl->sg.curr = orig_end;
588 msg_opl->sg.copybreak = 0;
589 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
590 msg_opl->sg.size += msg_npl->sg.size;
591
592 sk_msg_free(sk, &to->msg_encrypted);
593 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
594
595 kfree(from);
596}
597
Dave Watson3c4d7552017-06-14 11:37:39 -0700598static int tls_push_record(struct sock *sk, int flags,
599 unsigned char record_type)
600{
601 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300602 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200603 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
604 u32 i, split_point, uninitialized_var(orig_end);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200605 struct sk_msg *msg_pl, *msg_en;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200606 struct aead_request *req;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200607 bool split;
Dave Watson3c4d7552017-06-14 11:37:39 -0700608 int rc;
609
Vakul Garga42055e2018-09-21 09:46:13 +0530610 if (!rec)
611 return 0;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200612
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200613 msg_pl = &rec->msg_plaintext;
614 msg_en = &rec->msg_encrypted;
615
John Fastabendd3b18ad32018-10-13 02:46:01 +0200616 split_point = msg_pl->apply_bytes;
617 split = split_point && split_point < msg_pl->sg.size;
618 if (split) {
619 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
620 split_point, tls_ctx->tx.overhead_size,
621 &orig_end);
622 if (rc < 0)
623 return rc;
624 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
625 tls_ctx->tx.overhead_size);
626 }
627
Vakul Garga42055e2018-09-21 09:46:13 +0530628 rec->tx_flags = flags;
629 req = &rec->aead_req;
Dave Watson3c4d7552017-06-14 11:37:39 -0700630
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200631 i = msg_pl->sg.end;
632 sk_msg_iter_var_prev(i);
633 sg_mark_end(sk_msg_elem(msg_pl, i));
Vakul Garga42055e2018-09-21 09:46:13 +0530634
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200635 i = msg_pl->sg.start;
636 sg_chain(rec->sg_aead_in, 2, rec->inplace_crypto ?
637 &msg_en->sg.data[i] : &msg_pl->sg.data[i]);
638
639 i = msg_en->sg.end;
640 sk_msg_iter_var_prev(i);
641 sg_mark_end(sk_msg_elem(msg_en, i));
642
643 i = msg_en->sg.start;
644 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
645
646 tls_make_aad(rec->aad_space, msg_pl->sg.size,
Dave Watsondbe42552018-03-22 10:10:06 -0700647 tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size,
Dave Watson3c4d7552017-06-14 11:37:39 -0700648 record_type);
649
650 tls_fill_prepend(tls_ctx,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200651 page_address(sg_page(&msg_en->sg.data[i])) +
652 msg_en->sg.data[i].offset, msg_pl->sg.size,
653 record_type);
Dave Watson3c4d7552017-06-14 11:37:39 -0700654
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200655 tls_ctx->pending_open_record_frags = false;
Dave Watson3c4d7552017-06-14 11:37:39 -0700656
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200657 rc = tls_do_encryption(sk, tls_ctx, ctx, req, msg_pl->sg.size, i);
Dave Watson3c4d7552017-06-14 11:37:39 -0700658 if (rc < 0) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200659 if (rc != -EINPROGRESS) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200660 tls_err_abort(sk, EBADMSG);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200661 if (split) {
662 tls_ctx->pending_open_record_frags = true;
663 tls_merge_open_record(sk, rec, tmp, orig_end);
664 }
665 }
Vakul Garga42055e2018-09-21 09:46:13 +0530666 return rc;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200667 } else if (split) {
668 msg_pl = &tmp->msg_plaintext;
669 msg_en = &tmp->msg_encrypted;
670 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
671 tls_ctx->tx.overhead_size);
672 tls_ctx->pending_open_record_frags = true;
673 ctx->open_rec = tmp;
Dave Watson3c4d7552017-06-14 11:37:39 -0700674 }
675
Vakul Garg9932a292018-09-24 15:35:56 +0530676 return tls_tx_records(sk, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700677}
678
John Fastabendd3b18ad32018-10-13 02:46:01 +0200679static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
680 bool full_record, u8 record_type,
681 size_t *copied, int flags)
Dave Watson3c4d7552017-06-14 11:37:39 -0700682{
683 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300684 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200685 struct sk_msg msg_redir = { };
686 struct sk_psock *psock;
687 struct sock *sk_redir;
Vakul Garga42055e2018-09-21 09:46:13 +0530688 struct tls_rec *rec;
John Fastabend0608c692018-12-20 11:35:35 -0800689 bool enospc, policy;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200690 int err = 0, send;
John Fastabend7246d8e2018-11-26 14:16:17 -0800691 u32 delta = 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530692
John Fastabend0608c692018-12-20 11:35:35 -0800693 policy = !(flags & MSG_SENDPAGE_NOPOLICY);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200694 psock = sk_psock_get(sk);
John Fastabend0608c692018-12-20 11:35:35 -0800695 if (!psock || !policy)
John Fastabendd3b18ad32018-10-13 02:46:01 +0200696 return tls_push_record(sk, flags, record_type);
697more_data:
698 enospc = sk_msg_full(msg);
John Fastabend7246d8e2018-11-26 14:16:17 -0800699 if (psock->eval == __SK_NONE) {
700 delta = msg->sg.size;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200701 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
John Fastabend7246d8e2018-11-26 14:16:17 -0800702 if (delta < msg->sg.size)
703 delta -= msg->sg.size;
704 else
705 delta = 0;
706 }
John Fastabendd3b18ad32018-10-13 02:46:01 +0200707 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
708 !enospc && !full_record) {
709 err = -ENOSPC;
710 goto out_err;
711 }
712 msg->cork_bytes = 0;
713 send = msg->sg.size;
714 if (msg->apply_bytes && msg->apply_bytes < send)
715 send = msg->apply_bytes;
Vakul Garga42055e2018-09-21 09:46:13 +0530716
John Fastabendd3b18ad32018-10-13 02:46:01 +0200717 switch (psock->eval) {
718 case __SK_PASS:
719 err = tls_push_record(sk, flags, record_type);
720 if (err < 0) {
721 *copied -= sk_msg_free(sk, msg);
722 tls_free_open_rec(sk);
723 goto out_err;
724 }
725 break;
726 case __SK_REDIRECT:
727 sk_redir = psock->sk_redir;
728 memcpy(&msg_redir, msg, sizeof(*msg));
729 if (msg->apply_bytes < send)
730 msg->apply_bytes = 0;
731 else
732 msg->apply_bytes -= send;
733 sk_msg_return_zero(sk, msg, send);
734 msg->sg.size -= send;
735 release_sock(sk);
736 err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
737 lock_sock(sk);
738 if (err < 0) {
739 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
740 msg->sg.size = 0;
741 }
742 if (msg->sg.size == 0)
743 tls_free_open_rec(sk);
744 break;
745 case __SK_DROP:
746 default:
747 sk_msg_free_partial(sk, msg, send);
748 if (msg->apply_bytes < send)
749 msg->apply_bytes = 0;
750 else
751 msg->apply_bytes -= send;
752 if (msg->sg.size == 0)
753 tls_free_open_rec(sk);
John Fastabend7246d8e2018-11-26 14:16:17 -0800754 *copied -= (send + delta);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200755 err = -EACCES;
756 }
Vakul Garga42055e2018-09-21 09:46:13 +0530757
John Fastabendd3b18ad32018-10-13 02:46:01 +0200758 if (likely(!err)) {
759 bool reset_eval = !ctx->open_rec;
760
761 rec = ctx->open_rec;
762 if (rec) {
763 msg = &rec->msg_plaintext;
764 if (!msg->apply_bytes)
765 reset_eval = true;
766 }
767 if (reset_eval) {
768 psock->eval = __SK_NONE;
769 if (psock->sk_redir) {
770 sock_put(psock->sk_redir);
771 psock->sk_redir = NULL;
772 }
773 }
774 if (rec)
775 goto more_data;
776 }
777 out_err:
778 sk_psock_put(sk, psock);
779 return err;
780}
781
782static int tls_sw_push_pending_record(struct sock *sk, int flags)
783{
784 struct tls_context *tls_ctx = tls_get_ctx(sk);
785 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
786 struct tls_rec *rec = ctx->open_rec;
787 struct sk_msg *msg_pl;
788 size_t copied;
789
Vakul Garga42055e2018-09-21 09:46:13 +0530790 if (!rec)
John Fastabendd3b18ad32018-10-13 02:46:01 +0200791 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530792
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200793 msg_pl = &rec->msg_plaintext;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200794 copied = msg_pl->sg.size;
795 if (!copied)
796 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530797
John Fastabendd3b18ad32018-10-13 02:46:01 +0200798 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
799 &copied, flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530800}
801
802int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
803{
Dave Watson3c4d7552017-06-14 11:37:39 -0700804 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
Vakul Garga42055e2018-09-21 09:46:13 +0530805 struct tls_context *tls_ctx = tls_get_ctx(sk);
806 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
807 struct crypto_tfm *tfm = crypto_aead_tfm(ctx->aead_send);
808 bool async_capable = tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
809 unsigned char record_type = TLS_RECORD_TYPE_DATA;
David Howells00e23702018-10-22 13:07:28 +0100810 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Dave Watson3c4d7552017-06-14 11:37:39 -0700811 bool eor = !(msg->msg_flags & MSG_MORE);
812 size_t try_to_copy, copied = 0;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200813 struct sk_msg *msg_pl, *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530814 struct tls_rec *rec;
815 int required_size;
816 int num_async = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700817 bool full_record;
Vakul Garga42055e2018-09-21 09:46:13 +0530818 int record_room;
819 int num_zc = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700820 int orig_size;
Vakul Garg4128c0c2018-09-24 16:09:49 +0530821 int ret = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700822
823 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
824 return -ENOTSUPP;
825
826 lock_sock(sk);
827
Vakul Garga42055e2018-09-21 09:46:13 +0530828 /* Wait till there is any pending write on socket */
829 if (unlikely(sk->sk_write_pending)) {
830 ret = wait_on_pending_writer(sk, &timeo);
831 if (unlikely(ret))
832 goto send_end;
833 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700834
835 if (unlikely(msg->msg_controllen)) {
836 ret = tls_proccess_cmsg(sk, msg, &record_type);
Vakul Garga42055e2018-09-21 09:46:13 +0530837 if (ret) {
838 if (ret == -EINPROGRESS)
839 num_async++;
840 else if (ret != -EAGAIN)
841 goto send_end;
842 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700843 }
844
845 while (msg_data_left(msg)) {
846 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +0100847 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -0700848 goto send_end;
849 }
850
John Fastabendd3b18ad32018-10-13 02:46:01 +0200851 if (ctx->open_rec)
852 rec = ctx->open_rec;
853 else
854 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +0530855 if (!rec) {
856 ret = -ENOMEM;
857 goto send_end;
858 }
859
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200860 msg_pl = &rec->msg_plaintext;
861 msg_en = &rec->msg_encrypted;
862
863 orig_size = msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700864 full_record = false;
865 try_to_copy = msg_data_left(msg);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200866 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700867 if (try_to_copy >= record_room) {
868 try_to_copy = record_room;
869 full_record = true;
870 }
871
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200872 required_size = msg_pl->sg.size + try_to_copy +
Dave Watsondbe42552018-03-22 10:10:06 -0700873 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700874
875 if (!sk_stream_memory_free(sk))
876 goto wait_for_sndbuf;
Vakul Garga42055e2018-09-21 09:46:13 +0530877
Dave Watson3c4d7552017-06-14 11:37:39 -0700878alloc_encrypted:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200879 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700880 if (ret) {
881 if (ret != -ENOSPC)
882 goto wait_for_memory;
883
884 /* Adjust try_to_copy according to the amount that was
885 * actually allocated. The difference is due
886 * to max sg elements limit
887 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200888 try_to_copy -= required_size - msg_en->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700889 full_record = true;
890 }
Vakul Garga42055e2018-09-21 09:46:13 +0530891
892 if (!is_kvec && (full_record || eor) && !async_capable) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200893 u32 first = msg_pl->sg.end;
894
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200895 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
896 msg_pl, try_to_copy);
Dave Watson3c4d7552017-06-14 11:37:39 -0700897 if (ret)
898 goto fallback_to_reg_send;
899
Vakul Garg4e6d4722018-09-30 08:04:35 +0530900 rec->inplace_crypto = 0;
901
Vakul Garga42055e2018-09-21 09:46:13 +0530902 num_zc++;
Dave Watson3c4d7552017-06-14 11:37:39 -0700903 copied += try_to_copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200904
905 sk_msg_sg_copy_set(msg_pl, first);
906 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
907 record_type, &copied,
908 msg->msg_flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530909 if (ret) {
910 if (ret == -EINPROGRESS)
911 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200912 else if (ret == -ENOMEM)
913 goto wait_for_memory;
914 else if (ret == -ENOSPC)
915 goto rollback_iter;
Vakul Garga42055e2018-09-21 09:46:13 +0530916 else if (ret != -EAGAIN)
917 goto send_end;
918 }
Doron Roberts-Kedes5a3611e2018-07-26 07:59:35 -0700919 continue;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200920rollback_iter:
921 copied -= try_to_copy;
922 sk_msg_sg_copy_clear(msg_pl, first);
923 iov_iter_revert(&msg->msg_iter,
924 msg_pl->sg.size - orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700925fallback_to_reg_send:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200926 sk_msg_trim(sk, msg_pl, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700927 }
928
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200929 required_size = msg_pl->sg.size + try_to_copy;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530930
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200931 ret = tls_clone_plaintext_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700932 if (ret) {
933 if (ret != -ENOSPC)
Vakul Garg4e6d4722018-09-30 08:04:35 +0530934 goto send_end;
Dave Watson3c4d7552017-06-14 11:37:39 -0700935
936 /* Adjust try_to_copy according to the amount that was
937 * actually allocated. The difference is due
938 * to max sg elements limit
939 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200940 try_to_copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700941 full_record = true;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200942 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
943 tls_ctx->tx.overhead_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700944 }
945
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200946 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter, msg_pl,
947 try_to_copy);
948 if (ret < 0)
Dave Watson3c4d7552017-06-14 11:37:39 -0700949 goto trim_sgl;
950
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200951 /* Open records defined only if successfully copied, otherwise
952 * we would trim the sg but not reset the open record frags.
953 */
954 tls_ctx->pending_open_record_frags = true;
Dave Watson3c4d7552017-06-14 11:37:39 -0700955 copied += try_to_copy;
956 if (full_record || eor) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200957 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
958 record_type, &copied,
959 msg->msg_flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700960 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +0530961 if (ret == -EINPROGRESS)
962 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200963 else if (ret == -ENOMEM)
964 goto wait_for_memory;
965 else if (ret != -EAGAIN) {
966 if (ret == -ENOSPC)
967 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530968 goto send_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200969 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700970 }
971 }
972
973 continue;
974
975wait_for_sndbuf:
976 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
977wait_for_memory:
978 ret = sk_stream_wait_memory(sk, &timeo);
979 if (ret) {
980trim_sgl:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200981 tls_trim_both_msgs(sk, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700982 goto send_end;
983 }
984
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200985 if (msg_en->sg.size < required_size)
Dave Watson3c4d7552017-06-14 11:37:39 -0700986 goto alloc_encrypted;
Dave Watson3c4d7552017-06-14 11:37:39 -0700987 }
988
Vakul Garga42055e2018-09-21 09:46:13 +0530989 if (!num_async) {
990 goto send_end;
991 } else if (num_zc) {
992 /* Wait for pending encryptions to get completed */
993 smp_store_mb(ctx->async_notify, true);
994
995 if (atomic_read(&ctx->encrypt_pending))
996 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
997 else
998 reinit_completion(&ctx->async_wait.completion);
999
1000 WRITE_ONCE(ctx->async_notify, false);
1001
1002 if (ctx->async_wait.err) {
1003 ret = ctx->async_wait.err;
1004 copied = 0;
1005 }
1006 }
1007
1008 /* Transmit if any encryptions have completed */
1009 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1010 cancel_delayed_work(&ctx->tx_work.work);
1011 tls_tx_records(sk, msg->msg_flags);
1012 }
1013
Dave Watson3c4d7552017-06-14 11:37:39 -07001014send_end:
1015 ret = sk_stream_error(sk, msg->msg_flags, ret);
1016
1017 release_sock(sk);
1018 return copied ? copied : ret;
1019}
1020
John Fastabend0608c692018-12-20 11:35:35 -08001021int tls_sw_do_sendpage(struct sock *sk, struct page *page,
1022 int offset, size_t size, int flags)
Dave Watson3c4d7552017-06-14 11:37:39 -07001023{
Vakul Garga42055e2018-09-21 09:46:13 +05301024 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
Dave Watson3c4d7552017-06-14 11:37:39 -07001025 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001026 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -07001027 unsigned char record_type = TLS_RECORD_TYPE_DATA;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001028 struct sk_msg *msg_pl;
Vakul Garga42055e2018-09-21 09:46:13 +05301029 struct tls_rec *rec;
1030 int num_async = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001031 size_t copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001032 bool full_record;
1033 int record_room;
Vakul Garg4128c0c2018-09-24 16:09:49 +05301034 int ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301035 bool eor;
Dave Watson3c4d7552017-06-14 11:37:39 -07001036
Dave Watson3c4d7552017-06-14 11:37:39 -07001037 eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
Dave Watson3c4d7552017-06-14 11:37:39 -07001038 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1039
Vakul Garga42055e2018-09-21 09:46:13 +05301040 /* Wait till there is any pending write on socket */
1041 if (unlikely(sk->sk_write_pending)) {
1042 ret = wait_on_pending_writer(sk, &timeo);
1043 if (unlikely(ret))
1044 goto sendpage_end;
1045 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001046
1047 /* Call the sk_stream functions to manage the sndbuf mem. */
1048 while (size > 0) {
1049 size_t copy, required_size;
1050
1051 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +01001052 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -07001053 goto sendpage_end;
1054 }
1055
John Fastabendd3b18ad32018-10-13 02:46:01 +02001056 if (ctx->open_rec)
1057 rec = ctx->open_rec;
1058 else
1059 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +05301060 if (!rec) {
1061 ret = -ENOMEM;
1062 goto sendpage_end;
1063 }
1064
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001065 msg_pl = &rec->msg_plaintext;
1066
Dave Watson3c4d7552017-06-14 11:37:39 -07001067 full_record = false;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001068 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001069 copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001070 copy = size;
1071 if (copy >= record_room) {
1072 copy = record_room;
1073 full_record = true;
1074 }
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001075
1076 required_size = msg_pl->sg.size + copy +
1077 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001078
1079 if (!sk_stream_memory_free(sk))
1080 goto wait_for_sndbuf;
1081alloc_payload:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001082 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001083 if (ret) {
1084 if (ret != -ENOSPC)
1085 goto wait_for_memory;
1086
1087 /* Adjust copy according to the amount that was
1088 * actually allocated. The difference is due
1089 * to max sg elements limit
1090 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001091 copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001092 full_record = true;
1093 }
1094
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001095 sk_msg_page_add(msg_pl, page, copy, offset);
Dave Watson3c4d7552017-06-14 11:37:39 -07001096 sk_mem_charge(sk, copy);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001097
Dave Watson3c4d7552017-06-14 11:37:39 -07001098 offset += copy;
1099 size -= copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001100 copied += copy;
Dave Watson3c4d7552017-06-14 11:37:39 -07001101
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001102 tls_ctx->pending_open_record_frags = true;
1103 if (full_record || eor || sk_msg_full(msg_pl)) {
Vakul Garg4e6d4722018-09-30 08:04:35 +05301104 rec->inplace_crypto = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001105 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1106 record_type, &copied, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -07001107 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +05301108 if (ret == -EINPROGRESS)
1109 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001110 else if (ret == -ENOMEM)
1111 goto wait_for_memory;
1112 else if (ret != -EAGAIN) {
1113 if (ret == -ENOSPC)
1114 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301115 goto sendpage_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001116 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001117 }
1118 }
1119 continue;
1120wait_for_sndbuf:
1121 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1122wait_for_memory:
1123 ret = sk_stream_wait_memory(sk, &timeo);
1124 if (ret) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001125 tls_trim_both_msgs(sk, msg_pl->sg.size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001126 goto sendpage_end;
1127 }
1128
Dave Watson3c4d7552017-06-14 11:37:39 -07001129 goto alloc_payload;
1130 }
1131
Vakul Garga42055e2018-09-21 09:46:13 +05301132 if (num_async) {
1133 /* Transmit if any encryptions have completed */
1134 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1135 cancel_delayed_work(&ctx->tx_work.work);
1136 tls_tx_records(sk, flags);
1137 }
1138 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001139sendpage_end:
John Fastabendd3b18ad32018-10-13 02:46:01 +02001140 ret = sk_stream_error(sk, flags, ret);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001141 return copied ? copied : ret;
Dave Watson3c4d7552017-06-14 11:37:39 -07001142}
1143
John Fastabend0608c692018-12-20 11:35:35 -08001144int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
1145 int offset, size_t size, int flags)
1146{
1147 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1148 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1149 return -ENOTSUPP;
1150
1151 return tls_sw_do_sendpage(sk, page, offset, size, flags);
1152}
1153
1154int 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,
1282 int *chunk, bool *zc)
1283{
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;
1382 *chunk = 0;
1383 *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,
1388 data_len, aead_req, *zc);
1389 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 Garg0b243d02018-08-10 20:46:41 +05301401 struct iov_iter *dest, int *chunk, bool *zc)
Boris Pismennydafb67f2018-07-13 14:33:40 +03001402{
1403 struct tls_context *tls_ctx = tls_get_ctx(sk);
1404 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1405 struct strp_msg *rxm = strp_msg(skb);
1406 int err = 0;
1407
Boris Pismenny4799ac82018-07-13 14:33:43 +03001408#ifdef CONFIG_TLS_DEVICE
1409 err = tls_device_decrypted(sk, skb);
Boris Pismennydafb67f2018-07-13 14:33:40 +03001410 if (err < 0)
1411 return err;
Boris Pismenny4799ac82018-07-13 14:33:43 +03001412#endif
1413 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301414 err = decrypt_internal(sk, skb, dest, NULL, chunk, zc);
Vakul Garg94524d82018-08-29 15:26:55 +05301415 if (err < 0) {
1416 if (err == -EINPROGRESS)
1417 tls_advance_record_sn(sk, &tls_ctx->rx);
1418
Boris Pismenny4799ac82018-07-13 14:33:43 +03001419 return err;
Vakul Garg94524d82018-08-29 15:26:55 +05301420 }
Boris Pismenny4799ac82018-07-13 14:33:43 +03001421 } else {
1422 *zc = false;
1423 }
Boris Pismennydafb67f2018-07-13 14:33:40 +03001424
1425 rxm->offset += tls_ctx->rx.prepend_size;
1426 rxm->full_len -= tls_ctx->rx.overhead_size;
1427 tls_advance_record_sn(sk, &tls_ctx->rx);
1428 ctx->decrypted = true;
1429 ctx->saved_data_ready(sk);
1430
1431 return err;
1432}
1433
1434int decrypt_skb(struct sock *sk, struct sk_buff *skb,
1435 struct scatterlist *sgout)
Dave Watsonc46234e2018-03-22 10:10:35 -07001436{
Vakul Garg0b243d02018-08-10 20:46:41 +05301437 bool zc = true;
1438 int chunk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001439
Vakul Garg0b243d02018-08-10 20:46:41 +05301440 return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -07001441}
1442
1443static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb,
1444 unsigned int len)
1445{
1446 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001447 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001448
Vakul Garg94524d82018-08-29 15:26:55 +05301449 if (skb) {
1450 struct strp_msg *rxm = strp_msg(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001451
Vakul Garg94524d82018-08-29 15:26:55 +05301452 if (len < rxm->full_len) {
1453 rxm->offset += len;
1454 rxm->full_len -= len;
1455 return false;
1456 }
1457 kfree_skb(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001458 }
1459
1460 /* Finished with message */
1461 ctx->recv_pkt = NULL;
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -07001462 __strp_unpause(&ctx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07001463
1464 return true;
1465}
1466
1467int tls_sw_recvmsg(struct sock *sk,
1468 struct msghdr *msg,
1469 size_t len,
1470 int nonblock,
1471 int flags,
1472 int *addr_len)
1473{
1474 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001475 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001476 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001477 unsigned char control;
1478 struct strp_msg *rxm;
1479 struct sk_buff *skb;
1480 ssize_t copied = 0;
1481 bool cmsg = false;
Daniel Borkmann06030db2018-06-15 03:07:46 +02001482 int target, err = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001483 long timeo;
David Howells00e23702018-10-22 13:07:28 +01001484 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Vakul Garg94524d82018-08-29 15:26:55 +05301485 int num_async = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001486
1487 flags |= nonblock;
1488
1489 if (unlikely(flags & MSG_ERRQUEUE))
1490 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1491
John Fastabendd3b18ad32018-10-13 02:46:01 +02001492 psock = sk_psock_get(sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001493 lock_sock(sk);
1494
Daniel Borkmann06030db2018-06-15 03:07:46 +02001495 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
Dave Watsonc46234e2018-03-22 10:10:35 -07001496 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1497 do {
1498 bool zc = false;
Vakul Garg94524d82018-08-29 15:26:55 +05301499 bool async = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001500 int chunk = 0;
1501
John Fastabendd3b18ad32018-10-13 02:46:01 +02001502 skb = tls_wait_data(sk, psock, flags, timeo, &err);
1503 if (!skb) {
1504 if (psock) {
John Fastabend02c558b2018-10-16 11:08:04 -07001505 int ret = __tcp_bpf_recvmsg(sk, psock,
1506 msg, len, flags);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001507
1508 if (ret > 0) {
1509 copied += ret;
1510 len -= ret;
1511 continue;
1512 }
1513 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001514 goto recv_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001515 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001516
1517 rxm = strp_msg(skb);
Vakul Garg94524d82018-08-29 15:26:55 +05301518
Dave Watsonc46234e2018-03-22 10:10:35 -07001519 if (!cmsg) {
1520 int cerr;
1521
1522 cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1523 sizeof(ctx->control), &ctx->control);
1524 cmsg = true;
1525 control = ctx->control;
1526 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1527 if (cerr || msg->msg_flags & MSG_CTRUNC) {
1528 err = -EIO;
1529 goto recv_end;
1530 }
1531 }
1532 } else if (control != ctx->control) {
1533 goto recv_end;
1534 }
1535
1536 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301537 int to_copy = rxm->full_len - tls_ctx->rx.overhead_size;
Dave Watsonc46234e2018-03-22 10:10:35 -07001538
Vakul Garg0b243d02018-08-10 20:46:41 +05301539 if (!is_kvec && to_copy <= len &&
1540 likely(!(flags & MSG_PEEK)))
Dave Watsonc46234e2018-03-22 10:10:35 -07001541 zc = true;
Dave Watsonc46234e2018-03-22 10:10:35 -07001542
Vakul Garg0b243d02018-08-10 20:46:41 +05301543 err = decrypt_skb_update(sk, skb, &msg->msg_iter,
1544 &chunk, &zc);
Vakul Garg94524d82018-08-29 15:26:55 +05301545 if (err < 0 && err != -EINPROGRESS) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301546 tls_err_abort(sk, EBADMSG);
1547 goto recv_end;
Dave Watsonc46234e2018-03-22 10:10:35 -07001548 }
Vakul Garg94524d82018-08-29 15:26:55 +05301549
1550 if (err == -EINPROGRESS) {
1551 async = true;
1552 num_async++;
1553 goto pick_next_record;
1554 }
1555
Dave Watsonc46234e2018-03-22 10:10:35 -07001556 ctx->decrypted = true;
1557 }
1558
1559 if (!zc) {
1560 chunk = min_t(unsigned int, rxm->full_len, len);
Vakul Garg94524d82018-08-29 15:26:55 +05301561
Dave Watsonc46234e2018-03-22 10:10:35 -07001562 err = skb_copy_datagram_msg(skb, rxm->offset, msg,
1563 chunk);
1564 if (err < 0)
1565 goto recv_end;
1566 }
1567
Vakul Garg94524d82018-08-29 15:26:55 +05301568pick_next_record:
Dave Watsonc46234e2018-03-22 10:10:35 -07001569 copied += chunk;
1570 len -= chunk;
1571 if (likely(!(flags & MSG_PEEK))) {
1572 u8 control = ctx->control;
1573
Vakul Garg94524d82018-08-29 15:26:55 +05301574 /* For async, drop current skb reference */
1575 if (async)
1576 skb = NULL;
1577
Dave Watsonc46234e2018-03-22 10:10:35 -07001578 if (tls_sw_advance_skb(sk, skb, chunk)) {
1579 /* Return full control message to
1580 * userspace before trying to parse
1581 * another message type
1582 */
1583 msg->msg_flags |= MSG_EOR;
1584 if (control != TLS_RECORD_TYPE_DATA)
1585 goto recv_end;
Vakul Garg94524d82018-08-29 15:26:55 +05301586 } else {
1587 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001588 }
Daniel Borkmann50c6b582018-09-14 23:00:55 +02001589 } else {
1590 /* MSG_PEEK right now cannot look beyond current skb
1591 * from strparser, meaning we cannot advance skb here
1592 * and thus unpause strparser since we'd loose original
1593 * one.
1594 */
1595 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001596 }
Vakul Garg94524d82018-08-29 15:26:55 +05301597
Daniel Borkmann06030db2018-06-15 03:07:46 +02001598 /* If we have a new message from strparser, continue now. */
1599 if (copied >= target && !ctx->recv_pkt)
1600 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001601 } while (len);
1602
1603recv_end:
Vakul Garg94524d82018-08-29 15:26:55 +05301604 if (num_async) {
1605 /* Wait for all previously submitted records to be decrypted */
1606 smp_store_mb(ctx->async_notify, true);
1607 if (atomic_read(&ctx->decrypt_pending)) {
1608 err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1609 if (err) {
1610 /* one of async decrypt failed */
1611 tls_err_abort(sk, err);
1612 copied = 0;
1613 }
1614 } else {
1615 reinit_completion(&ctx->async_wait.completion);
1616 }
1617 WRITE_ONCE(ctx->async_notify, false);
1618 }
1619
Dave Watsonc46234e2018-03-22 10:10:35 -07001620 release_sock(sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001621 if (psock)
1622 sk_psock_put(sk, psock);
Dave Watsonc46234e2018-03-22 10:10:35 -07001623 return copied ? : err;
1624}
1625
1626ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
1627 struct pipe_inode_info *pipe,
1628 size_t len, unsigned int flags)
1629{
1630 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001631 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001632 struct strp_msg *rxm = NULL;
1633 struct sock *sk = sock->sk;
1634 struct sk_buff *skb;
1635 ssize_t copied = 0;
1636 int err = 0;
1637 long timeo;
1638 int chunk;
Vakul Garg0b243d02018-08-10 20:46:41 +05301639 bool zc = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001640
1641 lock_sock(sk);
1642
1643 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1644
John Fastabendd3b18ad32018-10-13 02:46:01 +02001645 skb = tls_wait_data(sk, NULL, flags, timeo, &err);
Dave Watsonc46234e2018-03-22 10:10:35 -07001646 if (!skb)
1647 goto splice_read_end;
1648
1649 /* splice does not support reading control messages */
1650 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1651 err = -ENOTSUPP;
1652 goto splice_read_end;
1653 }
1654
1655 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301656 err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -07001657
1658 if (err < 0) {
1659 tls_err_abort(sk, EBADMSG);
1660 goto splice_read_end;
1661 }
1662 ctx->decrypted = true;
1663 }
1664 rxm = strp_msg(skb);
1665
1666 chunk = min_t(unsigned int, rxm->full_len, len);
1667 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
1668 if (copied < 0)
1669 goto splice_read_end;
1670
1671 if (likely(!(flags & MSG_PEEK)))
1672 tls_sw_advance_skb(sk, skb, copied);
1673
1674splice_read_end:
1675 release_sock(sk);
1676 return copied ? : err;
1677}
1678
John Fastabend924ad652018-10-13 02:46:00 +02001679bool tls_sw_stream_read(const struct sock *sk)
Dave Watsonc46234e2018-03-22 10:10:35 -07001680{
Dave Watsonc46234e2018-03-22 10:10:35 -07001681 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001682 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001683 bool ingress_empty = true;
1684 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001685
John Fastabendd3b18ad32018-10-13 02:46:01 +02001686 rcu_read_lock();
1687 psock = sk_psock(sk);
1688 if (psock)
1689 ingress_empty = list_empty(&psock->ingress_msg);
1690 rcu_read_unlock();
Dave Watsonc46234e2018-03-22 10:10:35 -07001691
John Fastabendd3b18ad32018-10-13 02:46:01 +02001692 return !ingress_empty || ctx->recv_pkt;
Dave Watsonc46234e2018-03-22 10:10:35 -07001693}
1694
1695static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1696{
1697 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001698 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Kees Cook3463e512018-06-25 16:55:05 -07001699 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
Dave Watsonc46234e2018-03-22 10:10:35 -07001700 struct strp_msg *rxm = strp_msg(skb);
1701 size_t cipher_overhead;
1702 size_t data_len = 0;
1703 int ret;
1704
1705 /* Verify that we have a full TLS header, or wait for more data */
1706 if (rxm->offset + tls_ctx->rx.prepend_size > skb->len)
1707 return 0;
1708
Kees Cook3463e512018-06-25 16:55:05 -07001709 /* Sanity-check size of on-stack buffer. */
1710 if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) {
1711 ret = -EINVAL;
1712 goto read_failure;
1713 }
1714
Dave Watsonc46234e2018-03-22 10:10:35 -07001715 /* Linearize header to local buffer */
1716 ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size);
1717
1718 if (ret < 0)
1719 goto read_failure;
1720
1721 ctx->control = header[0];
1722
1723 data_len = ((header[4] & 0xFF) | (header[3] << 8));
1724
1725 cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size;
1726
1727 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) {
1728 ret = -EMSGSIZE;
1729 goto read_failure;
1730 }
1731 if (data_len < cipher_overhead) {
1732 ret = -EBADMSG;
1733 goto read_failure;
1734 }
1735
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001736 if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.info.version) ||
1737 header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.info.version)) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001738 ret = -EINVAL;
1739 goto read_failure;
1740 }
1741
Boris Pismenny4799ac82018-07-13 14:33:43 +03001742#ifdef CONFIG_TLS_DEVICE
1743 handle_device_resync(strp->sk, TCP_SKB_CB(skb)->seq + rxm->offset,
1744 *(u64*)tls_ctx->rx.rec_seq);
1745#endif
Dave Watsonc46234e2018-03-22 10:10:35 -07001746 return data_len + TLS_HEADER_SIZE;
1747
1748read_failure:
1749 tls_err_abort(strp->sk, ret);
1750
1751 return ret;
1752}
1753
1754static void tls_queue(struct strparser *strp, struct sk_buff *skb)
1755{
1756 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001757 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001758
1759 ctx->decrypted = false;
1760
1761 ctx->recv_pkt = skb;
1762 strp_pause(strp);
1763
Vakul Gargad13acc2018-07-30 16:08:33 +05301764 ctx->saved_data_ready(strp->sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001765}
1766
1767static void tls_data_ready(struct sock *sk)
1768{
1769 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001770 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001771 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001772
1773 strp_data_ready(&ctx->strp);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001774
1775 psock = sk_psock_get(sk);
1776 if (psock && !list_empty(&psock->ingress_msg)) {
1777 ctx->saved_data_ready(sk);
1778 sk_psock_put(sk, psock);
1779 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001780}
1781
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001782void tls_sw_free_resources_tx(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -07001783{
1784 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001785 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +05301786 struct tls_rec *rec, *tmp;
1787
1788 /* Wait for any pending async encryptions to complete */
1789 smp_store_mb(ctx->async_notify, true);
1790 if (atomic_read(&ctx->encrypt_pending))
1791 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1792
1793 cancel_delayed_work_sync(&ctx->tx_work.work);
1794
1795 /* Tx whatever records we can transmit and abandon the rest */
1796 tls_tx_records(sk, -1);
1797
Vakul Garg9932a292018-09-24 15:35:56 +05301798 /* Free up un-sent records in tx_list. First, free
Vakul Garga42055e2018-09-21 09:46:13 +05301799 * the partially sent record if any at head of tx_list.
1800 */
1801 if (tls_ctx->partially_sent_record) {
1802 struct scatterlist *sg = tls_ctx->partially_sent_record;
1803
1804 while (1) {
1805 put_page(sg_page(sg));
1806 sk_mem_uncharge(sk, sg->length);
1807
1808 if (sg_is_last(sg))
1809 break;
1810 sg++;
1811 }
1812
1813 tls_ctx->partially_sent_record = NULL;
1814
Vakul Garg9932a292018-09-24 15:35:56 +05301815 rec = list_first_entry(&ctx->tx_list,
Vakul Garga42055e2018-09-21 09:46:13 +05301816 struct tls_rec, list);
1817 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001818 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301819 kfree(rec);
1820 }
1821
Vakul Garg9932a292018-09-24 15:35:56 +05301822 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
Vakul Garga42055e2018-09-21 09:46:13 +05301823 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001824 sk_msg_free(sk, &rec->msg_encrypted);
1825 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301826 kfree(rec);
1827 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001828
Vakul Garg201876b2018-07-24 16:54:27 +05301829 crypto_free_aead(ctx->aead_send);
Vakul Gargc7749732018-09-25 20:21:51 +05301830 tls_free_open_rec(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001831
1832 kfree(ctx);
1833}
1834
Boris Pismenny39f56e12018-07-13 14:33:41 +03001835void tls_sw_release_resources_rx(struct sock *sk)
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001836{
1837 struct tls_context *tls_ctx = tls_get_ctx(sk);
1838 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1839
Dave Watsonc46234e2018-03-22 10:10:35 -07001840 if (ctx->aead_recv) {
Vakul Garg201876b2018-07-24 16:54:27 +05301841 kfree_skb(ctx->recv_pkt);
1842 ctx->recv_pkt = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001843 crypto_free_aead(ctx->aead_recv);
1844 strp_stop(&ctx->strp);
1845 write_lock_bh(&sk->sk_callback_lock);
1846 sk->sk_data_ready = ctx->saved_data_ready;
1847 write_unlock_bh(&sk->sk_callback_lock);
1848 release_sock(sk);
1849 strp_done(&ctx->strp);
1850 lock_sock(sk);
1851 }
Boris Pismenny39f56e12018-07-13 14:33:41 +03001852}
1853
1854void tls_sw_free_resources_rx(struct sock *sk)
1855{
1856 struct tls_context *tls_ctx = tls_get_ctx(sk);
1857 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1858
1859 tls_sw_release_resources_rx(sk);
Dave Watson3c4d7552017-06-14 11:37:39 -07001860
Dave Watson3c4d7552017-06-14 11:37:39 -07001861 kfree(ctx);
1862}
1863
Vakul Garg9932a292018-09-24 15:35:56 +05301864/* The work handler to transmitt the encrypted records in tx_list */
Vakul Garga42055e2018-09-21 09:46:13 +05301865static void tx_work_handler(struct work_struct *work)
1866{
1867 struct delayed_work *delayed_work = to_delayed_work(work);
1868 struct tx_work *tx_work = container_of(delayed_work,
1869 struct tx_work, work);
1870 struct sock *sk = tx_work->sk;
1871 struct tls_context *tls_ctx = tls_get_ctx(sk);
1872 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1873
1874 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
1875 return;
1876
1877 lock_sock(sk);
1878 tls_tx_records(sk, -1);
1879 release_sock(sk);
1880}
1881
Dave Watsonc46234e2018-03-22 10:10:35 -07001882int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
Dave Watson3c4d7552017-06-14 11:37:39 -07001883{
Dave Watson3c4d7552017-06-14 11:37:39 -07001884 struct tls_crypto_info *crypto_info;
1885 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001886 struct tls_sw_context_tx *sw_ctx_tx = NULL;
1887 struct tls_sw_context_rx *sw_ctx_rx = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001888 struct cipher_context *cctx;
1889 struct crypto_aead **aead;
1890 struct strp_callbacks cb;
Dave Watson3c4d7552017-06-14 11:37:39 -07001891 u16 nonce_size, tag_size, iv_size, rec_seq_size;
1892 char *iv, *rec_seq;
1893 int rc = 0;
1894
1895 if (!ctx) {
1896 rc = -EINVAL;
1897 goto out;
1898 }
1899
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001900 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001901 if (!ctx->priv_ctx_tx) {
1902 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
1903 if (!sw_ctx_tx) {
1904 rc = -ENOMEM;
1905 goto out;
1906 }
1907 ctx->priv_ctx_tx = sw_ctx_tx;
1908 } else {
1909 sw_ctx_tx =
1910 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
Dave Watsonc46234e2018-03-22 10:10:35 -07001911 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001912 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001913 if (!ctx->priv_ctx_rx) {
1914 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
1915 if (!sw_ctx_rx) {
1916 rc = -ENOMEM;
1917 goto out;
1918 }
1919 ctx->priv_ctx_rx = sw_ctx_rx;
1920 } else {
1921 sw_ctx_rx =
1922 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001923 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001924 }
1925
Dave Watsonc46234e2018-03-22 10:10:35 -07001926 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001927 crypto_init_wait(&sw_ctx_tx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001928 crypto_info = &ctx->crypto_send.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07001929 cctx = &ctx->tx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001930 aead = &sw_ctx_tx->aead_send;
Vakul Garg9932a292018-09-24 15:35:56 +05301931 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +05301932 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
1933 sw_ctx_tx->tx_work.sk = sk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001934 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001935 crypto_init_wait(&sw_ctx_rx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001936 crypto_info = &ctx->crypto_recv.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07001937 cctx = &ctx->rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001938 aead = &sw_ctx_rx->aead_recv;
Dave Watsonc46234e2018-03-22 10:10:35 -07001939 }
1940
Dave Watson3c4d7552017-06-14 11:37:39 -07001941 switch (crypto_info->cipher_type) {
1942 case TLS_CIPHER_AES_GCM_128: {
1943 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1944 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
1945 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1946 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1947 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
1948 rec_seq =
1949 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1950 gcm_128_info =
1951 (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
1952 break;
1953 }
1954 default:
1955 rc = -EINVAL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001956 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001957 }
1958
Kees Cookb16520f2018-04-10 17:52:34 -07001959 /* Sanity-check the IV size for stack allocations. */
Kees Cook3463e512018-06-25 16:55:05 -07001960 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) {
Kees Cookb16520f2018-04-10 17:52:34 -07001961 rc = -EINVAL;
1962 goto free_priv;
1963 }
1964
Dave Watsonc46234e2018-03-22 10:10:35 -07001965 cctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
1966 cctx->tag_size = tag_size;
1967 cctx->overhead_size = cctx->prepend_size + cctx->tag_size;
1968 cctx->iv_size = iv_size;
1969 cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1970 GFP_KERNEL);
1971 if (!cctx->iv) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001972 rc = -ENOMEM;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001973 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001974 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001975 memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
1976 memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
1977 cctx->rec_seq_size = rec_seq_size;
zhong jiang969d5092018-08-01 00:50:24 +08001978 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
Dave Watsonc46234e2018-03-22 10:10:35 -07001979 if (!cctx->rec_seq) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001980 rc = -ENOMEM;
1981 goto free_iv;
1982 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001983
Dave Watsonc46234e2018-03-22 10:10:35 -07001984 if (!*aead) {
1985 *aead = crypto_alloc_aead("gcm(aes)", 0, 0);
1986 if (IS_ERR(*aead)) {
1987 rc = PTR_ERR(*aead);
1988 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07001989 goto free_rec_seq;
1990 }
1991 }
1992
1993 ctx->push_pending_record = tls_sw_push_pending_record;
1994
Sabrina Dubroca7cba09c2018-09-12 17:44:41 +02001995 rc = crypto_aead_setkey(*aead, gcm_128_info->key,
Dave Watson3c4d7552017-06-14 11:37:39 -07001996 TLS_CIPHER_AES_GCM_128_KEY_SIZE);
1997 if (rc)
1998 goto free_aead;
1999
Dave Watsonc46234e2018-03-22 10:10:35 -07002000 rc = crypto_aead_setauthsize(*aead, cctx->tag_size);
2001 if (rc)
2002 goto free_aead;
2003
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002004 if (sw_ctx_rx) {
Dave Watsonc46234e2018-03-22 10:10:35 -07002005 /* Set up strparser */
2006 memset(&cb, 0, sizeof(cb));
2007 cb.rcv_msg = tls_queue;
2008 cb.parse_msg = tls_read_size;
2009
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002010 strp_init(&sw_ctx_rx->strp, sk, &cb);
Dave Watsonc46234e2018-03-22 10:10:35 -07002011
2012 write_lock_bh(&sk->sk_callback_lock);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002013 sw_ctx_rx->saved_data_ready = sk->sk_data_ready;
Dave Watsonc46234e2018-03-22 10:10:35 -07002014 sk->sk_data_ready = tls_data_ready;
2015 write_unlock_bh(&sk->sk_callback_lock);
2016
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002017 strp_check_rcv(&sw_ctx_rx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07002018 }
2019
2020 goto out;
Dave Watson3c4d7552017-06-14 11:37:39 -07002021
2022free_aead:
Dave Watsonc46234e2018-03-22 10:10:35 -07002023 crypto_free_aead(*aead);
2024 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002025free_rec_seq:
Dave Watsonc46234e2018-03-22 10:10:35 -07002026 kfree(cctx->rec_seq);
2027 cctx->rec_seq = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002028free_iv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002029 kfree(cctx->iv);
2030 cctx->iv = NULL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01002031free_priv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002032 if (tx) {
2033 kfree(ctx->priv_ctx_tx);
2034 ctx->priv_ctx_tx = NULL;
2035 } else {
2036 kfree(ctx->priv_ctx_rx);
2037 ctx->priv_ctx_rx = NULL;
2038 }
Dave Watson3c4d7552017-06-14 11:37:39 -07002039out:
2040 return rc;
2041}