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.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #include <linux/module.h>
38 #include <crypto/aead.h>
42 static void trim_sg(struct sock
*sk
, struct scatterlist
*sg
,
43 int *sg_num_elem
, unsigned int *sg_size
, int target_size
)
45 int i
= *sg_num_elem
- 1;
46 int trim
= *sg_size
- target_size
;
53 *sg_size
= target_size
;
54 while (trim
>= sg
[i
].length
) {
56 sk_mem_uncharge(sk
, sg
[i
].length
);
57 put_page(sg_page(&sg
[i
]));
65 sk_mem_uncharge(sk
, trim
);
71 static void trim_both_sgl(struct sock
*sk
, int target_size
)
73 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
74 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
76 trim_sg(sk
, ctx
->sg_plaintext_data
,
77 &ctx
->sg_plaintext_num_elem
,
78 &ctx
->sg_plaintext_size
,
82 target_size
+= tls_ctx
->overhead_size
;
84 trim_sg(sk
, ctx
->sg_encrypted_data
,
85 &ctx
->sg_encrypted_num_elem
,
86 &ctx
->sg_encrypted_size
,
90 static int alloc_sg(struct sock
*sk
, int len
, struct scatterlist
*sg
,
91 int *sg_num_elem
, unsigned int *sg_size
,
94 struct page_frag
*pfrag
;
95 unsigned int size
= *sg_size
;
96 int num_elem
= *sg_num_elem
, use
= 0, rc
= 0;
97 struct scatterlist
*sge
;
98 unsigned int orig_offset
;
101 pfrag
= sk_page_frag(sk
);
104 if (!sk_page_frag_refill(sk
, pfrag
)) {
109 use
= min_t(int, len
, pfrag
->size
- pfrag
->offset
);
111 if (!sk_wmem_schedule(sk
, use
)) {
116 sk_mem_charge(sk
, use
);
118 orig_offset
= pfrag
->offset
;
119 pfrag
->offset
+= use
;
121 sge
= sg
+ num_elem
- 1;
122 if (num_elem
> first_coalesce
&& sg_page(sg
) == pfrag
->page
&&
123 sg
->offset
+ sg
->length
== orig_offset
) {
128 sg_set_page(sge
, pfrag
->page
, use
, orig_offset
);
129 get_page(pfrag
->page
);
131 if (num_elem
== MAX_SKB_FRAGS
) {
143 *sg_num_elem
= num_elem
;
147 static int alloc_encrypted_sg(struct sock
*sk
, int len
)
149 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
150 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
153 rc
= alloc_sg(sk
, len
, ctx
->sg_encrypted_data
,
154 &ctx
->sg_encrypted_num_elem
, &ctx
->sg_encrypted_size
, 0);
159 static int alloc_plaintext_sg(struct sock
*sk
, int len
)
161 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
162 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
165 rc
= alloc_sg(sk
, len
, ctx
->sg_plaintext_data
,
166 &ctx
->sg_plaintext_num_elem
, &ctx
->sg_plaintext_size
,
167 tls_ctx
->pending_open_record_frags
);
172 static void free_sg(struct sock
*sk
, struct scatterlist
*sg
,
173 int *sg_num_elem
, unsigned int *sg_size
)
175 int i
, n
= *sg_num_elem
;
177 for (i
= 0; i
< n
; ++i
) {
178 sk_mem_uncharge(sk
, sg
[i
].length
);
179 put_page(sg_page(&sg
[i
]));
185 static void tls_free_both_sg(struct sock
*sk
)
187 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
188 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
190 free_sg(sk
, ctx
->sg_encrypted_data
, &ctx
->sg_encrypted_num_elem
,
191 &ctx
->sg_encrypted_size
);
193 free_sg(sk
, ctx
->sg_plaintext_data
, &ctx
->sg_plaintext_num_elem
,
194 &ctx
->sg_plaintext_size
);
197 static int tls_do_encryption(struct tls_context
*tls_ctx
,
198 struct tls_sw_context
*ctx
, size_t data_len
,
201 unsigned int req_size
= sizeof(struct aead_request
) +
202 crypto_aead_reqsize(ctx
->aead_send
);
203 struct aead_request
*aead_req
;
206 aead_req
= kzalloc(req_size
, flags
);
210 ctx
->sg_encrypted_data
[0].offset
+= tls_ctx
->prepend_size
;
211 ctx
->sg_encrypted_data
[0].length
-= tls_ctx
->prepend_size
;
213 aead_request_set_tfm(aead_req
, ctx
->aead_send
);
214 aead_request_set_ad(aead_req
, TLS_AAD_SPACE_SIZE
);
215 aead_request_set_crypt(aead_req
, ctx
->sg_aead_in
, ctx
->sg_aead_out
,
216 data_len
, tls_ctx
->iv
);
217 rc
= crypto_aead_encrypt(aead_req
);
219 ctx
->sg_encrypted_data
[0].offset
-= tls_ctx
->prepend_size
;
220 ctx
->sg_encrypted_data
[0].length
+= tls_ctx
->prepend_size
;
226 static int tls_push_record(struct sock
*sk
, int flags
,
227 unsigned char record_type
)
229 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
230 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
233 sg_mark_end(ctx
->sg_plaintext_data
+ ctx
->sg_plaintext_num_elem
- 1);
234 sg_mark_end(ctx
->sg_encrypted_data
+ ctx
->sg_encrypted_num_elem
- 1);
236 tls_make_aad(ctx
->aad_space
, ctx
->sg_plaintext_size
,
237 tls_ctx
->rec_seq
, tls_ctx
->rec_seq_size
,
240 tls_fill_prepend(tls_ctx
,
241 page_address(sg_page(&ctx
->sg_encrypted_data
[0])) +
242 ctx
->sg_encrypted_data
[0].offset
,
243 ctx
->sg_plaintext_size
, record_type
);
245 tls_ctx
->pending_open_record_frags
= 0;
246 set_bit(TLS_PENDING_CLOSED_RECORD
, &tls_ctx
->flags
);
248 rc
= tls_do_encryption(tls_ctx
, ctx
, ctx
->sg_plaintext_size
,
251 /* If we are called from write_space and
252 * we fail, we need to set this SOCK_NOSPACE
253 * to trigger another write_space in the future.
255 set_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
259 free_sg(sk
, ctx
->sg_plaintext_data
, &ctx
->sg_plaintext_num_elem
,
260 &ctx
->sg_plaintext_size
);
262 ctx
->sg_encrypted_num_elem
= 0;
263 ctx
->sg_encrypted_size
= 0;
265 /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */
266 rc
= tls_push_sg(sk
, tls_ctx
, ctx
->sg_encrypted_data
, 0, flags
);
267 if (rc
< 0 && rc
!= -EAGAIN
)
270 tls_advance_record_sn(sk
, tls_ctx
);
274 static int tls_sw_push_pending_record(struct sock
*sk
, int flags
)
276 return tls_push_record(sk
, flags
, TLS_RECORD_TYPE_DATA
);
279 static int zerocopy_from_iter(struct sock
*sk
, struct iov_iter
*from
,
282 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
283 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
284 struct page
*pages
[MAX_SKB_FRAGS
];
289 unsigned int size
= ctx
->sg_plaintext_size
;
290 int num_elem
= ctx
->sg_plaintext_num_elem
;
296 maxpages
= ARRAY_SIZE(ctx
->sg_plaintext_data
) - num_elem
;
301 copied
= iov_iter_get_pages(from
, pages
,
309 iov_iter_advance(from
, copied
);
314 use
= min_t(int, copied
, PAGE_SIZE
- offset
);
316 sg_set_page(&ctx
->sg_plaintext_data
[num_elem
],
317 pages
[i
], use
, offset
);
318 sg_unmark_end(&ctx
->sg_plaintext_data
[num_elem
]);
319 sk_mem_charge(sk
, use
);
330 ctx
->sg_plaintext_size
= size
;
331 ctx
->sg_plaintext_num_elem
= num_elem
;
335 static int memcopy_from_iter(struct sock
*sk
, struct iov_iter
*from
,
338 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
339 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
340 struct scatterlist
*sg
= ctx
->sg_plaintext_data
;
343 for (i
= tls_ctx
->pending_open_record_frags
;
344 i
< ctx
->sg_plaintext_num_elem
; ++i
) {
347 page_address(sg_page(&sg
[i
])) + sg
[i
].offset
,
348 copy
, from
) != copy
) {
354 ++tls_ctx
->pending_open_record_frags
;
364 int tls_sw_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t size
)
366 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
367 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
370 long timeo
= sock_sndtimeo(sk
, msg
->msg_flags
& MSG_DONTWAIT
);
371 bool eor
= !(msg
->msg_flags
& MSG_MORE
);
372 size_t try_to_copy
, copied
= 0;
373 unsigned char record_type
= TLS_RECORD_TYPE_DATA
;
378 if (msg
->msg_flags
& ~(MSG_MORE
| MSG_DONTWAIT
| MSG_NOSIGNAL
))
383 if (tls_complete_pending_work(sk
, tls_ctx
, msg
->msg_flags
, &timeo
))
386 if (unlikely(msg
->msg_controllen
)) {
387 ret
= tls_proccess_cmsg(sk
, msg
, &record_type
);
392 while (msg_data_left(msg
)) {
398 orig_size
= ctx
->sg_plaintext_size
;
400 try_to_copy
= msg_data_left(msg
);
401 record_room
= TLS_MAX_PAYLOAD_SIZE
- ctx
->sg_plaintext_size
;
402 if (try_to_copy
>= record_room
) {
403 try_to_copy
= record_room
;
407 required_size
= ctx
->sg_plaintext_size
+ try_to_copy
+
408 tls_ctx
->overhead_size
;
410 if (!sk_stream_memory_free(sk
))
411 goto wait_for_sndbuf
;
413 ret
= alloc_encrypted_sg(sk
, required_size
);
416 goto wait_for_memory
;
418 /* Adjust try_to_copy according to the amount that was
419 * actually allocated. The difference is due
420 * to max sg elements limit
422 try_to_copy
-= required_size
- ctx
->sg_encrypted_size
;
426 if (full_record
|| eor
) {
427 ret
= zerocopy_from_iter(sk
, &msg
->msg_iter
,
430 goto fallback_to_reg_send
;
432 copied
+= try_to_copy
;
433 ret
= tls_push_record(sk
, msg
->msg_flags
, record_type
);
439 copied
-= try_to_copy
;
440 fallback_to_reg_send
:
441 iov_iter_revert(&msg
->msg_iter
,
442 ctx
->sg_plaintext_size
- orig_size
);
443 trim_sg(sk
, ctx
->sg_plaintext_data
,
444 &ctx
->sg_plaintext_num_elem
,
445 &ctx
->sg_plaintext_size
,
449 required_size
= ctx
->sg_plaintext_size
+ try_to_copy
;
451 ret
= alloc_plaintext_sg(sk
, required_size
);
454 goto wait_for_memory
;
456 /* Adjust try_to_copy according to the amount that was
457 * actually allocated. The difference is due
458 * to max sg elements limit
460 try_to_copy
-= required_size
- ctx
->sg_plaintext_size
;
463 trim_sg(sk
, ctx
->sg_encrypted_data
,
464 &ctx
->sg_encrypted_num_elem
,
465 &ctx
->sg_encrypted_size
,
466 ctx
->sg_plaintext_size
+
467 tls_ctx
->overhead_size
);
470 ret
= memcopy_from_iter(sk
, &msg
->msg_iter
, try_to_copy
);
474 copied
+= try_to_copy
;
475 if (full_record
|| eor
) {
477 ret
= tls_push_record(sk
, msg
->msg_flags
, record_type
);
480 goto wait_for_memory
;
489 set_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
491 ret
= sk_stream_wait_memory(sk
, &timeo
);
494 trim_both_sgl(sk
, orig_size
);
498 if (tls_is_pending_closed_record(tls_ctx
))
501 if (ctx
->sg_encrypted_size
< required_size
)
502 goto alloc_encrypted
;
504 goto alloc_plaintext
;
508 ret
= sk_stream_error(sk
, msg
->msg_flags
, ret
);
511 return copied
? copied
: ret
;
514 int tls_sw_sendpage(struct sock
*sk
, struct page
*page
,
515 int offset
, size_t size
, int flags
)
517 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
518 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
520 long timeo
= sock_sndtimeo(sk
, flags
& MSG_DONTWAIT
);
522 size_t orig_size
= size
;
523 unsigned char record_type
= TLS_RECORD_TYPE_DATA
;
524 struct scatterlist
*sg
;
528 if (flags
& ~(MSG_MORE
| MSG_DONTWAIT
| MSG_NOSIGNAL
|
529 MSG_SENDPAGE_NOTLAST
))
532 /* No MSG_EOR from splice, only look at MSG_MORE */
533 eor
= !(flags
& (MSG_MORE
| MSG_SENDPAGE_NOTLAST
));
537 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE
, sk
);
539 if (tls_complete_pending_work(sk
, tls_ctx
, flags
, &timeo
))
542 /* Call the sk_stream functions to manage the sndbuf mem. */
544 size_t copy
, required_size
;
552 record_room
= TLS_MAX_PAYLOAD_SIZE
- ctx
->sg_plaintext_size
;
554 if (copy
>= record_room
) {
558 required_size
= ctx
->sg_plaintext_size
+ copy
+
559 tls_ctx
->overhead_size
;
561 if (!sk_stream_memory_free(sk
))
562 goto wait_for_sndbuf
;
564 ret
= alloc_encrypted_sg(sk
, required_size
);
567 goto wait_for_memory
;
569 /* Adjust copy according to the amount that was
570 * actually allocated. The difference is due
571 * to max sg elements limit
573 copy
-= required_size
- ctx
->sg_plaintext_size
;
578 sg
= ctx
->sg_plaintext_data
+ ctx
->sg_plaintext_num_elem
;
579 sg_set_page(sg
, page
, copy
, offset
);
580 ctx
->sg_plaintext_num_elem
++;
582 sk_mem_charge(sk
, copy
);
585 ctx
->sg_plaintext_size
+= copy
;
586 tls_ctx
->pending_open_record_frags
= ctx
->sg_plaintext_num_elem
;
588 if (full_record
|| eor
||
589 ctx
->sg_plaintext_num_elem
==
590 ARRAY_SIZE(ctx
->sg_plaintext_data
)) {
592 ret
= tls_push_record(sk
, flags
, record_type
);
595 goto wait_for_memory
;
602 set_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
);
604 ret
= sk_stream_wait_memory(sk
, &timeo
);
606 trim_both_sgl(sk
, ctx
->sg_plaintext_size
);
610 if (tls_is_pending_closed_record(tls_ctx
))
617 if (orig_size
> size
)
618 ret
= orig_size
- size
;
620 ret
= sk_stream_error(sk
, flags
, ret
);
626 void tls_sw_free_tx_resources(struct sock
*sk
)
628 struct tls_context
*tls_ctx
= tls_get_ctx(sk
);
629 struct tls_sw_context
*ctx
= tls_sw_ctx(tls_ctx
);
632 crypto_free_aead(ctx
->aead_send
);
634 tls_free_both_sg(sk
);
640 int tls_set_sw_offload(struct sock
*sk
, struct tls_context
*ctx
)
642 char keyval
[TLS_CIPHER_AES_GCM_128_KEY_SIZE
];
643 struct tls_crypto_info
*crypto_info
;
644 struct tls12_crypto_info_aes_gcm_128
*gcm_128_info
;
645 struct tls_sw_context
*sw_ctx
;
646 u16 nonce_size
, tag_size
, iv_size
, rec_seq_size
;
660 sw_ctx
= kzalloc(sizeof(*sw_ctx
), GFP_KERNEL
);
666 ctx
->priv_ctx
= (struct tls_offload_context
*)sw_ctx
;
668 crypto_info
= &ctx
->crypto_send
;
669 switch (crypto_info
->cipher_type
) {
670 case TLS_CIPHER_AES_GCM_128
: {
671 nonce_size
= TLS_CIPHER_AES_GCM_128_IV_SIZE
;
672 tag_size
= TLS_CIPHER_AES_GCM_128_TAG_SIZE
;
673 iv_size
= TLS_CIPHER_AES_GCM_128_IV_SIZE
;
674 iv
= ((struct tls12_crypto_info_aes_gcm_128
*)crypto_info
)->iv
;
675 rec_seq_size
= TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE
;
677 ((struct tls12_crypto_info_aes_gcm_128
*)crypto_info
)->rec_seq
;
679 (struct tls12_crypto_info_aes_gcm_128
*)crypto_info
;
687 ctx
->prepend_size
= TLS_HEADER_SIZE
+ nonce_size
;
688 ctx
->tag_size
= tag_size
;
689 ctx
->overhead_size
= ctx
->prepend_size
+ ctx
->tag_size
;
690 ctx
->iv_size
= iv_size
;
691 ctx
->iv
= kmalloc(iv_size
+ TLS_CIPHER_AES_GCM_128_SALT_SIZE
,
697 memcpy(ctx
->iv
, gcm_128_info
->salt
, TLS_CIPHER_AES_GCM_128_SALT_SIZE
);
698 memcpy(ctx
->iv
+ TLS_CIPHER_AES_GCM_128_SALT_SIZE
, iv
, iv_size
);
699 ctx
->rec_seq_size
= rec_seq_size
;
700 ctx
->rec_seq
= kmalloc(rec_seq_size
, GFP_KERNEL
);
705 memcpy(ctx
->rec_seq
, rec_seq
, rec_seq_size
);
707 sg_init_table(sw_ctx
->sg_encrypted_data
,
708 ARRAY_SIZE(sw_ctx
->sg_encrypted_data
));
709 sg_init_table(sw_ctx
->sg_plaintext_data
,
710 ARRAY_SIZE(sw_ctx
->sg_plaintext_data
));
712 sg_init_table(sw_ctx
->sg_aead_in
, 2);
713 sg_set_buf(&sw_ctx
->sg_aead_in
[0], sw_ctx
->aad_space
,
714 sizeof(sw_ctx
->aad_space
));
715 sg_unmark_end(&sw_ctx
->sg_aead_in
[1]);
716 sg_chain(sw_ctx
->sg_aead_in
, 2, sw_ctx
->sg_plaintext_data
);
717 sg_init_table(sw_ctx
->sg_aead_out
, 2);
718 sg_set_buf(&sw_ctx
->sg_aead_out
[0], sw_ctx
->aad_space
,
719 sizeof(sw_ctx
->aad_space
));
720 sg_unmark_end(&sw_ctx
->sg_aead_out
[1]);
721 sg_chain(sw_ctx
->sg_aead_out
, 2, sw_ctx
->sg_encrypted_data
);
723 if (!sw_ctx
->aead_send
) {
724 sw_ctx
->aead_send
= crypto_alloc_aead("gcm(aes)", 0, 0);
725 if (IS_ERR(sw_ctx
->aead_send
)) {
726 rc
= PTR_ERR(sw_ctx
->aead_send
);
727 sw_ctx
->aead_send
= NULL
;
732 ctx
->push_pending_record
= tls_sw_push_pending_record
;
734 memcpy(keyval
, gcm_128_info
->key
, TLS_CIPHER_AES_GCM_128_KEY_SIZE
);
736 rc
= crypto_aead_setkey(sw_ctx
->aead_send
, keyval
,
737 TLS_CIPHER_AES_GCM_128_KEY_SIZE
);
741 rc
= crypto_aead_setauthsize(sw_ctx
->aead_send
, ctx
->tag_size
);
746 crypto_free_aead(sw_ctx
->aead_send
);
747 sw_ctx
->aead_send
= NULL
;