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