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