8
8
#include <linux/init.h>
9
9
#include <linux/slab.h>
10
10
#include <linux/err.h>
11
+ #include <linux/key.h>
11
12
#include <linux/nvme-tcp.h>
13
+ #include <linux/nvme-keyring.h>
12
14
#include <net/sock.h>
13
15
#include <net/tcp.h>
16
+ #include <net/tls.h>
17
+ #include <net/handshake.h>
14
18
#include <linux/blk-mq.h>
15
19
#include <crypto/hash.h>
16
20
#include <net/busy_poll.h>
@@ -31,6 +35,16 @@ static int so_priority;
31
35
module_param (so_priority , int , 0644 );
32
36
MODULE_PARM_DESC (so_priority , "nvme tcp socket optimize priority" );
33
37
38
+ #ifdef CONFIG_NVME_TCP_TLS
39
+ /*
40
+ * TLS handshake timeout
41
+ */
42
+ static int tls_handshake_timeout = 10 ;
43
+ module_param (tls_handshake_timeout , int , 0644 );
44
+ MODULE_PARM_DESC (tls_handshake_timeout ,
45
+ "nvme TLS handshake timeout in seconds (default 10)" );
46
+ #endif
47
+
34
48
#ifdef CONFIG_DEBUG_LOCK_ALLOC
35
49
/* lockdep can detect a circular dependency of the form
36
50
* sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
@@ -146,7 +160,10 @@ struct nvme_tcp_queue {
146
160
struct ahash_request * snd_hash ;
147
161
__le32 exp_ddgst ;
148
162
__le32 recv_ddgst ;
149
-
163
+ #ifdef CONFIG_NVME_TCP_TLS
164
+ struct completion tls_complete ;
165
+ int tls_err ;
166
+ #endif
150
167
struct page_frag_cache pf_cache ;
151
168
152
169
void (* state_change )(struct sock * );
@@ -1509,7 +1526,92 @@ static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
1509
1526
queue -> io_cpu = cpumask_next_wrap (n - 1 , cpu_online_mask , -1 , false);
1510
1527
}
1511
1528
1512
- static int nvme_tcp_alloc_queue (struct nvme_ctrl * nctrl , int qid )
1529
+ #ifdef CONFIG_NVME_TCP_TLS
1530
+ static void nvme_tcp_tls_done (void * data , int status , key_serial_t pskid )
1531
+ {
1532
+ struct nvme_tcp_queue * queue = data ;
1533
+ struct nvme_tcp_ctrl * ctrl = queue -> ctrl ;
1534
+ int qid = nvme_tcp_queue_id (queue );
1535
+ struct key * tls_key ;
1536
+
1537
+ dev_dbg (ctrl -> ctrl .device , "queue %d: TLS handshake done, key %x, status %d\n" ,
1538
+ qid , pskid , status );
1539
+
1540
+ if (status ) {
1541
+ queue -> tls_err = - status ;
1542
+ goto out_complete ;
1543
+ }
1544
+
1545
+ tls_key = key_lookup (pskid );
1546
+ if (IS_ERR (tls_key )) {
1547
+ dev_warn (ctrl -> ctrl .device , "queue %d: Invalid key %x\n" ,
1548
+ qid , pskid );
1549
+ queue -> tls_err = - ENOKEY ;
1550
+ } else {
1551
+ ctrl -> ctrl .tls_key = tls_key ;
1552
+ queue -> tls_err = 0 ;
1553
+ }
1554
+
1555
+ out_complete :
1556
+ complete (& queue -> tls_complete );
1557
+ }
1558
+
1559
+ static int nvme_tcp_start_tls (struct nvme_ctrl * nctrl ,
1560
+ struct nvme_tcp_queue * queue ,
1561
+ key_serial_t pskid )
1562
+ {
1563
+ int qid = nvme_tcp_queue_id (queue );
1564
+ int ret ;
1565
+ struct tls_handshake_args args ;
1566
+ unsigned long tmo = tls_handshake_timeout * HZ ;
1567
+ key_serial_t keyring = nvme_keyring_id ();
1568
+
1569
+ dev_dbg (nctrl -> device , "queue %d: start TLS with key %x\n" ,
1570
+ qid , pskid );
1571
+ memset (& args , 0 , sizeof (args ));
1572
+ args .ta_sock = queue -> sock ;
1573
+ args .ta_done = nvme_tcp_tls_done ;
1574
+ args .ta_data = queue ;
1575
+ args .ta_my_peerids [0 ] = pskid ;
1576
+ args .ta_num_peerids = 1 ;
1577
+ args .ta_keyring = keyring ;
1578
+ args .ta_timeout_ms = tls_handshake_timeout * 1000 ;
1579
+ queue -> tls_err = - EOPNOTSUPP ;
1580
+ init_completion (& queue -> tls_complete );
1581
+ ret = tls_client_hello_psk (& args , GFP_KERNEL );
1582
+ if (ret ) {
1583
+ dev_err (nctrl -> device , "queue %d: failed to start TLS: %d\n" ,
1584
+ qid , ret );
1585
+ return ret ;
1586
+ }
1587
+ ret = wait_for_completion_interruptible_timeout (& queue -> tls_complete , tmo );
1588
+ if (ret <= 0 ) {
1589
+ if (ret == 0 )
1590
+ ret = - ETIMEDOUT ;
1591
+
1592
+ dev_err (nctrl -> device ,
1593
+ "queue %d: TLS handshake failed, error %d\n" ,
1594
+ qid , ret );
1595
+ tls_handshake_cancel (queue -> sock -> sk );
1596
+ } else {
1597
+ dev_dbg (nctrl -> device ,
1598
+ "queue %d: TLS handshake complete, error %d\n" ,
1599
+ qid , queue -> tls_err );
1600
+ ret = queue -> tls_err ;
1601
+ }
1602
+ return ret ;
1603
+ }
1604
+ #else
1605
+ static int nvme_tcp_start_tls (struct nvme_ctrl * nctrl ,
1606
+ struct nvme_tcp_queue * queue ,
1607
+ key_serial_t pskid )
1608
+ {
1609
+ return - EPROTONOSUPPORT ;
1610
+ }
1611
+ #endif
1612
+
1613
+ static int nvme_tcp_alloc_queue (struct nvme_ctrl * nctrl , int qid ,
1614
+ key_serial_t pskid )
1513
1615
{
1514
1616
struct nvme_tcp_ctrl * ctrl = to_tcp_ctrl (nctrl );
1515
1617
struct nvme_tcp_queue * queue = & ctrl -> queues [qid ];
@@ -1632,6 +1734,13 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)
1632
1734
goto err_rcv_pdu ;
1633
1735
}
1634
1736
1737
+ /* If PSKs are configured try to start TLS */
1738
+ if (pskid ) {
1739
+ ret = nvme_tcp_start_tls (nctrl , queue , pskid );
1740
+ if (ret )
1741
+ goto err_init_connect ;
1742
+ }
1743
+
1635
1744
ret = nvme_tcp_init_connection (queue );
1636
1745
if (ret )
1637
1746
goto err_init_connect ;
@@ -1781,10 +1890,22 @@ static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl,
1781
1890
static int nvme_tcp_alloc_admin_queue (struct nvme_ctrl * ctrl )
1782
1891
{
1783
1892
int ret ;
1893
+ key_serial_t pskid = 0 ;
1894
+
1895
+ if (ctrl -> opts -> tls ) {
1896
+ pskid = nvme_tls_psk_default (NULL ,
1897
+ ctrl -> opts -> host -> nqn ,
1898
+ ctrl -> opts -> subsysnqn );
1899
+ if (!pskid ) {
1900
+ dev_err (ctrl -> device , "no valid PSK found\n" );
1901
+ ret = - ENOKEY ;
1902
+ goto out_free_queue ;
1903
+ }
1904
+ }
1784
1905
1785
- ret = nvme_tcp_alloc_queue (ctrl , 0 );
1906
+ ret = nvme_tcp_alloc_queue (ctrl , 0 , pskid );
1786
1907
if (ret )
1787
- return ret ;
1908
+ goto out_free_queue ;
1788
1909
1789
1910
ret = nvme_tcp_alloc_async_req (to_tcp_ctrl (ctrl ));
1790
1911
if (ret )
@@ -1801,8 +1922,13 @@ static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
1801
1922
{
1802
1923
int i , ret ;
1803
1924
1925
+ if (ctrl -> opts -> tls && !ctrl -> tls_key ) {
1926
+ dev_err (ctrl -> device , "no PSK negotiated\n" );
1927
+ return - ENOKEY ;
1928
+ }
1804
1929
for (i = 1 ; i < ctrl -> queue_count ; i ++ ) {
1805
- ret = nvme_tcp_alloc_queue (ctrl , i );
1930
+ ret = nvme_tcp_alloc_queue (ctrl , i ,
1931
+ key_serial (ctrl -> tls_key ));
1806
1932
if (ret )
1807
1933
goto out_free_queues ;
1808
1934
}
@@ -2630,7 +2756,7 @@ static struct nvmf_transport_ops nvme_tcp_transport = {
2630
2756
NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
2631
2757
NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
2632
2758
NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
2633
- NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE ,
2759
+ NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS ,
2634
2760
.create_ctrl = nvme_tcp_create_ctrl ,
2635
2761
};
2636
2762
0 commit comments