@@ -248,15 +248,15 @@ static void trim_both_sgl(struct sock *sk, int target_size)
248
248
struct tls_sw_context_tx * ctx = tls_sw_ctx_tx (tls_ctx );
249
249
struct tls_rec * rec = ctx -> open_rec ;
250
250
251
- trim_sg (sk , rec -> sg_plaintext_data ,
251
+ trim_sg (sk , & rec -> sg_plaintext_data [ 1 ] ,
252
252
& rec -> sg_plaintext_num_elem ,
253
253
& rec -> sg_plaintext_size ,
254
254
target_size );
255
255
256
256
if (target_size > 0 )
257
257
target_size += tls_ctx -> tx .overhead_size ;
258
258
259
- trim_sg (sk , rec -> sg_encrypted_data ,
259
+ trim_sg (sk , & rec -> sg_encrypted_data [ 1 ] ,
260
260
& rec -> sg_encrypted_num_elem ,
261
261
& rec -> sg_encrypted_size ,
262
262
target_size );
@@ -270,12 +270,13 @@ static int alloc_encrypted_sg(struct sock *sk, int len)
270
270
int rc = 0 ;
271
271
272
272
rc = sk_alloc_sg (sk , len ,
273
- rec -> sg_encrypted_data , 0 ,
273
+ & rec -> sg_encrypted_data [ 1 ] , 0 ,
274
274
& rec -> sg_encrypted_num_elem ,
275
275
& rec -> sg_encrypted_size , 0 );
276
276
277
277
if (rc == - ENOSPC )
278
- rec -> sg_encrypted_num_elem = ARRAY_SIZE (rec -> sg_encrypted_data );
278
+ rec -> sg_encrypted_num_elem =
279
+ ARRAY_SIZE (rec -> sg_encrypted_data ) - 1 ;
279
280
280
281
return rc ;
281
282
}
@@ -287,12 +288,15 @@ static int alloc_plaintext_sg(struct sock *sk, int len)
287
288
struct tls_rec * rec = ctx -> open_rec ;
288
289
int rc = 0 ;
289
290
290
- rc = sk_alloc_sg (sk , len , rec -> sg_plaintext_data , 0 ,
291
- & rec -> sg_plaintext_num_elem , & rec -> sg_plaintext_size ,
291
+ rc = sk_alloc_sg (sk , len ,
292
+ & rec -> sg_plaintext_data [1 ], 0 ,
293
+ & rec -> sg_plaintext_num_elem ,
294
+ & rec -> sg_plaintext_size ,
292
295
tls_ctx -> pending_open_record_frags );
293
296
294
297
if (rc == - ENOSPC )
295
- rec -> sg_plaintext_num_elem = ARRAY_SIZE (rec -> sg_plaintext_data );
298
+ rec -> sg_plaintext_num_elem =
299
+ ARRAY_SIZE (rec -> sg_plaintext_data ) - 1 ;
296
300
297
301
return rc ;
298
302
}
@@ -320,11 +324,11 @@ static void tls_free_open_rec(struct sock *sk)
320
324
if (!rec )
321
325
return ;
322
326
323
- free_sg (sk , rec -> sg_encrypted_data ,
327
+ free_sg (sk , & rec -> sg_encrypted_data [ 1 ] ,
324
328
& rec -> sg_encrypted_num_elem ,
325
329
& rec -> sg_encrypted_size );
326
330
327
- free_sg (sk , rec -> sg_plaintext_data ,
331
+ free_sg (sk , & rec -> sg_plaintext_data [ 1 ] ,
328
332
& rec -> sg_plaintext_num_elem ,
329
333
& rec -> sg_plaintext_size );
330
334
@@ -355,7 +359,7 @@ int tls_tx_records(struct sock *sk, int flags)
355
359
* Remove the head of tx_list
356
360
*/
357
361
list_del (& rec -> list );
358
- free_sg (sk , rec -> sg_plaintext_data ,
362
+ free_sg (sk , & rec -> sg_plaintext_data [ 1 ] ,
359
363
& rec -> sg_plaintext_num_elem , & rec -> sg_plaintext_size );
360
364
361
365
kfree (rec );
@@ -370,13 +374,13 @@ int tls_tx_records(struct sock *sk, int flags)
370
374
tx_flags = flags ;
371
375
372
376
rc = tls_push_sg (sk , tls_ctx ,
373
- & rec -> sg_encrypted_data [0 ],
377
+ & rec -> sg_encrypted_data [1 ],
374
378
0 , tx_flags );
375
379
if (rc )
376
380
goto tx_err ;
377
381
378
382
list_del (& rec -> list );
379
- free_sg (sk , rec -> sg_plaintext_data ,
383
+ free_sg (sk , & rec -> sg_plaintext_data [ 1 ] ,
380
384
& rec -> sg_plaintext_num_elem ,
381
385
& rec -> sg_plaintext_size );
382
386
@@ -405,16 +409,12 @@ static void tls_encrypt_done(struct crypto_async_request *req, int err)
405
409
406
410
rec = container_of (aead_req , struct tls_rec , aead_req );
407
411
408
- rec -> sg_encrypted_data [0 ].offset -= tls_ctx -> tx .prepend_size ;
409
- rec -> sg_encrypted_data [0 ].length += tls_ctx -> tx .prepend_size ;
412
+ rec -> sg_encrypted_data [1 ].offset -= tls_ctx -> tx .prepend_size ;
413
+ rec -> sg_encrypted_data [1 ].length += tls_ctx -> tx .prepend_size ;
410
414
411
415
412
- /* Free the record if error is previously set on socket */
416
+ /* Check if error is previously set on socket */
413
417
if (err || sk -> sk_err ) {
414
- free_sg (sk , rec -> sg_encrypted_data ,
415
- & rec -> sg_encrypted_num_elem , & rec -> sg_encrypted_size );
416
-
417
- kfree (rec );
418
418
rec = NULL ;
419
419
420
420
/* If err is already set on socket, return the same code */
@@ -449,7 +449,7 @@ static void tls_encrypt_done(struct crypto_async_request *req, int err)
449
449
450
450
/* Schedule the transmission */
451
451
if (!test_and_set_bit (BIT_TX_SCHEDULED , & ctx -> tx_bitmask ))
452
- schedule_delayed_work (& ctx -> tx_work .work , 1 );
452
+ schedule_delayed_work (& ctx -> tx_work .work , 2 );
453
453
}
454
454
455
455
static int tls_do_encryption (struct sock * sk ,
@@ -461,13 +461,14 @@ static int tls_do_encryption(struct sock *sk,
461
461
struct tls_rec * rec = ctx -> open_rec ;
462
462
int rc ;
463
463
464
- rec -> sg_encrypted_data [0 ].offset += tls_ctx -> tx .prepend_size ;
465
- rec -> sg_encrypted_data [0 ].length -= tls_ctx -> tx .prepend_size ;
464
+ /* Skip the first index as it contains AAD data */
465
+ rec -> sg_encrypted_data [1 ].offset += tls_ctx -> tx .prepend_size ;
466
+ rec -> sg_encrypted_data [1 ].length -= tls_ctx -> tx .prepend_size ;
466
467
467
468
aead_request_set_tfm (aead_req , ctx -> aead_send );
468
469
aead_request_set_ad (aead_req , TLS_AAD_SPACE_SIZE );
469
- aead_request_set_crypt (aead_req , rec -> sg_aead_in ,
470
- rec -> sg_aead_out ,
470
+ aead_request_set_crypt (aead_req , rec -> sg_plaintext_data ,
471
+ rec -> sg_encrypted_data ,
471
472
data_len , tls_ctx -> tx .iv );
472
473
473
474
aead_request_set_callback (aead_req , CRYPTO_TFM_REQ_MAY_BACKLOG ,
@@ -480,8 +481,8 @@ static int tls_do_encryption(struct sock *sk,
480
481
rc = crypto_aead_encrypt (aead_req );
481
482
if (!rc || rc != - EINPROGRESS ) {
482
483
atomic_dec (& ctx -> encrypt_pending );
483
- rec -> sg_encrypted_data [0 ].offset -= tls_ctx -> tx .prepend_size ;
484
- rec -> sg_encrypted_data [0 ].length += tls_ctx -> tx .prepend_size ;
484
+ rec -> sg_encrypted_data [1 ].offset -= tls_ctx -> tx .prepend_size ;
485
+ rec -> sg_encrypted_data [1 ].length += tls_ctx -> tx .prepend_size ;
485
486
}
486
487
487
488
if (!rc ) {
@@ -512,16 +513,16 @@ static int tls_push_record(struct sock *sk, int flags,
512
513
rec -> tx_flags = flags ;
513
514
req = & rec -> aead_req ;
514
515
515
- sg_mark_end (rec -> sg_plaintext_data + rec -> sg_plaintext_num_elem - 1 );
516
- sg_mark_end (rec -> sg_encrypted_data + rec -> sg_encrypted_num_elem - 1 );
516
+ sg_mark_end (rec -> sg_plaintext_data + rec -> sg_plaintext_num_elem );
517
+ sg_mark_end (rec -> sg_encrypted_data + rec -> sg_encrypted_num_elem );
517
518
518
519
tls_make_aad (rec -> aad_space , rec -> sg_plaintext_size ,
519
520
tls_ctx -> tx .rec_seq , tls_ctx -> tx .rec_seq_size ,
520
521
record_type );
521
522
522
523
tls_fill_prepend (tls_ctx ,
523
- page_address (sg_page (& rec -> sg_encrypted_data [0 ])) +
524
- rec -> sg_encrypted_data [0 ].offset ,
524
+ page_address (sg_page (& rec -> sg_encrypted_data [1 ])) +
525
+ rec -> sg_encrypted_data [1 ].offset ,
525
526
rec -> sg_plaintext_size , record_type );
526
527
527
528
tls_ctx -> pending_open_record_frags = 0 ;
@@ -613,7 +614,7 @@ static int memcopy_from_iter(struct sock *sk, struct iov_iter *from,
613
614
struct tls_context * tls_ctx = tls_get_ctx (sk );
614
615
struct tls_sw_context_tx * ctx = tls_sw_ctx_tx (tls_ctx );
615
616
struct tls_rec * rec = ctx -> open_rec ;
616
- struct scatterlist * sg = rec -> sg_plaintext_data ;
617
+ struct scatterlist * sg = & rec -> sg_plaintext_data [ 1 ] ;
617
618
int copy , i , rc = 0 ;
618
619
619
620
for (i = tls_ctx -> pending_open_record_frags ;
@@ -659,17 +660,10 @@ static struct tls_rec *get_rec(struct sock *sk)
659
660
sg_init_table (& rec -> sg_encrypted_data [0 ],
660
661
ARRAY_SIZE (rec -> sg_encrypted_data ));
661
662
662
- sg_init_table (rec -> sg_aead_in , 2 );
663
- sg_set_buf (& rec -> sg_aead_in [0 ], rec -> aad_space ,
663
+ sg_set_buf (& rec -> sg_plaintext_data [0 ], rec -> aad_space ,
664
664
sizeof (rec -> aad_space ));
665
- sg_unmark_end (& rec -> sg_aead_in [1 ]);
666
- sg_chain (rec -> sg_aead_in , 2 , rec -> sg_plaintext_data );
667
-
668
- sg_init_table (rec -> sg_aead_out , 2 );
669
- sg_set_buf (& rec -> sg_aead_out [0 ], rec -> aad_space ,
665
+ sg_set_buf (& rec -> sg_encrypted_data [0 ], rec -> aad_space ,
670
666
sizeof (rec -> aad_space ));
671
- sg_unmark_end (& rec -> sg_aead_out [1 ]);
672
- sg_chain (rec -> sg_aead_out , 2 , rec -> sg_encrypted_data );
673
667
674
668
ctx -> open_rec = rec ;
675
669
@@ -763,8 +757,8 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
763
757
ret = zerocopy_from_iter (sk , & msg -> msg_iter ,
764
758
try_to_copy , & rec -> sg_plaintext_num_elem ,
765
759
& rec -> sg_plaintext_size ,
766
- rec -> sg_plaintext_data ,
767
- ARRAY_SIZE (rec -> sg_plaintext_data ),
760
+ & rec -> sg_plaintext_data [ 1 ] ,
761
+ ARRAY_SIZE (rec -> sg_plaintext_data ) - 1 ,
768
762
true);
769
763
if (ret )
770
764
goto fallback_to_reg_send ;
@@ -781,7 +775,7 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
781
775
continue ;
782
776
783
777
fallback_to_reg_send :
784
- trim_sg (sk , rec -> sg_plaintext_data ,
778
+ trim_sg (sk , & rec -> sg_plaintext_data [ 1 ] ,
785
779
& rec -> sg_plaintext_num_elem ,
786
780
& rec -> sg_plaintext_size ,
787
781
orig_size );
@@ -801,7 +795,7 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
801
795
try_to_copy -= required_size - rec -> sg_plaintext_size ;
802
796
full_record = true;
803
797
804
- trim_sg (sk , rec -> sg_encrypted_data ,
798
+ trim_sg (sk , & rec -> sg_encrypted_data [ 1 ] ,
805
799
& rec -> sg_encrypted_num_elem ,
806
800
& rec -> sg_encrypted_size ,
807
801
rec -> sg_plaintext_size +
@@ -949,7 +943,7 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,
949
943
}
950
944
951
945
get_page (page );
952
- sg = rec -> sg_plaintext_data + rec -> sg_plaintext_num_elem ;
946
+ sg = & rec -> sg_plaintext_data [ 1 ] + rec -> sg_plaintext_num_elem ;
953
947
sg_set_page (sg , page , copy , offset );
954
948
sg_unmark_end (sg );
955
949
@@ -963,7 +957,7 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,
963
957
964
958
if (full_record || eor ||
965
959
rec -> sg_plaintext_num_elem ==
966
- ARRAY_SIZE (rec -> sg_plaintext_data )) {
960
+ ARRAY_SIZE (rec -> sg_plaintext_data ) - 1 ) {
967
961
ret = tls_push_record (sk , flags , record_type );
968
962
if (ret ) {
969
963
if (ret == - EINPROGRESS )
@@ -1571,7 +1565,7 @@ void tls_sw_free_resources_tx(struct sock *sk)
1571
1565
rec = list_first_entry (& ctx -> tx_list ,
1572
1566
struct tls_rec , list );
1573
1567
1574
- free_sg (sk , rec -> sg_plaintext_data ,
1568
+ free_sg (sk , & rec -> sg_plaintext_data [ 1 ] ,
1575
1569
& rec -> sg_plaintext_num_elem ,
1576
1570
& rec -> sg_plaintext_size );
1577
1571
@@ -1580,11 +1574,11 @@ void tls_sw_free_resources_tx(struct sock *sk)
1580
1574
}
1581
1575
1582
1576
list_for_each_entry_safe (rec , tmp , & ctx -> tx_list , list ) {
1583
- free_sg (sk , rec -> sg_encrypted_data ,
1577
+ free_sg (sk , & rec -> sg_encrypted_data [ 1 ] ,
1584
1578
& rec -> sg_encrypted_num_elem ,
1585
1579
& rec -> sg_encrypted_size );
1586
1580
1587
- free_sg (sk , rec -> sg_plaintext_data ,
1581
+ free_sg (sk , & rec -> sg_plaintext_data [ 1 ] ,
1588
1582
& rec -> sg_plaintext_num_elem ,
1589
1583
& rec -> sg_plaintext_size );
1590
1584
0 commit comments