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