Skip to content

Commit 452a261

Browse files
committed
Merge branch 'tls-RX'
Dave Watson says: ==================== TLS Rx TLS tcp socket RX implementation, to match existing TX code. This patchset completes the software TLS socket, allowing full bi-directional communication over TLS using normal socket syscalls, after the handshake has been done in userspace. Only the symmetric encryption is done in the kernel. This allows usage of TLS sockets from within the kernel (for example with network block device, or from bpf). Performance can be better than userspace, with appropriate crypto routines [1]. sk->sk_socket->ops must be overridden to implement splice_read and poll, but otherwise the interface & implementation match TX closely. strparser is used to parse TLS framing on receive. There are Openssl RX patches that work with this interface [2], as well as a testing tool using the socket interface directly (without cmsg support) [3]. An example tcp socket setup is: // Normal tcp socket connect/accept, and TLS handshake // using any TLS library. setsockopt(sock, SOL_TCP, TCP_ULP, "tls", sizeof("tls")); struct tls12_crypto_info_aes_gcm_128 crypto_info_rx; // Fill in crypto_info based on negotiated keys. setsockopt(sock, SOL_TLS, TLS_RX, &crypto_info, sizeof(crypto_info_rx)); // You can optionally TLX_TX as well. char buffer[16384]; int ret = recv(sock, buffer, 16384); // cmsg can be received using recvmsg and a msg_control // of type TLS_GET_RECORD_TYPE will be set. V1 -> V2 * For too-small framing errors, return EBADMSG, to match openssl error code semantics. Docs and commit logs about this also updated. RFC -> V1 * Refactor 'tx' variable names to drop tx * Error return codes changed per discussion * Only call skb_cow_data based on in-place decryption, drop unnecessary frag list check. [1] Recent crypto patchset to remove copies, resulting in optimally zero copies vs. userspace's one, vs. previous kernel's two. https://marc.info/?l=linux-crypto-vger&m=151931242406416&w=2 [2] https://github.com/Mellanox/openssl/commits/tls_rx2 [3] https://github.com/ktls/af_ktls-tool/tree/RX ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents ae06c70 + b6c535b commit 452a261

File tree

6 files changed

+740
-126
lines changed

6 files changed

+740
-126
lines changed

Documentation/networking/tls.txt

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ the transmit and the receive into the kernel.
4848

4949
setsockopt(sock, SOL_TLS, TLS_TX, &crypto_info, sizeof(crypto_info));
5050

51+
Transmit and receive are set separately, but the setup is the same, using either
52+
TLS_TX or TLS_RX.
53+
5154
Sending TLS application data
5255
----------------------------
5356

@@ -79,6 +82,28 @@ for memory), or the encryption will always succeed. If send() returns
7982
-ENOMEM and some data was left on the socket buffer from a previous
8083
call using MSG_MORE, the MSG_MORE data is left on the socket buffer.
8184

85+
Receiving TLS application data
86+
------------------------------
87+
88+
After setting the TLS_RX socket option, all recv family socket calls
89+
are decrypted using TLS parameters provided. A full TLS record must
90+
be received before decryption can happen.
91+
92+
char buffer[16384];
93+
recv(sock, buffer, 16384);
94+
95+
Received data is decrypted directly in to the user buffer if it is
96+
large enough, and no additional allocations occur. If the userspace
97+
buffer is too small, data is decrypted in the kernel and copied to
98+
userspace.
99+
100+
EINVAL is returned if the TLS version in the received message does not
101+
match the version passed in setsockopt.
102+
103+
EMSGSIZE is returned if the received message is too big.
104+
105+
EBADMSG is returned if decryption failed for any other reason.
106+
82107
Send TLS control messages
83108
-------------------------
84109

@@ -118,6 +143,43 @@ using a record of type @record_type.
118143
Control message data should be provided unencrypted, and will be
119144
encrypted by the kernel.
120145

146+
Receiving TLS control messages
147+
------------------------------
148+
149+
TLS control messages are passed in the userspace buffer, with message
150+
type passed via cmsg. If no cmsg buffer is provided, an error is
151+
returned if a control message is received. Data messages may be
152+
received without a cmsg buffer set.
153+
154+
char buffer[16384];
155+
char cmsg[CMSG_SPACE(sizeof(unsigned char))];
156+
struct msghdr msg = {0};
157+
msg.msg_control = cmsg;
158+
msg.msg_controllen = sizeof(cmsg);
159+
160+
struct iovec msg_iov;
161+
msg_iov.iov_base = buffer;
162+
msg_iov.iov_len = 16384;
163+
164+
msg.msg_iov = &msg_iov;
165+
msg.msg_iovlen = 1;
166+
167+
int ret = recvmsg(sock, &msg, 0 /* flags */);
168+
169+
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
170+
if (cmsg->cmsg_level == SOL_TLS &&
171+
cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
172+
int record_type = *((unsigned char *)CMSG_DATA(cmsg));
173+
// Do something with record_type, and control message data in
174+
// buffer.
175+
//
176+
// Note that record_type may be == to application data (23).
177+
} else {
178+
// Buffer contains application data.
179+
}
180+
181+
recv will never return data from mixed types of TLS records.
182+
121183
Integrating in to userspace TLS library
122184
---------------------------------------
123185

