Skip to content

Commit c46234e

Browse files
Dave Watsondavem330
Dave Watson
authored andcommitted
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>
1 parent 5837158 commit c46234e

File tree

5 files changed

+609
-70
lines changed

5 files changed

+609
-70
lines changed

include/net/tls.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <linux/socket.h>
4141
#include <linux/tcp.h>
4242
#include <net/tcp.h>
43+
#include <net/strparser.h>
4344

4445
#include <uapi/linux/tls.h>
4546

@@ -58,8 +59,18 @@
5859

5960
struct tls_sw_context {
6061
struct crypto_aead *aead_send;
62+
struct crypto_aead *aead_recv;
6163
struct crypto_wait async_wait;
6264

65+
/* Receive context */
66+
struct strparser strp;
67+
void (*saved_data_ready)(struct sock *sk);
68+
unsigned int (*sk_poll)(struct file *file, struct socket *sock,
69+
struct poll_table_struct *wait);
70+
struct sk_buff *recv_pkt;
71+
u8 control;
72+
bool decrypted;
73+
6374
/* Sending context */
6475
char aad_space[TLS_AAD_SPACE_SIZE];
6576

@@ -96,12 +107,17 @@ struct tls_context {
96107
struct tls_crypto_info crypto_send;
97108
struct tls12_crypto_info_aes_gcm_128 crypto_send_aes_gcm_128;
98109
};
110+
union {
111+
struct tls_crypto_info crypto_recv;
112+
struct tls12_crypto_info_aes_gcm_128 crypto_recv_aes_gcm_128;
113+
};
99114

100115
void *priv_ctx;
101116

102117
u8 conf:2;
103118

104119
struct cipher_context tx;
120+
struct cipher_context rx;
105121

106122
struct scatterlist *partially_sent_record;
107123
u16 partially_sent_offset;
@@ -128,12 +144,19 @@ int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
128144
unsigned int optlen);
129145

130146

131-
int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx);
147+
int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
132148
int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
133149
int tls_sw_sendpage(struct sock *sk, struct page *page,
134150
int offset, size_t size, int flags);
135151
void tls_sw_close(struct sock *sk, long timeout);
136-
void tls_sw_free_tx_resources(struct sock *sk);
152+
void tls_sw_free_resources(struct sock *sk);
153+
int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
154+
int nonblock, int flags, int *addr_len);
155+
unsigned int tls_sw_poll(struct file *file, struct socket *sock,
156+
struct poll_table_struct *wait);
157+
ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
158+
struct pipe_inode_info *pipe,
159+
size_t len, unsigned int flags);
137160

138161
void tls_sk_destruct(struct sock *sk, struct tls_context *ctx);
139162
void tls_icsk_clean_acked(struct sock *sk);

include/uapi/linux/tls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
/* TLS socket options */
4040
#define TLS_TX 1 /* Set transmit parameters */
41+
#define TLS_RX 2 /* Set receive parameters */
4142

4243
/* Supported versions */
4344
#define TLS_VERSION_MINOR(ver) ((ver) & 0xFF)
@@ -59,6 +60,7 @@
5960
#define TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE 8
6061

6162
#define TLS_SET_RECORD_TYPE 1
63+
#define TLS_GET_RECORD_TYPE 2
6264

