tls.h 20.5 KB
Newer Older
D
Dave Watson 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * 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.
 */

#ifndef _TLS_OFFLOAD_H
#define _TLS_OFFLOAD_H

#include <linux/types.h>
38
#include <asm/byteorder.h>
39
#include <linux/crypto.h>
40 41
#include <linux/socket.h>
#include <linux/tcp.h>
42
#include <linux/skmsg.h>
J
Jakub Kicinski 已提交
43
#include <linux/mutex.h>
44
#include <linux/netdevice.h>
45
#include <linux/rcupdate.h>
46

47
#include <net/net_namespace.h>
48
#include <net/tcp.h>
D
Dave Watson 已提交
49
#include <net/strparser.h>
50
#include <crypto/aead.h>
D
Dave Watson 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <uapi/linux/tls.h>


/* Maximum data size carried in a TLS record */
#define TLS_MAX_PAYLOAD_SIZE		((size_t)1 << 14)

#define TLS_HEADER_SIZE			5
#define TLS_NONCE_OFFSET		TLS_HEADER_SIZE

#define TLS_CRYPTO_INFO_READY(info)	((info)->cipher_type)

#define TLS_RECORD_TYPE_DATA		0x17

#define TLS_AAD_SPACE_SIZE		13
A
Atul Gupta 已提交
65

66
#define MAX_IV_SIZE			16
67
#define TLS_MAX_REC_SEQ_SIZE		8
68 69 70 71 72 73 74 75 76 77

/* For AES-CCM, the full 16-bytes of IV is made of '4' fields of given sizes.
 *
 * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3]
 *
 * The field 'length' is encoded in field 'b0' as '(length width - 1)'.
 * Hence b0 contains (3 - 1) = 2.
 */
#define TLS_AES_CCM_IV_B0_BYTE		2

78 79 80 81 82 83 84 85 86
#define __TLS_INC_STATS(net, field)				\
	__SNMP_INC_STATS((net)->mib.tls_statistics, field)
#define TLS_INC_STATS(net, field)				\
	SNMP_INC_STATS((net)->mib.tls_statistics, field)
#define __TLS_DEC_STATS(net, field)				\
	__SNMP_DEC_STATS((net)->mib.tls_statistics, field)
#define TLS_DEC_STATS(net, field)				\
	SNMP_DEC_STATS((net)->mib.tls_statistics, field)

87 88 89 90 91 92 93 94
enum {
	TLS_BASE,
	TLS_SW,
	TLS_HW,
	TLS_HW_RECORD,
	TLS_NUM_CONFIG,
};

95 96 97 98 99 100
/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
 * allocated or mapped for each TLS record. After encryption, the records are
 * stores in a linked list.
 */
struct tls_rec {
	struct list_head list;
101
	int tx_ready;
102
	int tx_flags;
D
Dave Watson 已提交
103

104 105
	struct sk_msg msg_plaintext;
	struct sk_msg msg_encrypted;
106

107 108 109 110
	/* AAD | msg_plaintext.sg.data | sg_tag */
	struct scatterlist sg_aead_in[2];
	/* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
	struct scatterlist sg_aead_out[2];
111

D
Dave Watson 已提交
112 113 114
	char content_type;
	struct scatterlist sg_content_type;

115
	char aad_space[TLS_AAD_SPACE_SIZE];
116
	u8 iv_data[MAX_IV_SIZE];
117 118 119 120
	struct aead_request aead_req;
	u8 aead_req_ctx[];
};

121 122 123 124 125
struct tls_msg {
	struct strp_msg rxm;
	u8 control;
};

126 127 128 129 130 131 132 133 134 135
struct tx_work {
	struct delayed_work work;
	struct sock *sk;
};

struct tls_sw_context_tx {
	struct crypto_aead *aead_send;
	struct crypto_wait async_wait;
	struct tx_work tx_work;
	struct tls_rec *open_rec;
136
	struct list_head tx_list;
137
	atomic_t encrypt_pending;
138 139
	/* protect crypto_wait with encrypt_pending */
	spinlock_t encrypt_compl_lock;
140
	int async_notify;
141
	u8 async_capable:1;
142 143

#define BIT_TX_SCHEDULED	0
144
#define BIT_TX_CLOSING		1
145
	unsigned long tx_bitmask;
D
Dave Watson 已提交
146 147
};

