2017-06-14 18:37:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
|
|
|
|
* Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is available to you under a choice of one of two
|
|
|
|
* licenses. You may choose to be licensed under the terms of the GNU
|
|
|
|
* General Public License (GPL) Version 2, available from the file
|
|
|
|
* COPYING in the main directory of this source tree, or the
|
|
|
|
* OpenIB.org BSD license below:
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or
|
|
|
|
* without modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* - Redistributions of source code must retain the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer.
|
|
|
|
*
|
|
|
|
* - Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials
|
|
|
|
* provided with the distribution.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <net/tcp.h>
|
|
|
|
#include <net/inet_common.h>
|
|
|
|
#include <linux/highmem.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/sched/signal.h>
|
2018-03-31 16:11:52 +00:00
|
|
|
#include <linux/inetdevice.h>
|
2017-06-14 18:37:39 +00:00
|
|
|
|
|
|
|
#include <net/tls.h>
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Mellanox Technologies");
|
|
|
|
MODULE_DESCRIPTION("Transport Layer Security Support");
|
|
|
|
MODULE_LICENSE("Dual BSD/GPL");
|
|
|
|
|
2018-02-27 12:18:39 +00:00
|
|
|
enum {
|
|
|
|
TLSV4,
|
|
|
|
TLSV6,
|
|
|
|
TLS_NUM_PROTS,
|
|
|
|
};
|
|
|
|
|
2017-11-13 08:22:45 +00:00
|
|
|
enum {
|
2018-03-22 17:10:26 +00:00
|
|
|
TLS_BASE,
|
2017-11-13 08:22:45 +00:00
|
|
|
TLS_SW_TX,
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
TLS_SW_RX,
|
|
|
|
TLS_SW_RXTX,
|
2018-03-31 16:11:52 +00:00
|
|
|
TLS_HW_RECORD,
|
2017-11-13 08:22:45 +00:00
|
|
|
TLS_NUM_CONFIG,
|
|
|
|
};
|
|
|
|
|
2018-02-27 12:18:39 +00:00
|
|
|
static struct proto *saved_tcpv6_prot;
|
|
|
|
static DEFINE_MUTEX(tcpv6_prot_mutex);
|
2018-03-31 16:11:52 +00:00
|
|
|
static LIST_HEAD(device_list);
|
|
|
|
static DEFINE_MUTEX(device_mutex);
|
2018-02-27 12:18:39 +00:00
|
|
|
static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG];
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
static struct proto_ops tls_sw_proto_ops;
|
2017-11-13 08:22:45 +00:00
|
|
|
|
|
|
|
static inline void update_sk_prot(struct sock *sk, struct tls_context *ctx)
|
|
|
|
{
|
2018-02-27 12:18:39 +00:00
|
|
|
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
|
|
|
|
|
2018-03-22 17:10:26 +00:00
|
|
|
sk->sk_prot = &tls_prots[ip_ver][ctx->conf];
|
2017-11-13 08:22:45 +00:00
|
|
|
}
|
2017-06-14 18:37:39 +00:00
|
|
|
|
|
|
|
int wait_on_pending_writer(struct sock *sk, long *timeo)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
DEFINE_WAIT_FUNC(wait, woken_wake_function);
|
|
|
|
|
|
|
|
add_wait_queue(sk_sleep(sk), &wait);
|
|
|
|
while (1) {
|
|
|
|
if (!*timeo) {
|
|
|
|
rc = -EAGAIN;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (signal_pending(current)) {
|
|
|
|
rc = sock_intr_errno(*timeo);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sk_wait_event(sk, timeo, !sk->sk_write_pending, &wait))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
remove_wait_queue(sk_sleep(sk), &wait);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tls_push_sg(struct sock *sk,
|
|
|
|
struct tls_context *ctx,
|
|
|
|
struct scatterlist *sg,
|
|
|
|
u16 first_offset,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int sendpage_flags = flags | MSG_SENDPAGE_NOTLAST;
|
|
|
|
int ret = 0;
|
|
|
|
struct page *p;
|
|
|
|
size_t size;
|
|
|
|
int offset = first_offset;
|
|
|
|
|
|
|
|
size = sg->length - offset;
|
|
|
|
offset += sg->offset;
|
|
|
|
|
2018-05-01 20:05:39 +00:00
|
|
|
ctx->in_tcp_sendpages = true;
|
2017-06-14 18:37:39 +00:00
|
|
|
while (1) {
|
|
|
|
if (sg_is_last(sg))
|
|
|
|
sendpage_flags = flags;
|
|
|
|
|
|
|
|
/* is sending application-limited? */
|
|
|
|
tcp_rate_check_app_limited(sk);
|
|
|
|
p = sg_page(sg);
|
|
|
|
retry:
|
|
|
|
ret = do_tcp_sendpages(sk, p, offset, size, sendpage_flags);
|
|
|
|
|
|
|
|
if (ret != size) {
|
|
|
|
if (ret > 0) {
|
|
|
|
offset += ret;
|
|
|
|
size -= ret;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset -= sg->offset;
|
|
|
|
ctx->partially_sent_offset = offset;
|
|
|
|
ctx->partially_sent_record = (void *)sg;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
put_page(p);
|
|
|
|
sk_mem_uncharge(sk, sg->length);
|
|
|
|
sg = sg_next(sg);
|
|
|
|
if (!sg)
|
|
|
|
break;
|
|
|
|
|
|
|
|
offset = sg->offset;
|
|
|
|
size = sg->length;
|
|
|
|
}
|
|
|
|
|
|
|
|
clear_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags);
|
2018-05-01 20:05:39 +00:00
|
|
|
ctx->in_tcp_sendpages = false;
|
|
|
|
ctx->sk_write_space(sk);
|
2017-06-14 18:37:39 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tls_handle_open_record(struct sock *sk, int flags)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
|
|
|
|
if (tls_is_pending_open_record(ctx))
|
|
|
|
return ctx->push_pending_record(sk, flags);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
|
|
|
|
unsigned char *record_type)
|
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg;
|
|
|
|
int rc = -EINVAL;
|
|
|
|
|
|
|
|
for_each_cmsghdr(cmsg, msg) {
|
|
|
|
if (!CMSG_OK(msg, cmsg))
|
|
|
|
return -EINVAL;
|
|
|
|
if (cmsg->cmsg_level != SOL_TLS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (cmsg->cmsg_type) {
|
|
|
|
case TLS_SET_RECORD_TYPE:
|
|
|
|
if (cmsg->cmsg_len < CMSG_LEN(sizeof(*record_type)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (msg->msg_flags & MSG_MORE)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
rc = tls_handle_open_record(sk, msg->msg_flags);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
*record_type = *(unsigned char *)CMSG_DATA(cmsg);
|
|
|
|
rc = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
|
|
|
|
int flags, long *timeo)
|
|
|
|
{
|
|
|
|
struct scatterlist *sg;
|
|
|
|
u16 offset;
|
|
|
|
|
|
|
|
if (!tls_is_partially_sent_record(ctx))
|
|
|
|
return ctx->push_pending_record(sk, flags);
|
|
|
|
|
|
|
|
sg = ctx->partially_sent_record;
|
|
|
|
offset = ctx->partially_sent_offset;
|
|
|
|
|
|
|
|
ctx->partially_sent_record = NULL;
|
|
|
|
return tls_push_sg(sk, ctx, sg, offset, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tls_write_space(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
|
2018-05-01 20:05:39 +00:00
|
|
|
/* We are already sending pages, ignore notification */
|
|
|
|
if (ctx->in_tcp_sendpages)
|
|
|
|
return;
|
|
|
|
|
2017-06-14 18:37:39 +00:00
|
|
|
if (!sk->sk_write_pending && tls_is_pending_closed_record(ctx)) {
|
|
|
|
gfp_t sk_allocation = sk->sk_allocation;
|
|
|
|
int rc;
|
|
|
|
long timeo = 0;
|
|
|
|
|
|
|
|
sk->sk_allocation = GFP_ATOMIC;
|
|
|
|
rc = tls_push_pending_closed_record(sk, ctx,
|
|
|
|
MSG_DONTWAIT |
|
|
|
|
MSG_NOSIGNAL,
|
|
|
|
&timeo);
|
|
|
|
sk->sk_allocation = sk_allocation;
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->sk_write_space(sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tls_sk_proto_close(struct sock *sk, long timeout)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
long timeo = sock_sndtimeo(sk, 0);
|
|
|
|
void (*sk_proto_close)(struct sock *sk, long timeout);
|
2018-05-05 15:35:04 +00:00
|
|
|
bool free_ctx = false;
|
2017-06-14 18:37:39 +00:00
|
|
|
|
|
|
|
lock_sock(sk);
|
2017-11-13 08:22:46 +00:00
|
|
|
sk_proto_close = ctx->sk_proto_close;
|
|
|
|
|
2018-05-05 15:35:04 +00:00
|
|
|
if (ctx->conf == TLS_BASE || ctx->conf == TLS_HW_RECORD) {
|
|
|
|
free_ctx = true;
|
2017-11-13 08:22:46 +00:00
|
|
|
goto skip_tx_cleanup;
|
|
|
|
}
|
2017-06-14 18:37:39 +00:00
|
|
|
|
|
|
|
if (!tls_complete_pending_work(sk, ctx, 0, &timeo))
|
|
|
|
tls_handle_open_record(sk, 0);
|
|
|
|
|
|
|
|
if (ctx->partially_sent_record) {
|
|
|
|
struct scatterlist *sg = ctx->partially_sent_record;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
put_page(sg_page(sg));
|
|
|
|
sk_mem_uncharge(sk, sg->length);
|
|
|
|
|
|
|
|
if (sg_is_last(sg))
|
|
|
|
break;
|
|
|
|
sg++;
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 08:22:46 +00:00
|
|
|
|
2018-03-22 17:10:06 +00:00
|
|
|
kfree(ctx->tx.rec_seq);
|
|
|
|
kfree(ctx->tx.iv);
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
kfree(ctx->rx.rec_seq);
|
|
|
|
kfree(ctx->rx.iv);
|
2017-06-14 18:37:39 +00:00
|
|
|
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
if (ctx->conf == TLS_SW_TX ||
|
|
|
|
ctx->conf == TLS_SW_RX ||
|
|
|
|
ctx->conf == TLS_SW_RXTX) {
|
|
|
|
tls_sw_free_resources(sk);
|
|
|
|
}
|
2017-06-14 18:37:39 +00:00
|
|
|
|
2017-11-13 08:22:46 +00:00
|
|
|
skip_tx_cleanup:
|
2017-06-14 18:37:39 +00:00
|
|
|
release_sock(sk);
|
|
|
|
sk_proto_close(sk, timeout);
|
2018-03-31 16:11:52 +00:00
|
|
|
/* free ctx for TLS_HW_RECORD, used by tcp_set_state
|
|
|
|
* for sk->sk_prot->unhash [tls_hw_unhash]
|
|
|
|
*/
|
2018-05-05 15:35:04 +00:00
|
|
|
if (free_ctx)
|
2018-03-31 16:11:52 +00:00
|
|
|
kfree(ctx);
|
2017-06-14 18:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int do_tls_getsockopt_tx(struct sock *sk, char __user *optval,
|
|
|
|
int __user *optlen)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
struct tls_crypto_info *crypto_info;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (get_user(len, optlen))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (!optval || (len < sizeof(*crypto_info))) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ctx) {
|
|
|
|
rc = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get user crypto info */
|
|
|
|
crypto_info = &ctx->crypto_send;
|
|
|
|
|
|
|
|
if (!TLS_CRYPTO_INFO_READY(crypto_info)) {
|
|
|
|
rc = -EBUSY;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2017-07-06 04:56:36 +00:00
|
|
|
if (len == sizeof(*crypto_info)) {
|
2017-06-23 10:15:44 +00:00
|
|
|
if (copy_to_user(optval, crypto_info, sizeof(*crypto_info)))
|
|
|
|
rc = -EFAULT;
|
2017-06-14 18:37:39 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (crypto_info->cipher_type) {
|
|
|
|
case TLS_CIPHER_AES_GCM_128: {
|
|
|
|
struct tls12_crypto_info_aes_gcm_128 *
|
|
|
|
crypto_info_aes_gcm_128 =
|
|
|
|
container_of(crypto_info,
|
|
|
|
struct tls12_crypto_info_aes_gcm_128,
|
|
|
|
info);
|
|
|
|
|
|
|
|
if (len != sizeof(*crypto_info_aes_gcm_128)) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
lock_sock(sk);
|
2018-02-14 08:46:06 +00:00
|
|
|
memcpy(crypto_info_aes_gcm_128->iv,
|
2018-03-22 17:10:06 +00:00
|
|
|
ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
|
2017-06-14 18:37:39 +00:00
|
|
|
TLS_CIPHER_AES_GCM_128_IV_SIZE);
|
2018-03-22 17:10:06 +00:00
|
|
|
memcpy(crypto_info_aes_gcm_128->rec_seq, ctx->tx.rec_seq,
|
2018-02-14 08:46:08 +00:00
|
|
|
TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
|
2017-06-14 18:37:39 +00:00
|
|
|
release_sock(sk);
|
2017-06-23 10:15:44 +00:00
|
|
|
if (copy_to_user(optval,
|
|
|
|
crypto_info_aes_gcm_128,
|
|
|
|
sizeof(*crypto_info_aes_gcm_128)))
|
|
|
|
rc = -EFAULT;
|
2017-06-14 18:37:39 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rc = -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_tls_getsockopt(struct sock *sk, int optname,
|
|
|
|
char __user *optval, int __user *optlen)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
switch (optname) {
|
|
|
|
case TLS_TX:
|
|
|
|
rc = do_tls_getsockopt_tx(sk, optval, optlen);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = -ENOPROTOOPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tls_getsockopt(struct sock *sk, int level, int optname,
|
|
|
|
char __user *optval, int __user *optlen)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
|
|
|
|
if (level != SOL_TLS)
|
|
|
|
return ctx->getsockopt(sk, level, optname, optval, optlen);
|
|
|
|
|
|
|
|
return do_tls_getsockopt(sk, optname, optval, optlen);
|
|
|
|
}
|
|
|
|
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
|
|
|
|
unsigned int optlen, int tx)
|
2017-06-14 18:37:39 +00:00
|
|
|
{
|
2017-11-13 08:22:48 +00:00
|
|
|
struct tls_crypto_info *crypto_info;
|
2017-06-14 18:37:39 +00:00
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
int rc = 0;
|
2018-03-22 17:10:26 +00:00
|
|
|
int conf;
|
2017-06-14 18:37:39 +00:00
|
|
|
|
|
|
|
if (!optval || (optlen < sizeof(*crypto_info))) {
|
|
|
|
rc = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
if (tx)
|
|
|
|
crypto_info = &ctx->crypto_send;
|
|
|
|
else
|
|
|
|
crypto_info = &ctx->crypto_recv;
|
|
|
|
|
2017-11-13 08:22:48 +00:00
|
|
|
/* Currently we don't support set crypto info more than one time */
|
2018-01-16 15:04:27 +00:00
|
|
|
if (TLS_CRYPTO_INFO_READY(crypto_info)) {
|
|
|
|
rc = -EBUSY;
|
2017-11-13 08:22:48 +00:00
|
|
|
goto out;
|
2018-01-16 15:04:27 +00:00
|
|
|
}
|
2017-11-13 08:22:48 +00:00
|
|
|
|
|
|
|
rc = copy_from_user(crypto_info, optval, sizeof(*crypto_info));
|
2017-06-14 18:37:39 +00:00
|
|
|
if (rc) {
|
|
|
|
rc = -EFAULT;
|
2018-02-14 08:46:07 +00:00
|
|
|
goto err_crypto_info;
|
2017-06-14 18:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check version */
|
2017-11-13 08:22:48 +00:00
|
|
|
if (crypto_info->version != TLS_1_2_VERSION) {
|
2017-06-14 18:37:39 +00:00
|
|
|
rc = -ENOTSUPP;
|
2017-11-13 08:22:48 +00:00
|
|
|
goto err_crypto_info;
|
2017-06-14 18:37:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 08:22:48 +00:00
|
|
|
switch (crypto_info->cipher_type) {
|
2017-06-14 18:37:39 +00:00
|
|
|
case TLS_CIPHER_AES_GCM_128: {
|
|
|
|
if (optlen != sizeof(struct tls12_crypto_info_aes_gcm_128)) {
|
|
|
|
rc = -EINVAL;
|
tls: reset crypto_info when do_tls_setsockopt_tx fails
The current code copies directly from userspace to ctx->crypto_send, but
doesn't always reinitialize it to 0 on failure. This causes any
subsequent attempt to use this setsockopt to fail because of the
TLS_CRYPTO_INFO_READY check, eventhough crypto_info is not actually
ready.
This should result in a correctly set up socket after the 3rd call, but
currently it does not:
size_t s = sizeof(struct tls12_crypto_info_aes_gcm_128);
struct tls12_crypto_info_aes_gcm_128 crypto_good = {
.info.version = TLS_1_2_VERSION,
.info.cipher_type = TLS_CIPHER_AES_GCM_128,
};
struct tls12_crypto_info_aes_gcm_128 crypto_bad_type = crypto_good;
crypto_bad_type.info.cipher_type = 42;
setsockopt(sock, SOL_TLS, TLS_TX, &crypto_bad_type, s);
setsockopt(sock, SOL_TLS, TLS_TX, &crypto_good, s - 1);
setsockopt(sock, SOL_TLS, TLS_TX, &crypto_good, s);
Fixes: 3c4d7559159b ("tls: kernel TLS support")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-01-16 15:04:28 +00:00
|
|
|
goto err_crypto_info;
|
2017-06-14 18:37:39 +00:00
|
|
|
}
|
2017-11-13 08:22:48 +00:00
|
|
|
rc = copy_from_user(crypto_info + 1, optval + sizeof(*crypto_info),
|
|
|
|
optlen - sizeof(*crypto_info));
|
2017-06-14 18:37:39 +00:00
|
|
|
if (rc) {
|
|
|
|
rc = -EFAULT;
|
|
|
|
goto err_crypto_info;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rc = -EINVAL;
|
tls: reset crypto_info when do_tls_setsockopt_tx fails
The current code copies directly from userspace to ctx->crypto_send, but
doesn't always reinitialize it to 0 on failure. This causes any
subsequent attempt to use this setsockopt to fail because of the
TLS_CRYPTO_INFO_READY check, eventhough crypto_info is not actually
ready.
This should result in a correctly set up socket after the 3rd call, but
currently it does not:
size_t s = sizeof(struct tls12_crypto_info_aes_gcm_128);
struct tls12_crypto_info_aes_gcm_128 crypto_good = {
.info.version = TLS_1_2_VERSION,
.info.cipher_type = TLS_CIPHER_AES_GCM_128,
};
struct tls12_crypto_info_aes_gcm_128 crypto_bad_type = crypto_good;
crypto_bad_type.info.cipher_type = 42;
setsockopt(sock, SOL_TLS, TLS_TX, &crypto_bad_type, s);
setsockopt(sock, SOL_TLS, TLS_TX, &crypto_good, s - 1);
setsockopt(sock, SOL_TLS, TLS_TX, &crypto_good, s);
Fixes: 3c4d7559159b ("tls: kernel TLS support")
Signed-off-by: Sabrina Dubroca <sd@queasysnail.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-01-16 15:04:28 +00:00
|
|
|
goto err_crypto_info;
|
2017-06-14 18:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* currently SW is default, we will have ethtool in future */
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
if (tx) {
|
|
|
|
rc = tls_set_sw_offload(sk, ctx, 1);
|
|
|
|
if (ctx->conf == TLS_SW_RX)
|
|
|
|
conf = TLS_SW_RXTX;
|
|
|
|
else
|
|
|
|
conf = TLS_SW_TX;
|
|
|
|
} else {
|
|
|
|
rc = tls_set_sw_offload(sk, ctx, 0);
|
|
|
|
if (ctx->conf == TLS_SW_TX)
|
|
|
|
conf = TLS_SW_RXTX;
|
|
|
|
else
|
|
|
|
conf = TLS_SW_RX;
|
|
|
|
}
|
|
|
|
|
2017-06-14 18:37:39 +00:00
|
|
|
if (rc)
|
|
|
|
goto err_crypto_info;
|
|
|
|
|
2018-03-22 17:10:26 +00:00
|
|
|
ctx->conf = conf;
|
2017-11-13 08:22:45 +00:00
|
|
|
update_sk_prot(sk, ctx);
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
if (tx) {
|
|
|
|
ctx->sk_write_space = sk->sk_write_space;
|
|
|
|
sk->sk_write_space = tls_write_space;
|
|
|
|
} else {
|
|
|
|
sk->sk_socket->ops = &tls_sw_proto_ops;
|
|
|
|
}
|
2017-06-14 18:37:39 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
err_crypto_info:
|
|
|
|
memset(crypto_info, 0, sizeof(*crypto_info));
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_tls_setsockopt(struct sock *sk, int optname,
|
|
|
|
char __user *optval, unsigned int optlen)
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
switch (optname) {
|
|
|
|
case TLS_TX:
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
case TLS_RX:
|
2017-06-14 18:37:39 +00:00
|
|
|
lock_sock(sk);
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
rc = do_tls_setsockopt_conf(sk, optval, optlen,
|
|
|
|
optname == TLS_TX);
|
2017-06-14 18:37:39 +00:00
|
|
|
release_sock(sk);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rc = -ENOPROTOOPT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tls_setsockopt(struct sock *sk, int level, int optname,
|
|
|
|
char __user *optval, unsigned int optlen)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
|
|
|
|
if (level != SOL_TLS)
|
|
|
|
return ctx->setsockopt(sk, level, optname, optval, optlen);
|
|
|
|
|
|
|
|
return do_tls_setsockopt(sk, optname, optval, optlen);
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:11:52 +00:00
|
|
|
static struct tls_context *create_ctx(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct inet_connection_sock *icsk = inet_csk(sk);
|
|
|
|
struct tls_context *ctx;
|
|
|
|
|
|
|
|
ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
|
|
|
|
if (!ctx)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
icsk->icsk_ulp_data = ctx;
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tls_hw_prot(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx;
|
|
|
|
struct tls_device *dev;
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
mutex_lock(&device_mutex);
|
|
|
|
list_for_each_entry(dev, &device_list, dev_list) {
|
|
|
|
if (dev->feature && dev->feature(dev)) {
|
|
|
|
ctx = create_ctx(sk);
|
|
|
|
if (!ctx)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ctx->hash = sk->sk_prot->hash;
|
|
|
|
ctx->unhash = sk->sk_prot->unhash;
|
|
|
|
ctx->sk_proto_close = sk->sk_prot->close;
|
|
|
|
ctx->conf = TLS_HW_RECORD;
|
|
|
|
update_sk_prot(sk, ctx);
|
|
|
|
rc = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
mutex_unlock(&device_mutex);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tls_hw_unhash(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
struct tls_device *dev;
|
|
|
|
|
|
|
|
mutex_lock(&device_mutex);
|
|
|
|
list_for_each_entry(dev, &device_list, dev_list) {
|
|
|
|
if (dev->unhash)
|
|
|
|
dev->unhash(dev, sk);
|
|
|
|
}
|
|
|
|
mutex_unlock(&device_mutex);
|
|
|
|
ctx->unhash(sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tls_hw_hash(struct sock *sk)
|
|
|
|
{
|
|
|
|
struct tls_context *ctx = tls_get_ctx(sk);
|
|
|
|
struct tls_device *dev;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = ctx->hash(sk);
|
|
|
|
mutex_lock(&device_mutex);
|
|
|
|
list_for_each_entry(dev, &device_list, dev_list) {
|
|
|
|
if (dev->hash)
|
|
|
|
err |= dev->hash(dev, sk);
|
|
|
|
}
|
|
|
|
mutex_unlock(&device_mutex);
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
tls_hw_unhash(sk);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-02-27 12:18:39 +00:00
|
|
|
static void build_protos(struct proto *prot, struct proto *base)
|
|
|
|
{
|
2018-03-22 17:10:26 +00:00
|
|
|
prot[TLS_BASE] = *base;
|
|
|
|
prot[TLS_BASE].setsockopt = tls_setsockopt;
|
|
|
|
prot[TLS_BASE].getsockopt = tls_getsockopt;
|
|
|
|
prot[TLS_BASE].close = tls_sk_proto_close;
|
2018-02-27 12:18:39 +00:00
|
|
|
|
2018-03-22 17:10:26 +00:00
|
|
|
prot[TLS_SW_TX] = prot[TLS_BASE];
|
2018-02-27 12:18:39 +00:00
|
|
|
prot[TLS_SW_TX].sendmsg = tls_sw_sendmsg;
|
|
|
|
prot[TLS_SW_TX].sendpage = tls_sw_sendpage;
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
|
|
|
|
prot[TLS_SW_RX] = prot[TLS_BASE];
|
|
|
|
prot[TLS_SW_RX].recvmsg = tls_sw_recvmsg;
|
|
|
|
prot[TLS_SW_RX].close = tls_sk_proto_close;
|
|
|
|
|
|
|
|
prot[TLS_SW_RXTX] = prot[TLS_SW_TX];
|
|
|
|
prot[TLS_SW_RXTX].recvmsg = tls_sw_recvmsg;
|
|
|
|
prot[TLS_SW_RXTX].close = tls_sk_proto_close;
|
2018-03-31 16:11:52 +00:00
|
|
|
|
|
|
|
prot[TLS_HW_RECORD] = *base;
|
|
|
|
prot[TLS_HW_RECORD].hash = tls_hw_hash;
|
|
|
|
prot[TLS_HW_RECORD].unhash = tls_hw_unhash;
|
|
|
|
prot[TLS_HW_RECORD].close = tls_sk_proto_close;
|
2018-02-27 12:18:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 18:37:39 +00:00
|
|
|
static int tls_init(struct sock *sk)
|
|
|
|
{
|
2018-02-27 12:18:39 +00:00
|
|
|
int ip_ver = sk->sk_family == AF_INET6 ? TLSV6 : TLSV4;
|
2017-06-14 18:37:39 +00:00
|
|
|
struct tls_context *ctx;
|
|
|
|
int rc = 0;
|
|
|
|
|
2018-03-31 16:11:52 +00:00
|
|
|
if (tls_hw_prot(sk))
|
|
|
|
goto out;
|
|
|
|
|
2018-01-16 13:31:52 +00:00
|
|
|
/* The TLS ulp is currently supported only for TCP sockets
|
|
|
|
* in ESTABLISHED state.
|
|
|
|
* Supporting sockets in LISTEN state will require us
|
|
|
|
* to modify the accept implementation to clone rather then
|
|
|
|
* share the ulp context.
|
|
|
|
*/
|
|
|
|
if (sk->sk_state != TCP_ESTABLISHED)
|
|
|
|
return -ENOTSUPP;
|
|
|
|
|
2017-06-14 18:37:39 +00:00
|
|
|
/* allocate tls context */
|
2018-03-31 16:11:52 +00:00
|
|
|
ctx = create_ctx(sk);
|
2017-06-14 18:37:39 +00:00
|
|
|
if (!ctx) {
|
|
|
|
rc = -ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ctx->setsockopt = sk->sk_prot->setsockopt;
|
|
|
|
ctx->getsockopt = sk->sk_prot->getsockopt;
|
2017-11-13 08:22:46 +00:00
|
|
|
ctx->sk_proto_close = sk->sk_prot->close;
|
2017-11-13 08:22:45 +00:00
|
|
|
|
2018-02-27 12:18:39 +00:00
|
|
|
/* Build IPv6 TLS whenever the address of tcpv6_prot changes */
|
|
|
|
if (ip_ver == TLSV6 &&
|
|
|
|
unlikely(sk->sk_prot != smp_load_acquire(&saved_tcpv6_prot))) {
|
|
|
|
mutex_lock(&tcpv6_prot_mutex);
|
|
|
|
if (likely(sk->sk_prot != saved_tcpv6_prot)) {
|
|
|
|
build_protos(tls_prots[TLSV6], sk->sk_prot);
|
|
|
|
smp_store_release(&saved_tcpv6_prot, sk->sk_prot);
|
|
|
|
}
|
|
|
|
mutex_unlock(&tcpv6_prot_mutex);
|
|
|
|
}
|
|
|
|
|
2018-03-22 17:10:26 +00:00
|
|
|
ctx->conf = TLS_BASE;
|
2017-11-13 08:22:45 +00:00
|
|
|
update_sk_prot(sk, ctx);
|
2017-06-14 18:37:39 +00:00
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:11:52 +00:00
|
|
|
void tls_register_device(struct tls_device *device)
|
|
|
|
{
|
|
|
|
mutex_lock(&device_mutex);
|
|
|
|
list_add_tail(&device->dev_list, &device_list);
|
|
|
|
mutex_unlock(&device_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tls_register_device);
|
|
|
|
|
|
|
|
void tls_unregister_device(struct tls_device *device)
|
|
|
|
{
|
|
|
|
mutex_lock(&device_mutex);
|
|
|
|
list_del(&device->dev_list);
|
|
|
|
mutex_unlock(&device_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(tls_unregister_device);
|
|
|
|
|
2017-06-14 18:37:39 +00:00
|
|
|
static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
|
|
|
|
.name = "tls",
|
2018-02-05 18:17:43 +00:00
|
|
|
.uid = TCP_ULP_TLS,
|
|
|
|
.user_visible = true,
|
2017-06-14 18:37:39 +00:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.init = tls_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __init tls_register(void)
|
|
|
|
{
|
2018-02-27 12:18:39 +00:00
|
|
|
build_protos(tls_prots[TLSV4], &tcp_prot);
|
2017-06-14 18:37:39 +00:00
|
|
|
|
tls: RX path for ktls
Add rx path for tls software implementation.
recvmsg, splice_read, and poll implemented.
An additional sockopt TLS_RX is added, with the same interface as
TLS_TX. Either TLX_RX or TLX_TX may be provided separately, or
together (with two different setsockopt calls with appropriate keys).
Control messages are passed via CMSG in a similar way to transmit.
If no cmsg buffer is passed, then only application data records
will be passed to userspace, and EIO is returned for other types of
alerts.
EBADMSG is passed for decryption errors, and EMSGSIZE is passed for
framing too big, and EBADMSG for framing too small (matching openssl
semantics). EINVAL is returned for TLS versions that do not match the
original setsockopt call. All are unrecoverable.
strparser is used to parse TLS framing. Decryption is done directly
in to userspace buffers if they are large enough to support it, otherwise
sk_cow_data is called (similar to ipsec), and buffers are decrypted in
place and copied. splice_read always decrypts in place, since no
buffers are provided to decrypt in to.
sk_poll is overridden, and only returns POLLIN if a full TLS message is
received. Otherwise we wait for strparser to finish reading a full frame.
Actual decryption is only done during recvmsg or splice_read calls.
Signed-off-by: Dave Watson <davejwatson@fb.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-03-22 17:10:35 +00:00
|
|
|
tls_sw_proto_ops = inet_stream_ops;
|
|
|
|
tls_sw_proto_ops.poll = tls_sw_poll;
|
|
|
|
tls_sw_proto_ops.splice_read = tls_sw_splice_read;
|
|
|
|
|
2017-06-14 18:37:39 +00:00
|
|
|
tcp_register_ulp(&tcp_tls_ulp_ops);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit tls_unregister(void)
|
|
|
|
{
|
|
|
|
tcp_unregister_ulp(&tcp_tls_ulp_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
module_init(tls_register);
|
|
|
|
module_exit(tls_unregister);
|