2 * Copyright (c) 2016-2017, Mellanox Technologies. 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);
218 aead_request_set_callback(aead_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
219 crypto_req_done, &ctx->async_wait);
221 rc = crypto_wait_req(crypto_aead_encrypt(aead_req), &ctx->async_wait);
223 ctx->sg_encrypted_data[0].offset -= tls_ctx->prepend_size;
224 ctx->sg_encrypted_data[0].length += tls_ctx->prepend_size;
230 static int tls_push_record(struct sock *sk, int flags,
231 unsigned char record_type)
233 struct tls_context *tls_ctx = tls_get_ctx(sk);
234 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
237 sg_mark_end(ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem - 1);
238 sg_mark_end(ctx->sg_encrypted_data + ctx->sg_encrypted_num_elem - 1);
240 tls_make_aad(ctx->aad_space, ctx->sg_plaintext_size,
241 tls_ctx->rec_seq, tls_ctx->rec_seq_size,
244 tls_fill_prepend(tls_ctx,
245 page_address(sg_page(&ctx->sg_encrypted_data[0])) +
246 ctx->sg_encrypted_data[0].offset,
247 ctx->sg_plaintext_size, record_type);
249 tls_ctx->pending_open_record_frags = 0;
250 set_bit(TLS_PENDING_CLOSED_RECORD, &tls_ctx->flags);
252 rc = tls_do_encryption(tls_ctx, ctx, ctx->sg_plaintext_size,
255 /* If we are called from write_space and
256 * we fail, we need to set this SOCK_NOSPACE
257 * to trigger another write_space in the future.
259 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
263 free_sg(sk, ctx->sg_plaintext_data, &ctx->sg_plaintext_num_elem,
264 &ctx->sg_plaintext_size);
266 ctx->sg_encrypted_num_elem = 0;
267 ctx->sg_encrypted_size = 0;
269 /* Only pass through MSG_DONTWAIT and MSG_NOSIGNAL flags */
270 rc = tls_push_sg(sk, tls_ctx, ctx->sg_encrypted_data, 0, flags);
271 if (rc < 0 && rc != -EAGAIN)
274 tls_advance_record_sn(sk, tls_ctx);
278 static int tls_sw_push_pending_record(struct sock *sk, int flags)
280 return tls_push_record(sk, flags, TLS_RECORD_TYPE_DATA);
283 static int zerocopy_from_iter(struct sock *sk, struct iov_iter *from,
286 struct tls_context *tls_ctx = tls_get_ctx(sk);
287 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
288 struct page *pages[MAX_SKB_FRAGS];
293 unsigned int size = ctx->sg_plaintext_size;
294 int num_elem = ctx->sg_plaintext_num_elem;
300 maxpages = ARRAY_SIZE(ctx->sg_plaintext_data) - num_elem;
305 copied = iov_iter_get_pages(from, pages,
313 iov_iter_advance(from, copied);
318 use = min_t(int, copied, PAGE_SIZE - offset);
320 sg_set_page(&ctx->sg_plaintext_data[num_elem],
321 pages[i], use, offset);
322 sg_unmark_end(&ctx->sg_plaintext_data[num_elem]);
323 sk_mem_charge(sk, use);
334 ctx->sg_plaintext_size = size;
335 ctx->sg_plaintext_num_elem = num_elem;
339 static int memcopy_from_iter(struct sock *sk, struct iov_iter *from,
342 struct tls_context *tls_ctx = tls_get_ctx(sk);
343 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
344 struct scatterlist *sg = ctx->sg_plaintext_data;
347 for (i = tls_ctx->pending_open_record_frags;
348 i < ctx->sg_plaintext_num_elem; ++i) {
351 page_address(sg_page(&sg[i])) + sg[i].offset,
352 copy, from) != copy) {
358 ++tls_ctx->pending_open_record_frags;
368 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
370 struct tls_context *tls_ctx = tls_get_ctx(sk);
371 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
374 long timeo = sock_sndtimeo(sk, msg->msg_flags & MSG_DONTWAIT);
375 bool eor = !(msg->msg_flags & MSG_MORE);
376 size_t try_to_copy, copied = 0;
377 unsigned char record_type = TLS_RECORD_TYPE_DATA;
382 if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL))
387 if (tls_complete_pending_work(sk, tls_ctx, msg->msg_flags, &timeo))
390 if (unlikely(msg->msg_controllen)) {
391 ret = tls_proccess_cmsg(sk, msg, &record_type);
396 while (msg_data_left(msg)) {
402 orig_size = ctx->sg_plaintext_size;
404 try_to_copy = msg_data_left(msg);
405 record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size;
406 if (try_to_copy >= record_room) {
407 try_to_copy = record_room;
411 required_size = ctx->sg_plaintext_size + try_to_copy +
412 tls_ctx->overhead_size;
414 if (!sk_stream_memory_free(sk))
415 goto wait_for_sndbuf;
417 ret = alloc_encrypted_sg(sk, required_size);
420 goto wait_for_memory;
422 /* Adjust try_to_copy according to the amount that was
423 * actually allocated. The difference is due
424 * to max sg elements limit
426 try_to_copy -= required_size - ctx->sg_encrypted_size;
430 if (full_record || eor) {
431 ret = zerocopy_from_iter(sk, &msg->msg_iter,
434 goto fallback_to_reg_send;
436 copied += try_to_copy;
437 ret = tls_push_record(sk, msg->msg_flags, record_type);
443 copied -= try_to_copy;
444 fallback_to_reg_send:
445 iov_iter_revert(&msg->msg_iter,
446 ctx->sg_plaintext_size - orig_size);
447 trim_sg(sk, ctx->sg_plaintext_data,
448 &ctx->sg_plaintext_num_elem,
449 &ctx->sg_plaintext_size,
453 required_size = ctx->sg_plaintext_size + try_to_copy;
455 ret = alloc_plaintext_sg(sk, required_size);
458 goto wait_for_memory;
460 /* Adjust try_to_copy according to the amount that was
461 * actually allocated. The difference is due
462 * to max sg elements limit
464 try_to_copy -= required_size - ctx->sg_plaintext_size;
467 trim_sg(sk, ctx->sg_encrypted_data,
468 &ctx->sg_encrypted_num_elem,
469 &ctx->sg_encrypted_size,
470 ctx->sg_plaintext_size +
471 tls_ctx->overhead_size);
474 ret = memcopy_from_iter(sk, &msg->msg_iter, try_to_copy);
478 copied += try_to_copy;
479 if (full_record || eor) {
481 ret = tls_push_record(sk, msg->msg_flags, record_type);
484 goto wait_for_memory;
493 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
495 ret = sk_stream_wait_memory(sk, &timeo);
498 trim_both_sgl(sk, orig_size);
502 if (tls_is_pending_closed_record(tls_ctx))
505 if (ctx->sg_encrypted_size < required_size)
506 goto alloc_encrypted;
508 goto alloc_plaintext;
512 ret = sk_stream_error(sk, msg->msg_flags, ret);
515 return copied ? copied : ret;
518 int tls_sw_sendpage(struct sock *sk, struct page *page,
519 int offset, size_t size, int flags)
521 struct tls_context *tls_ctx = tls_get_ctx(sk);
522 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
524 long timeo = sock_sndtimeo(sk, flags & MSG_DONTWAIT);
526 size_t orig_size = size;
527 unsigned char record_type = TLS_RECORD_TYPE_DATA;
528 struct scatterlist *sg;
532 if (flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL |
533 MSG_SENDPAGE_NOTLAST))
536 /* No MSG_EOR from splice, only look at MSG_MORE */
537 eor = !(flags & (MSG_MORE | MSG_SENDPAGE_NOTLAST));
541 sk_clear_bit(SOCKWQ_ASYNC_NOSPACE, sk);
543 if (tls_complete_pending_work(sk, tls_ctx, flags, &timeo))
546 /* Call the sk_stream functions to manage the sndbuf mem. */
548 size_t copy, required_size;
556 record_room = TLS_MAX_PAYLOAD_SIZE - ctx->sg_plaintext_size;
558 if (copy >= record_room) {
562 required_size = ctx->sg_plaintext_size + copy +
563 tls_ctx->overhead_size;
565 if (!sk_stream_memory_free(sk))
566 goto wait_for_sndbuf;
568 ret = alloc_encrypted_sg(sk, required_size);
571 goto wait_for_memory;
573 /* Adjust copy according to the amount that was
574 * actually allocated. The difference is due
575 * to max sg elements limit
577 copy -= required_size - ctx->sg_plaintext_size;
582 sg = ctx->sg_plaintext_data + ctx->sg_plaintext_num_elem;
583 sg_set_page(sg, page, copy, offset);
586 ctx->sg_plaintext_num_elem++;
588 sk_mem_charge(sk, copy);
591 ctx->sg_plaintext_size += copy;
592 tls_ctx->pending_open_record_frags = ctx->sg_plaintext_num_elem;
594 if (full_record || eor ||
595 ctx->sg_plaintext_num_elem ==
596 ARRAY_SIZE(ctx->sg_plaintext_data)) {
598 ret = tls_push_record(sk, flags, record_type);
601 goto wait_for_memory;
608 set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
610 ret = sk_stream_wait_memory(sk, &timeo);
612 trim_both_sgl(sk, ctx->sg_plaintext_size);
616 if (tls_is_pending_closed_record(tls_ctx))
623 if (orig_size > size)
624 ret = orig_size - size;
626 ret = sk_stream_error(sk, flags, ret);
632 void tls_sw_free_tx_resources(struct sock *sk)
634 struct tls_context *tls_ctx = tls_get_ctx(sk);
635 struct tls_sw_context *ctx = tls_sw_ctx(tls_ctx);
638 crypto_free_aead(ctx->aead_send);
640 tls_free_both_sg(sk);
646 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx)
648 char keyval[TLS_CIPHER_AES_GCM_128_KEY_SIZE];
649 struct tls_crypto_info *crypto_info;
650 struct tls12_crypto_info_aes_gcm_128 *gcm_128_info;
651 struct tls_sw_context *sw_ctx;
652 u16 nonce_size, tag_size, iv_size, rec_seq_size;
666 sw_ctx = kzalloc(sizeof(*sw_ctx), GFP_KERNEL);
672 crypto_init_wait(&sw_ctx->async_wait);
674 ctx->priv_ctx = (struct tls_offload_context *)sw_ctx;
676 crypto_info = &ctx->crypto_send;
677 switch (crypto_info->cipher_type) {
678 case TLS_CIPHER_AES_GCM_128: {
679 nonce_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
680 tag_size = TLS_CIPHER_AES_GCM_128_TAG_SIZE;
681 iv_size = TLS_CIPHER_AES_GCM_128_IV_SIZE;
682 iv = ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->iv;
683 rec_seq_size = TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE;
685 ((struct tls12_crypto_info_aes_gcm_128 *)crypto_info)->rec_seq;
687 (struct tls12_crypto_info_aes_gcm_128 *)crypto_info;
695 ctx->prepend_size = TLS_HEADER_SIZE + nonce_size;
696 ctx->tag_size = tag_size;
697 ctx->overhead_size = ctx->prepend_size + ctx->tag_size;
698 ctx->iv_size = iv_size;
699 ctx->iv = kmalloc(iv_size + TLS_CIPHER_AES_GCM_128_SALT_SIZE, GFP_KERNEL);
704 memcpy(ctx->iv, gcm_128_info->salt, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
705 memcpy(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv, iv_size);
706 ctx->rec_seq_size = rec_seq_size;
707 ctx->rec_seq = kmalloc(rec_seq_size, GFP_KERNEL);
712 memcpy(ctx->rec_seq, rec_seq, rec_seq_size);
714 sg_init_table(sw_ctx->sg_encrypted_data,
715 ARRAY_SIZE(sw_ctx->sg_encrypted_data));
716 sg_init_table(sw_ctx->sg_plaintext_data,
717 ARRAY_SIZE(sw_ctx->sg_plaintext_data));
719 sg_init_table(sw_ctx->sg_aead_in, 2);
720 sg_set_buf(&sw_ctx->sg_aead_in[0], sw_ctx->aad_space,
721 sizeof(sw_ctx->aad_space));
722 sg_unmark_end(&sw_ctx->sg_aead_in[1]);
723 sg_chain(sw_ctx->sg_aead_in, 2, sw_ctx->sg_plaintext_data);
724 sg_init_table(sw_ctx->sg_aead_out, 2);
725 sg_set_buf(&sw_ctx->sg_aead_out[0], sw_ctx->aad_space,
726 sizeof(sw_ctx->aad_space));
727 sg_unmark_end(&sw_ctx->sg_aead_out[1]);
728 sg_chain(sw_ctx->sg_aead_out, 2, sw_ctx->sg_encrypted_data);
730 if (!sw_ctx->aead_send) {
731 sw_ctx->aead_send = crypto_alloc_aead("gcm(aes)", 0, 0);
732 if (IS_ERR(sw_ctx->aead_send)) {
733 rc = PTR_ERR(sw_ctx->aead_send);
734 sw_ctx->aead_send = NULL;
739 ctx->push_pending_record = tls_sw_push_pending_record;
741 memcpy(keyval, gcm_128_info->key, TLS_CIPHER_AES_GCM_128_KEY_SIZE);
743 rc = crypto_aead_setkey(sw_ctx->aead_send, keyval,
744 TLS_CIPHER_AES_GCM_128_KEY_SIZE);
748 rc = crypto_aead_setauthsize(sw_ctx->aead_send, ctx->tag_size);
753 crypto_free_aead(sw_ctx->aead_send);
754 sw_ctx->aead_send = NULL;
762 kfree(ctx->priv_ctx);
763 ctx->priv_ctx = NULL;