B
Boris Pismenny 已提交
148 149 150 151
struct tls_sw_context_rx {
	struct crypto_aead *aead_recv;
	struct crypto_wait async_wait;
	struct strparser strp;
152
	struct sk_buff_head rx_list;	/* list of decrypted 'data' records */
B
Boris Pismenny 已提交
153
	void (*saved_data_ready)(struct sock *sk);
154

B
Boris Pismenny 已提交
155 156
	struct sk_buff *recv_pkt;
	u8 control;
157
	u8 async_capable:1;
158
	u8 decrypted:1;
159
	atomic_t decrypt_pending;
160 161
	/* protect crypto_wait with decrypt_pending*/
	spinlock_t decrypt_compl_lock;
162 163 164
	bool async_notify;
};

165 166 167 168 169 170 171 172
struct tls_record_info {
	struct list_head list;
	u32 end_seq;
	int len;
	int num_frags;
	skb_frag_t frags[MAX_SKB_FRAGS];
};

173
struct tls_offload_context_tx {
174 175 176 177 178 179 180 181 182 183
	struct crypto_aead *aead_send;
	spinlock_t lock;	/* protects records list */
	struct list_head records_list;
	struct tls_record_info *open_record;
	struct tls_record_info *retransmit_hint;
	u64 hint_record_sn;
	u64 unacked_record_sn;

	struct scatterlist sg_tx_data[MAX_SKB_FRAGS];
	void (*sk_destruct)(struct sock *sk);
184
	u8 driver_state[] __aligned(8);
185 186 187 188
	/* The TLS layer reserves room for driver specific state
	 * Currently the belief is that there is not enough
	 * driver specific state to justify another layer of indirection
	 */
189
#define TLS_DRIVER_STATE_SIZE_TX	16
190 191
};

192
#define TLS_OFFLOAD_CONTEXT_SIZE_TX                                            \
193
	(sizeof(struct tls_offload_context_tx) + TLS_DRIVER_STATE_SIZE_TX)
194

195 196
enum tls_context_flags {
	TLS_RX_SYNC_RUNNING = 0,
197 198 199 200 201
	/* Unlike RX where resync is driven entirely by the core in TX only
	 * the driver knows when things went out of sync, so we need the flag
	 * to be atomic.
	 */
	TLS_TX_SYNC_SCHED = 1,
202 203
};

204 205 206 207 208
struct cipher_context {
	char *iv;
	char *rec_seq;
};

209 210
union tls_crypto_context {
	struct tls_crypto_info info;
D
Dave Watson 已提交
211 212 213
	union {
		struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
		struct tls12_crypto_info_aes_gcm_256 aes_gcm_256;
214
		struct tls12_crypto_info_chacha20_poly1305 chacha20_poly1305;
D
Dave Watson 已提交
215
	};
216 217
};

218 219 220 221 222 223 224
struct tls_prot_info {
	u16 version;
	u16 cipher_type;
	u16 prepend_size;
	u16 tag_size;
	u16 overhead_size;
	u16 iv_size;
225
	u16 salt_size;
226 227 228 229 230
	u16 rec_seq_size;
	u16 aad_size;
	u16 tail_size;
};

D
Dave Watson 已提交
231
struct tls_context {
232
	/* read-only cache line */
233 234
	struct tls_prot_info prot_info;

235 236
	u8 tx_conf:3;
	u8 rx_conf:3;
D
Dave Watson 已提交
237

238 239
	int (*push_pending_record)(struct sock *sk, int flags);
	void (*sk_write_space)(struct sock *sk);
B
Boris Pismenny 已提交
240 241 242

	void *priv_ctx_tx;
	void *priv_ctx_rx;
D
Dave Watson 已提交
243

244
	struct net_device *netdev;
245

246
	/* rw cache line */
247
	struct cipher_context tx;
D
Dave Watson 已提交
248
	struct cipher_context rx;
D
Dave Watson 已提交
249 250 251

	struct scatterlist *partially_sent_record;
	u16 partially_sent_offset;
252

253
	bool in_tcp_sendpages;
254
	bool pending_open_record_frags;
J
Jakub Kicinski 已提交
255 256 257 258

	struct mutex tx_lock; /* protects partially_sent_* fields and
			       * per-type TX fields
			       */
259
	unsigned long flags;
D
Dave Watson 已提交
260

261
	/* cache cold stuff */
262 263
	struct proto *sk_proto;

264
	void (*sk_destruct)(struct sock *sk);
265 266 267 268 269 270

	union tls_crypto_context crypto_send;
	union tls_crypto_context crypto_recv;

