This source file includes following definitions.
- tls_record_is_start_marker
- tls_record_start_seq
- tls_is_partially_sent_record
- tls_is_pending_open_record
- is_tx_ready
- tls_user_config
- tls_is_sk_tx_device_offloaded
- tls_err_abort
- tls_bigint_increment
- tls_get_ctx
- tls_advance_record_sn
- tls_fill_prepend
- tls_make_aad
- xor_iv_with_seq
- tls_sw_ctx_rx
- tls_sw_ctx_tx
- tls_offload_ctx_tx
- tls_sw_has_ctx_tx
- tls_offload_ctx_rx
- __tls_driver_ctx
- tls_driver_ctx
- tls_offload_rx_resync_request
- tls_offload_rx_resync_set_type
- tls_offload_tx_resync_request
- tls_offload_tx_resync_pending
- tls_device_init
- tls_device_cleanup
- tls_set_device_offload
- tls_device_free_resources_tx
- tls_set_device_offload_rx
- tls_device_offload_cleanup_rx
- tls_device_rx_resync_new_rec
- tls_device_decrypted
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 #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
47 #include <net/tcp.h>
48 #include <net/strparser.h>
49 #include <crypto/aead.h>
50 #include <uapi/linux/tls.h>
51
52
53
54 #define TLS_MAX_PAYLOAD_SIZE ((size_t)1 << 14)
55
56 #define TLS_HEADER_SIZE 5
57 #define TLS_NONCE_OFFSET TLS_HEADER_SIZE
58
59 #define TLS_CRYPTO_INFO_READY(info) ((info)->cipher_type)
60
61 #define TLS_RECORD_TYPE_DATA 0x17
62
63 #define TLS_AAD_SPACE_SIZE 13
64 #define TLS_DEVICE_NAME_MAX 32
65
66 #define MAX_IV_SIZE 16
67 #define TLS_MAX_REC_SEQ_SIZE 8
68
69
70
71
72
73
74
75
76 #define TLS_AES_CCM_IV_B0_BYTE 2
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 struct tls_device {
100 char name[TLS_DEVICE_NAME_MAX];
101 struct list_head dev_list;
102 int (*feature)(struct tls_device *device);
103 int (*hash)(struct tls_device *device, struct sock *sk);
104 void (*unhash)(struct tls_device *device, struct sock *sk);
105 void (*release)(struct kref *kref);
106 struct kref kref;
107 };
108
109 enum {
110 TLS_BASE,
111 TLS_SW,
112 TLS_HW,
113 TLS_HW_RECORD,
114 TLS_NUM_CONFIG,
115 };
116
117
118
119
120
121 struct tls_rec {
122 struct list_head list;
123 int tx_ready;
124 int tx_flags;
125
126 struct sk_msg msg_plaintext;
127 struct sk_msg msg_encrypted;
128
129
130 struct scatterlist sg_aead_in[2];
131
132 struct scatterlist sg_aead_out[2];
133
134 char content_type;
135 struct scatterlist sg_content_type;
136
137 char aad_space[TLS_AAD_SPACE_SIZE];
138 u8 iv_data[MAX_IV_SIZE];
139 struct aead_request aead_req;
140 u8 aead_req_ctx[];
141 };
142
143 struct tls_msg {
144 struct strp_msg rxm;
145 u8 control;
146 };
147
148 struct tx_work {
149 struct delayed_work work;
150 struct sock *sk;
151 };
152
153 struct tls_sw_context_tx {
154 struct crypto_aead *aead_send;
155 struct crypto_wait async_wait;
156 struct tx_work tx_work;
157 struct tls_rec *open_rec;
158 struct list_head tx_list;
159 atomic_t encrypt_pending;
160
161 spinlock_t encrypt_compl_lock;
162 int async_notify;
163 int async_capable;
164
165 #define BIT_TX_SCHEDULED 0
166 #define BIT_TX_CLOSING 1
167 unsigned long tx_bitmask;
168 };
169
170 struct tls_sw_context_rx {
171 struct crypto_aead *aead_recv;
172 struct crypto_wait async_wait;
173 struct strparser strp;
174 struct sk_buff_head rx_list;
175 void (*saved_data_ready)(struct sock *sk);
176
177 struct sk_buff *recv_pkt;
178 u8 control;
179 int async_capable;
180 bool decrypted;
181 atomic_t decrypt_pending;
182
183 spinlock_t decrypt_compl_lock;
184 bool async_notify;
185 };
186
187 struct tls_record_info {
188 struct list_head list;
189 u32 end_seq;
190 int len;
191 int num_frags;
192 skb_frag_t frags[MAX_SKB_FRAGS];
193 };
194
195 struct tls_offload_context_tx {
196 struct crypto_aead *aead_send;
197 spinlock_t lock;
198 struct list_head records_list;
199 struct tls_record_info *open_record;
200 struct tls_record_info *retransmit_hint;
201 u64 hint_record_sn;
202 u64 unacked_record_sn;
203
204 struct scatterlist sg_tx_data[MAX_SKB_FRAGS];
205 void (*sk_destruct)(struct sock *sk);
206 u8 driver_state[] __aligned(8);
207
208
209
210
211 #define TLS_DRIVER_STATE_SIZE_TX 16
212 };
213
214 #define TLS_OFFLOAD_CONTEXT_SIZE_TX \
215 (sizeof(struct tls_offload_context_tx) + TLS_DRIVER_STATE_SIZE_TX)
216
217 enum tls_context_flags {
218 TLS_RX_SYNC_RUNNING = 0,
219
220
221
222
223 TLS_TX_SYNC_SCHED = 1,
224 };
225
226 struct cipher_context {
227 char *iv;
228 char *rec_seq;
229 };
230
231 union tls_crypto_context {
232 struct tls_crypto_info info;
233 union {
234 struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
235 struct tls12_crypto_info_aes_gcm_256 aes_gcm_256;
236 };
237 };
238
239 struct tls_prot_info {
240 u16 version;
241 u16 cipher_type;
242 u16 prepend_size;
243 u16 tag_size;
244 u16 overhead_size;
245 u16 iv_size;
246 u16 salt_size;
247 u16 rec_seq_size;
248 u16 aad_size;
249 u16 tail_size;
250 };
251
252 struct tls_context {
253
254 struct tls_prot_info prot_info;
255
256 u8 tx_conf:3;
257 u8 rx_conf:3;
258
259 int (*push_pending_record)(struct sock *sk, int flags);
260 void (*sk_write_space)(struct sock *sk);
261
262 void *priv_ctx_tx;
263 void *priv_ctx_rx;
264
265 struct net_device *netdev;
266
267
268 struct cipher_context tx;
269 struct cipher_context rx;
270
271 struct scatterlist *partially_sent_record;
272 u16 partially_sent_offset;
273
274 bool in_tcp_sendpages;
275 bool pending_open_record_frags;
276
277 struct mutex tx_lock;
278
279
280 unsigned long flags;
281
282
283 struct proto *sk_proto;
284
285 void (*sk_destruct)(struct sock *sk);
286
287 union tls_crypto_context crypto_send;
288 union tls_crypto_context crypto_recv;
289
290 struct list_head list;
291 refcount_t refcount;
292 struct rcu_head rcu;
293 };
294
295 enum tls_offload_ctx_dir {
296 TLS_OFFLOAD_CTX_DIR_RX,
297 TLS_OFFLOAD_CTX_DIR_TX,
298 };
299
300 struct tlsdev_ops {
301 int (*tls_dev_add)(struct net_device *netdev, struct sock *sk,
302 enum tls_offload_ctx_dir direction,
303 struct tls_crypto_info *crypto_info,
304 u32 start_offload_tcp_sn);
305 void (*tls_dev_del)(struct net_device *netdev,
306 struct tls_context *ctx,
307 enum tls_offload_ctx_dir direction);
308 int (*tls_dev_resync)(struct net_device *netdev,
309 struct sock *sk, u32 seq, u8 *rcd_sn,
310 enum tls_offload_ctx_dir direction);
311 };
312
313 enum tls_offload_sync_type {
314 TLS_OFFLOAD_SYNC_TYPE_DRIVER_REQ = 0,
315 TLS_OFFLOAD_SYNC_TYPE_CORE_NEXT_HINT = 1,
316 };
317
318 #define TLS_DEVICE_RESYNC_NH_START_IVAL 2
319 #define TLS_DEVICE_RESYNC_NH_MAX_IVAL 128
320
321 struct tls_offload_context_rx {
322
323 struct tls_sw_context_rx sw;
324 enum tls_offload_sync_type resync_type;
325
326 u8 resync_nh_reset:1;
327
328 u8 resync_nh_do_now:1;
329 union {
330
331 struct {
332 atomic64_t resync_req;
333 };
334
335 struct {
336 u32 decrypted_failed;
337 u32 decrypted_tgt;
338 } resync_nh;
339 };
340 u8 driver_state[] __aligned(8);
341
342
343
344
345 #define TLS_DRIVER_STATE_SIZE_RX 8
346 };
347
348 #define TLS_OFFLOAD_CONTEXT_SIZE_RX \
349 (sizeof(struct tls_offload_context_rx) + TLS_DRIVER_STATE_SIZE_RX)
350
351 void tls_ctx_free(struct sock *sk, struct tls_context *ctx);
352 int wait_on_pending_writer(struct sock *sk, long *timeo);
353 int tls_sk_query(struct sock *sk, int optname, char __user *optval,
354 int __user *optlen);
355 int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
356 unsigned int optlen);
357
358 int tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx);
359 void tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx);
360 void tls_sw_strparser_done(struct tls_context *tls_ctx);
361 int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
362 int tls_sw_sendpage_locked(struct sock *sk, struct page *page,
363 int offset, size_t size, int flags);
364 int tls_sw_sendpage(struct sock *sk, struct page *page,
365 int offset, size_t size, int flags);
366 void tls_sw_cancel_work_tx(struct tls_context *tls_ctx);
367 void tls_sw_release_resources_tx(struct sock *sk);
368 void tls_sw_free_ctx_tx(struct tls_context *tls_ctx);
369 void tls_sw_free_resources_rx(struct sock *sk);
370 void tls_sw_release_resources_rx(struct sock *sk);
371 void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
372 int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
373 int nonblock, int flags, int *addr_len);
374 bool tls_sw_stream_read(const struct sock *sk);
375 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
376 struct pipe_inode_info *pipe,
377 size_t len, unsigned int flags);
378
379 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
380 int tls_device_sendpage(struct sock *sk, struct page *page,
381 int offset, size_t size, int flags);
382 int tls_tx_records(struct sock *sk, int flags);
383
384 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
385 u32 seq, u64 *p_record_sn);
386
387 static inline bool tls_record_is_start_marker(struct tls_record_info *rec)
388 {
389 return rec->len == 0;
390 }
391
392 static inline u32 tls_record_start_seq(struct tls_record_info *rec)
393 {
394 return rec->end_seq - rec->len;
395 }
396
397 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
398 struct scatterlist *sg, u16 first_offset,
399 int flags);
400 int tls_push_partial_record(struct sock *sk, struct tls_context *ctx,
401 int flags);
402 void tls_free_partial_record(struct sock *sk, struct tls_context *ctx);
403
404 static inline struct tls_msg *tls_msg(struct sk_buff *skb)
405 {
406 return (struct tls_msg *)strp_msg(skb);
407 }
408
409 static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
410 {
411 return !!ctx->partially_sent_record;
412 }
413
414 static inline bool tls_is_pending_open_record(struct tls_context *tls_ctx)
415 {
416 return tls_ctx->pending_open_record_frags;
417 }
418
419 static inline bool is_tx_ready(struct tls_sw_context_tx *ctx)
420 {
421 struct tls_rec *rec;
422
423 rec = list_first_entry(&ctx->tx_list, struct tls_rec, list);
424 if (!rec)
425 return false;
426
427 return READ_ONCE(rec->tx_ready);
428 }
429
430 static inline u16 tls_user_config(struct tls_context *ctx, bool tx)
431 {
432 u16 config = tx ? ctx->tx_conf : ctx->rx_conf;
433
434 switch (config) {
435 case TLS_BASE:
436 return TLS_CONF_BASE;
437 case TLS_SW:
438 return TLS_CONF_SW;
439 case TLS_HW:
440 return TLS_CONF_HW;
441 case TLS_HW_RECORD:
442 return TLS_CONF_HW_RECORD;
443 }
444 return 0;
445 }
446
447 struct sk_buff *
448 tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
449 struct sk_buff *skb);
450
451 static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
452 {
453 #ifdef CONFIG_SOCK_VALIDATE_XMIT
454 return sk_fullsock(sk) &&
455 (smp_load_acquire(&sk->sk_validate_xmit_skb) ==
456 &tls_validate_xmit_skb);
457 #else
458 return false;
459 #endif
460 }
461
462 static inline void tls_err_abort(struct sock *sk, int err)
463 {
464 sk->sk_err = err;
465 sk->sk_error_report(sk);
466 }
467
468 static inline bool tls_bigint_increment(unsigned char *seq, int len)
469 {
470 int i;
471
472 for (i = len - 1; i >= 0; i--) {
473 ++seq[i];
474 if (seq[i] != 0)
475 break;
476 }
477
478 return (i == -1);
479 }
480
481 static inline struct tls_context *tls_get_ctx(const struct sock *sk)
482 {
483 struct inet_connection_sock *icsk = inet_csk(sk);
484
485
486
487
488 return (__force void *)icsk->icsk_ulp_data;
489 }
490
491 static inline void tls_advance_record_sn(struct sock *sk,
492 struct tls_prot_info *prot,
493 struct cipher_context *ctx)
494 {
495 if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size))
496 tls_err_abort(sk, EBADMSG);
497
498 if (prot->version != TLS_1_3_VERSION)
499 tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
500 prot->iv_size);
501 }
502
503 static inline void tls_fill_prepend(struct tls_context *ctx,
504 char *buf,
505 size_t plaintext_len,
506 unsigned char record_type,
507 int version)
508 {
509 struct tls_prot_info *prot = &ctx->prot_info;
510 size_t pkt_len, iv_size = prot->iv_size;
511
512 pkt_len = plaintext_len + prot->tag_size;
513 if (version != TLS_1_3_VERSION) {
514 pkt_len += iv_size;
515
516 memcpy(buf + TLS_NONCE_OFFSET,
517 ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
518 }
519
520
521
522
523 buf[0] = version == TLS_1_3_VERSION ?
524 TLS_RECORD_TYPE_DATA : record_type;
525
526 buf[1] = TLS_1_2_VERSION_MINOR;
527 buf[2] = TLS_1_2_VERSION_MAJOR;
528
529 buf[3] = pkt_len >> 8;
530 buf[4] = pkt_len & 0xFF;
531 }
532
533 static inline void tls_make_aad(char *buf,
534 size_t size,
535 char *record_sequence,
536 int record_sequence_size,
537 unsigned char record_type,
538 int version)
539 {
540 if (version != TLS_1_3_VERSION) {
541 memcpy(buf, record_sequence, record_sequence_size);
542 buf += 8;
543 } else {
544 size += TLS_CIPHER_AES_GCM_128_TAG_SIZE;
545 }
546
547 buf[0] = version == TLS_1_3_VERSION ?
548 TLS_RECORD_TYPE_DATA : record_type;
549 buf[1] = TLS_1_2_VERSION_MAJOR;
550 buf[2] = TLS_1_2_VERSION_MINOR;
551 buf[3] = size >> 8;
552 buf[4] = size & 0xFF;
553 }
554
555 static inline void xor_iv_with_seq(int version, char *iv, char *seq)
556 {
557 int i;
558
559 if (version == TLS_1_3_VERSION) {
560 for (i = 0; i < 8; i++)
561 iv[i + 4] ^= seq[i];
562 }
563 }
564
565
566 static inline struct tls_sw_context_rx *tls_sw_ctx_rx(
567 const struct tls_context *tls_ctx)
568 {
569 return (struct tls_sw_context_rx *)tls_ctx->priv_ctx_rx;
570 }
571
572 static inline struct tls_sw_context_tx *tls_sw_ctx_tx(
573 const struct tls_context *tls_ctx)
574 {
575 return (struct tls_sw_context_tx *)tls_ctx->priv_ctx_tx;
576 }
577
578 static inline struct tls_offload_context_tx *
579 tls_offload_ctx_tx(const struct tls_context *tls_ctx)
580 {
581 return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx;
582 }
583
584 static inline bool tls_sw_has_ctx_tx(const struct sock *sk)
585 {
586 struct tls_context *ctx = tls_get_ctx(sk);
587
588 if (!ctx)
589 return false;
590 return !!tls_sw_ctx_tx(ctx);
591 }
592
593 void tls_sw_write_space(struct sock *sk, struct tls_context *ctx);
594 void tls_device_write_space(struct sock *sk, struct tls_context *ctx);
595
596 static inline struct tls_offload_context_rx *
597 tls_offload_ctx_rx(const struct tls_context *tls_ctx)
598 {
599 return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx;
600 }
601
602 #if IS_ENABLED(CONFIG_TLS_DEVICE)
603 static inline void *__tls_driver_ctx(struct tls_context *tls_ctx,
604 enum tls_offload_ctx_dir direction)
605 {
606 if (direction == TLS_OFFLOAD_CTX_DIR_TX)
607 return tls_offload_ctx_tx(tls_ctx)->driver_state;
608 else
609 return tls_offload_ctx_rx(tls_ctx)->driver_state;
610 }
611
612 static inline void *
613 tls_driver_ctx(const struct sock *sk, enum tls_offload_ctx_dir direction)
614 {
615 return __tls_driver_ctx(tls_get_ctx(sk), direction);
616 }
617 #endif
618
619
620 static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
621 {
622 struct tls_context *tls_ctx = tls_get_ctx(sk);
623 struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
624
625 atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | 1);
626 }
627
628 static inline void
629 tls_offload_rx_resync_set_type(struct sock *sk, enum tls_offload_sync_type type)
630 {
631 struct tls_context *tls_ctx = tls_get_ctx(sk);
632
633 tls_offload_ctx_rx(tls_ctx)->resync_type = type;
634 }
635
636 static inline void tls_offload_tx_resync_request(struct sock *sk)
637 {
638 struct tls_context *tls_ctx = tls_get_ctx(sk);
639
640 WARN_ON(test_and_set_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags));
641 }
642
643
644 static inline bool tls_offload_tx_resync_pending(struct sock *sk)
645 {
646 struct tls_context *tls_ctx = tls_get_ctx(sk);
647 bool ret;
648
649 ret = test_bit(TLS_TX_SYNC_SCHED, &tls_ctx->flags);
650 smp_mb__after_atomic();
651 return ret;
652 }
653
654 int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
655 unsigned char *record_type);
656 void tls_register_device(struct tls_device *device);
657 void tls_unregister_device(struct tls_device *device);
658 int decrypt_skb(struct sock *sk, struct sk_buff *skb,
659 struct scatterlist *sgout);
660 struct sk_buff *tls_encrypt_skb(struct sk_buff *skb);
661
662 struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
663 struct net_device *dev,
664 struct sk_buff *skb);
665
666 int tls_sw_fallback_init(struct sock *sk,
667 struct tls_offload_context_tx *offload_ctx,
668 struct tls_crypto_info *crypto_info);
669
670 #ifdef CONFIG_TLS_DEVICE
671 void tls_device_init(void);
672 void tls_device_cleanup(void);
673 int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
674 void tls_device_free_resources_tx(struct sock *sk);
675 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
676 void tls_device_offload_cleanup_rx(struct sock *sk);
677 void tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq);
678 int tls_device_decrypted(struct sock *sk, struct sk_buff *skb);
679 #else
680 static inline void tls_device_init(void) {}
681 static inline void tls_device_cleanup(void) {}
682
683 static inline int
684 tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
685 {
686 return -EOPNOTSUPP;
687 }
688
689 static inline void tls_device_free_resources_tx(struct sock *sk) {}
690
691 static inline int
692 tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
693 {
694 return -EOPNOTSUPP;
695 }
696
697 static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
698 static inline void
699 tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
700
701 static inline int tls_device_decrypted(struct sock *sk, struct sk_buff *skb)
702 {
703 return 0;
704 }
705 #endif
706 #endif