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