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 CHECK_CIPHER_DESC(cipher,ci) \
62 static_assert(cipher ## _IV_SIZE <= MAX_IV_SIZE); \
63 static_assert(cipher ## _REC_SEQ_SIZE <= TLS_MAX_REC_SEQ_SIZE); \
64 static_assert(cipher ## _TAG_SIZE == TLS_TAG_SIZE); \
65 static_assert(sizeof_field(struct ci, iv) == cipher ## _IV_SIZE); \
66 static_assert(sizeof_field(struct ci, key) == cipher ## _KEY_SIZE); \
67 static_assert(sizeof_field(struct ci, salt) == cipher ## _SALT_SIZE); \
68 static_assert(sizeof_field(struct ci, rec_seq) == cipher ## _REC_SEQ_SIZE);
70 #define __CIPHER_DESC(ci) \
71 .iv_offset = offsetof(struct ci, iv), \
72 .key_offset = offsetof(struct ci, key), \
73 .salt_offset = offsetof(struct ci, salt), \
74 .rec_seq_offset = offsetof(struct ci, rec_seq), \
75 .crypto_info = sizeof(struct ci)
77 #define CIPHER_DESC(cipher,ci,algname,_offloadable) [cipher - TLS_CIPHER_MIN] = { \
78 .nonce = cipher ## _IV_SIZE, \
79 .iv = cipher ## _IV_SIZE, \
80 .key = cipher ## _KEY_SIZE, \
81 .salt = cipher ## _SALT_SIZE, \
82 .tag = cipher ## _TAG_SIZE, \
83 .rec_seq = cipher ## _REC_SEQ_SIZE, \
84 .cipher_name = algname, \
85 .offloadable = _offloadable, \
89 #define CIPHER_DESC_NONCE0(cipher,ci,algname,_offloadable) [cipher - TLS_CIPHER_MIN] = { \
91 .iv = cipher ## _IV_SIZE, \
92 .key = cipher ## _KEY_SIZE, \
93 .salt = cipher ## _SALT_SIZE, \
94 .tag = cipher ## _TAG_SIZE, \
95 .rec_seq = cipher ## _REC_SEQ_SIZE, \
96 .cipher_name = algname, \
97 .offloadable = _offloadable, \
101 const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MIN] = {
102 CIPHER_DESC(TLS_CIPHER_AES_GCM_128, tls12_crypto_info_aes_gcm_128, "gcm(aes)", true),
103 CIPHER_DESC(TLS_CIPHER_AES_GCM_256, tls12_crypto_info_aes_gcm_256, "gcm(aes)", true),
104 CIPHER_DESC(TLS_CIPHER_AES_CCM_128, tls12_crypto_info_aes_ccm_128, "ccm(aes)", false),
105 CIPHER_DESC_NONCE0(TLS_CIPHER_CHACHA20_POLY1305, tls12_crypto_info_chacha20_poly1305, "rfc7539(chacha20,poly1305)", false),
106 CIPHER_DESC(TLS_CIPHER_SM4_GCM, tls12_crypto_info_sm4_gcm, "gcm(sm4)", false),
107 CIPHER_DESC(TLS_CIPHER_SM4_CCM, tls12_crypto_info_sm4_ccm, "ccm(sm4)", false),
108 CIPHER_DESC(TLS_CIPHER_ARIA_GCM_128, tls12_crypto_info_aria_gcm_128, "gcm(aria)", false),
109 CIPHER_DESC(TLS_CIPHER_ARIA_GCM_256, tls12_crypto_info_aria_gcm_256, "gcm(aria)", false),
112 CHECK_CIPHER_DESC(TLS_CIPHER_AES_GCM_128, tls12_crypto_info_aes_gcm_128);
113 CHECK_CIPHER_DESC(TLS_CIPHER_AES_GCM_256, tls12_crypto_info_aes_gcm_256);
114 CHECK_CIPHER_DESC(TLS_CIPHER_AES_CCM_128, tls12_crypto_info_aes_ccm_128);
115 CHECK_CIPHER_DESC(TLS_CIPHER_CHACHA20_POLY1305, tls12_crypto_info_chacha20_poly1305);
116 CHECK_CIPHER_DESC(TLS_CIPHER_SM4_GCM, tls12_crypto_info_sm4_gcm);
117 CHECK_CIPHER_DESC(TLS_CIPHER_SM4_CCM, tls12_crypto_info_sm4_ccm);
118 CHECK_CIPHER_DESC(TLS_CIPHER_ARIA_GCM_128, tls12_crypto_info_aria_gcm_128);
119 CHECK_CIPHER_DESC(TLS_CIPHER_ARIA_GCM_256, tls12_crypto_info_aria_gcm_256);
121 static const struct proto *saved_tcpv6_prot;
122 static DEFINE_MUTEX(tcpv6_prot_mutex);
123 static const struct proto *saved_tcpv4_prot;
124 static DEFINE_MUTEX(tcpv4_prot_mutex);
125 static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
126 static struct proto_ops tls_proto_ops[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
127 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
128 const struct proto *base);
130 void update_sk_prot(struct sock *sk, struct tls_context *ctx)
132 int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
134 WRITE_ONCE(sk->sk_prot,
135 &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf]);
136 WRITE_ONCE(sk->sk_socket->ops,
137 &tls_proto_ops[ip_ver][ctx->tx_conf][ctx->rx_conf]);
140 int wait_on_pending_writer(struct sock *sk, long *timeo)
142 DEFINE_WAIT_FUNC(wait, woken_wake_function);
145 add_wait_queue(sk_sleep(sk), &wait);
152 if (signal_pending(current)) {
153 rc = sock_intr_errno(*timeo);
157 ret = sk_wait_event(sk, timeo,
158 !READ_ONCE(sk->sk_write_pending), &wait);
165 remove_wait_queue(sk_sleep(sk), &wait);
169 int tls_push_sg(struct sock *sk,
170 struct tls_context *ctx,
171 struct scatterlist *sg,
176 struct msghdr msg = {
177 .msg_flags = MSG_SPLICE_PAGES | flags,
182 int offset = first_offset;
184 size = sg->length - offset;
185 offset += sg->offset;
187 ctx->splicing_pages = true;
189 /* is sending application-limited? */
190 tcp_rate_check_app_limited(sk);
193 bvec_set_page(&bvec, p, size, offset);
194 iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, size);
196 ret = tcp_sendmsg_locked(sk, &msg, size);
205 offset -= sg->offset;
206 ctx->partially_sent_offset = offset;
207 ctx->partially_sent_record = (void *)sg;
208 ctx->splicing_pages = false;
213 sk_mem_uncharge(sk, sg->length);
222 ctx->splicing_pages = false;
227 static int tls_handle_open_record(struct sock *sk, int flags)
229 struct tls_context *ctx = tls_get_ctx(sk);
231 if (tls_is_pending_open_record(ctx))
232 return ctx->push_pending_record(sk, flags);
237 int tls_process_cmsg(struct sock *sk, struct msghdr *msg,
238 unsigned char *record_type)
240 struct cmsghdr *cmsg;
243 for_each_cmsghdr(cmsg, msg) {
244 if (!CMSG_OK(msg, cmsg))
246 if (cmsg->cmsg_level != SOL_TLS)
249 switch (cmsg->cmsg_type) {
250 case TLS_SET_RECORD_TYPE:
251 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
254 if (msg->msg_flags & MSG_MORE)
257 rc = tls_handle_open_record(sk, msg->msg_flags);
261 *record_type = *(unsigned char *)CMSG_DATA(cmsg);
272 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
275 struct scatterlist *sg;
278 sg = ctx->partially_sent_record;
279 offset = ctx->partially_sent_offset;
281 ctx->partially_sent_record = NULL;
282 return tls_push_sg(sk, ctx, sg, offset, flags);
285 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx)
287 struct scatterlist *sg;
289 for (sg = ctx->partially_sent_record; sg; sg = sg_next(sg)) {
290 put_page(sg_page(sg));
291 sk_mem_uncharge(sk, sg->length);
293 ctx->partially_sent_record = NULL;
296 static void tls_write_space(struct sock *sk)
298 struct tls_context *ctx = tls_get_ctx(sk);
300 /* If splicing_pages call lower protocol write space handler
301 * to ensure we wake up any waiting operations there. For example
302 * if splicing pages where to call sk_wait_event.
304 if (ctx->splicing_pages) {
305 ctx->sk_write_space(sk);
309 #ifdef CONFIG_TLS_DEVICE
310 if (ctx->tx_conf == TLS_HW)
311 tls_device_write_space(sk, ctx);
314 tls_sw_write_space(sk, ctx);
316 ctx->sk_write_space(sk);
320 * tls_ctx_free() - free TLS ULP context
321 * @sk: socket to with @ctx is attached
322 * @ctx: TLS context structure
324 * Free TLS context. If @sk is %NULL caller guarantees that the socket
325 * to which @ctx was attached has no outstanding references.
327 void tls_ctx_free(struct sock *sk, struct tls_context *ctx)
332 memzero_explicit(&ctx->crypto_send, sizeof(ctx->crypto_send));
333 memzero_explicit(&ctx->crypto_recv, sizeof(ctx->crypto_recv));
334 mutex_destroy(&ctx->tx_lock);
342 static void tls_sk_proto_cleanup(struct sock *sk,
343 struct tls_context *ctx, long timeo)
345 if (unlikely(sk->sk_write_pending) &&
346 !wait_on_pending_writer(sk, &timeo))
347 tls_handle_open_record(sk, 0);
349 /* We need these for tls_sw_fallback handling of other packets */
350 if (ctx->tx_conf == TLS_SW) {
351 kfree(ctx->tx.rec_seq);
353 tls_sw_release_resources_tx(sk);
354 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
355 } else if (ctx->tx_conf == TLS_HW) {
356 tls_device_free_resources_tx(sk);
357 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
360 if (ctx->rx_conf == TLS_SW) {
361 tls_sw_release_resources_rx(sk);
362 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
363 } else if (ctx->rx_conf == TLS_HW) {
364 tls_device_offload_cleanup_rx(sk);
365 TLS_DEC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
369 static void tls_sk_proto_close(struct sock *sk, long timeout)
371 struct inet_connection_sock *icsk = inet_csk(sk);
372 struct tls_context *ctx = tls_get_ctx(sk);
373 long timeo = sock_sndtimeo(sk, 0);
376 if (ctx->tx_conf == TLS_SW)
377 tls_sw_cancel_work_tx(ctx);
380 free_ctx = ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW;
382 if (ctx->tx_conf != TLS_BASE || ctx->rx_conf != TLS_BASE)
383 tls_sk_proto_cleanup(sk, ctx, timeo);
385 write_lock_bh(&sk->sk_callback_lock);
387 rcu_assign_pointer(icsk->icsk_ulp_data, NULL);
388 WRITE_ONCE(sk->sk_prot, ctx->sk_proto);
389 if (sk->sk_write_space == tls_write_space)
390 sk->sk_write_space = ctx->sk_write_space;
391 write_unlock_bh(&sk->sk_callback_lock);
393 if (ctx->tx_conf == TLS_SW)
394 tls_sw_free_ctx_tx(ctx);
395 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
396 tls_sw_strparser_done(ctx);
397 if (ctx->rx_conf == TLS_SW)
398 tls_sw_free_ctx_rx(ctx);
399 ctx->sk_proto->close(sk, timeout);
402 tls_ctx_free(sk, ctx);
405 static __poll_t tls_sk_poll(struct file *file, struct socket *sock,
406 struct poll_table_struct *wait)
408 struct tls_sw_context_rx *ctx;
409 struct tls_context *tls_ctx;
410 struct sock *sk = sock->sk;
411 struct sk_psock *psock;
416 mask = tcp_poll(file, sock, wait);
418 state = inet_sk_state_load(sk);
419 shutdown = READ_ONCE(sk->sk_shutdown);
420 if (unlikely(state != TCP_ESTABLISHED || shutdown & RCV_SHUTDOWN))
423 tls_ctx = tls_get_ctx(sk);
424 ctx = tls_sw_ctx_rx(tls_ctx);
425 psock = sk_psock_get(sk);
427 if (skb_queue_empty_lockless(&ctx->rx_list) &&
428 !tls_strp_msg_ready(ctx) &&
429 sk_psock_queue_empty(psock))
430 mask &= ~(EPOLLIN | EPOLLRDNORM);
433 sk_psock_put(sk, psock);
438 static int do_tls_getsockopt_conf(struct sock *sk, char __user *optval,
439 int __user *optlen, int tx)
442 const struct tls_cipher_desc *cipher_desc;
443 struct tls_context *ctx = tls_get_ctx(sk);
444 struct tls_crypto_info *crypto_info;
445 struct cipher_context *cctx;
448 if (get_user(len, optlen))
451 if (!optval || (len < sizeof(*crypto_info))) {
461 /* get user crypto info */
463 crypto_info = &ctx->crypto_send.info;
466 crypto_info = &ctx->crypto_recv.info;
470 if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
475 if (len == sizeof(*crypto_info)) {
476 if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
481 cipher_desc = get_cipher_desc(crypto_info->cipher_type);
482 if (!cipher_desc || len != cipher_desc->crypto_info) {
487 memcpy(crypto_info_iv(crypto_info, cipher_desc),
488 cctx->iv + cipher_desc->salt, cipher_desc->iv);
489 memcpy(crypto_info_rec_seq(crypto_info, cipher_desc),
490 cctx->rec_seq, cipher_desc->rec_seq);
492 if (copy_to_user(optval, crypto_info, cipher_desc->crypto_info))
499 static int do_tls_getsockopt_tx_zc(struct sock *sk, char __user *optval,
502 struct tls_context *ctx = tls_get_ctx(sk);
506 if (get_user(len, optlen))
509 if (len != sizeof(value))
512 value = ctx->zerocopy_sendfile;
513 if (copy_to_user(optval, &value, sizeof(value)))
519 static int do_tls_getsockopt_no_pad(struct sock *sk, char __user *optval,
522 struct tls_context *ctx = tls_get_ctx(sk);
525 if (ctx->prot_info.version != TLS_1_3_VERSION)
528 if (get_user(len, optlen))
530 if (len < sizeof(value))
534 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW)
535 value = ctx->rx_no_pad;
539 if (put_user(sizeof(value), optlen))
541 if (copy_to_user(optval, &value, sizeof(value)))
547 static int do_tls_getsockopt(struct sock *sk, int optname,
548 char __user *optval, int __user *optlen)
557 rc = do_tls_getsockopt_conf(sk, optval, optlen,
560 case TLS_TX_ZEROCOPY_RO:
561 rc = do_tls_getsockopt_tx_zc(sk, optval, optlen);
563 case TLS_RX_EXPECT_NO_PAD:
564 rc = do_tls_getsockopt_no_pad(sk, optval, optlen);
576 static int tls_getsockopt(struct sock *sk, int level, int optname,
577 char __user *optval, int __user *optlen)
579 struct tls_context *ctx = tls_get_ctx(sk);
581 if (level != SOL_TLS)
582 return ctx->sk_proto->getsockopt(sk, level,
583 optname, optval, optlen);
585 return do_tls_getsockopt(sk, optname, optval, optlen);
588 static int do_tls_setsockopt_conf(struct sock *sk, sockptr_t optval,
589 unsigned int optlen, int tx)
591 struct tls_crypto_info *crypto_info;
592 struct tls_crypto_info *alt_crypto_info;
593 struct tls_context *ctx = tls_get_ctx(sk);
594 const struct tls_cipher_desc *cipher_desc;
598 if (sockptr_is_null(optval) || (optlen < sizeof(*crypto_info)))
602 crypto_info = &ctx->crypto_send.info;
603 alt_crypto_info = &ctx->crypto_recv.info;
605 crypto_info = &ctx->crypto_recv.info;
606 alt_crypto_info = &ctx->crypto_send.info;
609 /* Currently we don't support set crypto info more than one time */
610 if (TLS_CRYPTO_INFO_READY(crypto_info))
613 rc = copy_from_sockptr(crypto_info, optval, sizeof(*crypto_info));
616 goto err_crypto_info;
620 if (crypto_info->version != TLS_1_2_VERSION &&
621 crypto_info->version != TLS_1_3_VERSION) {
623 goto err_crypto_info;
626 /* Ensure that TLS version and ciphers are same in both directions */
627 if (TLS_CRYPTO_INFO_READY(alt_crypto_info)) {
628 if (alt_crypto_info->version != crypto_info->version ||
629 alt_crypto_info->cipher_type != crypto_info->cipher_type) {
631 goto err_crypto_info;
635 cipher_desc = get_cipher_desc(crypto_info->cipher_type);
638 goto err_crypto_info;
641 switch (crypto_info->cipher_type) {
642 case TLS_CIPHER_ARIA_GCM_128:
643 case TLS_CIPHER_ARIA_GCM_256:
644 if (crypto_info->version != TLS_1_2_VERSION) {
646 goto err_crypto_info;
651 if (optlen != cipher_desc->crypto_info) {
653 goto err_crypto_info;
656 rc = copy_from_sockptr_offset(crypto_info + 1, optval,
657 sizeof(*crypto_info),
658 optlen - sizeof(*crypto_info));
661 goto err_crypto_info;
665 rc = tls_set_device_offload(sk, ctx);
668 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXDEVICE);
669 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXDEVICE);
671 rc = tls_set_sw_offload(sk, ctx, 1);
673 goto err_crypto_info;
674 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSTXSW);
675 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRTXSW);
679 rc = tls_set_device_offload_rx(sk, ctx);
682 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXDEVICE);
683 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXDEVICE);
685 rc = tls_set_sw_offload(sk, ctx, 0);
687 goto err_crypto_info;
688 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSRXSW);
689 TLS_INC_STATS(sock_net(sk), LINUX_MIB_TLSCURRRXSW);
692 tls_sw_strparser_arm(sk, ctx);
699 update_sk_prot(sk, ctx);
701 ctx->sk_write_space = sk->sk_write_space;
702 sk->sk_write_space = tls_write_space;
704 struct tls_sw_context_rx *rx_ctx = tls_sw_ctx_rx(ctx);
706 tls_strp_check_rcv(&rx_ctx->strp);
711 memzero_explicit(crypto_info, sizeof(union tls_crypto_context));
715 static int do_tls_setsockopt_tx_zc(struct sock *sk, sockptr_t optval,
718 struct tls_context *ctx = tls_get_ctx(sk);
721 if (sockptr_is_null(optval) || optlen != sizeof(value))
724 if (copy_from_sockptr(&value, optval, sizeof(value)))
730 ctx->zerocopy_sendfile = value;
735 static int do_tls_setsockopt_no_pad(struct sock *sk, sockptr_t optval,
738 struct tls_context *ctx = tls_get_ctx(sk);
742 if (ctx->prot_info.version != TLS_1_3_VERSION ||
743 sockptr_is_null(optval) || optlen < sizeof(val))
746 rc = copy_from_sockptr(&val, optval, sizeof(val));
751 rc = check_zeroed_sockptr(optval, sizeof(val), optlen - sizeof(val));
753 return rc == 0 ? -EINVAL : rc;
757 if (ctx->rx_conf == TLS_SW || ctx->rx_conf == TLS_HW) {
758 ctx->rx_no_pad = val;
759 tls_update_rx_zc_capable(ctx);
767 static int do_tls_setsockopt(struct sock *sk, int optname, sockptr_t optval,
776 rc = do_tls_setsockopt_conf(sk, optval, optlen,
780 case TLS_TX_ZEROCOPY_RO:
782 rc = do_tls_setsockopt_tx_zc(sk, optval, optlen);
785 case TLS_RX_EXPECT_NO_PAD:
786 rc = do_tls_setsockopt_no_pad(sk, optval, optlen);
795 static int tls_setsockopt(struct sock *sk, int level, int optname,
796 sockptr_t optval, unsigned int optlen)
798 struct tls_context *ctx = tls_get_ctx(sk);
800 if (level != SOL_TLS)
801 return ctx->sk_proto->setsockopt(sk, level, optname, optval,
804 return do_tls_setsockopt(sk, optname, optval, optlen);
807 struct tls_context *tls_ctx_create(struct sock *sk)
809 struct inet_connection_sock *icsk = inet_csk(sk);
810 struct tls_context *ctx;
812 ctx = kzalloc(sizeof(*ctx), GFP_ATOMIC);
816 mutex_init(&ctx->tx_lock);
817 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
818 ctx->sk_proto = READ_ONCE(sk->sk_prot);
823 static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
824 const struct proto_ops *base)
826 ops[TLS_BASE][TLS_BASE] = *base;
828 ops[TLS_SW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
829 ops[TLS_SW ][TLS_BASE].splice_eof = tls_sw_splice_eof;
831 ops[TLS_BASE][TLS_SW ] = ops[TLS_BASE][TLS_BASE];
832 ops[TLS_BASE][TLS_SW ].splice_read = tls_sw_splice_read;
833 ops[TLS_BASE][TLS_SW ].poll = tls_sk_poll;
834 ops[TLS_BASE][TLS_SW ].read_sock = tls_sw_read_sock;
836 ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE];
837 ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read;
838 ops[TLS_SW ][TLS_SW ].poll = tls_sk_poll;
839 ops[TLS_SW ][TLS_SW ].read_sock = tls_sw_read_sock;
841 #ifdef CONFIG_TLS_DEVICE
842 ops[TLS_HW ][TLS_BASE] = ops[TLS_BASE][TLS_BASE];
844 ops[TLS_HW ][TLS_SW ] = ops[TLS_BASE][TLS_SW ];
846 ops[TLS_BASE][TLS_HW ] = ops[TLS_BASE][TLS_SW ];
848 ops[TLS_SW ][TLS_HW ] = ops[TLS_SW ][TLS_SW ];
850 ops[TLS_HW ][TLS_HW ] = ops[TLS_HW ][TLS_SW ];
852 #ifdef CONFIG_TLS_TOE
853 ops[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
857 static void tls_build_proto(struct sock *sk)
859 int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
860 struct proto *prot = READ_ONCE(sk->sk_prot);
862 /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
863 if (ip_ver == TLSV6 &&
864 unlikely(prot != smp_load_acquire(&saved_tcpv6_prot))) {
865 mutex_lock(&tcpv6_prot_mutex);
866 if (likely(prot != saved_tcpv6_prot)) {
867 build_protos(tls_prots[TLSV6], prot);
868 build_proto_ops(tls_proto_ops[TLSV6],
870 smp_store_release(&saved_tcpv6_prot, prot);
872 mutex_unlock(&tcpv6_prot_mutex);
875 if (ip_ver == TLSV4 &&
876 unlikely(prot != smp_load_acquire(&saved_tcpv4_prot))) {
877 mutex_lock(&tcpv4_prot_mutex);
878 if (likely(prot != saved_tcpv4_prot)) {
879 build_protos(tls_prots[TLSV4], prot);
880 build_proto_ops(tls_proto_ops[TLSV4],
882 smp_store_release(&saved_tcpv4_prot, prot);
884 mutex_unlock(&tcpv4_prot_mutex);
888 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
889 const struct proto *base)
891 prot[TLS_BASE][TLS_BASE] = *base;
892 prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
893 prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
894 prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
896 prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
897 prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
898 prot[TLS_SW][TLS_BASE].splice_eof = tls_sw_splice_eof;
900 prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
901 prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
902 prot[TLS_BASE][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
903 prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
905 prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
906 prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
907 prot[TLS_SW][TLS_SW].sock_is_readable = tls_sw_sock_is_readable;
908 prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
910 #ifdef CONFIG_TLS_DEVICE
911 prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
912 prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
913 prot[TLS_HW][TLS_BASE].splice_eof = tls_device_splice_eof;
915 prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
916 prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
917 prot[TLS_HW][TLS_SW].splice_eof = tls_device_splice_eof;
919 prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
921 prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
923 prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
925 #ifdef CONFIG_TLS_TOE
926 prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
927 prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_toe_hash;
928 prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_toe_unhash;
932 static int tls_init(struct sock *sk)
934 struct tls_context *ctx;
939 #ifdef CONFIG_TLS_TOE
940 if (tls_toe_bypass(sk))
944 /* The TLS ulp is currently supported only for TCP sockets
945 * in ESTABLISHED state.
946 * Supporting sockets in LISTEN state will require us
947 * to modify the accept implementation to clone rather then
948 * share the ulp context.
950 if (sk->sk_state != TCP_ESTABLISHED)
953 /* allocate tls context */
954 write_lock_bh(&sk->sk_callback_lock);
955 ctx = tls_ctx_create(sk);
961 ctx->tx_conf = TLS_BASE;
962 ctx->rx_conf = TLS_BASE;
963 update_sk_prot(sk, ctx);
965 write_unlock_bh(&sk->sk_callback_lock);
969 static void tls_update(struct sock *sk, struct proto *p,
970 void (*write_space)(struct sock *sk))
972 struct tls_context *ctx;
974 WARN_ON_ONCE(sk->sk_prot == p);
976 ctx = tls_get_ctx(sk);
978 ctx->sk_write_space = write_space;
981 /* Pairs with lockless read in sk_clone_lock(). */
982 WRITE_ONCE(sk->sk_prot, p);
983 sk->sk_write_space = write_space;
987 static u16 tls_user_config(struct tls_context *ctx, bool tx)
989 u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
993 return TLS_CONF_BASE;
999 return TLS_CONF_HW_RECORD;
1004 static int tls_get_info(const struct sock *sk, struct sk_buff *skb)
1006 u16 version, cipher_type;
1007 struct tls_context *ctx;
1008 struct nlattr *start;
1011 start = nla_nest_start_noflag(skb, INET_ULP_INFO_TLS);
1016 ctx = rcu_dereference(inet_csk(sk)->icsk_ulp_data);
1021 version = ctx->prot_info.version;
1023 err = nla_put_u16(skb, TLS_INFO_VERSION, version);
1027 cipher_type = ctx->prot_info.cipher_type;
1029 err = nla_put_u16(skb, TLS_INFO_CIPHER, cipher_type);
1033 err = nla_put_u16(skb, TLS_INFO_TXCONF, tls_user_config(ctx, true));
1037 err = nla_put_u16(skb, TLS_INFO_RXCONF, tls_user_config(ctx, false));
1041 if (ctx->tx_conf == TLS_HW && ctx->zerocopy_sendfile) {
1042 err = nla_put_flag(skb, TLS_INFO_ZC_RO_TX);
1046 if (ctx->rx_no_pad) {
1047 err = nla_put_flag(skb, TLS_INFO_RX_NO_PAD);
1053 nla_nest_end(skb, start);
1058 nla_nest_cancel(skb, start);
1062 static size_t tls_get_info_size(const struct sock *sk)
1066 size += nla_total_size(0) + /* INET_ULP_INFO_TLS */
1067 nla_total_size(sizeof(u16)) + /* TLS_INFO_VERSION */
1068 nla_total_size(sizeof(u16)) + /* TLS_INFO_CIPHER */
1069 nla_total_size(sizeof(u16)) + /* TLS_INFO_RXCONF */
1070 nla_total_size(sizeof(u16)) + /* TLS_INFO_TXCONF */
1071 nla_total_size(0) + /* TLS_INFO_ZC_RO_TX */
1072 nla_total_size(0) + /* TLS_INFO_RX_NO_PAD */
1078 static int __net_init tls_init_net(struct net *net)
1082 net->mib.tls_statistics = alloc_percpu(struct linux_tls_mib);
1083 if (!net->mib.tls_statistics)
1086 err = tls_proc_init(net);
1088 goto err_free_stats;
1092 free_percpu(net->mib.tls_statistics);
1096 static void __net_exit tls_exit_net(struct net *net)
1099 free_percpu(net->mib.tls_statistics);
1102 static struct pernet_operations tls_proc_ops = {
1103 .init = tls_init_net,
1104 .exit = tls_exit_net,
1107 static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
1109 .owner = THIS_MODULE,
1111 .update = tls_update,
1112 .get_info = tls_get_info,
1113 .get_info_size = tls_get_info_size,
1116 static int __init tls_register(void)
1120 err = register_pernet_subsys(&tls_proc_ops);
1124 err = tls_strp_dev_init();
1128 err = tls_device_init();
1132 tcp_register_ulp(&tcp_tls_ulp_ops);
1136 tls_strp_dev_exit();
1138 unregister_pernet_subsys(&tls_proc_ops);
1142 static void __exit tls_unregister(void)
1144 tcp_unregister_ulp(&tcp_tls_ulp_ops);
1145 tls_strp_dev_exit();
1146 tls_device_cleanup();
1147 unregister_pernet_subsys(&tls_proc_ops);
1150 module_init(tls_register);
1151 module_exit(tls_unregister);