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;
143 msg.msg_flags = flags;
145 /* is sending application-limited? */
146 tcp_rate_check_app_limited(sk);
149 bvec_set_page(&bvec, p, size, offset);
150 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
152 ret = tcp_sendmsg_locked(sk, &msg, size);
161 offset -= sg->offset;
162 ctx->partially_sent_offset = offset;
163 ctx->partially_sent_record = (void *)sg;
164 ctx->splicing_pages = false;
169 sk_mem_uncharge(sk, sg->length);
178 ctx->splicing_pages = false;
183 static int tls_handle_open_record(struct sock *sk, int flags)
185 struct tls_context *ctx = tls_get_ctx(sk);
187 if (tls_is_pending_open_record(ctx))
188 return ctx->push_pending_record(sk, flags);
193 int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
194 unsigned char *record_type)
196 struct cmsghdr *cmsg;
199 for_each_cmsghdr(cmsg, msg) {
200 if (!CMSG_OK(msg, cmsg))
202 if (cmsg->cmsg_level != SOL_TLS)
205 switch (cmsg->cmsg_type) {
206 case TLS_SET_RECORD_TYPE:
207 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
210 if (msg->msg_flags & MSG_MORE)
213 rc = tls_handle_open_record(sk, msg->msg_flags);
217 *record_type = *(unsigned char *)CMSG_DATA(cmsg);
228 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
231 struct scatterlist *sg;
234 sg = ctx->partially_sent_record;
235 offset = ctx->partially_sent_offset;
237 ctx->partially_sent_record = NULL;
238 return tls_push_sg(sk, ctx, sg, offset, flags);
241 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx)
243 struct scatterlist *sg;
245 for (sg = ctx->partially_sent_record; sg; sg = sg_next(sg)) {
246 put_page(sg_page(sg));
247 sk_mem_uncharge(sk, sg->length);
249 ctx->partially_sent_record = NULL;
252 static void tls_write_space(struct sock *sk)
254 struct tls_context *ctx = tls_get_ctx(sk);
256 /* If splicing_pages call lower protocol write space handler
257 * to ensure we wake up any waiting operations there. For example
258 * if splicing pages where to call sk_wait_event.
260 if (ctx->splicing_pages) {
261 ctx->sk_write_space(sk);
265 #ifdef CONFIG_TLS_DEVICE
266 if (ctx->tx_conf == TLS_HW)
267 tls_device_write_space(sk, ctx);
270 tls_sw_write_space(sk, ctx);
272 ctx->sk_write_space(sk);
276 * tls_ctx_free() - free TLS ULP context
277 * @sk: socket to with @ctx is attached
278 * @ctx: TLS context structure
280 * Free TLS context. If @sk is %NULL caller guarantees that the socket
281 * to which @ctx was attached has no outstanding references.
283 void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
288 memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
289 memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
290 mutex_destroy(&ctx->tx_lock);
298 static void tls_sk_proto_cleanup(struct sock *sk,
299 struct tls_context *ctx, long timeo)
301 if (unlikely(sk->sk_write_pending) &&
302 !wait_on_pending_writer(sk, &timeo))
303 tls_handle_open_record(sk, 0);
305 /* We need these for tls_sw_fallback handling of other packets */
306 if (ctx->tx_conf == TLS_SW) {
307 kfree(ctx->tx.rec_seq);
309 tls_sw_release_resources_tx(sk);
310 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
311 } else if (ctx->tx_conf == TLS_HW) {
312 tls_device_free_resources_tx(sk);
313 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
316 if (ctx->rx_conf == TLS_SW) {
317 tls_sw_release_resources_rx(sk);
318 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
319 } else if (ctx->rx_conf == TLS_HW) {
320 tls_device_offload_cleanup_rx(sk);
321 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
325 static void tls_sk_proto_close(struct sock *sk, long timeout)
327 struct inet_connection_sock *icsk = inet_csk(sk);
328 struct tls_context *ctx = tls_get_ctx(sk);
329 long timeo = sock_sndtimeo(sk, 0);
332 if (ctx->tx_conf == TLS_SW)
333 tls_sw_cancel_work_tx(ctx);
336 free_ctx = ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW;
338 if (ctx->tx_conf != TLS_BASE || ctx->rx_conf != TLS_BASE)
339 tls_sk_proto_cleanup(sk, ctx, timeo);
341 write_lock_bh(&sk->sk_callback_lock);
343 rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
344 WRITE_ONCE(sk->sk_prot, ctx->sk_proto);
345 if (sk->sk_write_space == tls_write_space)
346 sk->sk_write_space = ctx->sk_write_space;
347 write_unlock_bh(&sk->sk_callback_lock);
349 if (ctx->tx_conf == TLS_SW)
350 tls_sw_free_ctx_tx(ctx);
351 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
352 tls_sw_strparser_done(ctx);
353 if (ctx->rx_conf == TLS_SW)
354 tls_sw_free_ctx_rx(ctx);
355 ctx->sk_proto->close(sk, timeout);
358 tls_ctx_free(sk, ctx);
361 static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
362 struct poll_table_struct *wait)
364 struct tls_sw_context_rx *ctx;
365 struct tls_context *tls_ctx;
366 struct sock *sk = sock->sk;
367 struct sk_psock *psock;
372 mask = tcp_poll(file, sock, wait);
374 state = inet_sk_state_load(sk);
375 shutdown = READ_ONCE(sk->sk_shutdown);
376 if (unlikely(state != TCP_ESTABLISHED || shutdown & RCV_SHUTDOWN))
379 tls_ctx = tls_get_ctx(sk);
380 ctx = tls_sw_ctx_rx(tls_ctx);
381 psock = sk_psock_get(sk);
383 if (skb_queue_empty_lockless(&ctx->rx_list) &&
384 !tls_strp_msg_ready(ctx) &&
385 sk_psock_queue_empty(psock))
386 mask &= ~(EPOLLIN | EPOLLRDNORM);
389 sk_psock_put(sk, psock);
394 static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
395 int __user *optlen, int tx)
398 struct tls_context *ctx = tls_get_ctx(sk);
399 struct tls_crypto_info *crypto_info;
400 struct cipher_context *cctx;
403 if (get_user(len, optlen))
406 if (!optval || (len < sizeof(*crypto_info))) {
416 /* get user crypto info */
418 crypto_info = &ctx->crypto_send.info;
421 crypto_info = &ctx->crypto_recv.info;
425 if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
430 if (len == sizeof(*crypto_info)) {
431 if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
436 switch (crypto_info->cipher_type) {
437 case TLS_CIPHER_AES_GCM_128: {
438 struct tls12_crypto_info_aes_gcm_128 *
439 crypto_info_aes_gcm_128 =
440 container_of(crypto_info,
441 struct tls12_crypto_info_aes_gcm_128,
444 if (len != sizeof(*crypto_info_aes_gcm_128)) {
448 memcpy(crypto_info_aes_gcm_128->iv,
449 cctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
450 TLS_CIPHER_AES_GCM_128_IV_SIZE);
451 memcpy(crypto_info_aes_gcm_128->rec_seq, cctx->rec_seq,
452 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
453 if (copy_to_user(optval,
454 crypto_info_aes_gcm_128,
455 sizeof(*crypto_info_aes_gcm_128)))
459 case TLS_CIPHER_AES_GCM_256: {
460 struct tls12_crypto_info_aes_gcm_256 *
461 crypto_info_aes_gcm_256 =
462 container_of(crypto_info,
463 struct tls12_crypto_info_aes_gcm_256,
466 if (len != sizeof(*crypto_info_aes_gcm_256)) {
470 memcpy(crypto_info_aes_gcm_256->iv,
471 cctx->iv + TLS_CIPHER_AES_GCM_256_SALT_SIZE,
472 TLS_CIPHER_AES_GCM_256_IV_SIZE);
473 memcpy(crypto_info_aes_gcm_256->rec_seq, cctx->rec_seq,
474 TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
475 if (copy_to_user(optval,
476 crypto_info_aes_gcm_256,
477 sizeof(*crypto_info_aes_gcm_256)))
481 case TLS_CIPHER_AES_CCM_128: {
482 struct tls12_crypto_info_aes_ccm_128 *aes_ccm_128 =
483 container_of(crypto_info,
484 struct tls12_crypto_info_aes_ccm_128, info);
486 if (len != sizeof(*aes_ccm_128)) {
490 memcpy(aes_ccm_128->iv,
491 cctx->iv + TLS_CIPHER_AES_CCM_128_SALT_SIZE,
492 TLS_CIPHER_AES_CCM_128_IV_SIZE);
493 memcpy(aes_ccm_128->rec_seq, cctx->rec_seq,
494 TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
495 if (copy_to_user(optval, aes_ccm_128, sizeof(*aes_ccm_128)))
499 case TLS_CIPHER_CHACHA20_POLY1305: {
500 struct tls12_crypto_info_chacha20_poly1305 *chacha20_poly1305 =
501 container_of(crypto_info,
502 struct tls12_crypto_info_chacha20_poly1305,
505 if (len != sizeof(*chacha20_poly1305)) {
509 memcpy(chacha20_poly1305->iv,
510 cctx->iv + TLS_CIPHER_CHACHA20_POLY1305_SALT_SIZE,
511 TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
512 memcpy(chacha20_poly1305->rec_seq, cctx->rec_seq,
513 TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
514 if (copy_to_user(optval, chacha20_poly1305,
515 sizeof(*chacha20_poly1305)))
519 case TLS_CIPHER_SM4_GCM: {
520 struct tls12_crypto_info_sm4_gcm *sm4_gcm_info =
521 container_of(crypto_info,
522 struct tls12_crypto_info_sm4_gcm, info);
524 if (len != sizeof(*sm4_gcm_info)) {
528 memcpy(sm4_gcm_info->iv,
529 cctx->iv + TLS_CIPHER_SM4_GCM_SALT_SIZE,
530 TLS_CIPHER_SM4_GCM_IV_SIZE);
531 memcpy(sm4_gcm_info->rec_seq, cctx->rec_seq,
532 TLS_CIPHER_SM4_GCM_REC_SEQ_SIZE);
533 if (copy_to_user(optval, sm4_gcm_info, sizeof(*sm4_gcm_info)))
537 case TLS_CIPHER_SM4_CCM: {
538 struct tls12_crypto_info_sm4_ccm *sm4_ccm_info =
539 container_of(crypto_info,
540 struct tls12_crypto_info_sm4_ccm, info);
542 if (len != sizeof(*sm4_ccm_info)) {
546 memcpy(sm4_ccm_info->iv,
547 cctx->iv + TLS_CIPHER_SM4_CCM_SALT_SIZE,
548 TLS_CIPHER_SM4_CCM_IV_SIZE);
549 memcpy(sm4_ccm_info->rec_seq, cctx->rec_seq,
550 TLS_CIPHER_SM4_CCM_REC_SEQ_SIZE);
551 if (copy_to_user(optval, sm4_ccm_info, sizeof(*sm4_ccm_info)))
555 case TLS_CIPHER_ARIA_GCM_128: {
556 struct tls12_crypto_info_aria_gcm_128 *
557 crypto_info_aria_gcm_128 =
558 container_of(crypto_info,
559 struct tls12_crypto_info_aria_gcm_128,
562 if (len != sizeof(*crypto_info_aria_gcm_128)) {
566 memcpy(crypto_info_aria_gcm_128->iv,
567 cctx->iv + TLS_CIPHER_ARIA_GCM_128_SALT_SIZE,
568 TLS_CIPHER_ARIA_GCM_128_IV_SIZE);
569 memcpy(crypto_info_aria_gcm_128->rec_seq, cctx->rec_seq,
570 TLS_CIPHER_ARIA_GCM_128_REC_SEQ_SIZE);
571 if (copy_to_user(optval,
572 crypto_info_aria_gcm_128,
573 sizeof(*crypto_info_aria_gcm_128)))
577 case TLS_CIPHER_ARIA_GCM_256: {
578 struct tls12_crypto_info_aria_gcm_256 *
579 crypto_info_aria_gcm_256 =
580 container_of(crypto_info,
581 struct tls12_crypto_info_aria_gcm_256,
584 if (len != sizeof(*crypto_info_aria_gcm_256)) {
588 memcpy(crypto_info_aria_gcm_256->iv,
589 cctx->iv + TLS_CIPHER_ARIA_GCM_256_SALT_SIZE,
590 TLS_CIPHER_ARIA_GCM_256_IV_SIZE);
591 memcpy(crypto_info_aria_gcm_256->rec_seq, cctx->rec_seq,
592 TLS_CIPHER_ARIA_GCM_256_REC_SEQ_SIZE);
593 if (copy_to_user(optval,
594 crypto_info_aria_gcm_256,
595 sizeof(*crypto_info_aria_gcm_256)))
607 static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
610 struct tls_context *ctx = tls_get_ctx(sk);
614 if (get_user(len, optlen))
617 if (len != sizeof(value))
620 value = ctx->zerocopy_sendfile;
621 if (copy_to_user(optval, &value, sizeof(value)))
627 static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
630 struct tls_context *ctx = tls_get_ctx(sk);
633 if (ctx->prot_info.version != TLS_1_3_VERSION)
636 if (get_user(len, optlen))
638 if (len < sizeof(value))
642 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
643 value = ctx->rx_no_pad;
647 if (put_user(sizeof(value), optlen))
649 if (copy_to_user(optval, &value, sizeof(value)))
655 static int do_tls_getsockopt(struct sock *sk, int optname,
656 char __user *optval, int __user *optlen)
665 rc = do_tls_getsockopt_conf(sk, optval, optlen,
668 case TLS_TX_ZEROCOPY_RO:
669 rc = do_tls_getsockopt_tx_zc(sk, optval, optlen);
671 case TLS_RX_EXPECT_NO_PAD:
672 rc = do_tls_getsockopt_no_pad(sk, optval, optlen);
684 static int tls_getsockopt(struct sock *sk, int level, int optname,
685 char __user *optval, int __user *optlen)
687 struct tls_context *ctx = tls_get_ctx(sk);
689 if (level != SOL_TLS)
690 return ctx->sk_proto->getsockopt(sk, level,
691 optname, optval, optlen);
693 return do_tls_getsockopt(sk, optname, optval, optlen);
696 static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
697 unsigned int optlen, int tx)
699 struct tls_crypto_info *crypto_info;
700 struct tls_crypto_info *alt_crypto_info;
701 struct tls_context *ctx = tls_get_ctx(sk);
706 if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info)))
710 crypto_info = &ctx->crypto_send.info;
711 alt_crypto_info = &ctx->crypto_recv.info;
713 crypto_info = &ctx->crypto_recv.info;
714 alt_crypto_info = &ctx->crypto_send.info;
717 /* Currently we don't support set crypto info more than one time */
718 if (TLS_CRYPTO_INFO_READY(crypto_info))
721 rc = copy_from_sockptr(crypto_info, optval, sizeof(*crypto_info));
724 goto err_crypto_info;
728 if (crypto_info->version != TLS_1_2_VERSION &&
729 crypto_info->version != TLS_1_3_VERSION) {
731 goto err_crypto_info;
734 /* Ensure that TLS version and ciphers are same in both directions */
735 if (TLS_CRYPTO_INFO_READY(alt_crypto_info)) {
736 if (alt_crypto_info->version != crypto_info->version ||
737 alt_crypto_info->cipher_type != crypto_info->cipher_type) {
739 goto err_crypto_info;
743 switch (crypto_info->cipher_type) {
744 case TLS_CIPHER_AES_GCM_128:
745 optsize = sizeof(struct tls12_crypto_info_aes_gcm_128);
747 case TLS_CIPHER_AES_GCM_256: {
748 optsize = sizeof(struct tls12_crypto_info_aes_gcm_256);
751 case TLS_CIPHER_AES_CCM_128:
752 optsize = sizeof(struct tls12_crypto_info_aes_ccm_128);
754 case TLS_CIPHER_CHACHA20_POLY1305:
755 optsize = sizeof(struct tls12_crypto_info_chacha20_poly1305);
757 case TLS_CIPHER_SM4_GCM:
758 optsize = sizeof(struct tls12_crypto_info_sm4_gcm);
760 case TLS_CIPHER_SM4_CCM:
761 optsize = sizeof(struct tls12_crypto_info_sm4_ccm);
763 case TLS_CIPHER_ARIA_GCM_128:
764 if (crypto_info->version != TLS_1_2_VERSION) {
766 goto err_crypto_info;
768 optsize = sizeof(struct tls12_crypto_info_aria_gcm_128);
770 case TLS_CIPHER_ARIA_GCM_256:
771 if (crypto_info->version != TLS_1_2_VERSION) {
773 goto err_crypto_info;
775 optsize = sizeof(struct tls12_crypto_info_aria_gcm_256);
779 goto err_crypto_info;
782 if (optlen != optsize) {
784 goto err_crypto_info;
787 rc = copy_from_sockptr_offset(crypto_info + 1, optval,
788 sizeof(*crypto_info),
789 optlen - sizeof(*crypto_info));
792 goto err_crypto_info;
796 rc = tls_set_device_offload(sk, ctx);
799 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXDEVICE);
800 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
802 rc = tls_set_sw_offload(sk, ctx, 1);
804 goto err_crypto_info;
805 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXSW);
806 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
810 rc = tls_set_device_offload_rx(sk, ctx);
813 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICE);
814 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
816 rc = tls_set_sw_offload(sk, ctx, 0);
818 goto err_crypto_info;
819 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXSW);
820 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
823 tls_sw_strparser_arm(sk, ctx);
830 update_sk_prot(sk, ctx);
832 ctx->sk_write_space = sk->sk_write_space;
833 sk->sk_write_space = tls_write_space;
835 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
837 tls_strp_check_rcv(&rx_ctx->strp);
842 memzero_explicit(crypto_info, sizeof(union tls_crypto_context));
846 static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,
849 struct tls_context *ctx = tls_get_ctx(sk);
852 if (sockptr_is_null(optval) || optlen != sizeof(value))
855 if (copy_from_sockptr(&value, optval, sizeof(value)))
861 ctx->zerocopy_sendfile = value;
866 static int do_tls_setsockopt_no_pad(struct sock *sk, sockptr_t optval,
869 struct tls_context *ctx = tls_get_ctx(sk);
873 if (ctx->prot_info.version != TLS_1_3_VERSION ||
874 sockptr_is_null(optval) || optlen < sizeof(val))
877 rc = copy_from_sockptr(&val, optval, sizeof(val));
882 rc = check_zeroed_sockptr(optval, sizeof(val), optlen - sizeof(val));
884 return rc == 0 ? -EINVAL : rc;
888 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW) {
889 ctx->rx_no_pad = val;
890 tls_update_rx_zc_capable(ctx);
898 static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
907 rc = do_tls_setsockopt_conf(sk, optval, optlen,
911 case TLS_TX_ZEROCOPY_RO:
913 rc = do_tls_setsockopt_tx_zc(sk, optval, optlen);
916 case TLS_RX_EXPECT_NO_PAD:
917 rc = do_tls_setsockopt_no_pad(sk, optval, optlen);
926 static int tls_setsockopt(struct sock *sk, int level, int optname,
927 sockptr_t optval, unsigned int optlen)
929 struct tls_context *ctx = tls_get_ctx(sk);
931 if (level != SOL_TLS)
932 return ctx->sk_proto->setsockopt(sk, level, optname, optval,
935 return do_tls_setsockopt(sk, optname, optval, optlen);
938 struct tls_context *tls_ctx_create(struct sock *sk)
940 struct inet_connection_sock *icsk = inet_csk(sk);
941 struct tls_context *ctx;
943 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
947 mutex_init(&ctx->tx_lock);
948 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
949 ctx->sk_proto = READ_ONCE(sk->sk_prot);
954 static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
955 const struct proto_ops *base)
957 ops[TLS_BASE][TLS_BASE] = *base;
959 ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
960 ops[TLS_SW ][TLS_BASE].splice_eof = tls_sw_splice_eof;
962 ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
963 ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
964 ops[TLS_BASE][TLS_SW ].poll = tls_sk_poll;
966 ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
967 ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
968 ops[TLS_SW ][TLS_SW ].poll = tls_sk_poll;
970 #ifdef CONFIG_TLS_DEVICE
971 ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
973 ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
975 ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
977 ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
979 ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
981 #ifdef CONFIG_TLS_TOE
982 ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
986 static void tls_build_proto(struct sock *sk)
988 int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
989 struct proto *prot = READ_ONCE(sk->sk_prot);
991 /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
992 if (ip_ver == TLSV6 &&
993 unlikely(prot != smp_load_acquire(&saved_tcpv6_prot))) {
994 mutex_lock(&tcpv6_prot_mutex);
995 if (likely(prot != saved_tcpv6_prot)) {
996 build_protos(tls_prots[TLSV6], prot);
997 build_proto_ops(tls_proto_ops[TLSV6],
999 smp_store_release(&saved_tcpv6_prot, prot);
1001 mutex_unlock(&tcpv6_prot_mutex);
1004 if (ip_ver == TLSV4 &&
1005 unlikely(prot != smp_load_acquire(&saved_tcpv4_prot))) {
1006 mutex_lock(&tcpv4_prot_mutex);
1007 if (likely(prot != saved_tcpv4_prot)) {
1008 build_protos(tls_prots[TLSV4], prot);
1009 build_proto_ops(tls_proto_ops[TLSV4],
1010 sk->sk_socket->ops);
1011 smp_store_release(&saved_tcpv4_prot, prot);
1013 mutex_unlock(&tcpv4_prot_mutex);
1017 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
1018 const struct proto *base)
1020 prot[TLS_BASE][TLS_BASE] = *base;
1021 prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
1022 prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
1023 prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
1025 prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
1026 prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
1027 prot[TLS_SW][TLS_BASE].splice_eof = tls_sw_splice_eof;
1029 prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
1030 prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
1031 prot[TLS_BASE][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
1032 prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
1034 prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
1035 prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
1036 prot[TLS_SW][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
1037 prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
1039 #ifdef CONFIG_TLS_DEVICE
1040 prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
1041 prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
1042 prot[TLS_HW][TLS_BASE].splice_eof = tls_device_splice_eof;
1044 prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
1045 prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
1046 prot[TLS_HW][TLS_SW].splice_eof = tls_device_splice_eof;
1048 prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
1050 prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
1052 prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
1054 #ifdef CONFIG_TLS_TOE
1055 prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
1056 prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_toe_hash;
1057 prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_toe_unhash;
1061 static int tls_init(struct sock *sk)
1063 struct tls_context *ctx;
1066 tls_build_proto(sk);
1068 #ifdef CONFIG_TLS_TOE
1069 if (tls_toe_bypass(sk))
1073 /* The TLS ulp is currently supported only for TCP sockets
1074 * in ESTABLISHED state.
1075 * Supporting sockets in LISTEN state will require us
1076 * to modify the accept implementation to clone rather then
1077 * share the ulp context.
1079 if (sk->sk_state != TCP_ESTABLISHED)
1082 /* allocate tls context */
1083 write_lock_bh(&sk->sk_callback_lock);
1084 ctx = tls_ctx_create(sk);
1090 ctx->tx_conf = TLS_BASE;
1091 ctx->rx_conf = TLS_BASE;
1092 update_sk_prot(sk, ctx);
1094 write_unlock_bh(&sk->sk_callback_lock);
1098 static void tls_update(struct sock *sk, struct proto *p,
1099 void (*write_space)(struct sock *sk))
1101 struct tls_context *ctx;
1103 WARN_ON_ONCE(sk->sk_prot == p);
1105 ctx = tls_get_ctx(sk);
1107 ctx->sk_write_space = write_space;
1110 /* Pairs with lockless read in sk_clone_lock(). */
1111 WRITE_ONCE(sk->sk_prot, p);
1112 sk->sk_write_space = write_space;
1116 static u16 tls_user_config(struct tls_context *ctx, bool tx)
1118 u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
1122 return TLS_CONF_BASE;
1128 return TLS_CONF_HW_RECORD;
1133 static int tls_get_info(const struct sock *sk, struct sk_buff *skb)
1135 u16 version, cipher_type;
1136 struct tls_context *ctx;
1137 struct nlattr *start;
1140 start = nla_nest_start_noflag(skb, INET_ULP_INFO_TLS);
1145 ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
1150 version = ctx->prot_info.version;
1152 err = nla_put_u16(skb, TLS_INFO_VERSION, version);
1156 cipher_type = ctx->prot_info.cipher_type;
1158 err = nla_put_u16(skb, TLS_INFO_CIPHER, cipher_type);
1162 err = nla_put_u16(skb, TLS_INFO_TXCONF, tls_user_config(ctx, true));
1166 err = nla_put_u16(skb, TLS_INFO_RXCONF, tls_user_config(ctx, false));
1170 if (ctx->tx_conf == TLS_HW && ctx->zerocopy_sendfile) {
1171 err = nla_put_flag(skb, TLS_INFO_ZC_RO_TX);
1175 if (ctx->rx_no_pad) {
1176 err = nla_put_flag(skb, TLS_INFO_RX_NO_PAD);
1182 nla_nest_end(skb, start);
1187 nla_nest_cancel(skb, start);
1191 static size_t tls_get_info_size(const struct sock *sk)
1195 size += nla_total_size(0) + /* INET_ULP_INFO_TLS */
1196 nla_total_size(sizeof(u16)) + /* TLS_INFO_VERSION */
1197 nla_total_size(sizeof(u16)) + /* TLS_INFO_CIPHER */
1198 nla_total_size(sizeof(u16)) + /* TLS_INFO_RXCONF */
1199 nla_total_size(sizeof(u16)) + /* TLS_INFO_TXCONF */
1200 nla_total_size(0) + /* TLS_INFO_ZC_RO_TX */
1201 nla_total_size(0) + /* TLS_INFO_RX_NO_PAD */
1207 static int __net_init tls_init_net(struct net *net)
1211 net->mib.tls_statistics = alloc_percpu(struct linux_tls_mib);
1212 if (!net->mib.tls_statistics)
1215 err = tls_proc_init(net);
1217 goto err_free_stats;
1221 free_percpu(net->mib.tls_statistics);
1225 static void __net_exit tls_exit_net(struct net *net)
1228 free_percpu(net->mib.tls_statistics);
1231 static struct pernet_operations tls_proc_ops = {
1232 .init = tls_init_net,
1233 .exit = tls_exit_net,
1236 static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
1238 .owner = THIS_MODULE,
1240 .update = tls_update,
1241 .get_info = tls_get_info,
1242 .get_info_size = tls_get_info_size,
1245 static int __init tls_register(void)
1249 err = register_pernet_subsys(&tls_proc_ops);
1253 err = tls_strp_dev_init();
1257 err = tls_device_init();
1261 tcp_register_ulp(&tcp_tls_ulp_ops);
1265 tls_strp_dev_exit();
1267 unregister_pernet_subsys(&tls_proc_ops);
1271 static void __exit tls_unregister(void)
1273 tcp_unregister_ulp(&tcp_tls_ulp_ops);
1274 tls_strp_dev_exit();
1275 tls_device_cleanup();
1276 unregister_pernet_subsys(&tls_proc_ops);
1279 module_init(tls_register);
1280 module_exit(tls_unregister);