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