	struct list_head list;
	refcount_t refcount;
271
	struct rcu_head rcu;
D
Dave Watson 已提交
272 273
};

274 275 276 277 278 279 280 281 282 283 284 285 286
enum tls_offload_ctx_dir {
	TLS_OFFLOAD_CTX_DIR_RX,
	TLS_OFFLOAD_CTX_DIR_TX,
};

struct tlsdev_ops {
	int (*tls_dev_add)(struct net_device *netdev, struct sock *sk,
			   enum tls_offload_ctx_dir direction,
			   struct tls_crypto_info *crypto_info,
			   u32 start_offload_tcp_sn);
	void (*tls_dev_del)(struct net_device *netdev,
			    struct tls_context *ctx,
			    enum tls_offload_ctx_dir direction);
287 288 289
	int (*tls_dev_resync)(struct net_device *netdev,
			      struct sock *sk, u32 seq, u8 *rcd_sn,
			      enum tls_offload_ctx_dir direction);
290 291
};

292 293 294
enum tls_offload_sync_type {
	TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ = 0,
	TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT = 1,
295
	TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC = 2,
296 297 298 299 300
};

#define TLS_DEVICE_RESYNC_NH_START_IVAL		2
#define TLS_DEVICE_RESYNC_NH_MAX_IVAL		128

301 302 303
#define TLS_DEVICE_RESYNC_ASYNC_LOGMAX		13
struct tls_offload_resync_async {
	atomic64_t req;
304 305
	u16 loglen;
	u16 rcd_delta;
306 307 308
	u32 log[TLS_DEVICE_RESYNC_ASYNC_LOGMAX];
};

309 310 311
struct tls_offload_context_rx {
	/* sw must be the first member of tls_offload_context_rx */
	struct tls_sw_context_rx sw;
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	enum tls_offload_sync_type resync_type;
	/* this member is set regardless of resync_type, to avoid branches */
	u8 resync_nh_reset:1;
	/* CORE_NEXT_HINT-only member, but use the hole here */
	u8 resync_nh_do_now:1;
	union {
		/* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ */
		struct {
			atomic64_t resync_req;
		};
		/* TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT */
		struct {
			u32 decrypted_failed;
			u32 decrypted_tgt;
		} resync_nh;
327 328 329 330
		/* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC */
		struct {
			struct tls_offload_resync_async *resync_async;
		};
331
	};
332
	u8 driver_state[] __aligned(8);
333 334 335 336
	/* The TLS layer reserves room for driver specific state
	 * Currently the belief is that there is not enough
	 * driver specific state to justify another layer of indirection
	 */
337
#define TLS_DRIVER_STATE_SIZE_RX	8
338 339 340
};

#define TLS_OFFLOAD_CONTEXT_SIZE_RX					\
341
	(sizeof(struct tls_offload_context_rx) + TLS_DRIVER_STATE_SIZE_RX)
342

343
struct tls_context *tls_ctx_create(struct sock *sk);
344
void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
345 346
void update_sk_prot(struct sock *sk, struct tls_context *ctx);

D
Dave Watson 已提交
347 348 349 350 351 352
int wait_on_pending_writer(struct sock *sk, long *timeo);
int tls_sk_query(struct sock *sk, int optname, char __user *optval,
		int __user *optlen);
int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
		  unsigned int optlen);

D
Dave Watson 已提交
353
int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
354
void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
355
void tls_sw_strparser_done(struct tls_context *tls_ctx);
D
Dave Watson 已提交
356
int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
357 358
int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
			   int offset, size_t size, int flags);
D
Dave Watson 已提交
359 360
int tls_sw_sendpage(struct sock *sk, struct page *page,
		    int offset, size_t size, int flags);
361
void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
362 363
void tls_sw_release_resources_tx(struct sock *sk);
void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
B
Boris Pismenny 已提交
364
void tls_sw_free_resources_rx(struct sock *sk);
365
void tls_sw_release_resources_rx(struct sock *sk);
366
void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
D
Dave Watson 已提交
367 368
int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
		   int nonblock, int flags, int *addr_len);
369
bool tls_sw_stream_read(const struct sock *sk);
D
Dave Watson 已提交
370 371 372
ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
			   struct pipe_inode_info *pipe,
			   size_t len, unsigned int flags);
D
Dave Watson 已提交
373

374 375 376
int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
int tls_device_sendpage(struct sock *sk, struct page *page,
			int offset, size_t size, int flags);
