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