2 * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3 * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/module.h>
37 #include <net/inet_common.h>
38 #include <linux/highmem.h>
39 #include <linux/netdevice.h>
40 #include <linux/sched/signal.h>
41 #include <linux/inetdevice.h>
42 #include <linux/inet_diag.h>
46 #include <net/tls_toe.h>
50 MODULE_AUTHOR("Mellanox Technologies");
51 MODULE_DESCRIPTION("Transport Layer Security Support");
52 MODULE_LICENSE("Dual BSD/GPL");
53 MODULE_ALIAS_TCP_ULP("tls");
61 #define CIPHER_SIZE_DESC(cipher) [cipher] = { \
62 .iv = cipher ## _IV_SIZE, \
63 .key = cipher ## _KEY_SIZE, \
64 .salt = cipher ## _SALT_SIZE, \
65 .tag = cipher ## _TAG_SIZE, \
66 .rec_seq = cipher ## _REC_SEQ_SIZE, \
69 const struct tls_cipher_size_desc tls_cipher_size_desc[] = {
70 CIPHER_SIZE_DESC(TLS_CIPHER_AES_GCM_128),
71 CIPHER_SIZE_DESC(TLS_CIPHER_AES_GCM_256),
72 CIPHER_SIZE_DESC(TLS_CIPHER_AES_CCM_128),
73 CIPHER_SIZE_DESC(TLS_CIPHER_CHACHA20_POLY1305),
74 CIPHER_SIZE_DESC(TLS_CIPHER_SM4_GCM),
75 CIPHER_SIZE_DESC(TLS_CIPHER_SM4_CCM),
78 static const struct proto *saved_tcpv6_prot;
79 static DEFINE_MUTEX(tcpv6_prot_mutex);
80 static const struct proto *saved_tcpv4_prot;
81 static DEFINE_MUTEX(tcpv4_prot_mutex);
82 static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
83 static struct proto_ops tls_proto_ops[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
84 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
85 const struct proto *base);
87 void update_sk_prot(struct sock *sk, struct tls_context *ctx)
89 int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
91 WRITE_ONCE(sk->sk_prot,
92 &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
93 WRITE_ONCE(sk->sk_socket->ops,
94 &tls_proto_ops[ip_ver][ctx->tx_conf][ctx->rx_conf]);
97 int wait_on_pending_writer(struct sock *sk, long *timeo)
100 DEFINE_WAIT_FUNC(wait, woken_wake_function);
102 add_wait_queue(sk_sleep(sk), &wait);
109 if (signal_pending(current)) {
110 rc = sock_intr_errno(*timeo);
114 if (sk_wait_event(sk, timeo,
115 !READ_ONCE(sk->sk_write_pending), &wait))
118 remove_wait_queue(sk_sleep(sk), &wait);
122 int tls_push_sg(struct sock *sk,
123 struct tls_context *ctx,
124 struct scatterlist *sg,
129 struct msghdr msg = {
130 .msg_flags = MSG_SPLICE_PAGES | flags,
135 int offset = first_offset;
137 size = sg->length - offset;
138 offset += sg->offset;
140 ctx->splicing_pages = true;
142 /* is sending application-limited? */
143 tcp_rate_check_app_limited(sk);
146 bvec_set_page(&bvec, p, size, offset);
147 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
149 ret = tcp_sendmsg_locked(sk, &msg, size);
158 offset -= sg->offset;
159 ctx->partially_sent_offset = offset;
160 ctx->partially_sent_record = (void *)sg;
161 ctx->splicing_pages = false;
166 sk_mem_uncharge(sk, sg->length);
175 ctx->splicing_pages = false;
180 static int tls_handle_open_record(struct sock *sk, int flags)
182 struct tls_context *ctx = tls_get_ctx(sk);
184 if (tls_is_pending_open_record(ctx))
185 return ctx->push_pending_record(sk, flags);
190 int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
191 unsigned char *record_type)
193 struct cmsghdr *cmsg;
196 for_each_cmsghdr(cmsg, msg) {
197 if (!CMSG_OK(msg, cmsg))
199 if (cmsg->cmsg_level != SOL_TLS)
202 switch (cmsg->cmsg_type) {
203 case TLS_SET_RECORD_TYPE:
204 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
207 if (msg->msg_flags & MSG_MORE)
210 rc = tls_handle_open_record(sk, msg->msg_flags);
214 *record_type = *(unsigned char *)CMSG_DATA(cmsg);
225 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
228 struct scatterlist *sg;
231 sg = ctx->partially_sent_record;
232 offset = ctx->partially_sent_offset;
234 ctx->partially_sent_record = NULL;
235 return tls_push_sg(sk, ctx, sg, offset, flags);
238 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx)
240 struct scatterlist *sg;
242 for (sg = ctx->partially_sent_record; sg; sg = sg_next(sg)) {
243 put_page(sg_page(sg));
244 sk_mem_uncharge(sk, sg->length);
246 ctx->partially_sent_record = NULL;
249 static void tls_write_space(struct sock *sk)
251 struct tls_context *ctx = tls_get_ctx(sk);
253 /* If splicing_pages call lower protocol write space handler
254 * to ensure we wake up any waiting operations there. For example
255 * if splicing pages where to call sk_wait_event.
257 if (ctx->splicing_pages) {
258 ctx->sk_write_space(sk);
262 #ifdef CONFIG_TLS_DEVICE
263 if (ctx->tx_conf == TLS_HW)
264 tls_device_write_space(sk, ctx);
267 tls_sw_write_space(sk, ctx);
269 ctx->sk_write_space(sk);
273 * tls_ctx_free() - free TLS ULP context
274 * @sk: socket to with @ctx is attached
275 * @ctx: TLS context structure
277 * Free TLS context. If @sk is %NULL caller guarantees that the socket
278 * to which @ctx was attached has no outstanding references.
280 void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
285 memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
286 memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
287 mutex_destroy(&ctx->tx_lock);
295 static void tls_sk_proto_cleanup(struct sock *sk,
296 struct tls_context *ctx, long timeo)
298 if (unlikely(sk->sk_write_pending) &&
299 !wait_on_pending_writer(sk, &timeo))
300 tls_handle_open_record(sk, 0);
302 /* We need these for tls_sw_fallback handling of other packets */
303 if (ctx->tx_conf == TLS_SW) {
304 kfree(ctx->tx.rec_seq);
306 tls_sw_release_resources_tx(sk);
307 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
308 } else if (ctx->tx_conf == TLS_HW) {
309 tls_device_free_resources_tx(sk);
310 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
313 if (ctx->rx_conf == TLS_SW) {
314 tls_sw_release_resources_rx(sk);
315 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
316 } else if (ctx->rx_conf == TLS_HW) {
317 tls_device_offload_cleanup_rx(sk);
318 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
322 static void tls_sk_proto_close(struct sock *sk, long timeout)
324 struct inet_connection_sock *icsk = inet_csk(sk);
325 struct tls_context *ctx = tls_get_ctx(sk);
326 long timeo = sock_sndtimeo(sk, 0);
329 if (ctx->tx_conf == TLS_SW)
330 tls_sw_cancel_work_tx(ctx);
333 free_ctx = ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW;
335 if (ctx->tx_conf != TLS_BASE || ctx->rx_conf != TLS_BASE)
336 tls_sk_proto_cleanup(sk, ctx, timeo);
338 write_lock_bh(&sk->sk_callback_lock);
340 rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
341 WRITE_ONCE(sk->sk_prot, ctx->sk_proto);
342 if (sk->sk_write_space == tls_write_space)
343 sk->sk_write_space = ctx->sk_write_space;
344 write_unlock_bh(&sk->sk_callback_lock);
346 if (ctx->tx_conf == TLS_SW)
347 tls_sw_free_ctx_tx(ctx);
348 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
349 tls_sw_strparser_done(ctx);
350 if (ctx->rx_conf == TLS_SW)
351 tls_sw_free_ctx_rx(ctx);
352 ctx->sk_proto->close(sk, timeout);
355 tls_ctx_free(sk, ctx);
358 static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
359 struct poll_table_struct *wait)
361 struct tls_sw_context_rx *ctx;
362 struct tls_context *tls_ctx;
363 struct sock *sk = sock->sk;
364 struct sk_psock *psock;
369 mask = tcp_poll(file, sock, wait);
371 state = inet_sk_state_load(sk);
372 shutdown = READ_ONCE(sk->sk_shutdown);
373 if (unlikely(state != TCP_ESTABLISHED || shutdown & RCV_SHUTDOWN))
376 tls_ctx = tls_get_ctx(sk);
377 ctx = tls_sw_ctx_rx(tls_ctx);
378 psock = sk_psock_get(sk);
380 if (skb_queue_empty_lockless(&ctx->rx_list) &&
381 !tls_strp_msg_ready(ctx) &&
382 sk_psock_queue_empty(psock))
383 mask &= ~(EPOLLIN | EPOLLRDNORM);
386 sk_psock_put(sk, psock);
391 static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
392 int __user *optlen, int tx)
395 struct tls_context *ctx = tls_get_ctx(sk);
396 struct tls_crypto_info *crypto_info;
397 struct cipher_context *cctx;
400 if (get_user(len, optlen))
403 if (!optval || (len < sizeof(*crypto_info))) {
413 /* get user crypto info */
415 crypto_info = &ctx->crypto_send.info;
418 crypto_info = &ctx->crypto_recv.info;
422 if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
427 if (len == sizeof(*crypto_info)) {
428 if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
433 switch (crypto_info->cipher_type) {
434 case TLS_CIPHER_AES_GCM_128: {
435 struct tls12_crypto_info_aes_gcm_128 *
436 crypto_info_aes_gcm_128 =
437 container_of(crypto_info,
438 struct tls12_crypto_info_aes_gcm_128,
441 if (len != sizeof(*crypto_info_aes_gcm_128)) {
445 memcpy(crypto_info_aes_gcm_128->iv,
446 cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
447 TLS_CIPHER_AES_GCM_128_IV_SIZE);
448 memcpy(crypto_info_aes_gcm_128->rec_seq, cctx->rec_seq,
449 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
450 if (copy_to_user(optval,
451 crypto_info_aes_gcm_128,
452 sizeof(*crypto_info_aes_gcm_128)))
456 case TLS_CIPHER_AES_GCM_256: {
457 struct tls12_crypto_info_aes_gcm_256 *
458 crypto_info_aes_gcm_256 =
459 container_of(crypto_info,
460 struct tls12_crypto_info_aes_gcm_256,
463 if (len != sizeof(*crypto_info_aes_gcm_256)) {
467 memcpy(crypto_info_aes_gcm_256->iv,
468 cctx->iv + TLS_CIPHER_AES_GCM_256_SALT_SIZE,
469 TLS_CIPHER_AES_GCM_256_IV_SIZE);
470 memcpy(crypto_info_aes_gcm_256->rec_seq, cctx->rec_seq,
471 TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
472 if (copy_to_user(optval,
473 crypto_info_aes_gcm_256,
474 sizeof(*crypto_info_aes_gcm_256)))
478 case TLS_CIPHER_AES_CCM_128: {
479 struct tls12_crypto_info_aes_ccm_128 *aes_ccm_128 =
480 container_of(crypto_info,
481 struct tls12_crypto_info_aes_ccm_128, info);
483 if (len != sizeof(*aes_ccm_128)) {
487 memcpy(aes_ccm_128->iv,
488 cctx->iv + TLS_CIPHER_AES_CCM_128_SALT_SIZE,
489 TLS_CIPHER_AES_CCM_128_IV_SIZE);
490 memcpy(aes_ccm_128->rec_seq, cctx->rec_seq,
491 TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
492 if (copy_to_user(optval, aes_ccm_128, sizeof(*aes_ccm_128)))
496 case TLS_CIPHER_CHACHA20_POLY1305: {
497 struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305 =
498 container_of(crypto_info,
499 struct tls12_crypto_info_chacha20_poly1305,
502 if (len != sizeof(*chacha20_poly1305)) {
506 memcpy(chacha20_poly1305->iv,
507 cctx->iv + TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE,
508 TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
509 memcpy(chacha20_poly1305->rec_seq, cctx->rec_seq,
510 TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
511 if (copy_to_user(optval, chacha20_poly1305,
512 sizeof(*chacha20_poly1305)))
516 case TLS_CIPHER_SM4_GCM: {
517 struct tls12_crypto_info_sm4_gcm *sm4_gcm_info =
518 container_of(crypto_info,
519 struct tls12_crypto_info_sm4_gcm, info);
521 if (len != sizeof(*sm4_gcm_info)) {
525 memcpy(sm4_gcm_info->iv,
526 cctx->iv + TLS_CIPHER_SM4_GCM_SALT_SIZE,
527 TLS_CIPHER_SM4_GCM_IV_SIZE);
528 memcpy(sm4_gcm_info->rec_seq, cctx->rec_seq,
529 TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE);
530 if (copy_to_user(optval, sm4_gcm_info, sizeof(*sm4_gcm_info)))
534 case TLS_CIPHER_SM4_CCM: {
535 struct tls12_crypto_info_sm4_ccm *sm4_ccm_info =
536 container_of(crypto_info,
537 struct tls12_crypto_info_sm4_ccm, info);
539 if (len != sizeof(*sm4_ccm_info)) {
543 memcpy(sm4_ccm_info->iv,
544 cctx->iv + TLS_CIPHER_SM4_CCM_SALT_SIZE,
545 TLS_CIPHER_SM4_CCM_IV_SIZE);
546 memcpy(sm4_ccm_info->rec_seq, cctx->rec_seq,
547 TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE);
548 if (copy_to_user(optval, sm4_ccm_info, sizeof(*sm4_ccm_info)))
552 case TLS_CIPHER_ARIA_GCM_128: {
553 struct tls12_crypto_info_aria_gcm_128 *
554 crypto_info_aria_gcm_128 =
555 container_of(crypto_info,
556 struct tls12_crypto_info_aria_gcm_128,
559 if (len != sizeof(*crypto_info_aria_gcm_128)) {
563 memcpy(crypto_info_aria_gcm_128->iv,
564 cctx->iv + TLS_CIPHER_ARIA_GCM_128_SALT_SIZE,
565 TLS_CIPHER_ARIA_GCM_128_IV_SIZE);
566 memcpy(crypto_info_aria_gcm_128->rec_seq, cctx->rec_seq,
567 TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE);
568 if (copy_to_user(optval,
569 crypto_info_aria_gcm_128,
570 sizeof(*crypto_info_aria_gcm_128)))
574 case TLS_CIPHER_ARIA_GCM_256: {
575 struct tls12_crypto_info_aria_gcm_256 *
576 crypto_info_aria_gcm_256 =
577 container_of(crypto_info,
578 struct tls12_crypto_info_aria_gcm_256,
581 if (len != sizeof(*crypto_info_aria_gcm_256)) {
585 memcpy(crypto_info_aria_gcm_256->iv,
586 cctx->iv + TLS_CIPHER_ARIA_GCM_256_SALT_SIZE,
587 TLS_CIPHER_ARIA_GCM_256_IV_SIZE);
588 memcpy(crypto_info_aria_gcm_256->rec_seq, cctx->rec_seq,
589 TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE);
590 if (copy_to_user(optval,
591 crypto_info_aria_gcm_256,
592 sizeof(*crypto_info_aria_gcm_256)))
604 static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
607 struct tls_context *ctx = tls_get_ctx(sk);
611 if (get_user(len, optlen))
614 if (len != sizeof(value))
617 value = ctx->zerocopy_sendfile;
618 if (copy_to_user(optval, &value, sizeof(value)))
624 static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
627 struct tls_context *ctx = tls_get_ctx(sk);
630 if (ctx->prot_info.version != TLS_1_3_VERSION)
633 if (get_user(len, optlen))
635 if (len < sizeof(value))
639 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
640 value = ctx->rx_no_pad;
644 if (put_user(sizeof(value), optlen))
646 if (copy_to_user(optval, &value, sizeof(value)))
652 static int do_tls_getsockopt(struct sock *sk, int optname,
653 char __user *optval, int __user *optlen)
662 rc = do_tls_getsockopt_conf(sk, optval, optlen,
665 case TLS_TX_ZEROCOPY_RO:
666 rc = do_tls_getsockopt_tx_zc(sk, optval, optlen);
668 case TLS_RX_EXPECT_NO_PAD:
669 rc = do_tls_getsockopt_no_pad(sk, optval, optlen);
681 static int tls_getsockopt(struct sock *sk, int level, int optname,
682 char __user *optval, int __user *optlen)
684 struct tls_context *ctx = tls_get_ctx(sk);
686 if (level != SOL_TLS)
687 return ctx->sk_proto->getsockopt(sk, level,
688 optname, optval, optlen);
690 return do_tls_getsockopt(sk, optname, optval, optlen);
693 static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
694 unsigned int optlen, int tx)
696 struct tls_crypto_info *crypto_info;
697 struct tls_crypto_info *alt_crypto_info;
698 struct tls_context *ctx = tls_get_ctx(sk);
703 if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info)))
707 crypto_info = &ctx->crypto_send.info;
708 alt_crypto_info = &ctx->crypto_recv.info;
710 crypto_info = &ctx->crypto_recv.info;
711 alt_crypto_info = &ctx->crypto_send.info;
714 /* Currently we don't support set crypto info more than one time */
715 if (TLS_CRYPTO_INFO_READY(crypto_info))
718 rc = copy_from_sockptr(crypto_info, optval, sizeof(*crypto_info));
721 goto err_crypto_info;
725 if (crypto_info->version != TLS_1_2_VERSION &&
726 crypto_info->version != TLS_1_3_VERSION) {
728 goto err_crypto_info;
731 /* Ensure that TLS version and ciphers are same in both directions */
732 if (TLS_CRYPTO_INFO_READY(alt_crypto_info)) {
733 if (alt_crypto_info->version != crypto_info->version ||
734 alt_crypto_info->cipher_type != crypto_info->cipher_type) {
736 goto err_crypto_info;
740 switch (crypto_info->cipher_type) {
741 case TLS_CIPHER_AES_GCM_128:
742 optsize = sizeof(struct tls12_crypto_info_aes_gcm_128);
744 case TLS_CIPHER_AES_GCM_256: {
745 optsize = sizeof(struct tls12_crypto_info_aes_gcm_256);
748 case TLS_CIPHER_AES_CCM_128:
749 optsize = sizeof(struct tls12_crypto_info_aes_ccm_128);
751 case TLS_CIPHER_CHACHA20_POLY1305:
752 optsize = sizeof(struct tls12_crypto_info_chacha20_poly1305);
754 case TLS_CIPHER_SM4_GCM:
755 optsize = sizeof(struct tls12_crypto_info_sm4_gcm);
757 case TLS_CIPHER_SM4_CCM:
758 optsize = sizeof(struct tls12_crypto_info_sm4_ccm);
760 case TLS_CIPHER_ARIA_GCM_128:
761 if (crypto_info->version != TLS_1_2_VERSION) {
763 goto err_crypto_info;
765 optsize = sizeof(struct tls12_crypto_info_aria_gcm_128);
767 case TLS_CIPHER_ARIA_GCM_256:
768 if (crypto_info->version != TLS_1_2_VERSION) {
770 goto err_crypto_info;
772 optsize = sizeof(struct tls12_crypto_info_aria_gcm_256);
776 goto err_crypto_info;
779 if (optlen != optsize) {
781 goto err_crypto_info;
784 rc = copy_from_sockptr_offset(crypto_info + 1, optval,
785 sizeof(*crypto_info),
786 optlen - sizeof(*crypto_info));
789 goto err_crypto_info;
793 rc = tls_set_device_offload(sk, ctx);
796 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXDEVICE);
797 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
799 rc = tls_set_sw_offload(sk, ctx, 1);
801 goto err_crypto_info;
802 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXSW);
803 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
807 rc = tls_set_device_offload_rx(sk, ctx);
810 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICE);
811 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
813 rc = tls_set_sw_offload(sk, ctx, 0);
815 goto err_crypto_info;
816 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXSW);
817 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
820 tls_sw_strparser_arm(sk, ctx);
827 update_sk_prot(sk, ctx);
829 ctx->sk_write_space = sk->sk_write_space;
830 sk->sk_write_space = tls_write_space;
832 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
834 tls_strp_check_rcv(&rx_ctx->strp);
839 memzero_explicit(crypto_info, sizeof(union tls_crypto_context));
843 static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,
846 struct tls_context *ctx = tls_get_ctx(sk);
849 if (sockptr_is_null(optval) || optlen != sizeof(value))
852 if (copy_from_sockptr(&value, optval, sizeof(value)))
858 ctx->zerocopy_sendfile = value;
863 static int do_tls_setsockopt_no_pad(struct sock *sk, sockptr_t optval,
866 struct tls_context *ctx = tls_get_ctx(sk);
870 if (ctx->prot_info.version != TLS_1_3_VERSION ||
871 sockptr_is_null(optval) || optlen < sizeof(val))
874 rc = copy_from_sockptr(&val, optval, sizeof(val));
879 rc = check_zeroed_sockptr(optval, sizeof(val), optlen - sizeof(val));
881 return rc == 0 ? -EINVAL : rc;
885 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW) {
886 ctx->rx_no_pad = val;
887 tls_update_rx_zc_capable(ctx);
895 static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
904 rc = do_tls_setsockopt_conf(sk, optval, optlen,
908 case TLS_TX_ZEROCOPY_RO:
910 rc = do_tls_setsockopt_tx_zc(sk, optval, optlen);
913 case TLS_RX_EXPECT_NO_PAD:
914 rc = do_tls_setsockopt_no_pad(sk, optval, optlen);
923 static int tls_setsockopt(struct sock *sk, int level, int optname,
924 sockptr_t optval, unsigned int optlen)
926 struct tls_context *ctx = tls_get_ctx(sk);
928 if (level != SOL_TLS)
929 return ctx->sk_proto->setsockopt(sk, level, optname, optval,
932 return do_tls_setsockopt(sk, optname, optval, optlen);
935 struct tls_context *tls_ctx_create(struct sock *sk)
937 struct inet_connection_sock *icsk = inet_csk(sk);
938 struct tls_context *ctx;
940 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
944 mutex_init(&ctx->tx_lock);
945 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
946 ctx->sk_proto = READ_ONCE(sk->sk_prot);
951 static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
952 const struct proto_ops *base)
954 ops[TLS_BASE][TLS_BASE] = *base;
956 ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
957 ops[TLS_SW ][TLS_BASE].splice_eof = tls_sw_splice_eof;
959 ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
960 ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
961 ops[TLS_BASE][TLS_SW ].poll = tls_sk_poll;
963 ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
964 ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
965 ops[TLS_SW ][TLS_SW ].poll = tls_sk_poll;
967 #ifdef CONFIG_TLS_DEVICE
968 ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
970 ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
972 ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
974 ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
976 ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
978 #ifdef CONFIG_TLS_TOE
979 ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
983 static void tls_build_proto(struct sock *sk)
985 int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
986 struct proto *prot = READ_ONCE(sk->sk_prot);
988 /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
989 if (ip_ver == TLSV6 &&
990 unlikely(prot != smp_load_acquire(&saved_tcpv6_prot))) {
991 mutex_lock(&tcpv6_prot_mutex);
992 if (likely(prot != saved_tcpv6_prot)) {
993 build_protos(tls_prots[TLSV6], prot);
994 build_proto_ops(tls_proto_ops[TLSV6],
996 smp_store_release(&saved_tcpv6_prot, prot);
998 mutex_unlock(&tcpv6_prot_mutex);
1001 if (ip_ver == TLSV4 &&
1002 unlikely(prot != smp_load_acquire(&saved_tcpv4_prot))) {
1003 mutex_lock(&tcpv4_prot_mutex);
1004 if (likely(prot != saved_tcpv4_prot)) {
1005 build_protos(tls_prots[TLSV4], prot);
1006 build_proto_ops(tls_proto_ops[TLSV4],
1007 sk->sk_socket->ops);
1008 smp_store_release(&saved_tcpv4_prot, prot);
1010 mutex_unlock(&tcpv4_prot_mutex);
1014 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
1015 const struct proto *base)
1017 prot[TLS_BASE][TLS_BASE] = *base;
1018 prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
1019 prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
1020 prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
1022 prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
1023 prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
1024 prot[TLS_SW][TLS_BASE].splice_eof = tls_sw_splice_eof;
1026 prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
1027 prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
1028 prot[TLS_BASE][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
1029 prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
1031 prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
1032 prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
1033 prot[TLS_SW][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
1034 prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
1036 #ifdef CONFIG_TLS_DEVICE
1037 prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
1038 prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
1039 prot[TLS_HW][TLS_BASE].splice_eof = tls_device_splice_eof;
1041 prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
1042 prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
1043 prot[TLS_HW][TLS_SW].splice_eof = tls_device_splice_eof;
1045 prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
1047 prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
1049 prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
1051 #ifdef CONFIG_TLS_TOE
1052 prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
1053 prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_toe_hash;
1054 prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_toe_unhash;
1058 static int tls_init(struct sock *sk)
1060 struct tls_context *ctx;
1063 tls_build_proto(sk);
1065 #ifdef CONFIG_TLS_TOE
1066 if (tls_toe_bypass(sk))
1070 /* The TLS ulp is currently supported only for TCP sockets
1071 * in ESTABLISHED state.
1072 * Supporting sockets in LISTEN state will require us
1073 * to modify the accept implementation to clone rather then
1074 * share the ulp context.
1076 if (sk->sk_state != TCP_ESTABLISHED)
1079 /* allocate tls context */
1080 write_lock_bh(&sk->sk_callback_lock);
1081 ctx = tls_ctx_create(sk);
1087 ctx->tx_conf = TLS_BASE;
1088 ctx->rx_conf = TLS_BASE;
1089 update_sk_prot(sk, ctx);
1091 write_unlock_bh(&sk->sk_callback_lock);
1095 static void tls_update(struct sock *sk, struct proto *p,
1096 void (*write_space)(struct sock *sk))
1098 struct tls_context *ctx;
1100 WARN_ON_ONCE(sk->sk_prot == p);
1102 ctx = tls_get_ctx(sk);
1104 ctx->sk_write_space = write_space;
1107 /* Pairs with lockless read in sk_clone_lock(). */
1108 WRITE_ONCE(sk->sk_prot, p);
1109 sk->sk_write_space = write_space;
1113 static u16 tls_user_config(struct tls_context *ctx, bool tx)
1115 u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
1119 return TLS_CONF_BASE;
1125 return TLS_CONF_HW_RECORD;
1130 static int tls_get_info(const struct sock *sk, struct sk_buff *skb)
1132 u16 version, cipher_type;
1133 struct tls_context *ctx;
1134 struct nlattr *start;
1137 start = nla_nest_start_noflag(skb, INET_ULP_INFO_TLS);
1142 ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
1147 version = ctx->prot_info.version;
1149 err = nla_put_u16(skb, TLS_INFO_VERSION, version);
1153 cipher_type = ctx->prot_info.cipher_type;
1155 err = nla_put_u16(skb, TLS_INFO_CIPHER, cipher_type);
1159 err = nla_put_u16(skb, TLS_INFO_TXCONF, tls_user_config(ctx, true));
1163 err = nla_put_u16(skb, TLS_INFO_RXCONF, tls_user_config(ctx, false));
1167 if (ctx->tx_conf == TLS_HW && ctx->zerocopy_sendfile) {
1168 err = nla_put_flag(skb, TLS_INFO_ZC_RO_TX);
1172 if (ctx->rx_no_pad) {
1173 err = nla_put_flag(skb, TLS_INFO_RX_NO_PAD);
1179 nla_nest_end(skb, start);
1184 nla_nest_cancel(skb, start);
1188 static size_t tls_get_info_size(const struct sock *sk)
1192 size += nla_total_size(0) + /* INET_ULP_INFO_TLS */
1193 nla_total_size(sizeof(u16)) + /* TLS_INFO_VERSION */
1194 nla_total_size(sizeof(u16)) + /* TLS_INFO_CIPHER */
1195 nla_total_size(sizeof(u16)) + /* TLS_INFO_RXCONF */
1196 nla_total_size(sizeof(u16)) + /* TLS_INFO_TXCONF */
1197 nla_total_size(0) + /* TLS_INFO_ZC_RO_TX */
1198 nla_total_size(0) + /* TLS_INFO_RX_NO_PAD */
1204 static int __net_init tls_init_net(struct net *net)
1208 net->mib.tls_statistics = alloc_percpu(struct linux_tls_mib);
1209 if (!net->mib.tls_statistics)
1212 err = tls_proc_init(net);
1214 goto err_free_stats;
1218 free_percpu(net->mib.tls_statistics);
1222 static void __net_exit tls_exit_net(struct net *net)
1225 free_percpu(net->mib.tls_statistics);
1228 static struct pernet_operations tls_proc_ops = {
1229 .init = tls_init_net,
1230 .exit = tls_exit_net,
1233 static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
1235 .owner = THIS_MODULE,
1237 .update = tls_update,
1238 .get_info = tls_get_info,
1239 .get_info_size = tls_get_info_size,
1242 static int __init tls_register(void)
1246 err = register_pernet_subsys(&tls_proc_ops);
1250 err = tls_strp_dev_init();
1254 err = tls_device_init();
1258 tcp_register_ulp(&tcp_tls_ulp_ops);
1262 tls_strp_dev_exit();
1264 unregister_pernet_subsys(&tls_proc_ops);
1268 static void __exit tls_unregister(void)
1270 tcp_unregister_ulp(&tcp_tls_ulp_ops);
1271 tls_strp_dev_exit();
1272 tls_device_cleanup();
1273 unregister_pernet_subsys(&tls_proc_ops);
1276 module_init(tls_register);
1277 module_exit(tls_unregister);