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 inline void tls_make_aad(int recv,
45 char *record_sequence,
46 int record_sequence_size,
47 unsigned char record_type)
49 memcpy(buf, record_sequence, record_sequence_size);
52 buf[9] = TLS_1_2_VERSION_MAJOR;
53 buf[10] = TLS_1_2_VERSION_MINOR;
55 buf[12] = size & 0xFF;
58 static void trim_sg(struct sock *sk, struct scatterlist *sg,
59 int *sg_num_elem, unsigned int *sg_size, int target_size)
61 int i = *sg_num_elem - 1;
62 int trim = *sg_size - target_size;
69 *sg_size = target_size;
70 while (trim >= sg[i].length) {
72 sk_mem_uncharge(sk, sg[i].length);
73 put_page(sg_page(&sg[i]));
81 sk_mem_uncharge(sk, trim);
87 static void trim_both_sgl(struct sock *sk, int target_size)
89 struct tls_context *tls_ctx = tls_get_ctx(sk);
90 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
92 trim_sg(sk, ctx->sg_plaintext_data,
93 &ctx->sg_plaintext_num_elem,
94 &ctx->sg_plaintext_size,
98 target_size += tls_ctx->overhead_size;
100 trim_sg(sk, ctx->sg_encrypted_data,
101 &ctx->sg_encrypted_num_elem,
102 &ctx->sg_encrypted_size,
106 static int alloc_sg(struct sock *sk, int len, struct scatterlist *sg,
107 int *sg_num_elem, unsigned int *sg_size,
110 struct page_frag *pfrag;
111 unsigned int size = *sg_size;
112 int num_elem = *sg_num_elem, use = 0, rc = 0;
113 struct scatterlist *sge;
114 unsigned int orig_offset;
117 pfrag = sk_page_frag(sk);
120 if (!sk_page_frag_refill(sk, pfrag)) {
125 use = min_t(int, len, pfrag->size - pfrag->offset);
127 if (!sk_wmem_schedule(sk, use)) {
132 sk_mem_charge(sk, use);
134 orig_offset = pfrag->offset;
135 pfrag->offset += use;
137 sge = sg + num_elem - 1;
138 if (num_elem > first_coalesce && sg_page(sg) == pfrag->page &&
139 sg->offset + sg->length == orig_offset) {
144 sg_set_page(sge, pfrag->page, use, orig_offset);
145 get_page(pfrag->page);
147 if (num_elem == MAX_SKB_FRAGS) {
159 *sg_num_elem = num_elem;
163 static int alloc_encrypted_sg(struct sock *sk, int len)
165 struct tls_context *tls_ctx = tls_get_ctx(sk);
166 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
169 rc = alloc_sg(sk, len, ctx->sg_encrypted_data,
170 &ctx->sg_encrypted_num_elem, &ctx->sg_encrypted_size, 0);
175 static int alloc_plaintext_sg(struct sock *sk, int len)
177 struct tls_context *tls_ctx = tls_get_ctx(sk);
178 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
181 rc = alloc_sg(sk, len, ctx->sg_plaintext_data,
182 &ctx->sg_plaintext_num_elem, &ctx->sg_plaintext_size,
183 tls_ctx->pending_open_record_frags);
188 static void free_sg(struct sock *sk, struct scatterlist *sg,
189 int *sg_num_elem, unsigned int *sg_size)
191 int i, n = *sg_num_elem;
193 for (i = 0; i < n; ++i) {
194 sk_mem_uncharge(sk, sg[i].length);
195 put_page(sg_page(&sg[i]));
201 static void tls_free_both_sg(struct sock *sk)
203 struct tls_context *tls_ctx = tls_get_ctx(sk);
204 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
206 free_sg(sk, ctx->sg_encrypted_data, &ctx->sg_encrypted_num_elem,
207 &ctx->sg_encrypted_size);
209 free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem,
210 &ctx->sg_plaintext_size);
213 static int tls_do_encryption(struct tls_context *tls_ctx,
214 struct tls_sw_context *ctx, size_t data_len,
217 unsigned int req_size = sizeof(struct aead_request) +
218 crypto_aead_reqsize(ctx->aead_send);
219 struct aead_request *aead_req;
222 aead_req = kmalloc(req_size, flags);
226 ctx->sg_encrypted_data[0].offset += tls_ctx->prepend_size;
227 ctx->sg_encrypted_data[0].length -= tls_ctx->prepend_size;
229 aead_request_set_tfm(aead_req, ctx->aead_send);
230 aead_request_set_ad(aead_req, TLS_AAD_SPACE_SIZE);
231 aead_request_set_crypt(aead_req, ctx->sg_aead_in, ctx->sg_aead_out,
232 data_len, tls_ctx->iv);
233 rc = crypto_aead_encrypt(aead_req);
235 ctx->sg_encrypted_data[0].offset -= tls_ctx->prepend_size;
236 ctx->sg_encrypted_data[0].length += tls_ctx->prepend_size;
242 static int tls_push_record(struct sock *sk, int flags,
243 unsigned char record_type)
245 struct tls_context *tls_ctx = tls_get_ctx(sk);
246 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
249 sg_mark_end(ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem - 1);
250 sg_mark_end(ctx->sg_encrypted_data + ctx->sg_encrypted_num_elem - 1);
252 tls_make_aad(0, ctx->aad_space, ctx->sg_plaintext_size,
253 tls_ctx->rec_seq, tls_ctx->rec_seq_size,
256 tls_fill_prepend(tls_ctx,
257 page_address(sg_page(&ctx->sg_encrypted_data[0])) +
258 ctx->sg_encrypted_data[0].offset,
259 ctx->sg_plaintext_size, record_type);
261 tls_ctx->pending_open_record_frags = 0;
262 set_bit(TLS_PENDING_CLOSED_RECORD, &tls_ctx->flags);
264 rc = tls_do_encryption(tls_ctx, ctx, ctx->sg_plaintext_size,
267 /* If we are called from write_space and
268 * we fail, we need to set this SOCK_NOSPACE
269 * to trigger another write_space in the future.
271 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
275 free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem,
276 &ctx->sg_plaintext_size);
278 ctx->sg_encrypted_num_elem = 0;
279 ctx->sg_encrypted_size = 0;
281 /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */
282 rc = tls_push_sg(sk, tls_ctx, ctx->sg_encrypted_data, 0, flags);
283 if (rc < 0 && rc != -EAGAIN)
286 tls_advance_record_sn(sk, tls_ctx);
290 static int tls_sw_push_pending_record(struct sock *sk, int flags)
292 return tls_push_record(sk, flags, TLS_RECORD_TYPE_DATA);
295 static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
298 struct tls_context *tls_ctx = tls_get_ctx(sk);
299 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
300 struct page *pages[MAX_SKB_FRAGS];
305 unsigned int size = ctx->sg_plaintext_size;
306 int num_elem = ctx->sg_plaintext_num_elem;
312 maxpages = ARRAY_SIZE(ctx->sg_plaintext_data) - num_elem;
317 copied = iov_iter_get_pages(from, pages,
325 iov_iter_advance(from, copied);
330 use = min_t(int, copied, PAGE_SIZE - offset);
332 sg_set_page(&ctx->sg_plaintext_data[num_elem],
333 pages[i], use, offset);
334 sg_unmark_end(&ctx->sg_plaintext_data[num_elem]);
335 sk_mem_charge(sk, use);
346 ctx->sg_plaintext_size = size;
347 ctx->sg_plaintext_num_elem = num_elem;
351 static int memcopy_from_iter(struct sock *sk, struct iov_iter *from,
354 struct tls_context *tls_ctx = tls_get_ctx(sk);
355 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
356 struct scatterlist *sg = ctx->sg_plaintext_data;
359 for (i = tls_ctx->pending_open_record_frags;
360 i < ctx->sg_plaintext_num_elem; ++i) {
363 page_address(sg_page(&sg[i])) + sg[i].offset,
364 copy, from) != copy) {
370 ++tls_ctx->pending_open_record_frags;
380 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
382 struct tls_context *tls_ctx = tls_get_ctx(sk);
383 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
386 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
387 bool eor = !(msg->msg_flags & MSG_MORE);
388 size_t try_to_copy, copied = 0;
389 unsigned char record_type = TLS_RECORD_TYPE_DATA;
394 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
399 if (tls_complete_pending_work(sk, tls_ctx, msg->msg_flags, &timeo))
402 if (unlikely(msg->msg_controllen)) {
403 ret = tls_proccess_cmsg(sk, msg, &record_type);
408 while (msg_data_left(msg)) {
414 orig_size = ctx->sg_plaintext_size;
416 try_to_copy = msg_data_left(msg);
417 record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size;
418 if (try_to_copy >= record_room) {
419 try_to_copy = record_room;
423 required_size = ctx->sg_plaintext_size + try_to_copy +
424 tls_ctx->overhead_size;
426 if (!sk_stream_memory_free(sk))
427 goto wait_for_sndbuf;
429 ret = alloc_encrypted_sg(sk, required_size);
432 goto wait_for_memory;
434 /* Adjust try_to_copy according to the amount that was
435 * actually allocated. The difference is due
436 * to max sg elements limit
438 try_to_copy -= required_size - ctx->sg_encrypted_size;
442 if (full_record || eor) {
443 ret = zerocopy_from_iter(sk, &msg->msg_iter,
446 goto fallback_to_reg_send;
448 copied += try_to_copy;
449 ret = tls_push_record(sk, msg->msg_flags, record_type);
455 copied -= try_to_copy;
456 fallback_to_reg_send:
457 iov_iter_revert(&msg->msg_iter,
458 ctx->sg_plaintext_size - orig_size);
459 trim_sg(sk, ctx->sg_plaintext_data,
460 &ctx->sg_plaintext_num_elem,
461 &ctx->sg_plaintext_size,
465 required_size = ctx->sg_plaintext_size + try_to_copy;
467 ret = alloc_plaintext_sg(sk, required_size);
470 goto wait_for_memory;
472 /* Adjust try_to_copy according to the amount that was
473 * actually allocated. The difference is due
474 * to max sg elements limit
476 try_to_copy -= required_size - ctx->sg_plaintext_size;
479 trim_sg(sk, ctx->sg_encrypted_data,
480 &ctx->sg_encrypted_num_elem,
481 &ctx->sg_encrypted_size,
482 ctx->sg_plaintext_size +
483 tls_ctx->overhead_size);
486 ret = memcopy_from_iter(sk, &msg->msg_iter, try_to_copy);
490 copied += try_to_copy;
491 if (full_record || eor) {
493 ret = tls_push_record(sk, msg->msg_flags, record_type);
496 goto wait_for_memory;
505 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
507 ret = sk_stream_wait_memory(sk, &timeo);
510 trim_both_sgl(sk, orig_size);
514 if (tls_is_pending_closed_record(tls_ctx))
517 if (ctx->sg_encrypted_size < required_size)
518 goto alloc_encrypted;
520 goto alloc_plaintext;
524 ret = sk_stream_error(sk, msg->msg_flags, ret);
527 return copied ? copied : ret;
530 int tls_sw_sendpage(struct sock *sk, struct page *page,
531 int offset, size_t size, int flags)
533 struct tls_context *tls_ctx = tls_get_ctx(sk);
534 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
536 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
538 size_t orig_size = size;
539 unsigned char record_type = TLS_RECORD_TYPE_DATA;
540 struct scatterlist *sg;
544 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
545 MSG_SENDPAGE_NOTLAST))
548 /* No MSG_EOR from splice, only look at MSG_MORE */
549 eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
553 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
555 if (tls_complete_pending_work(sk, tls_ctx, flags, &timeo))
558 /* Call the sk_stream functions to manage the sndbuf mem. */
560 size_t copy, required_size;
568 record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size;
570 if (copy >= record_room) {
574 required_size = ctx->sg_plaintext_size + copy +
575 tls_ctx->overhead_size;
577 if (!sk_stream_memory_free(sk))
578 goto wait_for_sndbuf;
580 ret = alloc_encrypted_sg(sk, required_size);
583 goto wait_for_memory;
585 /* Adjust copy according to the amount that was
586 * actually allocated. The difference is due
587 * to max sg elements limit
589 copy -= required_size - ctx->sg_plaintext_size;
594 sg = ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem;
595 sg_set_page(sg, page, copy, offset);
596 ctx->sg_plaintext_num_elem++;
598 sk_mem_charge(sk, copy);
601 ctx->sg_plaintext_size += copy;
602 tls_ctx->pending_open_record_frags = ctx->sg_plaintext_num_elem;
604 if (full_record || eor ||
605 ctx->sg_plaintext_num_elem ==
606 ARRAY_SIZE(ctx->sg_plaintext_data)) {
608 ret = tls_push_record(sk, flags, record_type);
611 goto wait_for_memory;
618 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
620 ret = sk_stream_wait_memory(sk, &timeo);
622 trim_both_sgl(sk, ctx->sg_plaintext_size);
626 if (tls_is_pending_closed_record(tls_ctx))
633 if (orig_size > size)
634 ret = orig_size - size;
636 ret = sk_stream_error(sk, flags, ret);
642 void tls_sw_free_resources(struct sock *sk)
644 struct tls_context *tls_ctx = tls_get_ctx(sk);
645 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
648 crypto_free_aead(ctx->aead_send);
650 tls_free_both_sg(sk);
655 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx)
657 char keyval[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
658 struct tls_crypto_info *crypto_info;
659 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
660 struct tls_sw_context *sw_ctx;
661 u16 nonce_size, tag_size, iv_size, rec_seq_size;
675 sw_ctx = kzalloc(sizeof(*sw_ctx), GFP_KERNEL);
681 ctx->priv_ctx = (struct tls_offload_context *)sw_ctx;
682 ctx->free_resources = tls_sw_free_resources;
684 crypto_info = &ctx->crypto_send;
685 switch (crypto_info->cipher_type) {
686 case TLS_CIPHER_AES_GCM_128: {
687 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
688 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
689 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
690 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
691 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
693 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
695 (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
703 ctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
704 ctx->tag_size = tag_size;
705 ctx->overhead_size = ctx->prepend_size + ctx->tag_size;
706 ctx->iv_size = iv_size;
707 ctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
713 memcpy(ctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
714 memcpy(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
715 ctx->rec_seq_size = rec_seq_size;
716 ctx->rec_seq = kmalloc(rec_seq_size, GFP_KERNEL);
721 memcpy(ctx->rec_seq, rec_seq, rec_seq_size);
723 sg_init_table(sw_ctx->sg_encrypted_data,
724 ARRAY_SIZE(sw_ctx->sg_encrypted_data));
725 sg_init_table(sw_ctx->sg_plaintext_data,
726 ARRAY_SIZE(sw_ctx->sg_plaintext_data));
728 sg_init_table(sw_ctx->sg_aead_in, 2);
729 sg_set_buf(&sw_ctx->sg_aead_in[0], sw_ctx->aad_space,
730 sizeof(sw_ctx->aad_space));
731 sg_unmark_end(&sw_ctx->sg_aead_in[1]);
732 sg_chain(sw_ctx->sg_aead_in, 2, sw_ctx->sg_plaintext_data);
733 sg_init_table(sw_ctx->sg_aead_out, 2);
734 sg_set_buf(&sw_ctx->sg_aead_out[0], sw_ctx->aad_space,
735 sizeof(sw_ctx->aad_space));
736 sg_unmark_end(&sw_ctx->sg_aead_out[1]);
737 sg_chain(sw_ctx->sg_aead_out, 2, sw_ctx->sg_encrypted_data);
739 if (!sw_ctx->aead_send) {
740 sw_ctx->aead_send = crypto_alloc_aead("gcm(aes)", 0, 0);
741 if (IS_ERR(sw_ctx->aead_send)) {
742 rc = PTR_ERR(sw_ctx->aead_send);
743 sw_ctx->aead_send = NULL;
748 ctx->push_pending_record = tls_sw_push_pending_record;
750 memcpy(keyval, gcm_128_info->key, TLS_CIPHER_AES_GCM_128_KEY_SIZE);
752 rc = crypto_aead_setkey(sw_ctx->aead_send, keyval,
753 TLS_CIPHER_AES_GCM_128_KEY_SIZE);
757 rc = crypto_aead_setauthsize(sw_ctx->aead_send, ctx->tag_size);
762 crypto_free_aead(sw_ctx->aead_send);
763 sw_ctx->aead_send = NULL;