@@ -126,10 +188,10 @@ layer of a userspace TLS library.
126188

127189
A patchset to OpenSSL to use ktls as the record layer is here:
128190

129-
https://github.com/Mellanox/tls-openssl
191+
https://github.com/Mellanox/openssl/commits/tls_rx2
130192

131193
An example of calling send directly after a handshake using
132194
gnutls. Since it doesn't implement a full record layer, control
133195
messages are not supported:
134196

135-
https://github.com/Mellanox/tls-af_ktls_tool
197+
https://github.com/ktls/af_ktls-tool/commits/RX

include/net/tls.h

Lines changed: 44 additions & 17 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

@@ -81,23 +92,32 @@ enum {
8192
TLS_PENDING_CLOSED_RECORD
8293
};
8394

95+
struct cipher_context {
96+
u16 prepend_size;
97+
u16 tag_size;
98+
u16 overhead_size;
99+
u16 iv_size;
100+
char *iv;
101+
u16 rec_seq_size;
102+
char *rec_seq;
103+
};
104+
84105
struct tls_context {
85106
union {
86107
struct tls_crypto_info crypto_send;
87108
struct tls12_crypto_info_aes_gcm_128 crypto_send_aes_gcm_128;
88109
};
110+
union {
111+
struct tls_crypto_info crypto_recv;
112+
struct tls12_crypto_info_aes_gcm_128 crypto_recv_aes_gcm_128;
113+
};
89114

90115
void *priv_ctx;
91116

92-
u8 tx_conf:2;
117+
u8 conf:2;
93118

94-
u16 prepend_size;
95-
u16 tag_size;
96-
u16 overhead_size;
97-
u16 iv_size;
98-
char *iv;
99-
u16 rec_seq_size;
100-
char *rec_seq;
119+
struct cipher_context tx;
120+
struct cipher_context rx;
101121

102122
struct scatterlist *partially_sent_record;
103123
u16 partially_sent_offset;
@@ -124,12 +144,19 @@ int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
124144
unsigned int optlen);
125145

126146

127-
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);
128148
int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
129149
int tls_sw_sendpage(struct sock *sk, struct page *page,
130150
int offset, size_t size, int flags);
131151
void tls_sw_close(struct sock *sk, long timeout);
132-
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);
133160

134161
void tls_sk_destruct(struct sock *sk, struct tls_context *ctx);
135162
void tls_icsk_clean_acked(struct sock *sk);
@@ -170,9 +197,9 @@ static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
170197
return tls_ctx->pending_open_record_frags;
171198
}
172199

173-
static inline void tls_err_abort(struct sock *sk)
200+
static inline void tls_err_abort(struct sock *sk, int err)
174201
{
175-
sk->sk_err = EBADMSG;
202+
sk->sk_err = err;
176203
sk->sk_error_report(sk);
177204
}
178205

@@ -190,10 +217,10 @@ static inline bool tls_bigint_increment(unsigned char *seq, int len)
190217
}
191218

192219
static inline void tls_advance_record_sn(struct sock *sk,
193-
struct tls_context *ctx)
220+
struct cipher_context *ctx)
194221
{
195222
if (tls_bigint_increment(ctx->rec_seq, ctx->rec_seq_size))
196-
tls_err_abort(sk);
223+
tls_err_abort(sk, EBADMSG);
197224
tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
198225
ctx->iv_size);
199226
}
@@ -203,9 +230,9 @@ static inline void tls_fill_prepend(struct tls_context *ctx,
203230
size_t plaintext_len,
204231
unsigned char record_type)
205232
{
206-
size_t pkt_len, iv_size = ctx->iv_size;
233+
size_t pkt_len, iv_size = ctx->tx.iv_size;
207234

208-
pkt_len = plaintext_len + iv_size + ctx->tag_size;
235+
pkt_len = plaintext_len + iv_size + ctx->tx.tag_size;
209236

210237
/* we cover nonce explicit here as well, so buf should be of
211238
* size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
@@ -217,7 +244,7 @@ static inline void tls_fill_prepend(struct tls_context *ctx,
217244
buf[3] = pkt_len >> 8;
218245
buf[4] = pkt_len & 0xFF;
219246
memcpy(buf + TLS_NONCE_OFFSET,
220-
ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
247+
ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
221248
}
222249

223250
static inline void tls_make_aad(char *buf,

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

0 commit comments

Comments
 (0)