• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved.
3  * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved.
4  *
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:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
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.
23  *
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
31  * SOFTWARE.
32  */
33 
34 #ifndef _TLS_OFFLOAD_H
35 #define _TLS_OFFLOAD_H
36 
37 #include <linux/types.h>
38 #include <asm/byteorder.h>
39 #include <linux/crypto.h>
40 #include <linux/socket.h>
41 #include <linux/tcp.h>
42 #include <linux/skmsg.h>
43 #include <linux/mutex.h>
44 #include <linux/netdevice.h>
45 #include <linux/rcupdate.h>
46 #include <linux/android_kabi.h>
47 
48 #include <net/net_namespace.h>
49 #include <net/tcp.h>
50 #include <net/strparser.h>
51 #include <crypto/aead.h>
52 #include <uapi/linux/tls.h>
53 
54 
55 /* Maximum data size carried in a TLS record */
56 #define TLS_MAX_PAYLOAD_SIZE		((size_t)1 << 14)
57 
58 #define TLS_HEADER_SIZE			5
59 #define TLS_NONCE_OFFSET		TLS_HEADER_SIZE
60 
61 #define TLS_CRYPTO_INFO_READY(info)	((info)->cipher_type)
62 
63 #define TLS_RECORD_TYPE_DATA		0x17
64 
65 #define TLS_AAD_SPACE_SIZE		13
66 
67 #define MAX_IV_SIZE			16
68 #define TLS_MAX_REC_SEQ_SIZE		8
69 
70 /* For AES-CCM, the full 16-bytes of IV is made of '4' fields of given sizes.
71  *
72  * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3]
73  *
74  * The field 'length' is encoded in field 'b0' as '(length width - 1)'.
75  * Hence b0 contains (3 - 1) = 2.
76  */
77 #define TLS_AES_CCM_IV_B0_BYTE		2
78 
79 #define __TLS_INC_STATS(net, field)				\
80 	__SNMP_INC_STATS((net)->mib.tls_statistics, field)
81 #define TLS_INC_STATS(net, field)				\
82 	SNMP_INC_STATS((net)->mib.tls_statistics, field)
83 #define __TLS_DEC_STATS(net, field)				\
84 	__SNMP_DEC_STATS((net)->mib.tls_statistics, field)
85 #define TLS_DEC_STATS(net, field)				\
86 	SNMP_DEC_STATS((net)->mib.tls_statistics, field)
87 
88 enum {
89 	TLS_BASE,
90 	TLS_SW,
91 	TLS_HW,
92 	TLS_HW_RECORD,
93 	TLS_NUM_CONFIG,
94 };
95 
96 /* TLS records are maintained in 'struct tls_rec'. It stores the memory pages
97  * allocated or mapped for each TLS record. After encryption, the records are
98  * stores in a linked list.
99  */
100 struct tls_rec {
101 	struct list_head list;
102 	int tx_ready;
103 	int tx_flags;
104 
105 	struct sk_msg msg_plaintext;
106 	struct sk_msg msg_encrypted;
107 
108 	/* AAD | msg_plaintext.sg.data | sg_tag */
109 	struct scatterlist sg_aead_in[2];
110 	/* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */
111 	struct scatterlist sg_aead_out[2];
112 
113 	char content_type;
114 	struct scatterlist sg_content_type;
115 
116 	char aad_space[TLS_AAD_SPACE_SIZE];
117 	u8 iv_data[MAX_IV_SIZE];
118 	struct aead_request aead_req;
119 
120 	ANDROID_KABI_RESERVE(1);
121 
122 	u8 aead_req_ctx[];
123 };
124 
125 struct tls_msg {
126 	struct strp_msg rxm;
127 	u8 control;
128 };
129 
130 struct tx_work {
131 	struct delayed_work work;
132 	struct sock *sk;
133 };
134 
135 struct tls_sw_context_tx {
136 	struct crypto_aead *aead_send;
137 	struct crypto_wait async_wait;
138 	struct tx_work tx_work;
139 	struct tls_rec *open_rec;
140 	struct list_head tx_list;
141 	atomic_t encrypt_pending;
142 	/* protect crypto_wait with encrypt_pending */
143 	spinlock_t encrypt_compl_lock;
144 	int async_notify;
145 	u8 async_capable:1;
146 
147 #define BIT_TX_SCHEDULED	0
148 #define BIT_TX_CLOSING		1
149 	unsigned long tx_bitmask;
150 
151 	ANDROID_KABI_RESERVE(1);
152 };
153 
154 struct tls_sw_context_rx {
155 	struct crypto_aead *aead_recv;
156 	struct crypto_wait async_wait;
157 	struct strparser strp;
158 	struct sk_buff_head rx_list;	/* list of decrypted 'data' records */
159 	void (*saved_data_ready)(struct sock *sk);
160 
161 	struct sk_buff *recv_pkt;
162 	u8 control;
163 	u8 async_capable:1;
164 	u8 decrypted:1;
165 	atomic_t decrypt_pending;
166 	/* protect crypto_wait with decrypt_pending*/
167 	spinlock_t decrypt_compl_lock;
168 	bool async_notify;
169 
170 	ANDROID_KABI_RESERVE(1);
171 };
172 
173 struct tls_record_info {
174 	struct list_head list;
175 	u32 end_seq;
176 	int len;
177 	int num_frags;
178 	skb_frag_t frags[MAX_SKB_FRAGS];
179 };
180 
181 struct tls_offload_context_tx {
182 	struct crypto_aead *aead_send;
183 	spinlock_t lock;	/* protects records list */
184 	struct list_head records_list;
185 	struct tls_record_info *open_record;
186 	struct tls_record_info *retransmit_hint;
187 	u64 hint_record_sn;
188 	u64 unacked_record_sn;
189 
190 	struct scatterlist sg_tx_data[MAX_SKB_FRAGS];
191 	void (*sk_destruct)(struct sock *sk);
192 	u8 driver_state[] __aligned(8);
193 	/* The TLS layer reserves room for driver specific state
194 	 * Currently the belief is that there is not enough
195 	 * driver specific state to justify another layer of indirection
196 	 */
197 #define TLS_DRIVER_STATE_SIZE_TX	16
198 };
199 
200 #define TLS_OFFLOAD_CONTEXT_SIZE_TX                                            \
201 	(sizeof(struct tls_offload_context_tx) + TLS_DRIVER_STATE_SIZE_TX)
202 
203 enum tls_context_flags {
204 	/* tls_device_down was called after the netdev went down, device state
205 	 * was released, and kTLS works in software, even though rx_conf is
206 	 * still TLS_HW (needed for transition).
207 	 */
208 	TLS_RX_DEV_DEGRADED = 0,
209 	/* Unlike RX where resync is driven entirely by the core in TX only
210 	 * the driver knows when things went out of sync, so we need the flag
211 	 * to be atomic.
212 	 */
213 	TLS_TX_SYNC_SCHED = 1,
214 	/* tls_dev_del was called for the RX side, device state was released,
215 	 * but tls_ctx->netdev might still be kept, because TX-side driver
216 	 * resources might not be released yet. Used to prevent the second
217 	 * tls_dev_del call in tls_device_down if it happens simultaneously.
218 	 */
219 	TLS_RX_DEV_CLOSED = 2,
220 };
221 
222 struct cipher_context {
223 	char *iv;
224 	char *rec_seq;
225 };
226 
227 union tls_crypto_context {
228 	struct tls_crypto_info info;
229 	union {
230 		struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
231 		struct tls12_crypto_info_aes_gcm_256 aes_gcm_256;
232 	};
233 };
234 
235 struct tls_prot_info {
236 	u16 version;
237 	u16 cipher_type;
238 	u16 prepend_size;
239 	u16 tag_size;
240 	u16 overhead_size;
241 	u16 iv_size;
242 	u16 salt_size;
243 	u16 rec_seq_size;
244 	u16 aad_size;
245 	u16 tail_size;
246 };
247 
248 struct tls_context {
249 	/* read-only cache line */
250 	struct tls_prot_info prot_info;
251 
252 	u8 tx_conf:3;
253 	u8 rx_conf:3;
254 
255 	int (*push_pending_record)(struct sock *sk, int flags);
256 	void (*sk_write_space)(struct sock *sk);
257 
258 	void *priv_ctx_tx;
259 	void *priv_ctx_rx;
260 
261 	struct net_device *netdev;
262 
263 	/* rw cache line */
264 	struct cipher_context tx;
265 	struct cipher_context rx;
266 
267 	struct scatterlist *partially_sent_record;
268 	u16 partially_sent_offset;
269 
270 	bool in_tcp_sendpages;
271 	bool pending_open_record_frags;
272 
273 	struct mutex tx_lock; /* protects partially_sent_* fields and
274 			       * per-type TX fields
275 			       */
276 	unsigned long flags;
277 
278 	/* cache cold stuff */
279 	struct proto *sk_proto;
280 	struct sock *sk;
281 
282 	void (*sk_destruct)(struct sock *sk);
283 
284 	union tls_crypto_context crypto_send;
285 	union tls_crypto_context crypto_recv;
286 
287 	struct list_head list;
288 	refcount_t refcount;
289 	struct rcu_head rcu;
290 };
291 
292 enum tls_offload_ctx_dir {
293 	TLS_OFFLOAD_CTX_DIR_RX,
294 	TLS_OFFLOAD_CTX_DIR_TX,
295 };
296 
297 struct tlsdev_ops {
298 	int (*tls_dev_add)(struct net_device *netdev, struct sock *sk,
299 			   enum tls_offload_ctx_dir direction,
300 			   struct tls_crypto_info *crypto_info,
301 			   u32 start_offload_tcp_sn);
302 	void (*tls_dev_del)(struct net_device *netdev,
303 			    struct tls_context *ctx,
304 			    enum tls_offload_ctx_dir direction);
305 	int (*tls_dev_resync)(struct net_device *netdev,
306 			      struct sock *sk, u32 seq, u8 *rcd_sn,
307 			      enum tls_offload_ctx_dir direction);
308 
309 	ANDROID_KABI_RESERVE(1);
310 	ANDROID_KABI_RESERVE(2);
311 	ANDROID_KABI_RESERVE(3);
312 	ANDROID_KABI_RESERVE(4);
313 
314 };
315 
316 enum tls_offload_sync_type {
317 	TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ = 0,
318 	TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT = 1,
319 	TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC = 2,
320 };
321 
322 #define TLS_DEVICE_RESYNC_NH_START_IVAL		2
323 #define TLS_DEVICE_RESYNC_NH_MAX_IVAL		128
324 
325 #define TLS_DEVICE_RESYNC_ASYNC_LOGMAX		13
326 struct tls_offload_resync_async {
327 	atomic64_t req;
328 	u16 loglen;
329 	u16 rcd_delta;
330 	u32 log[TLS_DEVICE_RESYNC_ASYNC_LOGMAX];
331 };
332 
333 struct tls_offload_context_rx {
334 	/* sw must be the first member of tls_offload_context_rx */
335 	struct tls_sw_context_rx sw;
336 	enum tls_offload_sync_type resync_type;
337 	/* this member is set regardless of resync_type, to avoid branches */
338 	u8 resync_nh_reset:1;
339 	/* CORE_NEXT_HINT-only member, but use the hole here */
340 	u8 resync_nh_do_now:1;
341 	union {
342 		/* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ */
343 		struct {
344 			atomic64_t resync_req;
345 		};
346 		/* TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT */
347 		struct {
348 			u32 decrypted_failed;
349 			u32 decrypted_tgt;
350 		} resync_nh;
351 		/* TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ_ASYNC */
352 		struct {
353 			struct tls_offload_resync_async *resync_async;
354 		};
355 	};
356 	u8 driver_state[] __aligned(8);
357 	/* The TLS layer reserves room for driver specific state
358 	 * Currently the belief is that there is not enough
359 	 * driver specific state to justify another layer of indirection
360 	 */
361 #define TLS_DRIVER_STATE_SIZE_RX	8
362 };
363 
364 #define TLS_OFFLOAD_CONTEXT_SIZE_RX					\
365 	(sizeof(struct tls_offload_context_rx) + TLS_DRIVER_STATE_SIZE_RX)
366 
367 struct tls_context *tls_ctx_create(struct sock *sk);
368 void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
369 void update_sk_prot(struct sock *sk, struct tls_context *ctx);
370 
371 int wait_on_pending_writer(struct sock *sk, long *timeo);
372 int tls_sk_query(struct sock *sk, int optname, char __user *optval,
373 		int __user *optlen);
374 int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
375 		  unsigned int optlen);
376 void tls_err_abort(struct sock *sk, int err);
377 
378 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
379 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
380 void tls_sw_strparser_done(struct tls_context *tls_ctx);
381 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
382 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
383 			   int offset, size_t size, int flags);
384 int tls_sw_sendpage(struct sock *sk, struct page *page,
385 		    int offset, size_t size, int flags);
386 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
387 void tls_sw_release_resources_tx(struct sock *sk);
388 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
389 void tls_sw_free_resources_rx(struct sock *sk);
390 void tls_sw_release_resources_rx(struct sock *sk);
391 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
392 int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
393 		   int nonblock, int flags, int *addr_len);
394 bool tls_sw_stream_read(const struct sock *sk);
395 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
396 			   struct pipe_inode_info *pipe,
397 			   size_t len, unsigned int flags);
398 
399 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
400 int tls_device_sendpage(struct sock *sk, struct page *page,
401 			int offset, size_t size, int flags);
402 int tls_tx_records(struct sock *sk, int flags);
403 
404 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
405 				       u32 seq, u64 *p_record_sn);
406 
tls_record_is_start_marker(struct tls_record_info * rec)407 static inline bool tls_record_is_start_marker(struct tls_record_info *rec)
408 {
409 	return rec->len == 0;
410 }
411 
tls_record_start_seq(struct tls_record_info * rec)412 static inline u32 tls_record_start_seq(struct tls_record_info *rec)
413 {
414 	return rec->end_seq - rec->len;
415 }
416 
417 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
418 		struct scatterlist *sg, u16 first_offset,
419 		int flags);
420 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
421 			    int flags);
422 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
423 
tls_msg(struct sk_buff * skb)424 static inline struct tls_msg *tls_msg(struct sk_buff *skb)
425 {
426 	return (struct tls_msg *)strp_msg(skb);
427 }
428 
tls_is_partially_sent_record(struct tls_context * ctx)429 static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
430 {
431 	return !!ctx->partially_sent_record;
432 }
433 
tls_is_pending_open_record(struct tls_context * tls_ctx)434 static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
435 {
436 	return tls_ctx->pending_open_record_frags;
437 }
438 
is_tx_ready(struct tls_sw_context_tx * ctx)439 static inline bool is_tx_ready(struct tls_sw_context_tx *ctx)
440 {
441 	struct tls_rec *rec;
442 
443 	rec = list_first_entry(&ctx->tx_list, struct tls_rec, list);
444 	if (!rec)
445 		return false;
446 
447 	return READ_ONCE(rec->tx_ready);
448 }
449 
tls_user_config(struct tls_context * ctx,bool tx)450 static inline u16 tls_user_config(struct tls_context *ctx, bool tx)
451 {
452 	u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
453 
454 	switch (config) {
455 	case TLS_BASE:
456 		return TLS_CONF_BASE;
457 	case TLS_SW:
458 		return TLS_CONF_SW;
459 	case TLS_HW:
460 		return TLS_CONF_HW;
461 	case TLS_HW_RECORD:
462 		return TLS_CONF_HW_RECORD;
463 	}
464 	return 0;
465 }
466 
467 struct sk_buff *
468 tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
469 		      struct sk_buff *skb);
470 struct sk_buff *
471 tls_validate_xmit_skb_sw(struct sock *sk, struct net_device *dev,
472 			 struct sk_buff *skb);
473 
tls_is_sk_tx_device_offloaded(struct sock * sk)474 static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
475 {
476 #ifdef CONFIG_SOCK_VALIDATE_XMIT
477 	return sk_fullsock(sk) &&
478 	       (smp_load_acquire(&sk->sk_validate_xmit_skb) ==
479 	       &tls_validate_xmit_skb);
480 #else
481 	return false;
482 #endif
483 }
484 
tls_bigint_increment(unsigned char * seq,int len)485 static inline bool tls_bigint_increment(unsigned char *seq, int len)
486 {
487 	int i;
488 
489 	for (i = len - 1; i >= 0; i--) {
490 		++seq[i];
491 		if (seq[i] != 0)
492 			break;
493 	}
494 
495 	return (i == -1);
496 }
497 
tls_bigint_subtract(unsigned char * seq,int n)498 static inline void tls_bigint_subtract(unsigned char *seq, int  n)
499 {
500 	u64 rcd_sn;
501 	__be64 *p;
502 
503 	BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);
504 
505 	p = (__be64 *)seq;
506 	rcd_sn = be64_to_cpu(*p);
507 	*p = cpu_to_be64(rcd_sn - n);
508 }
509 
tls_get_ctx(const struct sock * sk)510 static inline struct tls_context *tls_get_ctx(const struct sock *sk)
511 {
512 	struct inet_connection_sock *icsk = inet_csk(sk);
513 
514 	/* Use RCU on icsk_ulp_data only for sock diag code,
515 	 * TLS data path doesn't need rcu_dereference().
516 	 */
517 	return (__force void *)icsk->icsk_ulp_data;
518 }
519 
tls_advance_record_sn(struct sock * sk,struct tls_prot_info * prot,struct cipher_context * ctx)520 static inline void tls_advance_record_sn(struct sock *sk,
521 					 struct tls_prot_info *prot,
522 					 struct cipher_context *ctx)
523 {
524 	if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
525 		tls_err_abort(sk, -EBADMSG);
526 
527 	if (prot->version != TLS_1_3_VERSION)
528 		tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
529 				     prot->iv_size);
530 }
531 
tls_fill_prepend(struct tls_context * ctx,char * buf,size_t plaintext_len,unsigned char record_type,int version)532 static inline void tls_fill_prepend(struct tls_context *ctx,
533 			     char *buf,
534 			     size_t plaintext_len,
535 			     unsigned char record_type,
536 			     int version)
537 {
538 	struct tls_prot_info *prot = &ctx->prot_info;
539 	size_t pkt_len, iv_size = prot->iv_size;
540 
541 	pkt_len = plaintext_len + prot->tag_size;
542 	if (version != TLS_1_3_VERSION) {
543 		pkt_len += iv_size;
544 
545 		memcpy(buf + TLS_NONCE_OFFSET,
546 		       ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
547 	}
548 
549 	/* we cover nonce explicit here as well, so buf should be of
550 	 * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
551 	 */
552 	buf[0] = version == TLS_1_3_VERSION ?
553 		   TLS_RECORD_TYPE_DATA : record_type;
554 	/* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */
555 	buf[1] = TLS_1_2_VERSION_MINOR;
556 	buf[2] = TLS_1_2_VERSION_MAJOR;
557 	/* we can use IV for nonce explicit according to spec */
558 	buf[3] = pkt_len >> 8;
559 	buf[4] = pkt_len & 0xFF;
560 }
561 
tls_make_aad(char * buf,size_t size,char * record_sequence,int record_sequence_size,unsigned char record_type,int version)562 static inline void tls_make_aad(char *buf,
563 				size_t size,
564 				char *record_sequence,
565 				int record_sequence_size,
566 				unsigned char record_type,
567 				int version)
568 {
569 	if (version != TLS_1_3_VERSION) {
570 		memcpy(buf, record_sequence, record_sequence_size);
571 		buf += 8;
572 	} else {
573 		size += TLS_CIPHER_AES_GCM_128_TAG_SIZE;
574 	}
575 
576 	buf[0] = version == TLS_1_3_VERSION ?
577 		  TLS_RECORD_TYPE_DATA : record_type;
578 	buf[1] = TLS_1_2_VERSION_MAJOR;
579 	buf[2] = TLS_1_2_VERSION_MINOR;
580 	buf[3] = size >> 8;
581 	buf[4] = size & 0xFF;
582 }
583 
xor_iv_with_seq(int version,char * iv,char * seq)584 static inline void xor_iv_with_seq(int version, char *iv, char *seq)
585 {
586 	int i;
587 
588 	if (version == TLS_1_3_VERSION) {
589 		for (i = 0; i < 8; i++)
590 			iv[i + 4] ^= seq[i];
591 	}
592 }
593 
594 
tls_sw_ctx_rx(const struct tls_context * tls_ctx)595 static inline struct tls_sw_context_rx *tls_sw_ctx_rx(
596 		const struct tls_context *tls_ctx)
597 {
598 	return (struct tls_sw_context_rx *)tls_ctx->priv_ctx_rx;
599 }
600 
tls_sw_ctx_tx(const struct tls_context * tls_ctx)601 static inline struct tls_sw_context_tx *tls_sw_ctx_tx(
602 		const struct tls_context *tls_ctx)
603 {
604 	return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx;
605 }
606 
607 static inline struct tls_offload_context_tx *
tls_offload_ctx_tx(const struct tls_context * tls_ctx)608 tls_offload_ctx_tx(const struct tls_context *tls_ctx)
609 {
610 	return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx;
611 }
612 
tls_sw_has_ctx_tx(const struct sock * sk)613 static inline bool tls_sw_has_ctx_tx(const struct sock *sk)
614 {
615 	struct tls_context *ctx = tls_get_ctx(sk);
616 
617 	if (!ctx)
618 		return false;
619 	return !!tls_sw_ctx_tx(ctx);
620 }
621 
tls_sw_has_ctx_rx(const struct sock * sk)622 static inline bool tls_sw_has_ctx_rx(const struct sock *sk)
623 {
624 	struct tls_context *ctx = tls_get_ctx(sk);
625 
626 	if (!ctx)
627 		return false;
628 	return !!tls_sw_ctx_rx(ctx);
629 }
630 
631 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
632 void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
633 
634 static inline struct tls_offload_context_rx *
tls_offload_ctx_rx(const struct tls_context * tls_ctx)635 tls_offload_ctx_rx(const struct tls_context *tls_ctx)
636 {
637 	return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx;
638 }
639 
640 #if IS_ENABLED(CONFIG_TLS_DEVICE)
__tls_driver_ctx(struct tls_context * tls_ctx,enum tls_offload_ctx_dir direction)641 static inline void *__tls_driver_ctx(struct tls_context *tls_ctx,
642 				     enum tls_offload_ctx_dir direction)
643 {
644 	if (direction == TLS_OFFLOAD_CTX_DIR_TX)
645 		return tls_offload_ctx_tx(tls_ctx)->driver_state;
646 	else
647 		return tls_offload_ctx_rx(tls_ctx)->driver_state;
648 }
649 
650 static inline void *
tls_driver_ctx(const struct sock * sk,enum tls_offload_ctx_dir direction)651 tls_driver_ctx(const struct sock *sk, enum tls_offload_ctx_dir direction)
652 {
653 	return __tls_driver_ctx(tls_get_ctx(sk), direction);
654 }
655 #endif
656 
657 #define RESYNC_REQ BIT(0)
658 #define RESYNC_REQ_ASYNC BIT(1)
659 /* The TLS context is valid until sk_destruct is called */
tls_offload_rx_resync_request(struct sock * sk,__be32 seq)660 static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
661 {
662 	struct tls_context *tls_ctx = tls_get_ctx(sk);
663 	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
664 
665 	atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | RESYNC_REQ);
666 }
667 
668 /* Log all TLS record header TCP sequences in [seq, seq+len] */
669 static inline void
tls_offload_rx_resync_async_request_start(struct sock * sk,__be32 seq,u16 len)670 tls_offload_rx_resync_async_request_start(struct sock *sk, __be32 seq, u16 len)
671 {
672 	struct tls_context *tls_ctx = tls_get_ctx(sk);
673 	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
674 
675 	atomic64_set(&rx_ctx->resync_async->req, ((u64)ntohl(seq) << 32) |
676 		     ((u64)len << 16) | RESYNC_REQ | RESYNC_REQ_ASYNC);
677 	rx_ctx->resync_async->loglen = 0;
678 	rx_ctx->resync_async->rcd_delta = 0;
679 }
680 
681 static inline void
tls_offload_rx_resync_async_request_end(struct sock * sk,__be32 seq)682 tls_offload_rx_resync_async_request_end(struct sock *sk, __be32 seq)
683 {
684 	struct tls_context *tls_ctx = tls_get_ctx(sk);
685 	struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
686 
687 	atomic64_set(&rx_ctx->resync_async->req,
688 		     ((u64)ntohl(seq) << 32) | RESYNC_REQ);
689 }
690 
691 static inline void
tls_offload_rx_resync_set_type(struct sock * sk,enum tls_offload_sync_type type)692 tls_offload_rx_resync_set_type(struct sock *sk, enum tls_offload_sync_type type)
693 {
694 	struct tls_context *tls_ctx = tls_get_ctx(sk);
695 
696 	tls_offload_ctx_rx(tls_ctx)->resync_type = type;
697 }
698 
699 /* Driver's seq tracking has to be disabled until resync succeeded */
tls_offload_tx_resync_pending(struct sock * sk)700 static inline bool tls_offload_tx_resync_pending(struct sock *sk)
701 {
702 	struct tls_context *tls_ctx = tls_get_ctx(sk);
703 	bool ret;
704 
705 	ret = test_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
706 	smp_mb__after_atomic();
707 	return ret;
708 }
709 
710 int __net_init tls_proc_init(struct net *net);
711 void __net_exit tls_proc_fini(struct net *net);
712 
713 int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
714 		      unsigned char *record_type);
715 int decrypt_skb(struct sock *sk, struct sk_buff *skb,
716 		struct scatterlist *sgout);
717 struct sk_buff *tls_encrypt_skb(struct sk_buff *skb);
718 
719 int tls_sw_fallback_init(struct sock *sk,
720 			 struct tls_offload_context_tx *offload_ctx,
721 			 struct tls_crypto_info *crypto_info);
722 
723 #ifdef CONFIG_TLS_DEVICE
724 int tls_device_init(void);
725 void tls_device_cleanup(void);
726 void tls_device_sk_destruct(struct sock *sk);
727 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
728 void tls_device_free_resources_tx(struct sock *sk);
729 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
730 void tls_device_offload_cleanup_rx(struct sock *sk);
731 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
732 void tls_offload_tx_resync_request(struct sock *sk, u32 got_seq, u32 exp_seq);
733 int tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx,
734 			 struct sk_buff *skb, struct strp_msg *rxm);
735 
tls_is_sk_rx_device_offloaded(struct sock * sk)736 static inline bool tls_is_sk_rx_device_offloaded(struct sock *sk)
737 {
738 	if (!sk_fullsock(sk) ||
739 	    smp_load_acquire(&sk->sk_destruct) != tls_device_sk_destruct)
740 		return false;
741 	return tls_get_ctx(sk)->rx_conf == TLS_HW;
742 }
743 #else
tls_device_init(void)744 static inline int tls_device_init(void) { return 0; }
tls_device_cleanup(void)745 static inline void tls_device_cleanup(void) {}
746 
747 static inline int
tls_set_device_offload(struct sock * sk,struct tls_context * ctx)748 tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
749 {
750 	return -EOPNOTSUPP;
751 }
752 
tls_device_free_resources_tx(struct sock * sk)753 static inline void tls_device_free_resources_tx(struct sock *sk) {}
754 
755 static inline int
tls_set_device_offload_rx(struct sock * sk,struct tls_context * ctx)756 tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
757 {
758 	return -EOPNOTSUPP;
759 }
760 
tls_device_offload_cleanup_rx(struct sock * sk)761 static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
762 static inline void
tls_device_rx_resync_new_rec(struct sock * sk,u32 rcd_len,u32 seq)763 tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
764 
765 static inline int
tls_device_decrypted(struct sock * sk,struct tls_context * tls_ctx,struct sk_buff * skb,struct strp_msg * rxm)766 tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx,
767 		     struct sk_buff *skb, struct strp_msg *rxm)
768 {
769 	return 0;
770 }
771 #endif
772 #endif /* _TLS_OFFLOAD_H */
773