38
38
#include <linux/highmem.h>
39
39
#include <linux/netdevice.h>
40
40
#include <linux/sched/signal.h>
41
+ #include <linux/inetdevice.h>
41
42
42
43
#include <net/tls.h>
43
44
@@ -56,11 +57,14 @@ enum {
56
57
TLS_SW_TX ,
57
58
TLS_SW_RX ,
58
59
TLS_SW_RXTX ,
60
+ TLS_HW_RECORD ,
59
61
TLS_NUM_CONFIG ,
60
62
};
61
63
62
64
static struct proto * saved_tcpv6_prot ;
63
65
static DEFINE_MUTEX (tcpv6_prot_mutex );
66
+ static LIST_HEAD (device_list );
67
+ static DEFINE_MUTEX (device_mutex );
64
68
static struct proto tls_prots [TLS_NUM_PROTS ][TLS_NUM_CONFIG ];
65
69
static struct proto_ops tls_sw_proto_ops ;
66
70
@@ -241,8 +245,12 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
241
245
lock_sock (sk );
242
246
sk_proto_close = ctx -> sk_proto_close ;
243
247
248
+ if (ctx -> conf == TLS_HW_RECORD )
249
+ goto skip_tx_cleanup ;
250
+
244
251
if (ctx -> conf == TLS_BASE ) {
245
252
kfree (ctx );
253
+ ctx = NULL ;
246
254
goto skip_tx_cleanup ;
247
255
}
248
256
@@ -276,6 +284,11 @@ static void tls_sk_proto_close(struct sock *sk, long timeout)
276
284
skip_tx_cleanup :
277
285
release_sock (sk );
278
286
sk_proto_close (sk , timeout );
287
+ /* free ctx for TLS_HW_RECORD, used by tcp_set_state
288
+ * for sk->sk_prot->unhash [tls_hw_unhash]
289
+ */
290
+ if (ctx && ctx -> conf == TLS_HW_RECORD )
291
+ kfree (ctx );
279
292
}
280
293
281
294
static int do_tls_getsockopt_tx (struct sock * sk , char __user * optval ,
@@ -493,6 +506,79 @@ static int tls_setsockopt(struct sock *sk, int level, int optname,
493
506
return do_tls_setsockopt (sk , optname , optval , optlen );
494
507
}
495
508
509
+ static struct tls_context * create_ctx (struct sock * sk )
510
+ {
511
+ struct inet_connection_sock * icsk = inet_csk (sk );
512
+ struct tls_context * ctx ;
513
+
514
+ ctx = kzalloc (sizeof (* ctx ), GFP_KERNEL );
515
+ if (!ctx )
516
+ return NULL ;
517
+
518
+ icsk -> icsk_ulp_data = ctx ;
519
+ return ctx ;
520
+ }
521
+
522
+ static int tls_hw_prot (struct sock * sk )
523
+ {
524
+ struct tls_context * ctx ;
525
+ struct tls_device * dev ;
526
+ int rc = 0 ;
527
+
528
+ mutex_lock (& device_mutex );
529
+ list_for_each_entry (dev , & device_list , dev_list ) {
530
+ if (dev -> feature && dev -> feature (dev )) {
531
+ ctx = create_ctx (sk );
532
+ if (!ctx )
533
+ goto out ;
534
+
535
+ ctx -> hash = sk -> sk_prot -> hash ;
536
+ ctx -> unhash = sk -> sk_prot -> unhash ;
537
+ ctx -> sk_proto_close = sk -> sk_prot -> close ;
538
+ ctx -> conf = TLS_HW_RECORD ;
539
+ update_sk_prot (sk , ctx );
540
+ rc = 1 ;
541
+ break ;
542
+ }
543
+ }
544
+ out :
545
+ mutex_unlock (& device_mutex );
546
+ return rc ;
547
+ }
548
+
549
+ static void tls_hw_unhash (struct sock * sk )
550
+ {
551
+ struct tls_context * ctx = tls_get_ctx (sk );
552
+ struct tls_device * dev ;
553
+
554
+ mutex_lock (& device_mutex );
555
+ list_for_each_entry (dev , & device_list , dev_list ) {
556
+ if (dev -> unhash )
557
+ dev -> unhash (dev , sk );
558
+ }
559
+ mutex_unlock (& device_mutex );
560
+ ctx -> unhash (sk );
561
+ }
562
+
563
+ static int tls_hw_hash (struct sock * sk )
564
+ {
565
+ struct tls_context * ctx = tls_get_ctx (sk );
566
+ struct tls_device * dev ;
567
+ int err ;
568
+
569
+ err = ctx -> hash (sk );
570
+ mutex_lock (& device_mutex );
571
+ list_for_each_entry (dev , & device_list , dev_list ) {
572
+ if (dev -> hash )
573
+ err |= dev -> hash (dev , sk );
574
+ }
575
+ mutex_unlock (& device_mutex );
576
+
577
+ if (err )
578
+ tls_hw_unhash (sk );
579
+ return err ;
580
+ }
581
+
496
582
static void build_protos (struct proto * prot , struct proto * base )
497
583
{
498
584
prot [TLS_BASE ] = * base ;
@@ -511,15 +597,22 @@ static void build_protos(struct proto *prot, struct proto *base)
511
597
prot [TLS_SW_RXTX ] = prot [TLS_SW_TX ];
512
598
prot [TLS_SW_RXTX ].recvmsg = tls_sw_recvmsg ;
513
599
prot [TLS_SW_RXTX ].close = tls_sk_proto_close ;
600
+
601
+ prot [TLS_HW_RECORD ] = * base ;
602
+ prot [TLS_HW_RECORD ].hash = tls_hw_hash ;
603
+ prot [TLS_HW_RECORD ].unhash = tls_hw_unhash ;
604
+ prot [TLS_HW_RECORD ].close = tls_sk_proto_close ;
514
605
}
515
606
516
607
static int tls_init (struct sock * sk )
517
608
{
518
609
int ip_ver = sk -> sk_family == AF_INET6 ? TLSV6 : TLSV4 ;
519
- struct inet_connection_sock * icsk = inet_csk (sk );
520
610
struct tls_context * ctx ;
521
611
int rc = 0 ;
522
612
613
+ if (tls_hw_prot (sk ))
614
+ goto out ;
615
+
523
616
/* The TLS ulp is currently supported only for TCP sockets
524
617
* in ESTABLISHED state.
525
618
* Supporting sockets in LISTEN state will require us
@@ -530,12 +623,11 @@ static int tls_init(struct sock *sk)
530
623
return - ENOTSUPP ;
531
624
532
625
/* allocate tls context */
533
- ctx = kzalloc ( sizeof ( * ctx ), GFP_KERNEL );
626
+ ctx = create_ctx ( sk );
534
627
if (!ctx ) {
535
628
rc = - ENOMEM ;
536
629
goto out ;
537
630
}
538
- icsk -> icsk_ulp_data = ctx ;
539
631
ctx -> setsockopt = sk -> sk_prot -> setsockopt ;
540
632
ctx -> getsockopt = sk -> sk_prot -> getsockopt ;
541
633
ctx -> sk_proto_close = sk -> sk_prot -> close ;
@@ -557,6 +649,22 @@ static int tls_init(struct sock *sk)
557
649
return rc ;
558
650
}
559
651
652
+ void tls_register_device (struct tls_device * device )
653
+ {
654
+ mutex_lock (& device_mutex );
655
+ list_add_tail (& device -> dev_list , & device_list );
656
+ mutex_unlock (& device_mutex );
657
+ }
658
+ EXPORT_SYMBOL (tls_register_device );
659
+
660
+ void tls_unregister_device (struct tls_device * device )
661
+ {
662
+ mutex_lock (& device_mutex );
663
+ list_del (& device -> dev_list );
664
+ mutex_unlock (& device_mutex );
665
+ }
666
+ EXPORT_SYMBOL (tls_unregister_device );
667
+
560
668
static struct tcp_ulp_ops tcp_tls_ulp_ops __read_mostly = {
561
669
.name = "tls" ,
562
670
.uid = TCP_ULP_TLS ,
0 commit comments