377
int tls_tx_records(struct sock *sk, int flags);
378

379
struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
380 381 382 383 384 385 386 387 388 389 390
				       u32 seq, u64 *p_record_sn);

static inline bool tls_record_is_start_marker(struct tls_record_info *rec)
{
	return rec->len == 0;
}

static inline u32 tls_record_start_seq(struct tls_record_info *rec)
{
	return rec->end_seq - rec->len;
}
D
Dave Watson 已提交
391 392 393 394

int tls_push_sg(struct sock *sk, struct tls_context *ctx,
		struct scatterlist *sg, u16 first_offset,
		int flags);
395 396
int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
			    int flags);
397
void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
398

399 400 401 402 403
static inline struct tls_msg *tls_msg(struct sk_buff *skb)
{
	return (struct tls_msg *)strp_msg(skb);
}

404
static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
D
Dave Watson 已提交
405
{
406
	return !!ctx->partially_sent_record;
D
Dave Watson 已提交
407 408 409 410 411 412 413
}

static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
{
	return tls_ctx->pending_open_record_frags;
}

414
static inline bool is_tx_ready(struct tls_sw_context_tx *ctx)
415 416 417
{
	struct tls_rec *rec;

418
	rec = list_first_entry(&ctx->tx_list, struct tls_rec, list);
419 420 421
	if (!rec)
		return false;

422
	return READ_ONCE(rec->tx_ready);
423 424
}

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
static inline u16 tls_user_config(struct tls_context *ctx, bool tx)
{
	u16 config = tx ? ctx->tx_conf : ctx->rx_conf;

	switch (config) {
	case TLS_BASE:
		return TLS_CONF_BASE;
	case TLS_SW:
		return TLS_CONF_SW;
	case TLS_HW:
		return TLS_CONF_HW;
	case TLS_HW_RECORD:
		return TLS_CONF_HW_RECORD;
	}
	return 0;
}

442 443 444 445
struct sk_buff *
tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
		      struct sk_buff *skb);

446 447
static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
{
448
#ifdef CONFIG_SOCK_VALIDATE_XMIT
449
	return sk_fullsock(sk) &&
450 451 452 453 454
	       (smp_load_acquire(&sk->sk_validate_xmit_skb) ==
	       &tls_validate_xmit_skb);
#else
	return false;
#endif
455 456
}

457
static inline void tls_err_abort(struct sock *sk, int err)
D
Dave Watson 已提交
458
{
459
	sk->sk_err = err;
D
Dave Watson 已提交
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	sk->sk_error_report(sk);
}

static inline bool tls_bigint_increment(unsigned char *seq, int len)
{
	int i;

	for (i = len - 1; i >= 0; i--) {
		++seq[i];
		if (seq[i] != 0)
			break;
	}

	return (i == -1);
}

476 477 478 479 480 481 482 483 484 485 486 487
static inline void tls_bigint_subtract(unsigned char *seq, int  n)
{
	u64 rcd_sn;
	__be64 *p;

	BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);

	p = (__be64 *)seq;
	rcd_sn = be64_to_cpu(*p);
	*p = cpu_to_be64(rcd_sn - n);
}

488 489 490 491
static inline struct tls_context *tls_get_ctx(const struct sock *sk)
{
	struct inet_connection_sock *icsk = inet_csk(sk);

492 493 494 495
	/* Use RCU on icsk_ulp_data only for sock diag code,
	 * TLS data path doesn't need rcu_dereference().
	 */
	return (__force void *)icsk->icsk_ulp_data;
496 497
}

D
Dave Watson 已提交
498
static inline void tls_advance_record_sn(struct sock *sk,
499 500
					 struct tls_prot_info *prot,
					 struct cipher_context *ctx)
D
Dave Watson 已提交
501
{
502
	if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
503
		tls_err_abort(sk, EBADMSG);
D
Dave Watson 已提交
504

505 506
	if (prot->version != TLS_1_3_VERSION &&
	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305)
507
		tls_bigint_increment(ctx->iv + prot->salt_size,
508
				     prot->iv_size);
D
Dave Watson 已提交
509 510 511 512 513
}

static inline void tls_fill_prepend(struct tls_context *ctx,
			     char *buf,
			     size_t plaintext_len,
514
			     unsigned char record_type)
