2 * Copyright (c) 2016-2017, Mellanox Technologies. 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>
45 MODULE_AUTHOR("Mellanox Technologies");
46 MODULE_DESCRIPTION("Transport Layer Security Support");
47 MODULE_LICENSE("Dual BSD/GPL");
55 static struct proto *saved_tcpv6_prot;
56 static DEFINE_MUTEX(tcpv6_prot_mutex);
57 static LIST_HEAD(device_list);
58 static DEFINE_MUTEX(device_mutex);
59 static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG][TLS_NUM_CONFIG];
60 static struct proto_ops tls_sw_proto_ops;
62 static void update_sk_prot(struct sock *sk, struct tls_context *ctx)
64 int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
66 sk->sk_prot = &tls_prots[ip_ver][ctx->tx_conf][ctx->rx_conf];
69 int wait_on_pending_writer(struct sock *sk, long *timeo)
72 DEFINE_WAIT_FUNC(wait, woken_wake_function);
74 add_wait_queue(sk_sleep(sk), &wait);
81 if (signal_pending(current)) {
82 rc = sock_intr_errno(*timeo);
86 if (sk_wait_event(sk, timeo, !sk->sk_write_pending, &wait))
89 remove_wait_queue(sk_sleep(sk), &wait);
93 int tls_push_sg(struct sock *sk,
94 struct tls_context *ctx,
95 struct scatterlist *sg,
99 int sendpage_flags = flags | MSG_SENDPAGE_NOTLAST;
103 int offset = first_offset;
105 size = sg->length - offset;
106 offset += sg->offset;
108 ctx->in_tcp_sendpages = true;
111 sendpage_flags = flags;
113 /* is sending application-limited? */
114 tcp_rate_check_app_limited(sk);
117 ret = do_tcp_sendpages(sk, p, offset, size, sendpage_flags);
126 offset -= sg->offset;
127 ctx->partially_sent_offset = offset;
128 ctx->partially_sent_record = (void *)sg;
129 ctx->in_tcp_sendpages = false;
134 sk_mem_uncharge(sk, sg->length);
143 clear_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags);
144 ctx->in_tcp_sendpages = false;
145 ctx->sk_write_space(sk);
150 static int tls_handle_open_record(struct sock *sk, int flags)
152 struct tls_context *ctx = tls_get_ctx(sk);
154 if (tls_is_pending_open_record(ctx))
155 return ctx->push_pending_record(sk, flags);
160 int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
161 unsigned char *record_type)
163 struct cmsghdr *cmsg;
166 for_each_cmsghdr(cmsg, msg) {
167 if (!CMSG_OK(msg, cmsg))
169 if (cmsg->cmsg_level != SOL_TLS)
172 switch (cmsg->cmsg_type) {
173 case TLS_SET_RECORD_TYPE:
174 if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
177 if (msg->msg_flags & MSG_MORE)
180 rc = tls_handle_open_record(sk, msg->msg_flags);
184 *record_type = *(unsigned char *)CMSG_DATA(cmsg);
195 int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
196 int flags, long *timeo)
198 struct scatterlist *sg;
201 if (!tls_is_partially_sent_record(ctx))
202 return ctx->push_pending_record(sk, flags);
204 sg = ctx->partially_sent_record;
205 offset = ctx->partially_sent_offset;
207 ctx->partially_sent_record = NULL;
208 return tls_push_sg(sk, ctx, sg, offset, flags);
211 static void tls_write_space(struct sock *sk)
213 struct tls_context *ctx = tls_get_ctx(sk);
215 /* We are already sending pages, ignore notification */
216 if (ctx->in_tcp_sendpages)
219 if (!sk->sk_write_pending && tls_is_pending_closed_record(ctx)) {
220 gfp_t sk_allocation = sk->sk_allocation;
224 sk->sk_allocation = GFP_ATOMIC;
225 rc = tls_push_pending_closed_record(sk, ctx,
229 sk->sk_allocation = sk_allocation;
235 ctx->sk_write_space(sk);
238 static void tls_sk_proto_close(struct sock *sk, long timeout)
240 struct tls_context *ctx = tls_get_ctx(sk);
241 long timeo = sock_sndtimeo(sk, 0);
242 void (*sk_proto_close)(struct sock *sk, long timeout);
243 bool free_ctx = false;
246 sk_proto_close = ctx->sk_proto_close;
248 if ((ctx->tx_conf == TLS_HW_RECORD && ctx->rx_conf == TLS_HW_RECORD) ||
249 (ctx->tx_conf == TLS_BASE && ctx->rx_conf == TLS_BASE)) {
251 goto skip_tx_cleanup;
254 if (!tls_complete_pending_work(sk, ctx, 0, &timeo))
255 tls_handle_open_record(sk, 0);
257 if (ctx->partially_sent_record) {
258 struct scatterlist *sg = ctx->partially_sent_record;
261 put_page(sg_page(sg));
262 sk_mem_uncharge(sk, sg->length);
270 /* We need these for tls_sw_fallback handling of other packets */
271 if (ctx->tx_conf == TLS_SW) {
272 kfree(ctx->tx.rec_seq);
274 tls_sw_free_resources_tx(sk);
277 if (ctx->rx_conf == TLS_SW) {
278 kfree(ctx->rx.rec_seq);
280 tls_sw_free_resources_rx(sk);
283 #ifdef CONFIG_TLS_DEVICE
284 if (ctx->rx_conf == TLS_HW)
285 tls_device_offload_cleanup_rx(sk);
287 if (ctx->tx_conf != TLS_HW && ctx->rx_conf != TLS_HW) {
297 sk_proto_close(sk, timeout);
298 /* free ctx for TLS_HW_RECORD, used by tcp_set_state
299 * for sk->sk_prot->unhash [tls_hw_unhash]
305 static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
309 struct tls_context *ctx = tls_get_ctx(sk);
310 struct tls_crypto_info *crypto_info;
313 if (get_user(len, optlen))
316 if (!optval || (len < sizeof(*crypto_info))) {
326 /* get user crypto info */
327 crypto_info = &ctx->crypto_send;
329 if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
334 if (len == sizeof(*crypto_info)) {
335 if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
340 switch (crypto_info->cipher_type) {
341 case TLS_CIPHER_AES_GCM_128: {
342 struct tls12_crypto_info_aes_gcm_128 *
343 crypto_info_aes_gcm_128 =
344 container_of(crypto_info,
345 struct tls12_crypto_info_aes_gcm_128,
348 if (len != sizeof(*crypto_info_aes_gcm_128)) {
353 memcpy(crypto_info_aes_gcm_128->iv,
354 ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
355 TLS_CIPHER_AES_GCM_128_IV_SIZE);
356 memcpy(crypto_info_aes_gcm_128->rec_seq, ctx->tx.rec_seq,
357 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
359 if (copy_to_user(optval,
360 crypto_info_aes_gcm_128,
361 sizeof(*crypto_info_aes_gcm_128)))
373 static int do_tls_getsockopt(struct sock *sk, int optname,
374 char __user *optval, int __user *optlen)
380 rc = do_tls_getsockopt_tx(sk, optval, optlen);
389 static int tls_getsockopt(struct sock *sk, int level, int optname,
390 char __user *optval, int __user *optlen)
392 struct tls_context *ctx = tls_get_ctx(sk);
394 if (level != SOL_TLS)
395 return ctx->getsockopt(sk, level, optname, optval, optlen);
397 return do_tls_getsockopt(sk, optname, optval, optlen);
400 static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
401 unsigned int optlen, int tx)
403 struct tls_crypto_info *crypto_info;
404 struct tls_context *ctx = tls_get_ctx(sk);
408 if (!optval || (optlen < sizeof(*crypto_info))) {
414 crypto_info = &ctx->crypto_send;
416 crypto_info = &ctx->crypto_recv;
418 /* Currently we don't support set crypto info more than one time */
419 if (TLS_CRYPTO_INFO_READY(crypto_info)) {
424 rc = copy_from_user(crypto_info, optval, sizeof(*crypto_info));
427 goto err_crypto_info;
431 if (crypto_info->version != TLS_1_2_VERSION) {
433 goto err_crypto_info;
436 switch (crypto_info->cipher_type) {
437 case TLS_CIPHER_AES_GCM_128: {
438 if (optlen != sizeof(struct tls12_crypto_info_aes_gcm_128)) {
440 goto err_crypto_info;
442 rc = copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info),
443 optlen - sizeof(*crypto_info));
446 goto err_crypto_info;
452 goto err_crypto_info;
456 #ifdef CONFIG_TLS_DEVICE
457 rc = tls_set_device_offload(sk, ctx);
463 rc = tls_set_sw_offload(sk, ctx, 1);
467 #ifdef CONFIG_TLS_DEVICE
468 rc = tls_set_device_offload_rx(sk, ctx);
474 rc = tls_set_sw_offload(sk, ctx, 0);
480 goto err_crypto_info;
486 update_sk_prot(sk, ctx);
488 ctx->sk_write_space = sk->sk_write_space;
489 sk->sk_write_space = tls_write_space;
491 sk->sk_socket->ops = &tls_sw_proto_ops;
496 memset(crypto_info, 0, sizeof(*crypto_info));
501 static int do_tls_setsockopt(struct sock *sk, int optname,
502 char __user *optval, unsigned int optlen)
510 rc = do_tls_setsockopt_conf(sk, optval, optlen,
521 static int tls_setsockopt(struct sock *sk, int level, int optname,
522 char __user *optval, unsigned int optlen)
524 struct tls_context *ctx = tls_get_ctx(sk);
526 if (level != SOL_TLS)
527 return ctx->setsockopt(sk, level, optname, optval, optlen);
529 return do_tls_setsockopt(sk, optname, optval, optlen);
532 static struct tls_context *create_ctx(struct sock *sk)
534 struct inet_connection_sock *icsk = inet_csk(sk);
535 struct tls_context *ctx;
537 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
541 icsk->icsk_ulp_data = ctx;
545 static int tls_hw_prot(struct sock *sk)
547 struct tls_context *ctx;
548 struct tls_device *dev;
551 mutex_lock(&device_mutex);
552 list_for_each_entry(dev, &device_list, dev_list) {
553 if (dev->feature && dev->feature(dev)) {
554 ctx = create_ctx(sk);
558 ctx->hash = sk->sk_prot->hash;
559 ctx->unhash = sk->sk_prot->unhash;
560 ctx->sk_proto_close = sk->sk_prot->close;
561 ctx->rx_conf = TLS_HW_RECORD;
562 ctx->tx_conf = TLS_HW_RECORD;
563 update_sk_prot(sk, ctx);
569 mutex_unlock(&device_mutex);
573 static void tls_hw_unhash(struct sock *sk)
575 struct tls_context *ctx = tls_get_ctx(sk);
576 struct tls_device *dev;
578 mutex_lock(&device_mutex);
579 list_for_each_entry(dev, &device_list, dev_list) {
581 dev->unhash(dev, sk);
583 mutex_unlock(&device_mutex);
587 static int tls_hw_hash(struct sock *sk)
589 struct tls_context *ctx = tls_get_ctx(sk);
590 struct tls_device *dev;
594 mutex_lock(&device_mutex);
595 list_for_each_entry(dev, &device_list, dev_list) {
597 err |= dev->hash(dev, sk);
599 mutex_unlock(&device_mutex);
606 static void build_protos(struct proto prot[TLS_NUM_CONFIG][TLS_NUM_CONFIG],
609 prot[TLS_BASE][TLS_BASE] = *base;
610 prot[TLS_BASE][TLS_BASE].setsockopt = tls_setsockopt;
611 prot[TLS_BASE][TLS_BASE].getsockopt = tls_getsockopt;
612 prot[TLS_BASE][TLS_BASE].close = tls_sk_proto_close;
614 prot[TLS_SW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
615 prot[TLS_SW][TLS_BASE].sendmsg = tls_sw_sendmsg;
616 prot[TLS_SW][TLS_BASE].sendpage = tls_sw_sendpage;
618 prot[TLS_BASE][TLS_SW] = prot[TLS_BASE][TLS_BASE];
619 prot[TLS_BASE][TLS_SW].recvmsg = tls_sw_recvmsg;
620 prot[TLS_BASE][TLS_SW].close = tls_sk_proto_close;
622 prot[TLS_SW][TLS_SW] = prot[TLS_SW][TLS_BASE];
623 prot[TLS_SW][TLS_SW].recvmsg = tls_sw_recvmsg;
624 prot[TLS_SW][TLS_SW].close = tls_sk_proto_close;
626 #ifdef CONFIG_TLS_DEVICE
627 prot[TLS_HW][TLS_BASE] = prot[TLS_BASE][TLS_BASE];
628 prot[TLS_HW][TLS_BASE].sendmsg = tls_device_sendmsg;
629 prot[TLS_HW][TLS_BASE].sendpage = tls_device_sendpage;
631 prot[TLS_HW][TLS_SW] = prot[TLS_BASE][TLS_SW];
632 prot[TLS_HW][TLS_SW].sendmsg = tls_device_sendmsg;
633 prot[TLS_HW][TLS_SW].sendpage = tls_device_sendpage;
635 prot[TLS_BASE][TLS_HW] = prot[TLS_BASE][TLS_SW];
637 prot[TLS_SW][TLS_HW] = prot[TLS_SW][TLS_SW];
639 prot[TLS_HW][TLS_HW] = prot[TLS_HW][TLS_SW];
642 prot[TLS_HW_RECORD][TLS_HW_RECORD] = *base;
643 prot[TLS_HW_RECORD][TLS_HW_RECORD].hash = tls_hw_hash;
644 prot[TLS_HW_RECORD][TLS_HW_RECORD].unhash = tls_hw_unhash;
645 prot[TLS_HW_RECORD][TLS_HW_RECORD].close = tls_sk_proto_close;
648 static int tls_init(struct sock *sk)
650 int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
651 struct tls_context *ctx;
657 /* The TLS ulp is currently supported only for TCP sockets
658 * in ESTABLISHED state.
659 * Supporting sockets in LISTEN state will require us
660 * to modify the accept implementation to clone rather then
661 * share the ulp context.
663 if (sk->sk_state != TCP_ESTABLISHED)
666 /* allocate tls context */
667 ctx = create_ctx(sk);
672 ctx->setsockopt = sk->sk_prot->setsockopt;
673 ctx->getsockopt = sk->sk_prot->getsockopt;
674 ctx->sk_proto_close = sk->sk_prot->close;
676 /* Build IPv6 TLS whenever the address of tcpv6 _prot changes */
677 if (ip_ver == TLSV6 &&
678 unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv6_prot))) {
679 mutex_lock(&tcpv6_prot_mutex);
680 if (likely(sk->sk_prot != saved_tcpv6_prot)) {
681 build_protos(tls_prots[TLSV6], sk->sk_prot);
682 smp_store_release(&saved_tcpv6_prot, sk->sk_prot);
684 mutex_unlock(&tcpv6_prot_mutex);
687 ctx->tx_conf = TLS_BASE;
688 ctx->rx_conf = TLS_BASE;
689 update_sk_prot(sk, ctx);
694 void tls_register_device(struct tls_device *device)
696 mutex_lock(&device_mutex);
697 list_add_tail(&device->dev_list, &device_list);
698 mutex_unlock(&device_mutex);
700 EXPORT_SYMBOL(tls_register_device);
702 void tls_unregister_device(struct tls_device *device)
704 mutex_lock(&device_mutex);
705 list_del(&device->dev_list);
706 mutex_unlock(&device_mutex);
708 EXPORT_SYMBOL(tls_unregister_device);
710 static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
713 .user_visible = true,
714 .owner = THIS_MODULE,
718 static int __init tls_register(void)
720 build_protos(tls_prots[TLSV4], &tcp_prot);
722 tls_sw_proto_ops = inet_stream_ops;
723 tls_sw_proto_ops.poll = tls_sw_poll;
724 tls_sw_proto_ops.splice_read = tls_sw_splice_read;
726 #ifdef CONFIG_TLS_DEVICE
729 tcp_register_ulp(&tcp_tls_ulp_ops);
734 static void __exit tls_unregister(void)
736 tcp_unregister_ulp(&tcp_tls_ulp_ops);
737 #ifdef CONFIG_TLS_DEVICE
738 tls_device_cleanup();
742 module_init(tls_register);
743 module_exit(tls_unregister);