hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/include/net/tls.h
....@@ -39,9 +39,16 @@
3939 #include <linux/crypto.h>
4040 #include <linux/socket.h>
4141 #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>
4249 #include <net/tcp.h>
4350 #include <net/strparser.h>
44
-
51
+#include <crypto/aead.h>
4552 #include <uapi/linux/tls.h>
4653
4754
....@@ -56,74 +63,111 @@
5663 #define TLS_RECORD_TYPE_DATA 0x17
5764
5865 #define TLS_AAD_SPACE_SIZE 13
59
-#define TLS_DEVICE_NAME_MAX 32
6066
61
-/*
62
- * This structure defines the routines for Inline TLS driver.
63
- * The following routines are optional and filled with a
64
- * null pointer if not defined.
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.
6571 *
66
- * @name: Its the name of registered Inline tls device
67
- * @dev_list: Inline tls device list
68
- * int (*feature)(struct tls_device *device);
69
- * Called to return Inline TLS driver capability
72
+ * IV[16] = b0[1] || implicit nonce[4] || explicit nonce[8] || length[3]
7073 *
71
- * int (*hash)(struct tls_device *device, struct sock *sk);
72
- * This function sets Inline driver for listen and program
73
- * device specific functioanlity as required
74
- *
75
- * void (*unhash)(struct tls_device *device, struct sock *sk);
76
- * This function cleans listen state set by Inline TLS driver
74
+ * The field 'length' is encoded in field 'b0' as '(length width - 1)'.
75
+ * Hence b0 contains (3 - 1) = 2.
7776 */
78
-struct tls_device {
79
- char name[TLS_DEVICE_NAME_MAX];
80
- struct list_head dev_list;
81
- int (*feature)(struct tls_device *device);
82
- int (*hash)(struct tls_device *device, struct sock *sk);
83
- void (*unhash)(struct tls_device *device, struct sock *sk);
84
-};
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)
8587
8688 enum {
8789 TLS_BASE,
8890 TLS_SW,
89
-#ifdef CONFIG_TLS_DEVICE
9091 TLS_HW,
91
-#endif
9292 TLS_HW_RECORD,
9393 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;
94133 };
95134
96135 struct tls_sw_context_tx {
97136 struct crypto_aead *aead_send;
98137 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;
99146
100
- char aad_space[TLS_AAD_SPACE_SIZE];
147
+#define BIT_TX_SCHEDULED 0
148
+#define BIT_TX_CLOSING 1
149
+ unsigned long tx_bitmask;
101150
102
- unsigned int sg_plaintext_size;
103
- int sg_plaintext_num_elem;
104
- struct scatterlist sg_plaintext_data[MAX_SKB_FRAGS];
105
-
106
- unsigned int sg_encrypted_size;
107
- int sg_encrypted_num_elem;
108
- struct scatterlist sg_encrypted_data[MAX_SKB_FRAGS];
109
-
110
- /* AAD | sg_plaintext_data | sg_tag */
111
- struct scatterlist sg_aead_in[2];
112
- /* AAD | sg_encrypted_data (data contain overhead for hdr&iv&tag) */
113
- struct scatterlist sg_aead_out[2];
151
+ ANDROID_KABI_RESERVE(1);
114152 };
115153
116154 struct tls_sw_context_rx {
117155 struct crypto_aead *aead_recv;
118156 struct crypto_wait async_wait;
119
-
120157 struct strparser strp;
158
+ struct sk_buff_head rx_list; /* list of decrypted 'data' records */
121159 void (*saved_data_ready)(struct sock *sk);
122
- unsigned int (*sk_poll)(struct file *file, struct socket *sock,
123
- struct poll_table_struct *wait);
160
+
124161 struct sk_buff *recv_pkt;
125162 u8 control;
126
- bool decrypted;
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);
127171 };
128172
129173 struct tls_record_info {
....@@ -145,24 +189,28 @@
145189
146190 struct scatterlist sg_tx_data[MAX_SKB_FRAGS];
147191 void (*sk_destruct)(struct sock *sk);
148
- u8 driver_state[];
192
+ u8 driver_state[] __aligned(8);
149193 /* The TLS layer reserves room for driver specific state
150194 * Currently the belief is that there is not enough
151195 * driver specific state to justify another layer of indirection
152196 */
153
-#define TLS_DRIVER_STATE_SIZE (max_t(size_t, 8, sizeof(void *)))
197
+#define TLS_DRIVER_STATE_SIZE_TX 16
154198 };
155199
156200 #define TLS_OFFLOAD_CONTEXT_SIZE_TX \
157
- (ALIGN(sizeof(struct tls_offload_context_tx), sizeof(void *)) + \
158
- TLS_DRIVER_STATE_SIZE)
159
-
160
-enum {
161
- TLS_PENDING_CLOSED_RECORD
162
-};
201
+ (sizeof(struct tls_offload_context_tx) + TLS_DRIVER_STATE_SIZE_TX)
163202
164203 enum tls_context_flags {
165
- TLS_RX_SYNC_RUNNING = 0,
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,
166214 /* tls_dev_del was called for the RX side, device state was released,
167215 * but tls_ctx->netdev might still be kept, because TX-side driver
168216 * resources might not be released yet. Used to prevent the second
....@@ -172,104 +220,186 @@
172220 };
173221
174222 struct cipher_context {
175
- u16 prepend_size;
176
- u16 tag_size;
177
- u16 overhead_size;
178
- u16 iv_size;
179223 char *iv;
180
- u16 rec_seq_size;
181224 char *rec_seq;
182225 };
183226
184227 union tls_crypto_context {
185228 struct tls_crypto_info info;
186
- struct tls12_crypto_info_aes_gcm_128 aes_gcm_128;
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;
187246 };
188247
189248 struct tls_context {
190
- union tls_crypto_context crypto_send;
191
- union tls_crypto_context crypto_recv;
192
-
193
- struct list_head list;
194
- struct net_device *netdev;
195
- refcount_t refcount;
196
-
197
- void *priv_ctx_tx;
198
- void *priv_ctx_rx;
249
+ /* read-only cache line */
250
+ struct tls_prot_info prot_info;
199251
200252 u8 tx_conf:3;
201253 u8 rx_conf:3;
202254
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 */
203264 struct cipher_context tx;
204265 struct cipher_context rx;
205266
206267 struct scatterlist *partially_sent_record;
207268 u16 partially_sent_offset;
208
- unsigned long flags;
269
+
209270 bool in_tcp_sendpages;
271
+ bool pending_open_record_frags;
210272
211
- u16 pending_open_record_frags;
212
- int (*push_pending_record)(struct sock *sk, int flags);
273
+ struct mutex tx_lock; /* protects partially_sent_* fields and
274
+ * per-type TX fields
275
+ */
276
+ unsigned long flags;
213277
214
- void (*sk_write_space)(struct sock *sk);
278
+ /* cache cold stuff */
279
+ struct proto *sk_proto;
280
+ struct sock *sk;
281
+
215282 void (*sk_destruct)(struct sock *sk);
216
- void (*sk_proto_close)(struct sock *sk, long timeout);
217283
218
- int (*setsockopt)(struct sock *sk, int level,
219
- int optname, char __user *optval,
220
- unsigned int optlen);
221
- int (*getsockopt)(struct sock *sk, int level,
222
- int optname, char __user *optval,
223
- int __user *optlen);
224
- int (*hash)(struct sock *sk);
225
- void (*unhash)(struct sock *sk);
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];
226331 };
227332
228333 struct tls_offload_context_rx {
229334 /* sw must be the first member of tls_offload_context_rx */
230335 struct tls_sw_context_rx sw;
231
- atomic64_t resync_req;
232
- u8 driver_state[];
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);
233357 /* The TLS layer reserves room for driver specific state
234358 * Currently the belief is that there is not enough
235359 * driver specific state to justify another layer of indirection
236360 */
361
+#define TLS_DRIVER_STATE_SIZE_RX 8
237362 };
238363
239364 #define TLS_OFFLOAD_CONTEXT_SIZE_RX \
240
- (ALIGN(sizeof(struct tls_offload_context_rx), sizeof(void *)) + \
241
- TLS_DRIVER_STATE_SIZE)
365
+ (sizeof(struct tls_offload_context_rx) + TLS_DRIVER_STATE_SIZE_RX)
242366
243
-void tls_ctx_free(struct tls_context *ctx);
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
+
244371 int wait_on_pending_writer(struct sock *sk, long *timeo);
245372 int tls_sk_query(struct sock *sk, int optname, char __user *optval,
246373 int __user *optlen);
247374 int tls_sk_attach(struct sock *sk, int optname, char __user *optval,
248375 unsigned int optlen);
376
+void tls_err_abort(struct sock *sk, int err);
249377
250378 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);
251381 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);
252384 int tls_sw_sendpage(struct sock *sk, struct page *page,
253385 int offset, size_t size, int flags);
254
-void tls_sw_close(struct sock *sk, long timeout);
255
-void tls_sw_free_resources_tx(struct sock *sk);
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);
256389 void tls_sw_free_resources_rx(struct sock *sk);
257390 void tls_sw_release_resources_rx(struct sock *sk);
391
+void tls_sw_free_ctx_rx(struct tls_context *tls_ctx);
258392 int tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
259393 int nonblock, int flags, int *addr_len);
260
-unsigned int tls_sw_poll(struct file *file, struct socket *sock,
261
- struct poll_table_struct *wait);
394
+bool tls_sw_stream_read(const struct sock *sk);
262395 ssize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos,
263396 struct pipe_inode_info *pipe,
264397 size_t len, unsigned int flags);
265398
266
-int tls_set_device_offload(struct sock *sk, struct tls_context *ctx);
267399 int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
268400 int tls_device_sendpage(struct sock *sk, struct page *page,
269401 int offset, size_t size, int flags);
270
-void tls_device_sk_destruct(struct sock *sk);
271
-void tls_device_init(void);
272
-void tls_device_cleanup(void);
402
+int tls_tx_records(struct sock *sk, int flags);
273403
274404 struct tls_record_info *tls_get_record(struct tls_offload_context_tx *context,
275405 u32 seq, u64 *p_record_sn);
....@@ -284,31 +414,16 @@
284414 return rec->end_seq - rec->len;
285415 }
286416
287
-void tls_sk_destruct(struct sock *sk, struct tls_context *ctx);
288417 int tls_push_sg(struct sock *sk, struct tls_context *ctx,
289418 struct scatterlist *sg, u16 first_offset,
290419 int flags);
291
-int tls_push_pending_closed_record(struct sock *sk, struct tls_context *ctx,
292
- int flags, long *timeo);
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);
293423
294
-static inline bool tls_is_pending_closed_record(struct tls_context *ctx)
424
+static inline struct tls_msg *tls_msg(struct sk_buff *skb)
295425 {
296
- return test_bit(TLS_PENDING_CLOSED_RECORD, &ctx->flags);
297
-}
298
-
299
-static inline int tls_complete_pending_work(struct sock *sk,
300
- struct tls_context *ctx,
301
- int flags, long *timeo)
302
-{
303
- int rc = 0;
304
-
305
- if (unlikely(sk->sk_write_pending))
306
- rc = wait_on_pending_writer(sk, timeo);
307
-
308
- if (!rc && tls_is_pending_closed_record(ctx))
309
- rc = tls_push_pending_closed_record(sk, ctx, flags, timeo);
310
-
311
- return rc;
426
+ return (struct tls_msg *)strp_msg(skb);
312427 }
313428
314429 static inline bool tls_is_partially_sent_record(struct tls_context *ctx)
....@@ -321,9 +436,40 @@
321436 return tls_ctx->pending_open_record_frags;
322437 }
323438
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
+
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
+
324467 struct sk_buff *
325468 tls_validate_xmit_skb(struct sock *sk, struct net_device *dev,
326469 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);
327473
328474 static inline bool tls_is_sk_tx_device_offloaded(struct sock *sk)
329475 {
....@@ -334,12 +480,6 @@
334480 #else
335481 return false;
336482 #endif
337
-}
338
-
339
-static inline void tls_err_abort(struct sock *sk, int err)
340
-{
341
- sk->sk_err = err;
342
- sk->sk_error_report(sk);
343483 }
344484
345485 static inline bool tls_bigint_increment(unsigned char *seq, int len)
....@@ -355,58 +495,102 @@
355495 return (i == -1);
356496 }
357497
358
-static inline void tls_advance_record_sn(struct sock *sk,
359
- struct cipher_context *ctx)
498
+static inline void tls_bigint_subtract(unsigned char *seq, int n)
360499 {
361
- if (tls_bigint_increment(ctx->rec_seq, ctx->rec_seq_size))
362
- tls_err_abort(sk, EBADMSG);
363
- tls_bigint_increment(ctx->iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE,
364
- ctx->iv_size);
365
-}
500
+ u64 rcd_sn;
501
+ __be64 *p;
366502
367
-static inline void tls_fill_prepend(struct tls_context *ctx,
368
- char *buf,
369
- size_t plaintext_len,
370
- unsigned char record_type)
371
-{
372
- size_t pkt_len, iv_size = ctx->tx.iv_size;
503
+ BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8);
373504
374
- pkt_len = plaintext_len + iv_size + ctx->tx.tag_size;
375
-
376
- /* we cover nonce explicit here as well, so buf should be of
377
- * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE
378
- */
379
- buf[0] = record_type;
380
- buf[1] = TLS_VERSION_MINOR(ctx->crypto_send.info.version);
381
- buf[2] = TLS_VERSION_MAJOR(ctx->crypto_send.info.version);
382
- /* we can use IV for nonce explicit according to spec */
383
- buf[3] = pkt_len >> 8;
384
- buf[4] = pkt_len & 0xFF;
385
- memcpy(buf + TLS_NONCE_OFFSET,
386
- ctx->tx.iv + TLS_CIPHER_AES_GCM_128_SALT_SIZE, iv_size);
387
-}
388
-
389
-static inline void tls_make_aad(char *buf,
390
- size_t size,
391
- char *record_sequence,
392
- int record_sequence_size,
393
- unsigned char record_type)
394
-{
395
- memcpy(buf, record_sequence, record_sequence_size);
396
-
397
- buf[8] = record_type;
398
- buf[9] = TLS_1_2_VERSION_MAJOR;
399
- buf[10] = TLS_1_2_VERSION_MINOR;
400
- buf[11] = size >> 8;
401
- buf[12] = size & 0xFF;
505
+ p = (__be64 *)seq;
506
+ rcd_sn = be64_to_cpu(*p);
507
+ *p = cpu_to_be64(rcd_sn - n);
402508 }
403509
404510 static inline struct tls_context *tls_get_ctx(const struct sock *sk)
405511 {
406512 struct inet_connection_sock *icsk = inet_csk(sk);
407513
408
- return icsk->icsk_ulp_data;
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;
409518 }
519
+
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
+
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
+
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
+
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
+
410594
411595 static inline struct tls_sw_context_rx *tls_sw_ctx_rx(
412596 const struct tls_context *tls_ctx)
....@@ -426,41 +610,163 @@
426610 return (struct tls_offload_context_tx *)tls_ctx->priv_ctx_tx;
427611 }
428612
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
+
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
+
429634 static inline struct tls_offload_context_rx *
430635 tls_offload_ctx_rx(const struct tls_context *tls_ctx)
431636 {
432637 return (struct tls_offload_context_rx *)tls_ctx->priv_ctx_rx;
433638 }
434639
640
+#if IS_ENABLED(CONFIG_TLS_DEVICE)
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 *
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)
435659 /* The TLS context is valid until sk_destruct is called */
436660 static inline void tls_offload_rx_resync_request(struct sock *sk, __be32 seq)
437661 {
438662 struct tls_context *tls_ctx = tls_get_ctx(sk);
439663 struct tls_offload_context_rx *rx_ctx = tls_offload_ctx_rx(tls_ctx);
440664
441
- atomic64_set(&rx_ctx->resync_req, ((((uint64_t)seq) << 32) | 1));
665
+ atomic64_set(&rx_ctx->resync_req, ((u64)ntohl(seq) << 32) | RESYNC_REQ);
442666 }
443667
668
+/* Log all TLS record header TCP sequences in [seq, seq+len] */
669
+static inline void
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
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
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 */
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);
444712
445713 int tls_proccess_cmsg(struct sock *sk, struct msghdr *msg,
446714 unsigned char *record_type);
447
-void tls_register_device(struct tls_device *device);
448
-void tls_unregister_device(struct tls_device *device);
449
-int tls_device_decrypted(struct sock *sk, struct sk_buff *skb);
450715 int decrypt_skb(struct sock *sk, struct sk_buff *skb,
451716 struct scatterlist *sgout);
452
-
453
-struct sk_buff *tls_validate_xmit_skb(struct sock *sk,
454
- struct net_device *dev,
455
- struct sk_buff *skb);
717
+struct sk_buff *tls_encrypt_skb(struct sk_buff *skb);
456718
457719 int tls_sw_fallback_init(struct sock *sk,
458720 struct tls_offload_context_tx *offload_ctx,
459721 struct tls_crypto_info *crypto_info);
460722
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);
461729 int tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx);
462
-
463730 void tls_device_offload_cleanup_rx(struct sock *sk);
464
-void handle_device_resync(struct sock *sk, u32 seq, u64 rcd_sn);
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);
465735
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
744
+static inline int tls_device_init(void) { return 0; }
745
+static inline void tls_device_cleanup(void) {}
746
+
747
+static inline int
748
+tls_set_device_offload(struct sock *sk, struct tls_context *ctx)
749
+{
750
+ return -EOPNOTSUPP;
751
+}
752
+
753
+static inline void tls_device_free_resources_tx(struct sock *sk) {}
754
+
755
+static inline int
756
+tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx)
757
+{
758
+ return -EOPNOTSUPP;
759
+}
760
+
761
+static inline void tls_device_offload_cleanup_rx(struct sock *sk) {}
762
+static inline void
763
+tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {}
764
+
765
+static inline int
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
466772 #endif /* _TLS_OFFLOAD_H */