D
Dave Watson 已提交
515
{
516 517
	struct tls_prot_info *prot = &ctx->prot_info;
	size_t pkt_len, iv_size = prot->iv_size;
D
Dave Watson 已提交
518

519
	pkt_len = plaintext_len + prot->tag_size;
520 521
	if (prot->version != TLS_1_3_VERSION &&
	    prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) {
D
Dave Watson 已提交
522 523 524
		pkt_len += iv_size;

		memcpy(buf + TLS_NONCE_OFFSET,
525
		       ctx->tx.iv + prot->salt_size, iv_size);
D
Dave Watson 已提交
526
	}
D
Dave Watson 已提交
527 528 529 530

	/* we cover nonce explicit here as well, so buf should be of
	 * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
	 */
531
	buf[0] = prot->version == TLS_1_3_VERSION ?
D
Dave Watson 已提交
532 533 534 535
		   TLS_RECORD_TYPE_DATA : record_type;
	/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
	buf[1] = TLS_1_2_VERSION_MINOR;
	buf[2] = TLS_1_2_VERSION_MAJOR;
D
Dave Watson 已提交
536 537 538 539 540
	/* we can use IV for nonce explicit according to spec */
	buf[3] = pkt_len >> 8;
	buf[4] = pkt_len & 0xFF;
}

541 542 543
static inline void tls_make_aad(char *buf,
				size_t size,
				char *record_sequence,
D
Dave Watson 已提交
544
				unsigned char record_type,
545
				struct tls_prot_info *prot)
D
Dave Watson 已提交
546
{
547 548
	if (prot->version != TLS_1_3_VERSION) {
		memcpy(buf, record_sequence, prot->rec_seq_size);
D
Dave Watson 已提交
549 550
		buf += 8;
	} else {
551
		size += prot->tag_size;
D
Dave Watson 已提交
552 553
	}

554
	buf[0] = prot->version == TLS_1_3_VERSION ?
D
Dave Watson 已提交
555 556 557 558 559 560 561
		  TLS_RECORD_TYPE_DATA : record_type;
	buf[1] = TLS_1_2_VERSION_MAJOR;
	buf[2] = TLS_1_2_VERSION_MINOR;
	buf[3] = size >> 8;
	buf[4] = size & 0xFF;
}

562
static inline void xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq)
563
{
D
Dave Watson 已提交
564
	int i;
565

566 567
	if (prot->version == TLS_1_3_VERSION ||
	    prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) {
D
Dave Watson 已提交
568 569 570
		for (i = 0; i < 8; i++)
			iv[i + 4] ^= seq[i];
	}
571 572
}

D
Dave Watson 已提交
573

B
Boris Pismenny 已提交
574 575 576 577 578 579 580
static inline struct tls_sw_context_rx *tls_sw_ctx_rx(
		const struct tls_context *tls_ctx)
{
	return (struct tls_sw_context_rx *)tls_ctx->priv_ctx_rx;
}

static inline struct tls_sw_context_tx *tls_sw_ctx_tx(
D
Dave Watson 已提交
581 582
		const struct tls_context *tls_ctx)
{
B
Boris Pismenny 已提交
583
	return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx;
D
Dave Watson 已提交
584 585
}

586 587
static inline struct tls_offload_context_tx *
tls_offload_ctx_tx(const struct tls_context *tls_ctx)
D
Dave Watson 已提交
588
{
589
	return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx;
D
Dave Watson 已提交
590 591
}

592 593 594 595 596 597 598 599 600
static inline bool tls_sw_has_ctx_tx(const struct sock *sk)
{
	struct tls_context *ctx = tls_get_ctx(sk);

	if (!ctx)
		return false;
	return !!tls_sw_ctx_tx(ctx);
}

601 602 603 604 605 606 607 608 609
static inline bool tls_sw_has_ctx_rx(const struct sock *sk)
{
	struct tls_context *ctx = tls_get_ctx(sk);

	if (!ctx)
		return false;
	return !!tls_sw_ctx_rx(ctx);
}

B
Boris Pismenny 已提交
610 611 612
void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
void tls_device_write_space(struct sock *sk, struct tls_context *ctx);

613 614 615 616 617 618
static inline struct tls_offload_context_rx *
tls_offload_ctx_rx(const struct tls_context *tls_ctx)
{
	return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx;
}

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
#if IS_ENABLED(CONFIG_TLS_DEVICE)
static inline void *__tls_driver_ctx(struct tls_context *tls_ctx,
				     enum tls_offload_ctx_dir direction)
{
	if (direction == TLS_OFFLOAD_CTX_DIR_TX)
		return tls_offload_ctx_tx(tls_ctx)->driver_state;
	else
		return tls_offload_ctx_rx(tls_ctx)->driver_state;
}