6365
struct tls_crypto_info {
6466
__u16 version;

net/tls/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ config TLS
77
select CRYPTO
88
select CRYPTO_AES
99
select CRYPTO_GCM
10+
select STREAM_PARSER
1011
default n
1112
---help---
1213
Enable kernel support for TLS protocol. This allows symmetric

net/tls/tls_main.c

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,15 @@ enum {
5454
enum {
5555
TLS_BASE,
5656
TLS_SW_TX,
57+
TLS_SW_RX,
58+
TLS_SW_RXTX,
5759
TLS_NUM_CONFIG,
5860
};
5961

6062
static struct proto *saved_tcpv6_prot;
6163
static DEFINE_MUTEX(tcpv6_prot_mutex);
6264
static struct proto tls_prots[TLS_NUM_PROTS][TLS_NUM_CONFIG];
65+
static struct proto_ops tls_sw_proto_ops;
6366

6467
static inline void update_sk_prot(struct sock *sk, struct tls_context *ctx)
6568
{
@@ -261,9 +264,14 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
261264

262265
kfree(ctx->tx.rec_seq);
263266
kfree(ctx->tx.iv);
267+
kfree(ctx->rx.rec_seq);
268+
kfree(ctx->rx.iv);
264269

265-
if (ctx->conf == TLS_SW_TX)
266-
tls_sw_free_tx_resources(sk);
270+
if (ctx->conf == TLS_SW_TX ||
271+
ctx->conf == TLS_SW_RX ||
272+
ctx->conf == TLS_SW_RXTX) {
273+
tls_sw_free_resources(sk);
274+
}
267275

268276
skip_tx_cleanup:
269277
release_sock(sk);
@@ -365,8 +373,8 @@ static int tls_getsockopt(struct sock *sk, int level, int optname,
365373
return do_tls_getsockopt(sk, optname, optval, optlen);
366374
}
367375

368-
static int do_tls_setsockopt_tx(struct sock *sk, char __user *optval,
369-
unsigned int optlen)
376+
static int do_tls_setsockopt_conf(struct sock *sk, char __user *optval,
377+
unsigned int optlen, int tx)
370378
{
371379
struct tls_crypto_info *crypto_info;
372380
struct tls_context *ctx = tls_get_ctx(sk);
@@ -378,7 +386,11 @@ static int do_tls_setsockopt_tx(struct sock *sk, char __user *optval,
378386
goto out;
379387
}
380388

381-
crypto_info = &ctx->crypto_send;
389+
if (tx)
390+
crypto_info = &ctx->crypto_send;
391+
else
392+
crypto_info = &ctx->crypto_recv;
393+
382394
/* Currently we don't support set crypto info more than one time */
383395
if (TLS_CRYPTO_INFO_READY(crypto_info)) {
384396
rc = -EBUSY;
@@ -417,15 +429,31 @@ static int do_tls_setsockopt_tx(struct sock *sk, char __user *optval,
417429
}
418430

419431
/* currently SW is default, we will have ethtool in future */
420-
rc = tls_set_sw_offload(sk, ctx);
421-
conf = TLS_SW_TX;
432+
if (tx) {
433+
rc = tls_set_sw_offload(sk, ctx, 1);
434+
if (ctx->conf == TLS_SW_RX)
435+
conf = TLS_SW_RXTX;
436+
else
437+
conf = TLS_SW_TX;
438+
} else {
439+
rc = tls_set_sw_offload(sk, ctx, 0);
440+
if (ctx->conf == TLS_SW_TX)
441+
conf = TLS_SW_RXTX;
442+
else
443+
conf = TLS_SW_RX;
444+
}
445+
422446
if (rc)
423447
goto err_crypto_info;
424448

425449
ctx->conf = conf;
426450
update_sk_prot(sk, ctx);
427-
ctx->sk_write_space = sk->sk_write_space;
428-
sk->sk_write_space = tls_write_space;
451+
if (tx) {
452+
ctx->sk_write_space = sk->sk_write_space;
453+
sk->sk_write_space = tls_write_space;
454+
} else {
455+
sk->sk_socket->ops = &tls_sw_proto_ops;
456+
}
429457
goto out;
430458

431459
err_crypto_info:
@@ -441,8 +469,10 @@ static int do_tls_setsockopt(struct sock *sk, int optname,
441469

442470
switch (optname) {
443471
case TLS_TX:
472+
case TLS_RX:
444473
lock_sock(sk);
445-
rc = do_tls_setsockopt_tx(sk, optval, optlen);
474+
rc = do_tls_setsockopt_conf(sk, optval, optlen,
475+
optname == TLS_TX);
446476
release_sock(sk);
447477
break;
448478
default:
@@ -473,6 +503,14 @@ static void build_protos(struct proto *prot, struct proto *base)
473503
prot[TLS_SW_TX] = prot[TLS_BASE];
474504
prot[TLS_SW_TX].sendmsg = tls_sw_sendmsg;
475505
prot[TLS_SW_TX].sendpage = tls_sw_sendpage;
506+
507+
prot[TLS_SW_RX] = prot[TLS_BASE];
508+
prot[TLS_SW_RX].recvmsg = tls_sw_recvmsg;
509+
prot[TLS_SW_RX].close = tls_sk_proto_close;
510+
511+
prot[TLS_SW_RXTX] = prot[TLS_SW_TX];
512+
prot[TLS_SW_RXTX].recvmsg = tls_sw_recvmsg;
513+
prot[TLS_SW_RXTX].close = tls_sk_proto_close;
476514
}
477515

478516
static int tls_init(struct sock *sk)
@@ -531,6 +569,10 @@ static int __init tls_register(void)
531569
{
532570
build_protos(tls_prots[TLSV4], &tcp_prot);
533571

572+
tls_sw_proto_ops = inet_stream_ops;
573+
tls_sw_proto_ops.poll = tls_sw_poll;
574+
tls_sw_proto_ops.splice_read = tls_sw_splice_read;
575+
534576
tcp_register_ulp(&tcp_tls_ulp_ops);
535577

536578
return 0;

0 commit comments

Comments
 (0)