blob: 7e963560edefad7db87bf2ad47761155fd27baeb [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
Dave Watson32eb67b2019-01-27 00:57:38 +0000442 memcpy(rec->iv_data, tls_ctx->tx.iv, sizeof(rec->iv_data));
443
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200444 sge->offset += tls_ctx->tx.prepend_size;
445 sge->length -= tls_ctx->tx.prepend_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700446
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200447 msg_en->sg.curr = start;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530448
Dave Watson3c4d7552017-06-14 11:37:39 -0700449 aead_request_set_tfm(aead_req, ctx->aead_send);
450 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200451 aead_request_set_crypt(aead_req, rec->sg_aead_in,
452 rec->sg_aead_out,
Dave Watson32eb67b2019-01-27 00:57:38 +0000453 data_len, rec->iv_data);
Vakul Garga54667f2018-01-31 21:34:37 +0530454
455 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Vakul Garga42055e2018-09-21 09:46:13 +0530456 tls_encrypt_done, sk);
Vakul Garga54667f2018-01-31 21:34:37 +0530457
Vakul Garg9932a292018-09-24 15:35:56 +0530458 /* Add the record in tx_list */
459 list_add_tail((struct list_head *)&rec->list, &ctx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +0530460 atomic_inc(&ctx->encrypt_pending);
Dave Watson3c4d7552017-06-14 11:37:39 -0700461
Vakul Garga42055e2018-09-21 09:46:13 +0530462 rc = crypto_aead_encrypt(aead_req);
463 if (!rc || rc != -EINPROGRESS) {
464 atomic_dec(&ctx->encrypt_pending);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200465 sge->offset -= tls_ctx->tx.prepend_size;
466 sge->length += tls_ctx->tx.prepend_size;
Vakul Garga42055e2018-09-21 09:46:13 +0530467 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700468
Vakul Garg9932a292018-09-24 15:35:56 +0530469 if (!rc) {
470 WRITE_ONCE(rec->tx_ready, true);
471 } else if (rc != -EINPROGRESS) {
472 list_del(&rec->list);
Vakul Garga42055e2018-09-21 09:46:13 +0530473 return rc;
Vakul Garg9932a292018-09-24 15:35:56 +0530474 }
Vakul Garga42055e2018-09-21 09:46:13 +0530475
476 /* Unhook the record from context if encryption is not failure */
477 ctx->open_rec = NULL;
478 tls_advance_record_sn(sk, &tls_ctx->tx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700479 return rc;
480}
481
John Fastabendd3b18ad32018-10-13 02:46:01 +0200482static int tls_split_open_record(struct sock *sk, struct tls_rec *from,
483 struct tls_rec **to, struct sk_msg *msg_opl,
484 struct sk_msg *msg_oen, u32 split_point,
485 u32 tx_overhead_size, u32 *orig_end)
486{
487 u32 i, j, bytes = 0, apply = msg_opl->apply_bytes;
488 struct scatterlist *sge, *osge, *nsge;
489 u32 orig_size = msg_opl->sg.size;
490 struct scatterlist tmp = { };
491 struct sk_msg *msg_npl;
492 struct tls_rec *new;
493 int ret;
494
495 new = tls_get_rec(sk);
496 if (!new)
497 return -ENOMEM;
498 ret = sk_msg_alloc(sk, &new->msg_encrypted, msg_opl->sg.size +
499 tx_overhead_size, 0);
500 if (ret < 0) {
501 tls_free_rec(sk, new);
502 return ret;
503 }
504
505 *orig_end = msg_opl->sg.end;
506 i = msg_opl->sg.start;
507 sge = sk_msg_elem(msg_opl, i);
508 while (apply && sge->length) {
509 if (sge->length > apply) {
510 u32 len = sge->length - apply;
511
512 get_page(sg_page(sge));
513 sg_set_page(&tmp, sg_page(sge), len,
514 sge->offset + apply);
515 sge->length = apply;
516 bytes += apply;
517 apply = 0;
518 } else {
519 apply -= sge->length;
520 bytes += sge->length;
521 }
522
523 sk_msg_iter_var_next(i);
524 if (i == msg_opl->sg.end)
525 break;
526 sge = sk_msg_elem(msg_opl, i);
527 }
528
529 msg_opl->sg.end = i;
530 msg_opl->sg.curr = i;
531 msg_opl->sg.copybreak = 0;
532 msg_opl->apply_bytes = 0;
533 msg_opl->sg.size = bytes;
534
535 msg_npl = &new->msg_plaintext;
536 msg_npl->apply_bytes = apply;
537 msg_npl->sg.size = orig_size - bytes;
538
539 j = msg_npl->sg.start;
540 nsge = sk_msg_elem(msg_npl, j);
541 if (tmp.length) {
542 memcpy(nsge, &tmp, sizeof(*nsge));
543 sk_msg_iter_var_next(j);
544 nsge = sk_msg_elem(msg_npl, j);
545 }
546
547 osge = sk_msg_elem(msg_opl, i);
548 while (osge->length) {
549 memcpy(nsge, osge, sizeof(*nsge));
550 sg_unmark_end(nsge);
551 sk_msg_iter_var_next(i);
552 sk_msg_iter_var_next(j);
553 if (i == *orig_end)
554 break;
555 osge = sk_msg_elem(msg_opl, i);
556 nsge = sk_msg_elem(msg_npl, j);
557 }
558
559 msg_npl->sg.end = j;
560 msg_npl->sg.curr = j;
561 msg_npl->sg.copybreak = 0;
562
563 *to = new;
564 return 0;
565}
566
567static void tls_merge_open_record(struct sock *sk, struct tls_rec *to,
568 struct tls_rec *from, u32 orig_end)
569{
570 struct sk_msg *msg_npl = &from->msg_plaintext;
571 struct sk_msg *msg_opl = &to->msg_plaintext;
572 struct scatterlist *osge, *nsge;
573 u32 i, j;
574
575 i = msg_opl->sg.end;
576 sk_msg_iter_var_prev(i);
577 j = msg_npl->sg.start;
578
579 osge = sk_msg_elem(msg_opl, i);
580 nsge = sk_msg_elem(msg_npl, j);
581
582 if (sg_page(osge) == sg_page(nsge) &&
583 osge->offset + osge->length == nsge->offset) {
584 osge->length += nsge->length;
585 put_page(sg_page(nsge));
586 }
587
588 msg_opl->sg.end = orig_end;
589 msg_opl->sg.curr = orig_end;
590 msg_opl->sg.copybreak = 0;
591 msg_opl->apply_bytes = msg_opl->sg.size + msg_npl->sg.size;
592 msg_opl->sg.size += msg_npl->sg.size;
593
594 sk_msg_free(sk, &to->msg_encrypted);
595 sk_msg_xfer_full(&to->msg_encrypted, &from->msg_encrypted);
596
597 kfree(from);
598}
599
Dave Watson3c4d7552017-06-14 11:37:39 -0700600static int tls_push_record(struct sock *sk, int flags,
601 unsigned char record_type)
602{
603 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300604 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200605 struct tls_rec *rec = ctx->open_rec, *tmp = NULL;
606 u32 i, split_point, uninitialized_var(orig_end);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200607 struct sk_msg *msg_pl, *msg_en;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200608 struct aead_request *req;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200609 bool split;
Dave Watson3c4d7552017-06-14 11:37:39 -0700610 int rc;
611
Vakul Garga42055e2018-09-21 09:46:13 +0530612 if (!rec)
613 return 0;
Daniel Borkmanna447da72018-06-15 03:07:45 +0200614
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200615 msg_pl = &rec->msg_plaintext;
616 msg_en = &rec->msg_encrypted;
617
John Fastabendd3b18ad32018-10-13 02:46:01 +0200618 split_point = msg_pl->apply_bytes;
619 split = split_point && split_point < msg_pl->sg.size;
620 if (split) {
621 rc = tls_split_open_record(sk, rec, &tmp, msg_pl, msg_en,
622 split_point, tls_ctx->tx.overhead_size,
623 &orig_end);
624 if (rc < 0)
625 return rc;
626 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
627 tls_ctx->tx.overhead_size);
628 }
629
Vakul Garga42055e2018-09-21 09:46:13 +0530630 rec->tx_flags = flags;
631 req = &rec->aead_req;
Dave Watson3c4d7552017-06-14 11:37:39 -0700632
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200633 i = msg_pl->sg.end;
634 sk_msg_iter_var_prev(i);
635 sg_mark_end(sk_msg_elem(msg_pl, i));
Vakul Garga42055e2018-09-21 09:46:13 +0530636
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200637 i = msg_pl->sg.start;
638 sg_chain(rec->sg_aead_in, 2, rec->inplace_crypto ?
639 &msg_en->sg.data[i] : &msg_pl->sg.data[i]);
640
641 i = msg_en->sg.end;
642 sk_msg_iter_var_prev(i);
643 sg_mark_end(sk_msg_elem(msg_en, i));
644
645 i = msg_en->sg.start;
646 sg_chain(rec->sg_aead_out, 2, &msg_en->sg.data[i]);
647
648 tls_make_aad(rec->aad_space, msg_pl->sg.size,
Dave Watsondbe42552018-03-22 10:10:06 -0700649 tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size,
Dave Watson3c4d7552017-06-14 11:37:39 -0700650 record_type);
651
652 tls_fill_prepend(tls_ctx,
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200653 page_address(sg_page(&msg_en->sg.data[i])) +
654 msg_en->sg.data[i].offset, msg_pl->sg.size,
655 record_type);
Dave Watson3c4d7552017-06-14 11:37:39 -0700656
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200657 tls_ctx->pending_open_record_frags = false;
Dave Watson3c4d7552017-06-14 11:37:39 -0700658
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200659 rc = tls_do_encryption(sk, tls_ctx, ctx, req, msg_pl->sg.size, i);
Dave Watson3c4d7552017-06-14 11:37:39 -0700660 if (rc < 0) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200661 if (rc != -EINPROGRESS) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200662 tls_err_abort(sk, EBADMSG);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200663 if (split) {
664 tls_ctx->pending_open_record_frags = true;
665 tls_merge_open_record(sk, rec, tmp, orig_end);
666 }
667 }
Vakul Garga42055e2018-09-21 09:46:13 +0530668 return rc;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200669 } else if (split) {
670 msg_pl = &tmp->msg_plaintext;
671 msg_en = &tmp->msg_encrypted;
672 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
673 tls_ctx->tx.overhead_size);
674 tls_ctx->pending_open_record_frags = true;
675 ctx->open_rec = tmp;
Dave Watson3c4d7552017-06-14 11:37:39 -0700676 }
677
Vakul Garg9932a292018-09-24 15:35:56 +0530678 return tls_tx_records(sk, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700679}
680
John Fastabendd3b18ad32018-10-13 02:46:01 +0200681static int bpf_exec_tx_verdict(struct sk_msg *msg, struct sock *sk,
682 bool full_record, u8 record_type,
683 size_t *copied, int flags)
Dave Watson3c4d7552017-06-14 11:37:39 -0700684{
685 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300686 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200687 struct sk_msg msg_redir = { };
688 struct sk_psock *psock;
689 struct sock *sk_redir;
Vakul Garga42055e2018-09-21 09:46:13 +0530690 struct tls_rec *rec;
John Fastabend0608c692018-12-20 11:35:35 -0800691 bool enospc, policy;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200692 int err = 0, send;
John Fastabend7246d8e2018-11-26 14:16:17 -0800693 u32 delta = 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530694
John Fastabend0608c692018-12-20 11:35:35 -0800695 policy = !(flags & MSG_SENDPAGE_NOPOLICY);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200696 psock = sk_psock_get(sk);
John Fastabend0608c692018-12-20 11:35:35 -0800697 if (!psock || !policy)
John Fastabendd3b18ad32018-10-13 02:46:01 +0200698 return tls_push_record(sk, flags, record_type);
699more_data:
700 enospc = sk_msg_full(msg);
John Fastabend7246d8e2018-11-26 14:16:17 -0800701 if (psock->eval == __SK_NONE) {
702 delta = msg->sg.size;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200703 psock->eval = sk_psock_msg_verdict(sk, psock, msg);
John Fastabend7246d8e2018-11-26 14:16:17 -0800704 if (delta < msg->sg.size)
705 delta -= msg->sg.size;
706 else
707 delta = 0;
708 }
John Fastabendd3b18ad32018-10-13 02:46:01 +0200709 if (msg->cork_bytes && msg->cork_bytes > msg->sg.size &&
710 !enospc && !full_record) {
711 err = -ENOSPC;
712 goto out_err;
713 }
714 msg->cork_bytes = 0;
715 send = msg->sg.size;
716 if (msg->apply_bytes && msg->apply_bytes < send)
717 send = msg->apply_bytes;
Vakul Garga42055e2018-09-21 09:46:13 +0530718
John Fastabendd3b18ad32018-10-13 02:46:01 +0200719 switch (psock->eval) {
720 case __SK_PASS:
721 err = tls_push_record(sk, flags, record_type);
722 if (err < 0) {
723 *copied -= sk_msg_free(sk, msg);
724 tls_free_open_rec(sk);
725 goto out_err;
726 }
727 break;
728 case __SK_REDIRECT:
729 sk_redir = psock->sk_redir;
730 memcpy(&msg_redir, msg, sizeof(*msg));
731 if (msg->apply_bytes < send)
732 msg->apply_bytes = 0;
733 else
734 msg->apply_bytes -= send;
735 sk_msg_return_zero(sk, msg, send);
736 msg->sg.size -= send;
737 release_sock(sk);
738 err = tcp_bpf_sendmsg_redir(sk_redir, &msg_redir, send, flags);
739 lock_sock(sk);
740 if (err < 0) {
741 *copied -= sk_msg_free_nocharge(sk, &msg_redir);
742 msg->sg.size = 0;
743 }
744 if (msg->sg.size == 0)
745 tls_free_open_rec(sk);
746 break;
747 case __SK_DROP:
748 default:
749 sk_msg_free_partial(sk, msg, send);
750 if (msg->apply_bytes < send)
751 msg->apply_bytes = 0;
752 else
753 msg->apply_bytes -= send;
754 if (msg->sg.size == 0)
755 tls_free_open_rec(sk);
John Fastabend7246d8e2018-11-26 14:16:17 -0800756 *copied -= (send + delta);
John Fastabendd3b18ad32018-10-13 02:46:01 +0200757 err = -EACCES;
758 }
Vakul Garga42055e2018-09-21 09:46:13 +0530759
John Fastabendd3b18ad32018-10-13 02:46:01 +0200760 if (likely(!err)) {
761 bool reset_eval = !ctx->open_rec;
762
763 rec = ctx->open_rec;
764 if (rec) {
765 msg = &rec->msg_plaintext;
766 if (!msg->apply_bytes)
767 reset_eval = true;
768 }
769 if (reset_eval) {
770 psock->eval = __SK_NONE;
771 if (psock->sk_redir) {
772 sock_put(psock->sk_redir);
773 psock->sk_redir = NULL;
774 }
775 }
776 if (rec)
777 goto more_data;
778 }
779 out_err:
780 sk_psock_put(sk, psock);
781 return err;
782}
783
784static int tls_sw_push_pending_record(struct sock *sk, int flags)
785{
786 struct tls_context *tls_ctx = tls_get_ctx(sk);
787 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
788 struct tls_rec *rec = ctx->open_rec;
789 struct sk_msg *msg_pl;
790 size_t copied;
791
Vakul Garga42055e2018-09-21 09:46:13 +0530792 if (!rec)
John Fastabendd3b18ad32018-10-13 02:46:01 +0200793 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530794
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200795 msg_pl = &rec->msg_plaintext;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200796 copied = msg_pl->sg.size;
797 if (!copied)
798 return 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530799
John Fastabendd3b18ad32018-10-13 02:46:01 +0200800 return bpf_exec_tx_verdict(msg_pl, sk, true, TLS_RECORD_TYPE_DATA,
801 &copied, flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530802}
803
804int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
805{
Dave Watson3c4d7552017-06-14 11:37:39 -0700806 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
Vakul Garga42055e2018-09-21 09:46:13 +0530807 struct tls_context *tls_ctx = tls_get_ctx(sk);
808 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
809 struct crypto_tfm *tfm = crypto_aead_tfm(ctx->aead_send);
810 bool async_capable = tfm->__crt_alg->cra_flags & CRYPTO_ALG_ASYNC;
811 unsigned char record_type = TLS_RECORD_TYPE_DATA;
David Howells00e23702018-10-22 13:07:28 +0100812 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Dave Watson3c4d7552017-06-14 11:37:39 -0700813 bool eor = !(msg->msg_flags & MSG_MORE);
814 size_t try_to_copy, copied = 0;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200815 struct sk_msg *msg_pl, *msg_en;
Vakul Garga42055e2018-09-21 09:46:13 +0530816 struct tls_rec *rec;
817 int required_size;
818 int num_async = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700819 bool full_record;
Vakul Garga42055e2018-09-21 09:46:13 +0530820 int record_room;
821 int num_zc = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700822 int orig_size;
Vakul Garg4128c0c2018-09-24 16:09:49 +0530823 int ret = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -0700824
825 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
826 return -ENOTSUPP;
827
828 lock_sock(sk);
829
Vakul Garga42055e2018-09-21 09:46:13 +0530830 /* Wait till there is any pending write on socket */
831 if (unlikely(sk->sk_write_pending)) {
832 ret = wait_on_pending_writer(sk, &timeo);
833 if (unlikely(ret))
834 goto send_end;
835 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700836
837 if (unlikely(msg->msg_controllen)) {
838 ret = tls_proccess_cmsg(sk, msg, &record_type);
Vakul Garga42055e2018-09-21 09:46:13 +0530839 if (ret) {
840 if (ret == -EINPROGRESS)
841 num_async++;
842 else if (ret != -EAGAIN)
843 goto send_end;
844 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700845 }
846
847 while (msg_data_left(msg)) {
848 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +0100849 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -0700850 goto send_end;
851 }
852
John Fastabendd3b18ad32018-10-13 02:46:01 +0200853 if (ctx->open_rec)
854 rec = ctx->open_rec;
855 else
856 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +0530857 if (!rec) {
858 ret = -ENOMEM;
859 goto send_end;
860 }
861
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200862 msg_pl = &rec->msg_plaintext;
863 msg_en = &rec->msg_encrypted;
864
865 orig_size = msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700866 full_record = false;
867 try_to_copy = msg_data_left(msg);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200868 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700869 if (try_to_copy >= record_room) {
870 try_to_copy = record_room;
871 full_record = true;
872 }
873
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200874 required_size = msg_pl->sg.size + try_to_copy +
Dave Watsondbe42552018-03-22 10:10:06 -0700875 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700876
877 if (!sk_stream_memory_free(sk))
878 goto wait_for_sndbuf;
Vakul Garga42055e2018-09-21 09:46:13 +0530879
Dave Watson3c4d7552017-06-14 11:37:39 -0700880alloc_encrypted:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200881 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700882 if (ret) {
883 if (ret != -ENOSPC)
884 goto wait_for_memory;
885
886 /* Adjust try_to_copy according to the amount that was
887 * actually allocated. The difference is due
888 * to max sg elements limit
889 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200890 try_to_copy -= required_size - msg_en->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700891 full_record = true;
892 }
Vakul Garga42055e2018-09-21 09:46:13 +0530893
894 if (!is_kvec && (full_record || eor) && !async_capable) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200895 u32 first = msg_pl->sg.end;
896
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200897 ret = sk_msg_zerocopy_from_iter(sk, &msg->msg_iter,
898 msg_pl, try_to_copy);
Dave Watson3c4d7552017-06-14 11:37:39 -0700899 if (ret)
900 goto fallback_to_reg_send;
901
Vakul Garg4e6d4722018-09-30 08:04:35 +0530902 rec->inplace_crypto = 0;
903
Vakul Garga42055e2018-09-21 09:46:13 +0530904 num_zc++;
Dave Watson3c4d7552017-06-14 11:37:39 -0700905 copied += try_to_copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200906
907 sk_msg_sg_copy_set(msg_pl, first);
908 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
909 record_type, &copied,
910 msg->msg_flags);
Vakul Garga42055e2018-09-21 09:46:13 +0530911 if (ret) {
912 if (ret == -EINPROGRESS)
913 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200914 else if (ret == -ENOMEM)
915 goto wait_for_memory;
916 else if (ret == -ENOSPC)
917 goto rollback_iter;
Vakul Garga42055e2018-09-21 09:46:13 +0530918 else if (ret != -EAGAIN)
919 goto send_end;
920 }
Doron Roberts-Kedes5a3611e2018-07-26 07:59:35 -0700921 continue;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200922rollback_iter:
923 copied -= try_to_copy;
924 sk_msg_sg_copy_clear(msg_pl, first);
925 iov_iter_revert(&msg->msg_iter,
926 msg_pl->sg.size - orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700927fallback_to_reg_send:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200928 sk_msg_trim(sk, msg_pl, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700929 }
930
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200931 required_size = msg_pl->sg.size + try_to_copy;
Vakul Garg4e6d4722018-09-30 08:04:35 +0530932
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200933 ret = tls_clone_plaintext_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700934 if (ret) {
935 if (ret != -ENOSPC)
Vakul Garg4e6d4722018-09-30 08:04:35 +0530936 goto send_end;
Dave Watson3c4d7552017-06-14 11:37:39 -0700937
938 /* Adjust try_to_copy according to the amount that was
939 * actually allocated. The difference is due
940 * to max sg elements limit
941 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200942 try_to_copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700943 full_record = true;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200944 sk_msg_trim(sk, msg_en, msg_pl->sg.size +
945 tls_ctx->tx.overhead_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700946 }
947
Vakul Garg65a10e22018-12-21 15:16:52 +0000948 if (try_to_copy) {
949 ret = sk_msg_memcopy_from_iter(sk, &msg->msg_iter,
950 msg_pl, try_to_copy);
951 if (ret < 0)
952 goto trim_sgl;
953 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700954
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200955 /* Open records defined only if successfully copied, otherwise
956 * we would trim the sg but not reset the open record frags.
957 */
958 tls_ctx->pending_open_record_frags = true;
Dave Watson3c4d7552017-06-14 11:37:39 -0700959 copied += try_to_copy;
960 if (full_record || eor) {
John Fastabendd3b18ad32018-10-13 02:46:01 +0200961 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
962 record_type, &copied,
963 msg->msg_flags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700964 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +0530965 if (ret == -EINPROGRESS)
966 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200967 else if (ret == -ENOMEM)
968 goto wait_for_memory;
969 else if (ret != -EAGAIN) {
970 if (ret == -ENOSPC)
971 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +0530972 goto send_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +0200973 }
Dave Watson3c4d7552017-06-14 11:37:39 -0700974 }
975 }
976
977 continue;
978
979wait_for_sndbuf:
980 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
981wait_for_memory:
982 ret = sk_stream_wait_memory(sk, &timeo);
983 if (ret) {
984trim_sgl:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200985 tls_trim_both_msgs(sk, orig_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700986 goto send_end;
987 }
988
Daniel Borkmannd829e9c2018-10-13 02:45:59 +0200989 if (msg_en->sg.size < required_size)
Dave Watson3c4d7552017-06-14 11:37:39 -0700990 goto alloc_encrypted;
Dave Watson3c4d7552017-06-14 11:37:39 -0700991 }
992
Vakul Garga42055e2018-09-21 09:46:13 +0530993 if (!num_async) {
994 goto send_end;
995 } else if (num_zc) {
996 /* Wait for pending encryptions to get completed */
997 smp_store_mb(ctx->async_notify, true);
998
999 if (atomic_read(&ctx->encrypt_pending))
1000 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1001 else
1002 reinit_completion(&ctx->async_wait.completion);
1003
1004 WRITE_ONCE(ctx->async_notify, false);
1005
1006 if (ctx->async_wait.err) {
1007 ret = ctx->async_wait.err;
1008 copied = 0;
1009 }
1010 }
1011
1012 /* Transmit if any encryptions have completed */
1013 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1014 cancel_delayed_work(&ctx->tx_work.work);
1015 tls_tx_records(sk, msg->msg_flags);
1016 }
1017
Dave Watson3c4d7552017-06-14 11:37:39 -07001018send_end:
1019 ret = sk_stream_error(sk, msg->msg_flags, ret);
1020
1021 release_sock(sk);
1022 return copied ? copied : ret;
1023}
1024
John Fastabend0608c692018-12-20 11:35:35 -08001025int tls_sw_do_sendpage(struct sock *sk, struct page *page,
1026 int offset, size_t size, int flags)
Dave Watson3c4d7552017-06-14 11:37:39 -07001027{
Vakul Garga42055e2018-09-21 09:46:13 +05301028 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
Dave Watson3c4d7552017-06-14 11:37:39 -07001029 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001030 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -07001031 unsigned char record_type = TLS_RECORD_TYPE_DATA;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001032 struct sk_msg *msg_pl;
Vakul Garga42055e2018-09-21 09:46:13 +05301033 struct tls_rec *rec;
1034 int num_async = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001035 size_t copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001036 bool full_record;
1037 int record_room;
Vakul Garg4128c0c2018-09-24 16:09:49 +05301038 int ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301039 bool eor;
Dave Watson3c4d7552017-06-14 11:37:39 -07001040
Dave Watson3c4d7552017-06-14 11:37:39 -07001041 eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
Dave Watson3c4d7552017-06-14 11:37:39 -07001042 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
1043
Vakul Garga42055e2018-09-21 09:46:13 +05301044 /* Wait till there is any pending write on socket */
1045 if (unlikely(sk->sk_write_pending)) {
1046 ret = wait_on_pending_writer(sk, &timeo);
1047 if (unlikely(ret))
1048 goto sendpage_end;
1049 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001050
1051 /* Call the sk_stream functions to manage the sndbuf mem. */
1052 while (size > 0) {
1053 size_t copy, required_size;
1054
1055 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +01001056 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -07001057 goto sendpage_end;
1058 }
1059
John Fastabendd3b18ad32018-10-13 02:46:01 +02001060 if (ctx->open_rec)
1061 rec = ctx->open_rec;
1062 else
1063 rec = ctx->open_rec = tls_get_rec(sk);
Vakul Garga42055e2018-09-21 09:46:13 +05301064 if (!rec) {
1065 ret = -ENOMEM;
1066 goto sendpage_end;
1067 }
1068
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001069 msg_pl = &rec->msg_plaintext;
1070
Dave Watson3c4d7552017-06-14 11:37:39 -07001071 full_record = false;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001072 record_room = TLS_MAX_PAYLOAD_SIZE - msg_pl->sg.size;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001073 copied = 0;
Dave Watson3c4d7552017-06-14 11:37:39 -07001074 copy = size;
1075 if (copy >= record_room) {
1076 copy = record_room;
1077 full_record = true;
1078 }
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001079
1080 required_size = msg_pl->sg.size + copy +
1081 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001082
1083 if (!sk_stream_memory_free(sk))
1084 goto wait_for_sndbuf;
1085alloc_payload:
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001086 ret = tls_alloc_encrypted_msg(sk, required_size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001087 if (ret) {
1088 if (ret != -ENOSPC)
1089 goto wait_for_memory;
1090
1091 /* Adjust copy according to the amount that was
1092 * actually allocated. The difference is due
1093 * to max sg elements limit
1094 */
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001095 copy -= required_size - msg_pl->sg.size;
Dave Watson3c4d7552017-06-14 11:37:39 -07001096 full_record = true;
1097 }
1098
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001099 sk_msg_page_add(msg_pl, page, copy, offset);
Dave Watson3c4d7552017-06-14 11:37:39 -07001100 sk_mem_charge(sk, copy);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001101
Dave Watson3c4d7552017-06-14 11:37:39 -07001102 offset += copy;
1103 size -= copy;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001104 copied += copy;
Dave Watson3c4d7552017-06-14 11:37:39 -07001105
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001106 tls_ctx->pending_open_record_frags = true;
1107 if (full_record || eor || sk_msg_full(msg_pl)) {
Vakul Garg4e6d4722018-09-30 08:04:35 +05301108 rec->inplace_crypto = 0;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001109 ret = bpf_exec_tx_verdict(msg_pl, sk, full_record,
1110 record_type, &copied, flags);
Dave Watson3c4d7552017-06-14 11:37:39 -07001111 if (ret) {
Vakul Garga42055e2018-09-21 09:46:13 +05301112 if (ret == -EINPROGRESS)
1113 num_async++;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001114 else if (ret == -ENOMEM)
1115 goto wait_for_memory;
1116 else if (ret != -EAGAIN) {
1117 if (ret == -ENOSPC)
1118 ret = 0;
Vakul Garga42055e2018-09-21 09:46:13 +05301119 goto sendpage_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001120 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001121 }
1122 }
1123 continue;
1124wait_for_sndbuf:
1125 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1126wait_for_memory:
1127 ret = sk_stream_wait_memory(sk, &timeo);
1128 if (ret) {
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001129 tls_trim_both_msgs(sk, msg_pl->sg.size);
Dave Watson3c4d7552017-06-14 11:37:39 -07001130 goto sendpage_end;
1131 }
1132
Dave Watson3c4d7552017-06-14 11:37:39 -07001133 goto alloc_payload;
1134 }
1135
Vakul Garga42055e2018-09-21 09:46:13 +05301136 if (num_async) {
1137 /* Transmit if any encryptions have completed */
1138 if (test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask)) {
1139 cancel_delayed_work(&ctx->tx_work.work);
1140 tls_tx_records(sk, flags);
1141 }
1142 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001143sendpage_end:
John Fastabendd3b18ad32018-10-13 02:46:01 +02001144 ret = sk_stream_error(sk, flags, ret);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001145 return copied ? copied : ret;
Dave Watson3c4d7552017-06-14 11:37:39 -07001146}
1147
John Fastabend0608c692018-12-20 11:35:35 -08001148int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
1149 int offset, size_t size, int flags)
1150{
1151 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1152 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1153 return -ENOTSUPP;
1154
1155 return tls_sw_do_sendpage(sk, page, offset, size, flags);
1156}
1157
1158int tls_sw_sendpage(struct sock *sk, struct page *page,
1159 int offset, size_t size, int flags)
1160{
1161 int ret;
1162
1163 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
1164 MSG_SENDPAGE_NOTLAST | MSG_SENDPAGE_NOPOLICY))
1165 return -ENOTSUPP;
1166
1167 lock_sock(sk);
1168 ret = tls_sw_do_sendpage(sk, page, offset, size, flags);
1169 release_sock(sk);
1170 return ret;
1171}
1172
John Fastabendd3b18ad32018-10-13 02:46:01 +02001173static struct sk_buff *tls_wait_data(struct sock *sk, struct sk_psock *psock,
1174 int flags, long timeo, int *err)
Dave Watsonc46234e2018-03-22 10:10:35 -07001175{
1176 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001177 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001178 struct sk_buff *skb;
1179 DEFINE_WAIT_FUNC(wait, woken_wake_function);
1180
John Fastabendd3b18ad32018-10-13 02:46:01 +02001181 while (!(skb = ctx->recv_pkt) && sk_psock_queue_empty(psock)) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001182 if (sk->sk_err) {
1183 *err = sock_error(sk);
1184 return NULL;
1185 }
1186
Doron Roberts-Kedesfcf47932018-07-18 16:22:27 -07001187 if (sk->sk_shutdown & RCV_SHUTDOWN)
1188 return NULL;
1189
Dave Watsonc46234e2018-03-22 10:10:35 -07001190 if (sock_flag(sk, SOCK_DONE))
1191 return NULL;
1192
1193 if ((flags & MSG_DONTWAIT) || !timeo) {
1194 *err = -EAGAIN;
1195 return NULL;
1196 }
1197
1198 add_wait_queue(sk_sleep(sk), &wait);
1199 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001200 sk_wait_event(sk, &timeo,
1201 ctx->recv_pkt != skb ||
1202 !sk_psock_queue_empty(psock),
1203 &wait);
Dave Watsonc46234e2018-03-22 10:10:35 -07001204 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
1205 remove_wait_queue(sk_sleep(sk), &wait);
1206
1207 /* Handle signals */
1208 if (signal_pending(current)) {
1209 *err = sock_intr_errno(timeo);
1210 return NULL;
1211 }
1212 }
1213
1214 return skb;
1215}
1216
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001217static int tls_setup_from_iter(struct sock *sk, struct iov_iter *from,
1218 int length, int *pages_used,
1219 unsigned int *size_used,
1220 struct scatterlist *to,
1221 int to_max_pages)
1222{
1223 int rc = 0, i = 0, num_elem = *pages_used, maxpages;
1224 struct page *pages[MAX_SKB_FRAGS];
1225 unsigned int size = *size_used;
1226 ssize_t copied, use;
1227 size_t offset;
1228
1229 while (length > 0) {
1230 i = 0;
1231 maxpages = to_max_pages - num_elem;
1232 if (maxpages == 0) {
1233 rc = -EFAULT;
1234 goto out;
1235 }
1236 copied = iov_iter_get_pages(from, pages,
1237 length,
1238 maxpages, &offset);
1239 if (copied <= 0) {
1240 rc = -EFAULT;
1241 goto out;
1242 }
1243
1244 iov_iter_advance(from, copied);
1245
1246 length -= copied;
1247 size += copied;
1248 while (copied) {
1249 use = min_t(int, copied, PAGE_SIZE - offset);
1250
1251 sg_set_page(&to[num_elem],
1252 pages[i], use, offset);
1253 sg_unmark_end(&to[num_elem]);
1254 /* We do not uncharge memory from this API */
1255
1256 offset = 0;
1257 copied -= use;
1258
1259 i++;
1260 num_elem++;
1261 }
1262 }
1263 /* Mark the end in the last sg entry if newly added */
1264 if (num_elem > *pages_used)
1265 sg_mark_end(&to[num_elem - 1]);
1266out:
1267 if (rc)
1268 iov_iter_revert(from, size - *size_used);
1269 *size_used = size;
1270 *pages_used = num_elem;
1271
1272 return rc;
1273}
1274
Vakul Garg0b243d02018-08-10 20:46:41 +05301275/* This function decrypts the input skb into either out_iov or in out_sg
1276 * or in skb buffers itself. The input parameter 'zc' indicates if
1277 * zero-copy mode needs to be tried or not. With zero-copy mode, either
1278 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
1279 * NULL, then the decryption happens inside skb buffers itself, i.e.
1280 * zero-copy gets disabled and 'zc' is updated.
1281 */
1282
1283static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
1284 struct iov_iter *out_iov,
1285 struct scatterlist *out_sg,
1286 int *chunk, bool *zc)
1287{
1288 struct tls_context *tls_ctx = tls_get_ctx(sk);
1289 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1290 struct strp_msg *rxm = strp_msg(skb);
1291 int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0;
1292 struct aead_request *aead_req;
1293 struct sk_buff *unused;
1294 u8 *aad, *iv, *mem = NULL;
1295 struct scatterlist *sgin = NULL;
1296 struct scatterlist *sgout = NULL;
1297 const int data_len = rxm->full_len - tls_ctx->rx.overhead_size;
1298
1299 if (*zc && (out_iov || out_sg)) {
1300 if (out_iov)
1301 n_sgout = iov_iter_npages(out_iov, INT_MAX) + 1;
1302 else
1303 n_sgout = sg_nents(out_sg);
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -07001304 n_sgin = skb_nsg(skb, rxm->offset + tls_ctx->rx.prepend_size,
1305 rxm->full_len - tls_ctx->rx.prepend_size);
Vakul Garg0b243d02018-08-10 20:46:41 +05301306 } else {
1307 n_sgout = 0;
1308 *zc = false;
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -07001309 n_sgin = skb_cow_data(skb, 0, &unused);
Vakul Garg0b243d02018-08-10 20:46:41 +05301310 }
1311
Vakul Garg0b243d02018-08-10 20:46:41 +05301312 if (n_sgin < 1)
1313 return -EBADMSG;
1314
1315 /* Increment to accommodate AAD */
1316 n_sgin = n_sgin + 1;
1317
1318 nsg = n_sgin + n_sgout;
1319
1320 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
1321 mem_size = aead_size + (nsg * sizeof(struct scatterlist));
1322 mem_size = mem_size + TLS_AAD_SPACE_SIZE;
1323 mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv);
1324
1325 /* Allocate a single block of memory which contains
1326 * aead_req || sgin[] || sgout[] || aad || iv.
1327 * This order achieves correct alignment for aead_req, sgin, sgout.
1328 */
1329 mem = kmalloc(mem_size, sk->sk_allocation);
1330 if (!mem)
1331 return -ENOMEM;
1332
1333 /* Segment the allocated memory */
1334 aead_req = (struct aead_request *)mem;
1335 sgin = (struct scatterlist *)(mem + aead_size);
1336 sgout = sgin + n_sgin;
1337 aad = (u8 *)(sgout + n_sgout);
1338 iv = aad + TLS_AAD_SPACE_SIZE;
1339
1340 /* Prepare IV */
1341 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
1342 iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1343 tls_ctx->rx.iv_size);
1344 if (err < 0) {
1345 kfree(mem);
1346 return err;
1347 }
1348 memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
1349
1350 /* Prepare AAD */
1351 tls_make_aad(aad, rxm->full_len - tls_ctx->rx.overhead_size,
1352 tls_ctx->rx.rec_seq, tls_ctx->rx.rec_seq_size,
1353 ctx->control);
1354
1355 /* Prepare sgin */
1356 sg_init_table(sgin, n_sgin);
1357 sg_set_buf(&sgin[0], aad, TLS_AAD_SPACE_SIZE);
1358 err = skb_to_sgvec(skb, &sgin[1],
1359 rxm->offset + tls_ctx->rx.prepend_size,
1360 rxm->full_len - tls_ctx->rx.prepend_size);
1361 if (err < 0) {
1362 kfree(mem);
1363 return err;
1364 }
1365
1366 if (n_sgout) {
1367 if (out_iov) {
1368 sg_init_table(sgout, n_sgout);
1369 sg_set_buf(&sgout[0], aad, TLS_AAD_SPACE_SIZE);
1370
1371 *chunk = 0;
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001372 err = tls_setup_from_iter(sk, out_iov, data_len,
1373 &pages, chunk, &sgout[1],
1374 (n_sgout - 1));
Vakul Garg0b243d02018-08-10 20:46:41 +05301375 if (err < 0)
1376 goto fallback_to_reg_recv;
1377 } else if (out_sg) {
1378 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
1379 } else {
1380 goto fallback_to_reg_recv;
1381 }
1382 } else {
1383fallback_to_reg_recv:
1384 sgout = sgin;
1385 pages = 0;
1386 *chunk = 0;
1387 *zc = false;
1388 }
1389
1390 /* Prepare and submit AEAD request */
Vakul Garg94524d82018-08-29 15:26:55 +05301391 err = tls_do_decryption(sk, skb, sgin, sgout, iv,
1392 data_len, aead_req, *zc);
1393 if (err == -EINPROGRESS)
1394 return err;
Vakul Garg0b243d02018-08-10 20:46:41 +05301395
1396 /* Release the pages in case iov was mapped to pages */
1397 for (; pages > 0; pages--)
1398 put_page(sg_page(&sgout[pages]));
1399
1400 kfree(mem);
1401 return err;
1402}
1403
Boris Pismennydafb67f2018-07-13 14:33:40 +03001404static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
Vakul Garg0b243d02018-08-10 20:46:41 +05301405 struct iov_iter *dest, int *chunk, bool *zc)
Boris Pismennydafb67f2018-07-13 14:33:40 +03001406{
1407 struct tls_context *tls_ctx = tls_get_ctx(sk);
1408 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1409 struct strp_msg *rxm = strp_msg(skb);
1410 int err = 0;
1411
Boris Pismenny4799ac82018-07-13 14:33:43 +03001412#ifdef CONFIG_TLS_DEVICE
1413 err = tls_device_decrypted(sk, skb);
Boris Pismennydafb67f2018-07-13 14:33:40 +03001414 if (err < 0)
1415 return err;
Boris Pismenny4799ac82018-07-13 14:33:43 +03001416#endif
1417 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301418 err = decrypt_internal(sk, skb, dest, NULL, chunk, zc);
Vakul Garg94524d82018-08-29 15:26:55 +05301419 if (err < 0) {
1420 if (err == -EINPROGRESS)
1421 tls_advance_record_sn(sk, &tls_ctx->rx);
1422
Boris Pismenny4799ac82018-07-13 14:33:43 +03001423 return err;
Vakul Garg94524d82018-08-29 15:26:55 +05301424 }
Boris Pismenny4799ac82018-07-13 14:33:43 +03001425 } else {
1426 *zc = false;
1427 }
Boris Pismennydafb67f2018-07-13 14:33:40 +03001428
1429 rxm->offset += tls_ctx->rx.prepend_size;
1430 rxm->full_len -= tls_ctx->rx.overhead_size;
1431 tls_advance_record_sn(sk, &tls_ctx->rx);
1432 ctx->decrypted = true;
1433 ctx->saved_data_ready(sk);
1434
1435 return err;
1436}
1437
1438int decrypt_skb(struct sock *sk, struct sk_buff *skb,
1439 struct scatterlist *sgout)
Dave Watsonc46234e2018-03-22 10:10:35 -07001440{
Vakul Garg0b243d02018-08-10 20:46:41 +05301441 bool zc = true;
1442 int chunk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001443
Vakul Garg0b243d02018-08-10 20:46:41 +05301444 return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -07001445}
1446
1447static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb,
1448 unsigned int len)
1449{
1450 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001451 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001452
Vakul Garg94524d82018-08-29 15:26:55 +05301453 if (skb) {
1454 struct strp_msg *rxm = strp_msg(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001455
Vakul Garg94524d82018-08-29 15:26:55 +05301456 if (len < rxm->full_len) {
1457 rxm->offset += len;
1458 rxm->full_len -= len;
1459 return false;
1460 }
1461 kfree_skb(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001462 }
1463
1464 /* Finished with message */
1465 ctx->recv_pkt = NULL;
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -07001466 __strp_unpause(&ctx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07001467
1468 return true;
1469}
1470
1471int tls_sw_recvmsg(struct sock *sk,
1472 struct msghdr *msg,
1473 size_t len,
1474 int nonblock,
1475 int flags,
1476 int *addr_len)
1477{
1478 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001479 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001480 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001481 unsigned char control;
1482 struct strp_msg *rxm;
1483 struct sk_buff *skb;
1484 ssize_t copied = 0;
1485 bool cmsg = false;
Daniel Borkmann06030db2018-06-15 03:07:46 +02001486 int target, err = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001487 long timeo;
David Howells00e23702018-10-22 13:07:28 +01001488 bool is_kvec = iov_iter_is_kvec(&msg->msg_iter);
Vakul Garg94524d82018-08-29 15:26:55 +05301489 int num_async = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001490
1491 flags |= nonblock;
1492
1493 if (unlikely(flags & MSG_ERRQUEUE))
1494 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1495
John Fastabendd3b18ad32018-10-13 02:46:01 +02001496 psock = sk_psock_get(sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001497 lock_sock(sk);
1498
Daniel Borkmann06030db2018-06-15 03:07:46 +02001499 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
Dave Watsonc46234e2018-03-22 10:10:35 -07001500 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1501 do {
1502 bool zc = false;
Vakul Garg94524d82018-08-29 15:26:55 +05301503 bool async = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001504 int chunk = 0;
1505
John Fastabendd3b18ad32018-10-13 02:46:01 +02001506 skb = tls_wait_data(sk, psock, flags, timeo, &err);
1507 if (!skb) {
1508 if (psock) {
John Fastabend02c558b2018-10-16 11:08:04 -07001509 int ret = __tcp_bpf_recvmsg(sk, psock,
1510 msg, len, flags);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001511
1512 if (ret > 0) {
1513 copied += ret;
1514 len -= ret;
1515 continue;
1516 }
1517 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001518 goto recv_end;
John Fastabendd3b18ad32018-10-13 02:46:01 +02001519 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001520
1521 rxm = strp_msg(skb);
Vakul Garg94524d82018-08-29 15:26:55 +05301522
Dave Watsonc46234e2018-03-22 10:10:35 -07001523 if (!cmsg) {
1524 int cerr;
1525
1526 cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1527 sizeof(ctx->control), &ctx->control);
1528 cmsg = true;
1529 control = ctx->control;
1530 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1531 if (cerr || msg->msg_flags & MSG_CTRUNC) {
1532 err = -EIO;
1533 goto recv_end;
1534 }
1535 }
1536 } else if (control != ctx->control) {
1537 goto recv_end;
1538 }
1539
1540 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301541 int to_copy = rxm->full_len - tls_ctx->rx.overhead_size;
Dave Watsonc46234e2018-03-22 10:10:35 -07001542
Vakul Garg0b243d02018-08-10 20:46:41 +05301543 if (!is_kvec && to_copy <= len &&
1544 likely(!(flags & MSG_PEEK)))
Dave Watsonc46234e2018-03-22 10:10:35 -07001545 zc = true;
Dave Watsonc46234e2018-03-22 10:10:35 -07001546
Vakul Garg0b243d02018-08-10 20:46:41 +05301547 err = decrypt_skb_update(sk, skb, &msg->msg_iter,
1548 &chunk, &zc);
Vakul Garg94524d82018-08-29 15:26:55 +05301549 if (err < 0 && err != -EINPROGRESS) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301550 tls_err_abort(sk, EBADMSG);
1551 goto recv_end;
Dave Watsonc46234e2018-03-22 10:10:35 -07001552 }
Vakul Garg94524d82018-08-29 15:26:55 +05301553
1554 if (err == -EINPROGRESS) {
1555 async = true;
1556 num_async++;
1557 goto pick_next_record;
1558 }
1559
Dave Watsonc46234e2018-03-22 10:10:35 -07001560 ctx->decrypted = true;
1561 }
1562
1563 if (!zc) {
1564 chunk = min_t(unsigned int, rxm->full_len, len);
Vakul Garg94524d82018-08-29 15:26:55 +05301565
Dave Watsonc46234e2018-03-22 10:10:35 -07001566 err = skb_copy_datagram_msg(skb, rxm->offset, msg,
1567 chunk);
1568 if (err < 0)
1569 goto recv_end;
1570 }
1571
Vakul Garg94524d82018-08-29 15:26:55 +05301572pick_next_record:
Dave Watsonc46234e2018-03-22 10:10:35 -07001573 copied += chunk;
1574 len -= chunk;
1575 if (likely(!(flags & MSG_PEEK))) {
1576 u8 control = ctx->control;
1577
Vakul Garg94524d82018-08-29 15:26:55 +05301578 /* For async, drop current skb reference */
1579 if (async)
1580 skb = NULL;
1581
Dave Watsonc46234e2018-03-22 10:10:35 -07001582 if (tls_sw_advance_skb(sk, skb, chunk)) {
1583 /* Return full control message to
1584 * userspace before trying to parse
1585 * another message type
1586 */
1587 msg->msg_flags |= MSG_EOR;
1588 if (control != TLS_RECORD_TYPE_DATA)
1589 goto recv_end;
Vakul Garg94524d82018-08-29 15:26:55 +05301590 } else {
1591 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001592 }
Daniel Borkmann50c6b582018-09-14 23:00:55 +02001593 } else {
1594 /* MSG_PEEK right now cannot look beyond current skb
1595 * from strparser, meaning we cannot advance skb here
1596 * and thus unpause strparser since we'd loose original
1597 * one.
1598 */
1599 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001600 }
Vakul Garg94524d82018-08-29 15:26:55 +05301601
Daniel Borkmann06030db2018-06-15 03:07:46 +02001602 /* If we have a new message from strparser, continue now. */
1603 if (copied >= target && !ctx->recv_pkt)
1604 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001605 } while (len);
1606
1607recv_end:
Vakul Garg94524d82018-08-29 15:26:55 +05301608 if (num_async) {
1609 /* Wait for all previously submitted records to be decrypted */
1610 smp_store_mb(ctx->async_notify, true);
1611 if (atomic_read(&ctx->decrypt_pending)) {
1612 err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1613 if (err) {
1614 /* one of async decrypt failed */
1615 tls_err_abort(sk, err);
1616 copied = 0;
1617 }
1618 } else {
1619 reinit_completion(&ctx->async_wait.completion);
1620 }
1621 WRITE_ONCE(ctx->async_notify, false);
1622 }
1623
Dave Watsonc46234e2018-03-22 10:10:35 -07001624 release_sock(sk);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001625 if (psock)
1626 sk_psock_put(sk, psock);
Dave Watsonc46234e2018-03-22 10:10:35 -07001627 return copied ? : err;
1628}
1629
1630ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
1631 struct pipe_inode_info *pipe,
1632 size_t len, unsigned int flags)
1633{
1634 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001635 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001636 struct strp_msg *rxm = NULL;
1637 struct sock *sk = sock->sk;
1638 struct sk_buff *skb;
1639 ssize_t copied = 0;
1640 int err = 0;
1641 long timeo;
1642 int chunk;
Vakul Garg0b243d02018-08-10 20:46:41 +05301643 bool zc = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001644
1645 lock_sock(sk);
1646
1647 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1648
John Fastabendd3b18ad32018-10-13 02:46:01 +02001649 skb = tls_wait_data(sk, NULL, flags, timeo, &err);
Dave Watsonc46234e2018-03-22 10:10:35 -07001650 if (!skb)
1651 goto splice_read_end;
1652
1653 /* splice does not support reading control messages */
1654 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1655 err = -ENOTSUPP;
1656 goto splice_read_end;
1657 }
1658
1659 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301660 err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -07001661
1662 if (err < 0) {
1663 tls_err_abort(sk, EBADMSG);
1664 goto splice_read_end;
1665 }
1666 ctx->decrypted = true;
1667 }
1668 rxm = strp_msg(skb);
1669
1670 chunk = min_t(unsigned int, rxm->full_len, len);
1671 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
1672 if (copied < 0)
1673 goto splice_read_end;
1674
1675 if (likely(!(flags & MSG_PEEK)))
1676 tls_sw_advance_skb(sk, skb, copied);
1677
1678splice_read_end:
1679 release_sock(sk);
1680 return copied ? : err;
1681}
1682
John Fastabend924ad652018-10-13 02:46:00 +02001683bool tls_sw_stream_read(const struct sock *sk)
Dave Watsonc46234e2018-03-22 10:10:35 -07001684{
Dave Watsonc46234e2018-03-22 10:10:35 -07001685 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001686 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001687 bool ingress_empty = true;
1688 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001689
John Fastabendd3b18ad32018-10-13 02:46:01 +02001690 rcu_read_lock();
1691 psock = sk_psock(sk);
1692 if (psock)
1693 ingress_empty = list_empty(&psock->ingress_msg);
1694 rcu_read_unlock();
Dave Watsonc46234e2018-03-22 10:10:35 -07001695
John Fastabendd3b18ad32018-10-13 02:46:01 +02001696 return !ingress_empty || ctx->recv_pkt;
Dave Watsonc46234e2018-03-22 10:10:35 -07001697}
1698
1699static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1700{
1701 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001702 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Kees Cook3463e512018-06-25 16:55:05 -07001703 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
Dave Watsonc46234e2018-03-22 10:10:35 -07001704 struct strp_msg *rxm = strp_msg(skb);
1705 size_t cipher_overhead;
1706 size_t data_len = 0;
1707 int ret;
1708
1709 /* Verify that we have a full TLS header, or wait for more data */
1710 if (rxm->offset + tls_ctx->rx.prepend_size > skb->len)
1711 return 0;
1712
Kees Cook3463e512018-06-25 16:55:05 -07001713 /* Sanity-check size of on-stack buffer. */
1714 if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) {
1715 ret = -EINVAL;
1716 goto read_failure;
1717 }
1718
Dave Watsonc46234e2018-03-22 10:10:35 -07001719 /* Linearize header to local buffer */
1720 ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size);
1721
1722 if (ret < 0)
1723 goto read_failure;
1724
1725 ctx->control = header[0];
1726
1727 data_len = ((header[4] & 0xFF) | (header[3] << 8));
1728
1729 cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size;
1730
1731 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) {
1732 ret = -EMSGSIZE;
1733 goto read_failure;
1734 }
1735 if (data_len < cipher_overhead) {
1736 ret = -EBADMSG;
1737 goto read_failure;
1738 }
1739
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001740 if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.info.version) ||
1741 header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.info.version)) {
Dave Watsonc46234e2018-03-22 10:10:35 -07001742 ret = -EINVAL;
1743 goto read_failure;
1744 }
1745
Boris Pismenny4799ac82018-07-13 14:33:43 +03001746#ifdef CONFIG_TLS_DEVICE
1747 handle_device_resync(strp->sk, TCP_SKB_CB(skb)->seq + rxm->offset,
1748 *(u64*)tls_ctx->rx.rec_seq);
1749#endif
Dave Watsonc46234e2018-03-22 10:10:35 -07001750 return data_len + TLS_HEADER_SIZE;
1751
1752read_failure:
1753 tls_err_abort(strp->sk, ret);
1754
1755 return ret;
1756}
1757
1758static void tls_queue(struct strparser *strp, struct sk_buff *skb)
1759{
1760 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001761 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001762
1763 ctx->decrypted = false;
1764
1765 ctx->recv_pkt = skb;
1766 strp_pause(strp);
1767
Vakul Gargad13acc2018-07-30 16:08:33 +05301768 ctx->saved_data_ready(strp->sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001769}
1770
1771static void tls_data_ready(struct sock *sk)
1772{
1773 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001774 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001775 struct sk_psock *psock;
Dave Watsonc46234e2018-03-22 10:10:35 -07001776
1777 strp_data_ready(&ctx->strp);
John Fastabendd3b18ad32018-10-13 02:46:01 +02001778
1779 psock = sk_psock_get(sk);
1780 if (psock && !list_empty(&psock->ingress_msg)) {
1781 ctx->saved_data_ready(sk);
1782 sk_psock_put(sk, psock);
1783 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001784}
1785
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001786void tls_sw_free_resources_tx(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -07001787{
1788 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001789 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Vakul Garga42055e2018-09-21 09:46:13 +05301790 struct tls_rec *rec, *tmp;
1791
1792 /* Wait for any pending async encryptions to complete */
1793 smp_store_mb(ctx->async_notify, true);
1794 if (atomic_read(&ctx->encrypt_pending))
1795 crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1796
1797 cancel_delayed_work_sync(&ctx->tx_work.work);
1798
1799 /* Tx whatever records we can transmit and abandon the rest */
1800 tls_tx_records(sk, -1);
1801
Vakul Garg9932a292018-09-24 15:35:56 +05301802 /* Free up un-sent records in tx_list. First, free
Vakul Garga42055e2018-09-21 09:46:13 +05301803 * the partially sent record if any at head of tx_list.
1804 */
1805 if (tls_ctx->partially_sent_record) {
1806 struct scatterlist *sg = tls_ctx->partially_sent_record;
1807
1808 while (1) {
1809 put_page(sg_page(sg));
1810 sk_mem_uncharge(sk, sg->length);
1811
1812 if (sg_is_last(sg))
1813 break;
1814 sg++;
1815 }
1816
1817 tls_ctx->partially_sent_record = NULL;
1818
Vakul Garg9932a292018-09-24 15:35:56 +05301819 rec = list_first_entry(&ctx->tx_list,
Vakul Garga42055e2018-09-21 09:46:13 +05301820 struct tls_rec, list);
1821 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001822 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301823 kfree(rec);
1824 }
1825
Vakul Garg9932a292018-09-24 15:35:56 +05301826 list_for_each_entry_safe(rec, tmp, &ctx->tx_list, list) {
Vakul Garga42055e2018-09-21 09:46:13 +05301827 list_del(&rec->list);
Daniel Borkmannd829e9c2018-10-13 02:45:59 +02001828 sk_msg_free(sk, &rec->msg_encrypted);
1829 sk_msg_free(sk, &rec->msg_plaintext);
Vakul Garga42055e2018-09-21 09:46:13 +05301830 kfree(rec);
1831 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001832
Vakul Garg201876b2018-07-24 16:54:27 +05301833 crypto_free_aead(ctx->aead_send);
Vakul Gargc7749732018-09-25 20:21:51 +05301834 tls_free_open_rec(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001835
1836 kfree(ctx);
1837}
1838
Boris Pismenny39f56e12018-07-13 14:33:41 +03001839void tls_sw_release_resources_rx(struct sock *sk)
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001840{
1841 struct tls_context *tls_ctx = tls_get_ctx(sk);
1842 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1843
Dave Watsonc46234e2018-03-22 10:10:35 -07001844 if (ctx->aead_recv) {
Vakul Garg201876b2018-07-24 16:54:27 +05301845 kfree_skb(ctx->recv_pkt);
1846 ctx->recv_pkt = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001847 crypto_free_aead(ctx->aead_recv);
1848 strp_stop(&ctx->strp);
1849 write_lock_bh(&sk->sk_callback_lock);
1850 sk->sk_data_ready = ctx->saved_data_ready;
1851 write_unlock_bh(&sk->sk_callback_lock);
1852 release_sock(sk);
1853 strp_done(&ctx->strp);
1854 lock_sock(sk);
1855 }
Boris Pismenny39f56e12018-07-13 14:33:41 +03001856}
1857
1858void tls_sw_free_resources_rx(struct sock *sk)
1859{
1860 struct tls_context *tls_ctx = tls_get_ctx(sk);
1861 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1862
1863 tls_sw_release_resources_rx(sk);
Dave Watson3c4d7552017-06-14 11:37:39 -07001864
Dave Watson3c4d7552017-06-14 11:37:39 -07001865 kfree(ctx);
1866}
1867
Vakul Garg9932a292018-09-24 15:35:56 +05301868/* The work handler to transmitt the encrypted records in tx_list */
Vakul Garga42055e2018-09-21 09:46:13 +05301869static void tx_work_handler(struct work_struct *work)
1870{
1871 struct delayed_work *delayed_work = to_delayed_work(work);
1872 struct tx_work *tx_work = container_of(delayed_work,
1873 struct tx_work, work);
1874 struct sock *sk = tx_work->sk;
1875 struct tls_context *tls_ctx = tls_get_ctx(sk);
1876 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
1877
1878 if (!test_and_clear_bit(BIT_TX_SCHEDULED, &ctx->tx_bitmask))
1879 return;
1880
1881 lock_sock(sk);
1882 tls_tx_records(sk, -1);
1883 release_sock(sk);
1884}
1885
Dave Watsonc46234e2018-03-22 10:10:35 -07001886int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
Dave Watson3c4d7552017-06-14 11:37:39 -07001887{
Dave Watson3c4d7552017-06-14 11:37:39 -07001888 struct tls_crypto_info *crypto_info;
1889 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001890 struct tls_sw_context_tx *sw_ctx_tx = NULL;
1891 struct tls_sw_context_rx *sw_ctx_rx = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001892 struct cipher_context *cctx;
1893 struct crypto_aead **aead;
1894 struct strp_callbacks cb;
Dave Watson3c4d7552017-06-14 11:37:39 -07001895 u16 nonce_size, tag_size, iv_size, rec_seq_size;
1896 char *iv, *rec_seq;
1897 int rc = 0;
1898
1899 if (!ctx) {
1900 rc = -EINVAL;
1901 goto out;
1902 }
1903
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001904 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001905 if (!ctx->priv_ctx_tx) {
1906 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
1907 if (!sw_ctx_tx) {
1908 rc = -ENOMEM;
1909 goto out;
1910 }
1911 ctx->priv_ctx_tx = sw_ctx_tx;
1912 } else {
1913 sw_ctx_tx =
1914 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
Dave Watsonc46234e2018-03-22 10:10:35 -07001915 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001916 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001917 if (!ctx->priv_ctx_rx) {
1918 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
1919 if (!sw_ctx_rx) {
1920 rc = -ENOMEM;
1921 goto out;
1922 }
1923 ctx->priv_ctx_rx = sw_ctx_rx;
1924 } else {
1925 sw_ctx_rx =
1926 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001927 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001928 }
1929
Dave Watsonc46234e2018-03-22 10:10:35 -07001930 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001931 crypto_init_wait(&sw_ctx_tx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001932 crypto_info = &ctx->crypto_send.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07001933 cctx = &ctx->tx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001934 aead = &sw_ctx_tx->aead_send;
Vakul Garg9932a292018-09-24 15:35:56 +05301935 INIT_LIST_HEAD(&sw_ctx_tx->tx_list);
Vakul Garga42055e2018-09-21 09:46:13 +05301936 INIT_DELAYED_WORK(&sw_ctx_tx->tx_work.work, tx_work_handler);
1937 sw_ctx_tx->tx_work.sk = sk;
Dave Watsonc46234e2018-03-22 10:10:35 -07001938 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001939 crypto_init_wait(&sw_ctx_rx->async_wait);
Sabrina Dubroca86029d12018-09-12 17:44:42 +02001940 crypto_info = &ctx->crypto_recv.info;
Dave Watsonc46234e2018-03-22 10:10:35 -07001941 cctx = &ctx->rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001942 aead = &sw_ctx_rx->aead_recv;
Dave Watsonc46234e2018-03-22 10:10:35 -07001943 }
1944
Dave Watson3c4d7552017-06-14 11:37:39 -07001945 switch (crypto_info->cipher_type) {
1946 case TLS_CIPHER_AES_GCM_128: {
1947 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1948 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
1949 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1950 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1951 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
1952 rec_seq =
1953 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1954 gcm_128_info =
1955 (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
1956 break;
1957 }
1958 default:
1959 rc = -EINVAL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001960 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001961 }
1962
Kees Cookb16520f2018-04-10 17:52:34 -07001963 /* Sanity-check the IV size for stack allocations. */
Kees Cook3463e512018-06-25 16:55:05 -07001964 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) {
Kees Cookb16520f2018-04-10 17:52:34 -07001965 rc = -EINVAL;
1966 goto free_priv;
1967 }
1968
Dave Watsonc46234e2018-03-22 10:10:35 -07001969 cctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
1970 cctx->tag_size = tag_size;
1971 cctx->overhead_size = cctx->prepend_size + cctx->tag_size;
1972 cctx->iv_size = iv_size;
1973 cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1974 GFP_KERNEL);
1975 if (!cctx->iv) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001976 rc = -ENOMEM;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001977 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001978 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001979 memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
1980 memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
1981 cctx->rec_seq_size = rec_seq_size;
zhong jiang969d5092018-08-01 00:50:24 +08001982 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
Dave Watsonc46234e2018-03-22 10:10:35 -07001983 if (!cctx->rec_seq) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001984 rc = -ENOMEM;
1985 goto free_iv;
1986 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001987
Dave Watsonc46234e2018-03-22 10:10:35 -07001988 if (!*aead) {
1989 *aead = crypto_alloc_aead("gcm(aes)", 0, 0);
1990 if (IS_ERR(*aead)) {
1991 rc = PTR_ERR(*aead);
1992 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07001993 goto free_rec_seq;
1994 }
1995 }
1996
1997 ctx->push_pending_record = tls_sw_push_pending_record;
1998
Sabrina Dubroca7cba09c2018-09-12 17:44:41 +02001999 rc = crypto_aead_setkey(*aead, gcm_128_info->key,
Dave Watson3c4d7552017-06-14 11:37:39 -07002000 TLS_CIPHER_AES_GCM_128_KEY_SIZE);
2001 if (rc)
2002 goto free_aead;
2003
Dave Watsonc46234e2018-03-22 10:10:35 -07002004 rc = crypto_aead_setauthsize(*aead, cctx->tag_size);
2005 if (rc)
2006 goto free_aead;
2007
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002008 if (sw_ctx_rx) {
Dave Watsonc46234e2018-03-22 10:10:35 -07002009 /* Set up strparser */
2010 memset(&cb, 0, sizeof(cb));
2011 cb.rcv_msg = tls_queue;
2012 cb.parse_msg = tls_read_size;
2013
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002014 strp_init(&sw_ctx_rx->strp, sk, &cb);
Dave Watsonc46234e2018-03-22 10:10:35 -07002015
2016 write_lock_bh(&sk->sk_callback_lock);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002017 sw_ctx_rx->saved_data_ready = sk->sk_data_ready;
Dave Watsonc46234e2018-03-22 10:10:35 -07002018 sk->sk_data_ready = tls_data_ready;
2019 write_unlock_bh(&sk->sk_callback_lock);
2020
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002021 strp_check_rcv(&sw_ctx_rx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07002022 }
2023
2024 goto out;
Dave Watson3c4d7552017-06-14 11:37:39 -07002025
2026free_aead:
Dave Watsonc46234e2018-03-22 10:10:35 -07002027 crypto_free_aead(*aead);
2028 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002029free_rec_seq:
Dave Watsonc46234e2018-03-22 10:10:35 -07002030 kfree(cctx->rec_seq);
2031 cctx->rec_seq = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07002032free_iv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002033 kfree(cctx->iv);
2034 cctx->iv = NULL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01002035free_priv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03002036 if (tx) {
2037 kfree(ctx->priv_ctx_tx);
2038 ctx->priv_ctx_tx = NULL;
2039 } else {
2040 kfree(ctx->priv_ctx_rx);
2041 ctx->priv_ctx_rx = NULL;
2042 }
Dave Watson3c4d7552017-06-14 11:37:39 -07002043out:
2044 return rc;
2045}