static inline void *
tls_driver_ctx(const struct sock *sk, enum tls_offload_ctx_dir direction)
{
	return __tls_driver_ctx(tls_get_ctx(sk), direction);
}
#endif

636 637
#define RESYNC_REQ BIT(0)
#define RESYNC_REQ_ASYNC BIT(1)
638 639 640 641 642 643
/* The TLS context is valid until sk_destruct is called */
static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
{
	struct tls_context *tls_ctx = tls_get_ctx(sk);
	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);

644 645 646 647 648 649 650 651 652 653 654
	atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | RESYNC_REQ);
}

/* Log all TLS record header TCP sequences in [seq, seq+len] */
static inline void
tls_offload_rx_resync_async_request_start(struct sock *sk, __be32 seq, u16 len)
{
	struct tls_context *tls_ctx = tls_get_ctx(sk);
	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);

	atomic64_set(&rx_ctx->resync_async->req, ((u64)ntohl(seq) << 32) |
655
		     ((u64)len << 16) | RESYNC_REQ | RESYNC_REQ_ASYNC);
656
	rx_ctx->resync_async->loglen = 0;
657
	rx_ctx->resync_async->rcd_delta = 0;
658 659 660 661 662 663 664 665 666 667
}

static inline void
tls_offload_rx_resync_async_request_end(struct sock *sk, __be32 seq)
{
	struct tls_context *tls_ctx = tls_get_ctx(sk);
	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);

	atomic64_set(&rx_ctx->resync_async->req,
		     ((u64)ntohl(seq) << 32) | RESYNC_REQ);
668 669
}

670 671 672 673 674 675 676
static inline void
tls_offload_rx_resync_set_type(struct sock *sk, enum tls_offload_sync_type type)
{
	struct tls_context *tls_ctx = tls_get_ctx(sk);

	tls_offload_ctx_rx(tls_ctx)->resync_type = type;
}
677

678 679 680 681 682 683 684 685 686 687 688
/* Driver's seq tracking has to be disabled until resync succeeded */
static inline bool tls_offload_tx_resync_pending(struct sock *sk)
{
	struct tls_context *tls_ctx = tls_get_ctx(sk);
	bool ret;

	ret = test_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
	smp_mb__after_atomic();
	return ret;
}

689 690 691
int __net_init tls_proc_init(struct net *net);
void __net_exit tls_proc_fini(struct net *net);

D
Dave Watson 已提交
692 693
int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
		      unsigned char *record_type);
694 695
int decrypt_skb(struct sock *sk, struct sk_buff *skb,
		struct scatterlist *sgout);
696
struct sk_buff *tls_encrypt_skb(struct sk_buff *skb);
D
Dave Watson 已提交
697

698
int tls_sw_fallback_init(struct sock *sk,
699
			 struct tls_offload_context_tx *offload_ctx,
700 701
			 struct tls_crypto_info *crypto_info);

702 703 704
#ifdef CONFIG_TLS_DEVICE
void tls_device_init(void);
void tls_device_cleanup(void);
705
void tls_device_sk_destruct(struct sock *sk);
706 707
int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
void tls_device_free_resources_tx(struct sock *sk);
708 709
int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
void tls_device_offload_cleanup_rx(struct sock *sk);
710
void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
711
void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq);
712 713
int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx,
			 struct sk_buff *skb, struct strp_msg *rxm);
714 715 716 717 718 719 720 721

static inline bool tls_is_sk_rx_device_offloaded(struct sock *sk)
{
	if (!sk_fullsock(sk) ||
	    smp_load_acquire(&sk->sk_destruct) != tls_device_sk_destruct)
		return false;
	return tls_get_ctx(sk)->rx_conf == TLS_HW;
}
722 723 724
#else
static inline void tls_device_init(void) {}
static inline void tls_device_cleanup(void) {}
725

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
static inline int
tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
{
	return -EOPNOTSUPP;
}

static inline void tls_device_free_resources_tx(struct sock *sk) {}

static inline int
tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
{
	return -EOPNOTSUPP;
}

static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
static inline void
tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}

744 745 746
static inline int
tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx,
		     struct sk_buff *skb, struct strp_msg *rxm)
747 748 749 750
{
	return 0;
}
#endif
D
Dave Watson 已提交
751
#endif /* _TLS_OFFLOAD_H */