blob: be4f2e990f9ffe33639c0d5aae1698b3ba034055 [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.
7 *
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
13 *
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
16 * conditions are met:
17 *
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer.
21 *
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 * SOFTWARE.
35 */
36
Dave Watsonc46234e2018-03-22 10:10:35 -070037#include <linux/sched/signal.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070038#include <linux/module.h>
39#include <crypto/aead.h>
40
Dave Watsonc46234e2018-03-22 10:10:35 -070041#include <net/strparser.h>
Dave Watson3c4d7552017-06-14 11:37:39 -070042#include <net/tls.h>
43
Kees Cookb16520f2018-04-10 17:52:34 -070044#define MAX_IV_SIZE TLS_CIPHER_AES_GCM_128_IV_SIZE
45
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -070046static int __skb_nsg(struct sk_buff *skb, int offset, int len,
47 unsigned int recursion_level)
48{
49 int start = skb_headlen(skb);
50 int i, chunk = start - offset;
51 struct sk_buff *frag_iter;
52 int elt = 0;
53
54 if (unlikely(recursion_level >= 24))
55 return -EMSGSIZE;
56
57 if (chunk > 0) {
58 if (chunk > len)
59 chunk = len;
60 elt++;
61 len -= chunk;
62 if (len == 0)
63 return elt;
64 offset += chunk;
65 }
66
67 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
68 int end;
69
70 WARN_ON(start > offset + len);
71
72 end = start + skb_frag_size(&skb_shinfo(skb)->frags[i]);
73 chunk = end - offset;
74 if (chunk > 0) {
75 if (chunk > len)
76 chunk = len;
77 elt++;
78 len -= chunk;
79 if (len == 0)
80 return elt;
81 offset += chunk;
82 }
83 start = end;
84 }
85
86 if (unlikely(skb_has_frag_list(skb))) {
87 skb_walk_frags(skb, frag_iter) {
88 int end, ret;
89
90 WARN_ON(start > offset + len);
91
92 end = start + frag_iter->len;
93 chunk = end - offset;
94 if (chunk > 0) {
95 if (chunk > len)
96 chunk = len;
97 ret = __skb_nsg(frag_iter, offset - start, chunk,
98 recursion_level + 1);
99 if (unlikely(ret < 0))
100 return ret;
101 elt += ret;
102 len -= chunk;
103 if (len == 0)
104 return elt;
105 offset += chunk;
106 }
107 start = end;
108 }
109 }
110 BUG_ON(len);
111 return elt;
112}
113
114/* Return the number of scatterlist elements required to completely map the
115 * skb, or -EMSGSIZE if the recursion depth is exceeded.
116 */
117static int skb_nsg(struct sk_buff *skb, int offset, int len)
118{
119 return __skb_nsg(skb, offset, len, 0);
120}
121
Vakul Garg94524d82018-08-29 15:26:55 +0530122static void tls_decrypt_done(struct crypto_async_request *req, int err)
123{
124 struct aead_request *aead_req = (struct aead_request *)req;
125 struct decrypt_req_ctx *req_ctx =
126 (struct decrypt_req_ctx *)(aead_req + 1);
127
128 struct scatterlist *sgout = aead_req->dst;
129
130 struct tls_context *tls_ctx = tls_get_ctx(req_ctx->sk);
131 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
132 int pending = atomic_dec_return(&ctx->decrypt_pending);
133 struct scatterlist *sg;
134 unsigned int pages;
135
136 /* Propagate if there was an err */
137 if (err) {
138 ctx->async_wait.err = err;
139 tls_err_abort(req_ctx->sk, err);
140 }
141
142 /* Release the skb, pages and memory allocated for crypto req */
143 kfree_skb(req->data);
144
145 /* Skip the first S/G entry as it points to AAD */
146 for_each_sg(sg_next(sgout), sg, UINT_MAX, pages) {
147 if (!sg)
148 break;
149 put_page(sg_page(sg));
150 }
151
152 kfree(aead_req);
153
154 if (!pending && READ_ONCE(ctx->async_notify))
155 complete(&ctx->async_wait.completion);
156}
157
Dave Watsonc46234e2018-03-22 10:10:35 -0700158static int tls_do_decryption(struct sock *sk,
Vakul Garg94524d82018-08-29 15:26:55 +0530159 struct sk_buff *skb,
Dave Watsonc46234e2018-03-22 10:10:35 -0700160 struct scatterlist *sgin,
161 struct scatterlist *sgout,
162 char *iv_recv,
163 size_t data_len,
Vakul Garg94524d82018-08-29 15:26:55 +0530164 struct aead_request *aead_req,
165 bool async)
Dave Watsonc46234e2018-03-22 10:10:35 -0700166{
167 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300168 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -0700169 int ret;
Dave Watsonc46234e2018-03-22 10:10:35 -0700170
Vakul Garg0b243d02018-08-10 20:46:41 +0530171 aead_request_set_tfm(aead_req, ctx->aead_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700172 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
173 aead_request_set_crypt(aead_req, sgin, sgout,
174 data_len + tls_ctx->rx.tag_size,
175 (u8 *)iv_recv);
Dave Watsonc46234e2018-03-22 10:10:35 -0700176
Vakul Garg94524d82018-08-29 15:26:55 +0530177 if (async) {
178 struct decrypt_req_ctx *req_ctx;
179
180 req_ctx = (struct decrypt_req_ctx *)(aead_req + 1);
181 req_ctx->sk = sk;
182
183 aead_request_set_callback(aead_req,
184 CRYPTO_TFM_REQ_MAY_BACKLOG,
185 tls_decrypt_done, skb);
186 atomic_inc(&ctx->decrypt_pending);
187 } else {
188 aead_request_set_callback(aead_req,
189 CRYPTO_TFM_REQ_MAY_BACKLOG,
190 crypto_req_done, &ctx->async_wait);
191 }
192
193 ret = crypto_aead_decrypt(aead_req);
194 if (ret == -EINPROGRESS) {
195 if (async)
196 return ret;
197
198 ret = crypto_wait_req(ret, &ctx->async_wait);
199 }
200
201 if (async)
202 atomic_dec(&ctx->decrypt_pending);
203
Dave Watsonc46234e2018-03-22 10:10:35 -0700204 return ret;
205}
206
Dave Watson3c4d7552017-06-14 11:37:39 -0700207static void trim_sg(struct sock *sk, struct scatterlist *sg,
208 int *sg_num_elem, unsigned int *sg_size, int target_size)
209{
210 int i = *sg_num_elem - 1;
211 int trim = *sg_size - target_size;
212
213 if (trim <= 0) {
214 WARN_ON(trim < 0);
215 return;
216 }
217
218 *sg_size = target_size;
219 while (trim >= sg[i].length) {
220 trim -= sg[i].length;
221 sk_mem_uncharge(sk, sg[i].length);
222 put_page(sg_page(&sg[i]));
223 i--;
224
225 if (i < 0)
226 goto out;
227 }
228
229 sg[i].length -= trim;
230 sk_mem_uncharge(sk, trim);
231
232out:
233 *sg_num_elem = i + 1;
234}
235
236static void trim_both_sgl(struct sock *sk, int target_size)
237{
238 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300239 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700240
241 trim_sg(sk, ctx->sg_plaintext_data,
242 &ctx->sg_plaintext_num_elem,
243 &ctx->sg_plaintext_size,
244 target_size);
245
246 if (target_size > 0)
Dave Watsondbe42552018-03-22 10:10:06 -0700247 target_size += tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700248
249 trim_sg(sk, ctx->sg_encrypted_data,
250 &ctx->sg_encrypted_num_elem,
251 &ctx->sg_encrypted_size,
252 target_size);
253}
254
Dave Watson3c4d7552017-06-14 11:37:39 -0700255static int alloc_encrypted_sg(struct sock *sk, int len)
256{
257 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300258 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700259 int rc = 0;
260
John Fastabend2c3682f2018-03-18 12:56:49 -0700261 rc = sk_alloc_sg(sk, len,
John Fastabend8c05dbf2018-03-18 12:57:05 -0700262 ctx->sg_encrypted_data, 0,
John Fastabend2c3682f2018-03-18 12:56:49 -0700263 &ctx->sg_encrypted_num_elem,
264 &ctx->sg_encrypted_size, 0);
Dave Watson3c4d7552017-06-14 11:37:39 -0700265
266 return rc;
267}
268
269static int alloc_plaintext_sg(struct sock *sk, int len)
270{
271 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300272 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700273 int rc = 0;
274
John Fastabend8c05dbf2018-03-18 12:57:05 -0700275 rc = sk_alloc_sg(sk, len, ctx->sg_plaintext_data, 0,
John Fastabend2c3682f2018-03-18 12:56:49 -0700276 &ctx->sg_plaintext_num_elem, &ctx->sg_plaintext_size,
277 tls_ctx->pending_open_record_frags);
Dave Watson3c4d7552017-06-14 11:37:39 -0700278
279 return rc;
280}
281
282static void free_sg(struct sock *sk, struct scatterlist *sg,
283 int *sg_num_elem, unsigned int *sg_size)
284{
285 int i, n = *sg_num_elem;
286
287 for (i = 0; i < n; ++i) {
288 sk_mem_uncharge(sk, sg[i].length);
289 put_page(sg_page(&sg[i]));
290 }
291 *sg_num_elem = 0;
292 *sg_size = 0;
293}
294
295static void tls_free_both_sg(struct sock *sk)
296{
297 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300298 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700299
300 free_sg(sk, ctx->sg_encrypted_data, &ctx->sg_encrypted_num_elem,
301 &ctx->sg_encrypted_size);
302
303 free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem,
304 &ctx->sg_plaintext_size);
305}
306
307static int tls_do_encryption(struct tls_context *tls_ctx,
Daniel Borkmanna447da72018-06-15 03:07:45 +0200308 struct tls_sw_context_tx *ctx,
309 struct aead_request *aead_req,
310 size_t data_len)
Dave Watson3c4d7552017-06-14 11:37:39 -0700311{
Dave Watson3c4d7552017-06-14 11:37:39 -0700312 int rc;
313
Dave Watsondbe42552018-03-22 10:10:06 -0700314 ctx->sg_encrypted_data[0].offset += tls_ctx->tx.prepend_size;
315 ctx->sg_encrypted_data[0].length -= tls_ctx->tx.prepend_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700316
317 aead_request_set_tfm(aead_req, ctx->aead_send);
318 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
319 aead_request_set_crypt(aead_req, ctx->sg_aead_in, ctx->sg_aead_out,
Dave Watsondbe42552018-03-22 10:10:06 -0700320 data_len, tls_ctx->tx.iv);
Vakul Garga54667f2018-01-31 21:34:37 +0530321
322 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
323 crypto_req_done, &ctx->async_wait);
324
325 rc = crypto_wait_req(crypto_aead_encrypt(aead_req), &ctx->async_wait);
Dave Watson3c4d7552017-06-14 11:37:39 -0700326
Dave Watsondbe42552018-03-22 10:10:06 -0700327 ctx->sg_encrypted_data[0].offset -= tls_ctx->tx.prepend_size;
328 ctx->sg_encrypted_data[0].length += tls_ctx->tx.prepend_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700329
Dave Watson3c4d7552017-06-14 11:37:39 -0700330 return rc;
331}
332
333static int tls_push_record(struct sock *sk, int flags,
334 unsigned char record_type)
335{
336 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300337 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Daniel Borkmanna447da72018-06-15 03:07:45 +0200338 struct aead_request *req;
Dave Watson3c4d7552017-06-14 11:37:39 -0700339 int rc;
340
Vakul Gargd2bdd262018-07-11 14:32:20 +0530341 req = aead_request_alloc(ctx->aead_send, sk->sk_allocation);
Daniel Borkmanna447da72018-06-15 03:07:45 +0200342 if (!req)
343 return -ENOMEM;
344
Dave Watson3c4d7552017-06-14 11:37:39 -0700345 sg_mark_end(ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem - 1);
346 sg_mark_end(ctx->sg_encrypted_data + ctx->sg_encrypted_num_elem - 1);
347
Ilya Lesokhin213ef6e2017-11-13 10:22:47 +0200348 tls_make_aad(ctx->aad_space, ctx->sg_plaintext_size,
Dave Watsondbe42552018-03-22 10:10:06 -0700349 tls_ctx->tx.rec_seq, tls_ctx->tx.rec_seq_size,
Dave Watson3c4d7552017-06-14 11:37:39 -0700350 record_type);
351
352 tls_fill_prepend(tls_ctx,
353 page_address(sg_page(&ctx->sg_encrypted_data[0])) +
354 ctx->sg_encrypted_data[0].offset,
355 ctx->sg_plaintext_size, record_type);
356
357 tls_ctx->pending_open_record_frags = 0;
358 set_bit(TLS_PENDING_CLOSED_RECORD, &tls_ctx->flags);
359
Daniel Borkmanna447da72018-06-15 03:07:45 +0200360 rc = tls_do_encryption(tls_ctx, ctx, req, ctx->sg_plaintext_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700361 if (rc < 0) {
362 /* If we are called from write_space and
363 * we fail, we need to set this SOCK_NOSPACE
364 * to trigger another write_space in the future.
365 */
366 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
Daniel Borkmanna447da72018-06-15 03:07:45 +0200367 goto out_req;
Dave Watson3c4d7552017-06-14 11:37:39 -0700368 }
369
370 free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem,
371 &ctx->sg_plaintext_size);
372
373 ctx->sg_encrypted_num_elem = 0;
374 ctx->sg_encrypted_size = 0;
375
376 /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */
377 rc = tls_push_sg(sk, tls_ctx, ctx->sg_encrypted_data, 0, flags);
378 if (rc < 0 && rc != -EAGAIN)
Dave Watsonf4a8e432018-03-22 10:10:15 -0700379 tls_err_abort(sk, EBADMSG);
Dave Watson3c4d7552017-06-14 11:37:39 -0700380
Dave Watsondbe42552018-03-22 10:10:06 -0700381 tls_advance_record_sn(sk, &tls_ctx->tx);
Daniel Borkmanna447da72018-06-15 03:07:45 +0200382out_req:
Vakul Gargd2bdd262018-07-11 14:32:20 +0530383 aead_request_free(req);
Dave Watson3c4d7552017-06-14 11:37:39 -0700384 return rc;
385}
386
387static int tls_sw_push_pending_record(struct sock *sk, int flags)
388{
389 return tls_push_record(sk, flags, TLS_RECORD_TYPE_DATA);
390}
391
392static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
Dave Watson69ca9292018-03-22 10:09:53 -0700393 int length, int *pages_used,
394 unsigned int *size_used,
395 struct scatterlist *to, int to_max_pages,
Doron Roberts-Kedes2da19ed2018-07-26 07:59:36 -0700396 bool charge)
Dave Watson3c4d7552017-06-14 11:37:39 -0700397{
Dave Watson3c4d7552017-06-14 11:37:39 -0700398 struct page *pages[MAX_SKB_FRAGS];
399
400 size_t offset;
401 ssize_t copied, use;
402 int i = 0;
Dave Watson69ca9292018-03-22 10:09:53 -0700403 unsigned int size = *size_used;
404 int num_elem = *pages_used;
Dave Watson3c4d7552017-06-14 11:37:39 -0700405 int rc = 0;
406 int maxpages;
407
408 while (length > 0) {
409 i = 0;
Dave Watson69ca9292018-03-22 10:09:53 -0700410 maxpages = to_max_pages - num_elem;
Dave Watson3c4d7552017-06-14 11:37:39 -0700411 if (maxpages == 0) {
412 rc = -EFAULT;
413 goto out;
414 }
415 copied = iov_iter_get_pages(from, pages,
416 length,
417 maxpages, &offset);
418 if (copied <= 0) {
419 rc = -EFAULT;
420 goto out;
421 }
422
423 iov_iter_advance(from, copied);
424
425 length -= copied;
426 size += copied;
427 while (copied) {
428 use = min_t(int, copied, PAGE_SIZE - offset);
429
Dave Watson69ca9292018-03-22 10:09:53 -0700430 sg_set_page(&to[num_elem],
Dave Watson3c4d7552017-06-14 11:37:39 -0700431 pages[i], use, offset);
Dave Watson69ca9292018-03-22 10:09:53 -0700432 sg_unmark_end(&to[num_elem]);
433 if (charge)
434 sk_mem_charge(sk, use);
Dave Watson3c4d7552017-06-14 11:37:39 -0700435
436 offset = 0;
437 copied -= use;
438
439 ++i;
440 ++num_elem;
441 }
442 }
443
Vakul Gargcfb40992018-08-02 20:43:10 +0530444 /* Mark the end in the last sg entry if newly added */
445 if (num_elem > *pages_used)
446 sg_mark_end(&to[num_elem - 1]);
Dave Watson3c4d7552017-06-14 11:37:39 -0700447out:
Doron Roberts-Kedes2da19ed2018-07-26 07:59:36 -0700448 if (rc)
449 iov_iter_revert(from, size - *size_used);
Dave Watson69ca9292018-03-22 10:09:53 -0700450 *size_used = size;
451 *pages_used = num_elem;
452
Dave Watson3c4d7552017-06-14 11:37:39 -0700453 return rc;
454}
455
456static int memcopy_from_iter(struct sock *sk, struct iov_iter *from,
457 int bytes)
458{
459 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300460 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700461 struct scatterlist *sg = ctx->sg_plaintext_data;
462 int copy, i, rc = 0;
463
464 for (i = tls_ctx->pending_open_record_frags;
465 i < ctx->sg_plaintext_num_elem; ++i) {
466 copy = sg[i].length;
467 if (copy_from_iter(
468 page_address(sg_page(&sg[i])) + sg[i].offset,
469 copy, from) != copy) {
470 rc = -EFAULT;
471 goto out;
472 }
473 bytes -= copy;
474
475 ++tls_ctx->pending_open_record_frags;
476
477 if (!bytes)
478 break;
479 }
480
481out:
482 return rc;
483}
484
485int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
486{
487 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300488 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700489 int ret = 0;
490 int required_size;
491 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
492 bool eor = !(msg->msg_flags & MSG_MORE);
493 size_t try_to_copy, copied = 0;
494 unsigned char record_type = TLS_RECORD_TYPE_DATA;
495 int record_room;
496 bool full_record;
497 int orig_size;
Doron Roberts-Kedes0a26cf32018-07-25 14:48:21 -0700498 bool is_kvec = msg->msg_iter.type & ITER_KVEC;
Dave Watson3c4d7552017-06-14 11:37:39 -0700499
500 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
501 return -ENOTSUPP;
502
503 lock_sock(sk);
504
505 if (tls_complete_pending_work(sk, tls_ctx, msg->msg_flags, &timeo))
506 goto send_end;
507
508 if (unlikely(msg->msg_controllen)) {
509 ret = tls_proccess_cmsg(sk, msg, &record_type);
510 if (ret)
511 goto send_end;
512 }
513
514 while (msg_data_left(msg)) {
515 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +0100516 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -0700517 goto send_end;
518 }
519
520 orig_size = ctx->sg_plaintext_size;
521 full_record = false;
522 try_to_copy = msg_data_left(msg);
523 record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size;
524 if (try_to_copy >= record_room) {
525 try_to_copy = record_room;
526 full_record = true;
527 }
528
529 required_size = ctx->sg_plaintext_size + try_to_copy +
Dave Watsondbe42552018-03-22 10:10:06 -0700530 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700531
532 if (!sk_stream_memory_free(sk))
533 goto wait_for_sndbuf;
534alloc_encrypted:
535 ret = alloc_encrypted_sg(sk, required_size);
536 if (ret) {
537 if (ret != -ENOSPC)
538 goto wait_for_memory;
539
540 /* Adjust try_to_copy according to the amount that was
541 * actually allocated. The difference is due
542 * to max sg elements limit
543 */
544 try_to_copy -= required_size - ctx->sg_encrypted_size;
545 full_record = true;
546 }
Doron Roberts-Kedes0a26cf32018-07-25 14:48:21 -0700547 if (!is_kvec && (full_record || eor)) {
Dave Watson3c4d7552017-06-14 11:37:39 -0700548 ret = zerocopy_from_iter(sk, &msg->msg_iter,
Dave Watson69ca9292018-03-22 10:09:53 -0700549 try_to_copy, &ctx->sg_plaintext_num_elem,
550 &ctx->sg_plaintext_size,
551 ctx->sg_plaintext_data,
552 ARRAY_SIZE(ctx->sg_plaintext_data),
Doron Roberts-Kedes2da19ed2018-07-26 07:59:36 -0700553 true);
Dave Watson3c4d7552017-06-14 11:37:39 -0700554 if (ret)
555 goto fallback_to_reg_send;
556
557 copied += try_to_copy;
558 ret = tls_push_record(sk, msg->msg_flags, record_type);
Doron Roberts-Kedes5a3611e2018-07-26 07:59:35 -0700559 if (ret)
Dave Watson3c4d7552017-06-14 11:37:39 -0700560 goto send_end;
Doron Roberts-Kedes5a3611e2018-07-26 07:59:35 -0700561 continue;
Dave Watson3c4d7552017-06-14 11:37:39 -0700562
Dave Watson3c4d7552017-06-14 11:37:39 -0700563fallback_to_reg_send:
Dave Watson3c4d7552017-06-14 11:37:39 -0700564 trim_sg(sk, ctx->sg_plaintext_data,
565 &ctx->sg_plaintext_num_elem,
566 &ctx->sg_plaintext_size,
567 orig_size);
568 }
569
570 required_size = ctx->sg_plaintext_size + try_to_copy;
571alloc_plaintext:
572 ret = alloc_plaintext_sg(sk, required_size);
573 if (ret) {
574 if (ret != -ENOSPC)
575 goto wait_for_memory;
576
577 /* Adjust try_to_copy according to the amount that was
578 * actually allocated. The difference is due
579 * to max sg elements limit
580 */
581 try_to_copy -= required_size - ctx->sg_plaintext_size;
582 full_record = true;
583
584 trim_sg(sk, ctx->sg_encrypted_data,
585 &ctx->sg_encrypted_num_elem,
586 &ctx->sg_encrypted_size,
587 ctx->sg_plaintext_size +
Dave Watsondbe42552018-03-22 10:10:06 -0700588 tls_ctx->tx.overhead_size);
Dave Watson3c4d7552017-06-14 11:37:39 -0700589 }
590
591 ret = memcopy_from_iter(sk, &msg->msg_iter, try_to_copy);
592 if (ret)
593 goto trim_sgl;
594
595 copied += try_to_copy;
596 if (full_record || eor) {
597push_record:
598 ret = tls_push_record(sk, msg->msg_flags, record_type);
599 if (ret) {
600 if (ret == -ENOMEM)
601 goto wait_for_memory;
602
603 goto send_end;
604 }
605 }
606
607 continue;
608
609wait_for_sndbuf:
610 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
611wait_for_memory:
612 ret = sk_stream_wait_memory(sk, &timeo);
613 if (ret) {
614trim_sgl:
615 trim_both_sgl(sk, orig_size);
616 goto send_end;
617 }
618
619 if (tls_is_pending_closed_record(tls_ctx))
620 goto push_record;
621
622 if (ctx->sg_encrypted_size < required_size)
623 goto alloc_encrypted;
624
625 goto alloc_plaintext;
626 }
627
628send_end:
629 ret = sk_stream_error(sk, msg->msg_flags, ret);
630
631 release_sock(sk);
632 return copied ? copied : ret;
633}
634
635int tls_sw_sendpage(struct sock *sk, struct page *page,
636 int offset, size_t size, int flags)
637{
638 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300639 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -0700640 int ret = 0;
641 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
642 bool eor;
643 size_t orig_size = size;
644 unsigned char record_type = TLS_RECORD_TYPE_DATA;
645 struct scatterlist *sg;
646 bool full_record;
647 int record_room;
648
649 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
650 MSG_SENDPAGE_NOTLAST))
651 return -ENOTSUPP;
652
653 /* No MSG_EOR from splice, only look at MSG_MORE */
654 eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
655
656 lock_sock(sk);
657
658 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
659
660 if (tls_complete_pending_work(sk, tls_ctx, flags, &timeo))
661 goto sendpage_end;
662
663 /* Call the sk_stream functions to manage the sndbuf mem. */
664 while (size > 0) {
665 size_t copy, required_size;
666
667 if (sk->sk_err) {
r.hering@avm.de30be8f82018-01-12 15:42:06 +0100668 ret = -sk->sk_err;
Dave Watson3c4d7552017-06-14 11:37:39 -0700669 goto sendpage_end;
670 }
671
672 full_record = false;
673 record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size;
674 copy = size;
675 if (copy >= record_room) {
676 copy = record_room;
677 full_record = true;
678 }
679 required_size = ctx->sg_plaintext_size + copy +
Dave Watsondbe42552018-03-22 10:10:06 -0700680 tls_ctx->tx.overhead_size;
Dave Watson3c4d7552017-06-14 11:37:39 -0700681
682 if (!sk_stream_memory_free(sk))
683 goto wait_for_sndbuf;
684alloc_payload:
685 ret = alloc_encrypted_sg(sk, required_size);
686 if (ret) {
687 if (ret != -ENOSPC)
688 goto wait_for_memory;
689
690 /* Adjust copy according to the amount that was
691 * actually allocated. The difference is due
692 * to max sg elements limit
693 */
694 copy -= required_size - ctx->sg_plaintext_size;
695 full_record = true;
696 }
697
698 get_page(page);
699 sg = ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem;
700 sg_set_page(sg, page, copy, offset);
Dave Watson7a8c4dd2018-01-19 12:30:13 -0800701 sg_unmark_end(sg);
702
Dave Watson3c4d7552017-06-14 11:37:39 -0700703 ctx->sg_plaintext_num_elem++;
704
705 sk_mem_charge(sk, copy);
706 offset += copy;
707 size -= copy;
708 ctx->sg_plaintext_size += copy;
709 tls_ctx->pending_open_record_frags = ctx->sg_plaintext_num_elem;
710
711 if (full_record || eor ||
712 ctx->sg_plaintext_num_elem ==
713 ARRAY_SIZE(ctx->sg_plaintext_data)) {
714push_record:
715 ret = tls_push_record(sk, flags, record_type);
716 if (ret) {
717 if (ret == -ENOMEM)
718 goto wait_for_memory;
719
720 goto sendpage_end;
721 }
722 }
723 continue;
724wait_for_sndbuf:
725 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
726wait_for_memory:
727 ret = sk_stream_wait_memory(sk, &timeo);
728 if (ret) {
729 trim_both_sgl(sk, ctx->sg_plaintext_size);
730 goto sendpage_end;
731 }
732
733 if (tls_is_pending_closed_record(tls_ctx))
734 goto push_record;
735
736 goto alloc_payload;
737 }
738
739sendpage_end:
740 if (orig_size > size)
741 ret = orig_size - size;
742 else
743 ret = sk_stream_error(sk, flags, ret);
744
745 release_sock(sk);
746 return ret;
747}
748
Dave Watsonc46234e2018-03-22 10:10:35 -0700749static struct sk_buff *tls_wait_data(struct sock *sk, int flags,
750 long timeo, int *err)
751{
752 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300753 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -0700754 struct sk_buff *skb;
755 DEFINE_WAIT_FUNC(wait, woken_wake_function);
756
757 while (!(skb = ctx->recv_pkt)) {
758 if (sk->sk_err) {
759 *err = sock_error(sk);
760 return NULL;
761 }
762
Doron Roberts-Kedesfcf47932018-07-18 16:22:27 -0700763 if (sk->sk_shutdown & RCV_SHUTDOWN)
764 return NULL;
765
Dave Watsonc46234e2018-03-22 10:10:35 -0700766 if (sock_flag(sk, SOCK_DONE))
767 return NULL;
768
769 if ((flags & MSG_DONTWAIT) || !timeo) {
770 *err = -EAGAIN;
771 return NULL;
772 }
773
774 add_wait_queue(sk_sleep(sk), &wait);
775 sk_set_bit(SOCKWQ_ASYNC_WAITDATA, sk);
776 sk_wait_event(sk, &timeo, ctx->recv_pkt != skb, &wait);
777 sk_clear_bit(SOCKWQ_ASYNC_WAITDATA, sk);
778 remove_wait_queue(sk_sleep(sk), &wait);
779
780 /* Handle signals */
781 if (signal_pending(current)) {
782 *err = sock_intr_errno(timeo);
783 return NULL;
784 }
785 }
786
787 return skb;
788}
789
Vakul Garg0b243d02018-08-10 20:46:41 +0530790/* This function decrypts the input skb into either out_iov or in out_sg
791 * or in skb buffers itself. The input parameter 'zc' indicates if
792 * zero-copy mode needs to be tried or not. With zero-copy mode, either
793 * out_iov or out_sg must be non-NULL. In case both out_iov and out_sg are
794 * NULL, then the decryption happens inside skb buffers itself, i.e.
795 * zero-copy gets disabled and 'zc' is updated.
796 */
797
798static int decrypt_internal(struct sock *sk, struct sk_buff *skb,
799 struct iov_iter *out_iov,
800 struct scatterlist *out_sg,
801 int *chunk, bool *zc)
802{
803 struct tls_context *tls_ctx = tls_get_ctx(sk);
804 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
805 struct strp_msg *rxm = strp_msg(skb);
806 int n_sgin, n_sgout, nsg, mem_size, aead_size, err, pages = 0;
807 struct aead_request *aead_req;
808 struct sk_buff *unused;
809 u8 *aad, *iv, *mem = NULL;
810 struct scatterlist *sgin = NULL;
811 struct scatterlist *sgout = NULL;
812 const int data_len = rxm->full_len - tls_ctx->rx.overhead_size;
813
814 if (*zc && (out_iov || out_sg)) {
815 if (out_iov)
816 n_sgout = iov_iter_npages(out_iov, INT_MAX) + 1;
817 else
818 n_sgout = sg_nents(out_sg);
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -0700819 n_sgin = skb_nsg(skb, rxm->offset + tls_ctx->rx.prepend_size,
820 rxm->full_len - tls_ctx->rx.prepend_size);
Vakul Garg0b243d02018-08-10 20:46:41 +0530821 } else {
822 n_sgout = 0;
823 *zc = false;
Doron Roberts-Kedes0927f712018-08-28 16:33:57 -0700824 n_sgin = skb_cow_data(skb, 0, &unused);
Vakul Garg0b243d02018-08-10 20:46:41 +0530825 }
826
Vakul Garg0b243d02018-08-10 20:46:41 +0530827 if (n_sgin < 1)
828 return -EBADMSG;
829
830 /* Increment to accommodate AAD */
831 n_sgin = n_sgin + 1;
832
833 nsg = n_sgin + n_sgout;
834
835 aead_size = sizeof(*aead_req) + crypto_aead_reqsize(ctx->aead_recv);
836 mem_size = aead_size + (nsg * sizeof(struct scatterlist));
837 mem_size = mem_size + TLS_AAD_SPACE_SIZE;
838 mem_size = mem_size + crypto_aead_ivsize(ctx->aead_recv);
839
840 /* Allocate a single block of memory which contains
841 * aead_req || sgin[] || sgout[] || aad || iv.
842 * This order achieves correct alignment for aead_req, sgin, sgout.
843 */
844 mem = kmalloc(mem_size, sk->sk_allocation);
845 if (!mem)
846 return -ENOMEM;
847
848 /* Segment the allocated memory */
849 aead_req = (struct aead_request *)mem;
850 sgin = (struct scatterlist *)(mem + aead_size);
851 sgout = sgin + n_sgin;
852 aad = (u8 *)(sgout + n_sgout);
853 iv = aad + TLS_AAD_SPACE_SIZE;
854
855 /* Prepare IV */
856 err = skb_copy_bits(skb, rxm->offset + TLS_HEADER_SIZE,
857 iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
858 tls_ctx->rx.iv_size);
859 if (err < 0) {
860 kfree(mem);
861 return err;
862 }
863 memcpy(iv, tls_ctx->rx.iv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
864
865 /* Prepare AAD */
866 tls_make_aad(aad, rxm->full_len - tls_ctx->rx.overhead_size,
867 tls_ctx->rx.rec_seq, tls_ctx->rx.rec_seq_size,
868 ctx->control);
869
870 /* Prepare sgin */
871 sg_init_table(sgin, n_sgin);
872 sg_set_buf(&sgin[0], aad, TLS_AAD_SPACE_SIZE);
873 err = skb_to_sgvec(skb, &sgin[1],
874 rxm->offset + tls_ctx->rx.prepend_size,
875 rxm->full_len - tls_ctx->rx.prepend_size);
876 if (err < 0) {
877 kfree(mem);
878 return err;
879 }
880
881 if (n_sgout) {
882 if (out_iov) {
883 sg_init_table(sgout, n_sgout);
884 sg_set_buf(&sgout[0], aad, TLS_AAD_SPACE_SIZE);
885
886 *chunk = 0;
887 err = zerocopy_from_iter(sk, out_iov, data_len, &pages,
888 chunk, &sgout[1],
889 (n_sgout - 1), false);
890 if (err < 0)
891 goto fallback_to_reg_recv;
892 } else if (out_sg) {
893 memcpy(sgout, out_sg, n_sgout * sizeof(*sgout));
894 } else {
895 goto fallback_to_reg_recv;
896 }
897 } else {
898fallback_to_reg_recv:
899 sgout = sgin;
900 pages = 0;
901 *chunk = 0;
902 *zc = false;
903 }
904
905 /* Prepare and submit AEAD request */
Vakul Garg94524d82018-08-29 15:26:55 +0530906 err = tls_do_decryption(sk, skb, sgin, sgout, iv,
907 data_len, aead_req, *zc);
908 if (err == -EINPROGRESS)
909 return err;
Vakul Garg0b243d02018-08-10 20:46:41 +0530910
911 /* Release the pages in case iov was mapped to pages */
912 for (; pages > 0; pages--)
913 put_page(sg_page(&sgout[pages]));
914
915 kfree(mem);
916 return err;
917}
918
Boris Pismennydafb67f2018-07-13 14:33:40 +0300919static int decrypt_skb_update(struct sock *sk, struct sk_buff *skb,
Vakul Garg0b243d02018-08-10 20:46:41 +0530920 struct iov_iter *dest, int *chunk, bool *zc)
Boris Pismennydafb67f2018-07-13 14:33:40 +0300921{
922 struct tls_context *tls_ctx = tls_get_ctx(sk);
923 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
924 struct strp_msg *rxm = strp_msg(skb);
925 int err = 0;
926
Boris Pismenny4799ac82018-07-13 14:33:43 +0300927#ifdef CONFIG_TLS_DEVICE
928 err = tls_device_decrypted(sk, skb);
Boris Pismennydafb67f2018-07-13 14:33:40 +0300929 if (err < 0)
930 return err;
Boris Pismenny4799ac82018-07-13 14:33:43 +0300931#endif
932 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +0530933 err = decrypt_internal(sk, skb, dest, NULL, chunk, zc);
Vakul Garg94524d82018-08-29 15:26:55 +0530934 if (err < 0) {
935 if (err == -EINPROGRESS)
936 tls_advance_record_sn(sk, &tls_ctx->rx);
937
Boris Pismenny4799ac82018-07-13 14:33:43 +0300938 return err;
Vakul Garg94524d82018-08-29 15:26:55 +0530939 }
Boris Pismenny4799ac82018-07-13 14:33:43 +0300940 } else {
941 *zc = false;
942 }
Boris Pismennydafb67f2018-07-13 14:33:40 +0300943
944 rxm->offset += tls_ctx->rx.prepend_size;
945 rxm->full_len -= tls_ctx->rx.overhead_size;
946 tls_advance_record_sn(sk, &tls_ctx->rx);
947 ctx->decrypted = true;
948 ctx->saved_data_ready(sk);
949
950 return err;
951}
952
953int decrypt_skb(struct sock *sk, struct sk_buff *skb,
954 struct scatterlist *sgout)
Dave Watsonc46234e2018-03-22 10:10:35 -0700955{
Vakul Garg0b243d02018-08-10 20:46:41 +0530956 bool zc = true;
957 int chunk;
Dave Watsonc46234e2018-03-22 10:10:35 -0700958
Vakul Garg0b243d02018-08-10 20:46:41 +0530959 return decrypt_internal(sk, skb, NULL, sgout, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -0700960}
961
962static bool tls_sw_advance_skb(struct sock *sk, struct sk_buff *skb,
963 unsigned int len)
964{
965 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300966 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -0700967
Vakul Garg94524d82018-08-29 15:26:55 +0530968 if (skb) {
969 struct strp_msg *rxm = strp_msg(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -0700970
Vakul Garg94524d82018-08-29 15:26:55 +0530971 if (len < rxm->full_len) {
972 rxm->offset += len;
973 rxm->full_len -= len;
974 return false;
975 }
976 kfree_skb(skb);
Dave Watsonc46234e2018-03-22 10:10:35 -0700977 }
978
979 /* Finished with message */
980 ctx->recv_pkt = NULL;
Doron Roberts-Kedes7170e602018-06-06 09:33:28 -0700981 __strp_unpause(&ctx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -0700982
983 return true;
984}
985
986int tls_sw_recvmsg(struct sock *sk,
987 struct msghdr *msg,
988 size_t len,
989 int nonblock,
990 int flags,
991 int *addr_len)
992{
993 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +0300994 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -0700995 unsigned char control;
996 struct strp_msg *rxm;
997 struct sk_buff *skb;
998 ssize_t copied = 0;
999 bool cmsg = false;
Daniel Borkmann06030db2018-06-15 03:07:46 +02001000 int target, err = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001001 long timeo;
Doron Roberts-Kedes0a26cf32018-07-25 14:48:21 -07001002 bool is_kvec = msg->msg_iter.type & ITER_KVEC;
Vakul Garg94524d82018-08-29 15:26:55 +05301003 int num_async = 0;
Dave Watsonc46234e2018-03-22 10:10:35 -07001004
1005 flags |= nonblock;
1006
1007 if (unlikely(flags & MSG_ERRQUEUE))
1008 return sock_recv_errqueue(sk, msg, len, SOL_IP, IP_RECVERR);
1009
1010 lock_sock(sk);
1011
Daniel Borkmann06030db2018-06-15 03:07:46 +02001012 target = sock_rcvlowat(sk, flags & MSG_WAITALL, len);
Dave Watsonc46234e2018-03-22 10:10:35 -07001013 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1014 do {
1015 bool zc = false;
Vakul Garg94524d82018-08-29 15:26:55 +05301016 bool async = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001017 int chunk = 0;
1018
1019 skb = tls_wait_data(sk, flags, timeo, &err);
1020 if (!skb)
1021 goto recv_end;
1022
1023 rxm = strp_msg(skb);
Vakul Garg94524d82018-08-29 15:26:55 +05301024
Dave Watsonc46234e2018-03-22 10:10:35 -07001025 if (!cmsg) {
1026 int cerr;
1027
1028 cerr = put_cmsg(msg, SOL_TLS, TLS_GET_RECORD_TYPE,
1029 sizeof(ctx->control), &ctx->control);
1030 cmsg = true;
1031 control = ctx->control;
1032 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1033 if (cerr || msg->msg_flags & MSG_CTRUNC) {
1034 err = -EIO;
1035 goto recv_end;
1036 }
1037 }
1038 } else if (control != ctx->control) {
1039 goto recv_end;
1040 }
1041
1042 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301043 int to_copy = rxm->full_len - tls_ctx->rx.overhead_size;
Dave Watsonc46234e2018-03-22 10:10:35 -07001044
Vakul Garg0b243d02018-08-10 20:46:41 +05301045 if (!is_kvec && to_copy <= len &&
1046 likely(!(flags & MSG_PEEK)))
Dave Watsonc46234e2018-03-22 10:10:35 -07001047 zc = true;
Dave Watsonc46234e2018-03-22 10:10:35 -07001048
Vakul Garg0b243d02018-08-10 20:46:41 +05301049 err = decrypt_skb_update(sk, skb, &msg->msg_iter,
1050 &chunk, &zc);
Vakul Garg94524d82018-08-29 15:26:55 +05301051 if (err < 0 && err != -EINPROGRESS) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301052 tls_err_abort(sk, EBADMSG);
1053 goto recv_end;
Dave Watsonc46234e2018-03-22 10:10:35 -07001054 }
Vakul Garg94524d82018-08-29 15:26:55 +05301055
1056 if (err == -EINPROGRESS) {
1057 async = true;
1058 num_async++;
1059 goto pick_next_record;
1060 }
1061
Dave Watsonc46234e2018-03-22 10:10:35 -07001062 ctx->decrypted = true;
1063 }
1064
1065 if (!zc) {
1066 chunk = min_t(unsigned int, rxm->full_len, len);
Vakul Garg94524d82018-08-29 15:26:55 +05301067
Dave Watsonc46234e2018-03-22 10:10:35 -07001068 err = skb_copy_datagram_msg(skb, rxm->offset, msg,
1069 chunk);
1070 if (err < 0)
1071 goto recv_end;
1072 }
1073
Vakul Garg94524d82018-08-29 15:26:55 +05301074pick_next_record:
Dave Watsonc46234e2018-03-22 10:10:35 -07001075 copied += chunk;
1076 len -= chunk;
1077 if (likely(!(flags & MSG_PEEK))) {
1078 u8 control = ctx->control;
1079
Vakul Garg94524d82018-08-29 15:26:55 +05301080 /* For async, drop current skb reference */
1081 if (async)
1082 skb = NULL;
1083
Dave Watsonc46234e2018-03-22 10:10:35 -07001084 if (tls_sw_advance_skb(sk, skb, chunk)) {
1085 /* Return full control message to
1086 * userspace before trying to parse
1087 * another message type
1088 */
1089 msg->msg_flags |= MSG_EOR;
1090 if (control != TLS_RECORD_TYPE_DATA)
1091 goto recv_end;
Vakul Garg94524d82018-08-29 15:26:55 +05301092 } else {
1093 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001094 }
1095 }
Vakul Garg94524d82018-08-29 15:26:55 +05301096
Daniel Borkmann06030db2018-06-15 03:07:46 +02001097 /* If we have a new message from strparser, continue now. */
1098 if (copied >= target && !ctx->recv_pkt)
1099 break;
Dave Watsonc46234e2018-03-22 10:10:35 -07001100 } while (len);
1101
1102recv_end:
Vakul Garg94524d82018-08-29 15:26:55 +05301103 if (num_async) {
1104 /* Wait for all previously submitted records to be decrypted */
1105 smp_store_mb(ctx->async_notify, true);
1106 if (atomic_read(&ctx->decrypt_pending)) {
1107 err = crypto_wait_req(-EINPROGRESS, &ctx->async_wait);
1108 if (err) {
1109 /* one of async decrypt failed */
1110 tls_err_abort(sk, err);
1111 copied = 0;
1112 }
1113 } else {
1114 reinit_completion(&ctx->async_wait.completion);
1115 }
1116 WRITE_ONCE(ctx->async_notify, false);
1117 }
1118
Dave Watsonc46234e2018-03-22 10:10:35 -07001119 release_sock(sk);
1120 return copied ? : err;
1121}
1122
1123ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
1124 struct pipe_inode_info *pipe,
1125 size_t len, unsigned int flags)
1126{
1127 struct tls_context *tls_ctx = tls_get_ctx(sock->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001128 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001129 struct strp_msg *rxm = NULL;
1130 struct sock *sk = sock->sk;
1131 struct sk_buff *skb;
1132 ssize_t copied = 0;
1133 int err = 0;
1134 long timeo;
1135 int chunk;
Vakul Garg0b243d02018-08-10 20:46:41 +05301136 bool zc = false;
Dave Watsonc46234e2018-03-22 10:10:35 -07001137
1138 lock_sock(sk);
1139
1140 timeo = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1141
1142 skb = tls_wait_data(sk, flags, timeo, &err);
1143 if (!skb)
1144 goto splice_read_end;
1145
1146 /* splice does not support reading control messages */
1147 if (ctx->control != TLS_RECORD_TYPE_DATA) {
1148 err = -ENOTSUPP;
1149 goto splice_read_end;
1150 }
1151
1152 if (!ctx->decrypted) {
Vakul Garg0b243d02018-08-10 20:46:41 +05301153 err = decrypt_skb_update(sk, skb, NULL, &chunk, &zc);
Dave Watsonc46234e2018-03-22 10:10:35 -07001154
1155 if (err < 0) {
1156 tls_err_abort(sk, EBADMSG);
1157 goto splice_read_end;
1158 }
1159 ctx->decrypted = true;
1160 }
1161 rxm = strp_msg(skb);
1162
1163 chunk = min_t(unsigned int, rxm->full_len, len);
1164 copied = skb_splice_bits(skb, sk, rxm->offset, pipe, chunk, flags);
1165 if (copied < 0)
1166 goto splice_read_end;
1167
1168 if (likely(!(flags & MSG_PEEK)))
1169 tls_sw_advance_skb(sk, skb, copied);
1170
1171splice_read_end:
1172 release_sock(sk);
1173 return copied ? : err;
1174}
1175
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001176unsigned int tls_sw_poll(struct file *file, struct socket *sock,
1177 struct poll_table_struct *wait)
Dave Watsonc46234e2018-03-22 10:10:35 -07001178{
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001179 unsigned int ret;
Dave Watsonc46234e2018-03-22 10:10:35 -07001180 struct sock *sk = sock->sk;
1181 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001182 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001183
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001184 /* Grab POLLOUT and POLLHUP from the underlying socket */
1185 ret = ctx->sk_poll(file, sock, wait);
Dave Watsonc46234e2018-03-22 10:10:35 -07001186
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001187 /* Clear POLLIN bits, and set based on recv_pkt */
1188 ret &= ~(POLLIN | POLLRDNORM);
Dave Watsonc46234e2018-03-22 10:10:35 -07001189 if (ctx->recv_pkt)
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001190 ret |= POLLIN | POLLRDNORM;
Dave Watsonc46234e2018-03-22 10:10:35 -07001191
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001192 return ret;
Dave Watsonc46234e2018-03-22 10:10:35 -07001193}
1194
1195static int tls_read_size(struct strparser *strp, struct sk_buff *skb)
1196{
1197 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001198 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Kees Cook3463e512018-06-25 16:55:05 -07001199 char header[TLS_HEADER_SIZE + MAX_IV_SIZE];
Dave Watsonc46234e2018-03-22 10:10:35 -07001200 struct strp_msg *rxm = strp_msg(skb);
1201 size_t cipher_overhead;
1202 size_t data_len = 0;
1203 int ret;
1204
1205 /* Verify that we have a full TLS header, or wait for more data */
1206 if (rxm->offset + tls_ctx->rx.prepend_size > skb->len)
1207 return 0;
1208
Kees Cook3463e512018-06-25 16:55:05 -07001209 /* Sanity-check size of on-stack buffer. */
1210 if (WARN_ON(tls_ctx->rx.prepend_size > sizeof(header))) {
1211 ret = -EINVAL;
1212 goto read_failure;
1213 }
1214
Dave Watsonc46234e2018-03-22 10:10:35 -07001215 /* Linearize header to local buffer */
1216 ret = skb_copy_bits(skb, rxm->offset, header, tls_ctx->rx.prepend_size);
1217
1218 if (ret < 0)
1219 goto read_failure;
1220
1221 ctx->control = header[0];
1222
1223 data_len = ((header[4] & 0xFF) | (header[3] << 8));
1224
1225 cipher_overhead = tls_ctx->rx.tag_size + tls_ctx->rx.iv_size;
1226
1227 if (data_len > TLS_MAX_PAYLOAD_SIZE + cipher_overhead) {
1228 ret = -EMSGSIZE;
1229 goto read_failure;
1230 }
1231 if (data_len < cipher_overhead) {
1232 ret = -EBADMSG;
1233 goto read_failure;
1234 }
1235
1236 if (header[1] != TLS_VERSION_MINOR(tls_ctx->crypto_recv.version) ||
1237 header[2] != TLS_VERSION_MAJOR(tls_ctx->crypto_recv.version)) {
1238 ret = -EINVAL;
1239 goto read_failure;
1240 }
1241
Boris Pismenny4799ac82018-07-13 14:33:43 +03001242#ifdef CONFIG_TLS_DEVICE
1243 handle_device_resync(strp->sk, TCP_SKB_CB(skb)->seq + rxm->offset,
1244 *(u64*)tls_ctx->rx.rec_seq);
1245#endif
Dave Watsonc46234e2018-03-22 10:10:35 -07001246 return data_len + TLS_HEADER_SIZE;
1247
1248read_failure:
1249 tls_err_abort(strp->sk, ret);
1250
1251 return ret;
1252}
1253
1254static void tls_queue(struct strparser *strp, struct sk_buff *skb)
1255{
1256 struct tls_context *tls_ctx = tls_get_ctx(strp->sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001257 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001258
1259 ctx->decrypted = false;
1260
1261 ctx->recv_pkt = skb;
1262 strp_pause(strp);
1263
Vakul Gargad13acc2018-07-30 16:08:33 +05301264 ctx->saved_data_ready(strp->sk);
Dave Watsonc46234e2018-03-22 10:10:35 -07001265}
1266
1267static void tls_data_ready(struct sock *sk)
1268{
1269 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001270 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
Dave Watsonc46234e2018-03-22 10:10:35 -07001271
1272 strp_data_ready(&ctx->strp);
1273}
1274
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001275void tls_sw_free_resources_tx(struct sock *sk)
Dave Watson3c4d7552017-06-14 11:37:39 -07001276{
1277 struct tls_context *tls_ctx = tls_get_ctx(sk);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001278 struct tls_sw_context_tx *ctx = tls_sw_ctx_tx(tls_ctx);
Dave Watson3c4d7552017-06-14 11:37:39 -07001279
Vakul Garg201876b2018-07-24 16:54:27 +05301280 crypto_free_aead(ctx->aead_send);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001281 tls_free_both_sg(sk);
1282
1283 kfree(ctx);
1284}
1285
Boris Pismenny39f56e12018-07-13 14:33:41 +03001286void tls_sw_release_resources_rx(struct sock *sk)
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001287{
1288 struct tls_context *tls_ctx = tls_get_ctx(sk);
1289 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1290
Dave Watsonc46234e2018-03-22 10:10:35 -07001291 if (ctx->aead_recv) {
Vakul Garg201876b2018-07-24 16:54:27 +05301292 kfree_skb(ctx->recv_pkt);
1293 ctx->recv_pkt = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001294 crypto_free_aead(ctx->aead_recv);
1295 strp_stop(&ctx->strp);
1296 write_lock_bh(&sk->sk_callback_lock);
1297 sk->sk_data_ready = ctx->saved_data_ready;
1298 write_unlock_bh(&sk->sk_callback_lock);
1299 release_sock(sk);
1300 strp_done(&ctx->strp);
1301 lock_sock(sk);
1302 }
Boris Pismenny39f56e12018-07-13 14:33:41 +03001303}
1304
1305void tls_sw_free_resources_rx(struct sock *sk)
1306{
1307 struct tls_context *tls_ctx = tls_get_ctx(sk);
1308 struct tls_sw_context_rx *ctx = tls_sw_ctx_rx(tls_ctx);
1309
1310 tls_sw_release_resources_rx(sk);
Dave Watson3c4d7552017-06-14 11:37:39 -07001311
Dave Watson3c4d7552017-06-14 11:37:39 -07001312 kfree(ctx);
1313}
1314
Dave Watsonc46234e2018-03-22 10:10:35 -07001315int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx)
Dave Watson3c4d7552017-06-14 11:37:39 -07001316{
1317 char keyval[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
1318 struct tls_crypto_info *crypto_info;
1319 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001320 struct tls_sw_context_tx *sw_ctx_tx = NULL;
1321 struct tls_sw_context_rx *sw_ctx_rx = NULL;
Dave Watsonc46234e2018-03-22 10:10:35 -07001322 struct cipher_context *cctx;
1323 struct crypto_aead **aead;
1324 struct strp_callbacks cb;
Dave Watson3c4d7552017-06-14 11:37:39 -07001325 u16 nonce_size, tag_size, iv_size, rec_seq_size;
1326 char *iv, *rec_seq;
1327 int rc = 0;
1328
1329 if (!ctx) {
1330 rc = -EINVAL;
1331 goto out;
1332 }
1333
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001334 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001335 if (!ctx->priv_ctx_tx) {
1336 sw_ctx_tx = kzalloc(sizeof(*sw_ctx_tx), GFP_KERNEL);
1337 if (!sw_ctx_tx) {
1338 rc = -ENOMEM;
1339 goto out;
1340 }
1341 ctx->priv_ctx_tx = sw_ctx_tx;
1342 } else {
1343 sw_ctx_tx =
1344 (struct tls_sw_context_tx *)ctx->priv_ctx_tx;
Dave Watsonc46234e2018-03-22 10:10:35 -07001345 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001346 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001347 if (!ctx->priv_ctx_rx) {
1348 sw_ctx_rx = kzalloc(sizeof(*sw_ctx_rx), GFP_KERNEL);
1349 if (!sw_ctx_rx) {
1350 rc = -ENOMEM;
1351 goto out;
1352 }
1353 ctx->priv_ctx_rx = sw_ctx_rx;
1354 } else {
1355 sw_ctx_rx =
1356 (struct tls_sw_context_rx *)ctx->priv_ctx_rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001357 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001358 }
1359
Dave Watsonc46234e2018-03-22 10:10:35 -07001360 if (tx) {
Boris Pismennyb190a582018-07-13 14:33:42 +03001361 crypto_init_wait(&sw_ctx_tx->async_wait);
Dave Watsonc46234e2018-03-22 10:10:35 -07001362 crypto_info = &ctx->crypto_send;
1363 cctx = &ctx->tx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001364 aead = &sw_ctx_tx->aead_send;
Dave Watsonc46234e2018-03-22 10:10:35 -07001365 } else {
Boris Pismennyb190a582018-07-13 14:33:42 +03001366 crypto_init_wait(&sw_ctx_rx->async_wait);
Dave Watsonc46234e2018-03-22 10:10:35 -07001367 crypto_info = &ctx->crypto_recv;
1368 cctx = &ctx->rx;
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001369 aead = &sw_ctx_rx->aead_recv;
Dave Watsonc46234e2018-03-22 10:10:35 -07001370 }
1371
Dave Watson3c4d7552017-06-14 11:37:39 -07001372 switch (crypto_info->cipher_type) {
1373 case TLS_CIPHER_AES_GCM_128: {
1374 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1375 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
1376 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
1377 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
1378 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
1379 rec_seq =
1380 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
1381 gcm_128_info =
1382 (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
1383 break;
1384 }
1385 default:
1386 rc = -EINVAL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001387 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001388 }
1389
Kees Cookb16520f2018-04-10 17:52:34 -07001390 /* Sanity-check the IV size for stack allocations. */
Kees Cook3463e512018-06-25 16:55:05 -07001391 if (iv_size > MAX_IV_SIZE || nonce_size > MAX_IV_SIZE) {
Kees Cookb16520f2018-04-10 17:52:34 -07001392 rc = -EINVAL;
1393 goto free_priv;
1394 }
1395
Dave Watsonc46234e2018-03-22 10:10:35 -07001396 cctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
1397 cctx->tag_size = tag_size;
1398 cctx->overhead_size = cctx->prepend_size + cctx->tag_size;
1399 cctx->iv_size = iv_size;
1400 cctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
1401 GFP_KERNEL);
1402 if (!cctx->iv) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001403 rc = -ENOMEM;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001404 goto free_priv;
Dave Watson3c4d7552017-06-14 11:37:39 -07001405 }
Dave Watsonc46234e2018-03-22 10:10:35 -07001406 memcpy(cctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
1407 memcpy(cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
1408 cctx->rec_seq_size = rec_seq_size;
zhong jiang969d5092018-08-01 00:50:24 +08001409 cctx->rec_seq = kmemdup(rec_seq, rec_seq_size, GFP_KERNEL);
Dave Watsonc46234e2018-03-22 10:10:35 -07001410 if (!cctx->rec_seq) {
Dave Watson3c4d7552017-06-14 11:37:39 -07001411 rc = -ENOMEM;
1412 goto free_iv;
1413 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001414
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001415 if (sw_ctx_tx) {
1416 sg_init_table(sw_ctx_tx->sg_encrypted_data,
1417 ARRAY_SIZE(sw_ctx_tx->sg_encrypted_data));
1418 sg_init_table(sw_ctx_tx->sg_plaintext_data,
1419 ARRAY_SIZE(sw_ctx_tx->sg_plaintext_data));
Dave Watson3c4d7552017-06-14 11:37:39 -07001420
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001421 sg_init_table(sw_ctx_tx->sg_aead_in, 2);
1422 sg_set_buf(&sw_ctx_tx->sg_aead_in[0], sw_ctx_tx->aad_space,
1423 sizeof(sw_ctx_tx->aad_space));
1424 sg_unmark_end(&sw_ctx_tx->sg_aead_in[1]);
1425 sg_chain(sw_ctx_tx->sg_aead_in, 2,
1426 sw_ctx_tx->sg_plaintext_data);
1427 sg_init_table(sw_ctx_tx->sg_aead_out, 2);
1428 sg_set_buf(&sw_ctx_tx->sg_aead_out[0], sw_ctx_tx->aad_space,
1429 sizeof(sw_ctx_tx->aad_space));
1430 sg_unmark_end(&sw_ctx_tx->sg_aead_out[1]);
1431 sg_chain(sw_ctx_tx->sg_aead_out, 2,
1432 sw_ctx_tx->sg_encrypted_data);
Dave Watsonc46234e2018-03-22 10:10:35 -07001433 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001434
Dave Watsonc46234e2018-03-22 10:10:35 -07001435 if (!*aead) {
1436 *aead = crypto_alloc_aead("gcm(aes)", 0, 0);
1437 if (IS_ERR(*aead)) {
1438 rc = PTR_ERR(*aead);
1439 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07001440 goto free_rec_seq;
1441 }
1442 }
1443
1444 ctx->push_pending_record = tls_sw_push_pending_record;
1445
1446 memcpy(keyval, gcm_128_info->key, TLS_CIPHER_AES_GCM_128_KEY_SIZE);
1447
Dave Watsonc46234e2018-03-22 10:10:35 -07001448 rc = crypto_aead_setkey(*aead, keyval,
Dave Watson3c4d7552017-06-14 11:37:39 -07001449 TLS_CIPHER_AES_GCM_128_KEY_SIZE);
1450 if (rc)
1451 goto free_aead;
1452
Dave Watsonc46234e2018-03-22 10:10:35 -07001453 rc = crypto_aead_setauthsize(*aead, cctx->tag_size);
1454 if (rc)
1455 goto free_aead;
1456
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001457 if (sw_ctx_rx) {
Vakul Garg94524d82018-08-29 15:26:55 +05301458 (*aead)->reqsize = sizeof(struct decrypt_req_ctx);
1459
Dave Watsonc46234e2018-03-22 10:10:35 -07001460 /* Set up strparser */
1461 memset(&cb, 0, sizeof(cb));
1462 cb.rcv_msg = tls_queue;
1463 cb.parse_msg = tls_read_size;
1464
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001465 strp_init(&sw_ctx_rx->strp, sk, &cb);
Dave Watsonc46234e2018-03-22 10:10:35 -07001466
1467 write_lock_bh(&sk->sk_callback_lock);
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001468 sw_ctx_rx->saved_data_ready = sk->sk_data_ready;
Dave Watsonc46234e2018-03-22 10:10:35 -07001469 sk->sk_data_ready = tls_data_ready;
1470 write_unlock_bh(&sk->sk_callback_lock);
1471
Linus Torvaldsa11e1d42018-06-28 09:43:44 -07001472 sw_ctx_rx->sk_poll = sk->sk_socket->ops->poll;
Dave Watsonc46234e2018-03-22 10:10:35 -07001473
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001474 strp_check_rcv(&sw_ctx_rx->strp);
Dave Watsonc46234e2018-03-22 10:10:35 -07001475 }
1476
1477 goto out;
Dave Watson3c4d7552017-06-14 11:37:39 -07001478
1479free_aead:
Dave Watsonc46234e2018-03-22 10:10:35 -07001480 crypto_free_aead(*aead);
1481 *aead = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07001482free_rec_seq:
Dave Watsonc46234e2018-03-22 10:10:35 -07001483 kfree(cctx->rec_seq);
1484 cctx->rec_seq = NULL;
Dave Watson3c4d7552017-06-14 11:37:39 -07001485free_iv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001486 kfree(cctx->iv);
1487 cctx->iv = NULL;
Sabrina Dubrocacf6d43e2018-01-16 16:04:26 +01001488free_priv:
Boris Pismennyf66de3e2018-04-30 10:16:15 +03001489 if (tx) {
1490 kfree(ctx->priv_ctx_tx);
1491 ctx->priv_ctx_tx = NULL;
1492 } else {
1493 kfree(ctx->priv_ctx_rx);
1494 ctx->priv_ctx_rx = NULL;
1495 }
Dave Watson3c4d7552017-06-14 11:37:39 -07001496out:
1497 return rc;
1498}