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