@@ -165,6 +165,7 @@ struct nvme_tcp_queue {
165165
166166 bool hdr_digest ;
167167 bool data_digest ;
168+ bool tls_enabled ;
168169 struct ahash_request * rcv_hash ;
169170 struct ahash_request * snd_hash ;
170171 __le32 exp_ddgst ;
@@ -213,7 +214,21 @@ static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
213214 return queue - queue -> ctrl -> queues ;
214215}
215216
216- static inline bool nvme_tcp_tls (struct nvme_ctrl * ctrl )
217+ /*
218+ * Check if the queue is TLS encrypted
219+ */
220+ static inline bool nvme_tcp_queue_tls (struct nvme_tcp_queue * queue )
221+ {
222+ if (!IS_ENABLED (CONFIG_NVME_TCP_TLS ))
223+ return 0 ;
224+
225+ return queue -> tls_enabled ;
226+ }
227+
228+ /*
229+ * Check if TLS is configured for the controller.
230+ */
231+ static inline bool nvme_tcp_tls_configured (struct nvme_ctrl * ctrl )
217232{
218233 if (!IS_ENABLED (CONFIG_NVME_TCP_TLS ))
219234 return 0 ;
@@ -368,7 +383,7 @@ static inline bool nvme_tcp_queue_has_pending(struct nvme_tcp_queue *queue)
368383
369384static inline bool nvme_tcp_queue_more (struct nvme_tcp_queue * queue )
370385{
371- return !nvme_tcp_tls ( & queue -> ctrl -> ctrl ) &&
386+ return !nvme_tcp_queue_tls ( queue ) &&
372387 nvme_tcp_queue_has_pending (queue );
373388}
374389
@@ -1427,7 +1442,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
14271442 memset (& msg , 0 , sizeof (msg ));
14281443 iov .iov_base = icresp ;
14291444 iov .iov_len = sizeof (* icresp );
1430- if (nvme_tcp_tls ( & queue -> ctrl -> ctrl )) {
1445+ if (nvme_tcp_queue_tls ( queue )) {
14311446 msg .msg_control = cbuf ;
14321447 msg .msg_controllen = sizeof (cbuf );
14331448 }
@@ -1439,7 +1454,7 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
14391454 goto free_icresp ;
14401455 }
14411456 ret = - ENOTCONN ;
1442- if (nvme_tcp_tls ( & queue -> ctrl -> ctrl )) {
1457+ if (nvme_tcp_queue_tls ( queue )) {
14431458 ctype = tls_get_record_type (queue -> sock -> sk ,
14441459 (struct cmsghdr * )cbuf );
14451460 if (ctype != TLS_RECORD_TYPE_DATA ) {
@@ -1587,7 +1602,10 @@ static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid)
15871602 qid , pskid );
15881603 queue -> tls_err = - ENOKEY ;
15891604 } else {
1590- ctrl -> ctrl .tls_key = tls_key ;
1605+ queue -> tls_enabled = true;
1606+ if (qid == 0 )
1607+ ctrl -> ctrl .tls_pskid = key_serial (tls_key );
1608+ key_put (tls_key );
15911609 queue -> tls_err = 0 ;
15921610 }
15931611
@@ -1768,7 +1786,7 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
17681786 }
17691787
17701788 /* If PSKs are configured try to start TLS */
1771- if (IS_ENABLED ( CONFIG_NVME_TCP_TLS ) && pskid ) {
1789+ if (nvme_tcp_tls_configured ( nctrl ) && pskid ) {
17721790 ret = nvme_tcp_start_tls (nctrl , queue , pskid );
17731791 if (ret )
17741792 goto err_init_connect ;
@@ -1829,6 +1847,8 @@ static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
18291847 mutex_lock (& queue -> queue_lock );
18301848 if (test_and_clear_bit (NVME_TCP_Q_LIVE , & queue -> flags ))
18311849 __nvme_tcp_stop_queue (queue );
1850+ /* Stopping the queue will disable TLS */
1851+ queue -> tls_enabled = false;
18321852 mutex_unlock (& queue -> queue_lock );
18331853}
18341854
@@ -1925,16 +1945,17 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
19251945 int ret ;
19261946 key_serial_t pskid = 0 ;
19271947
1928- if (nvme_tcp_tls (ctrl )) {
1948+ if (nvme_tcp_tls_configured (ctrl )) {
19291949 if (ctrl -> opts -> tls_key )
19301950 pskid = key_serial (ctrl -> opts -> tls_key );
1931- else
1951+ else {
19321952 pskid = nvme_tls_psk_default (ctrl -> opts -> keyring ,
19331953 ctrl -> opts -> host -> nqn ,
19341954 ctrl -> opts -> subsysnqn );
1935- if (!pskid ) {
1936- dev_err (ctrl -> device , "no valid PSK found\n" );
1937- return - ENOKEY ;
1955+ if (!pskid ) {
1956+ dev_err (ctrl -> device , "no valid PSK found\n" );
1957+ return - ENOKEY ;
1958+ }
19381959 }
19391960 }
19401961
@@ -1957,13 +1978,14 @@ static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
19571978{
19581979 int i , ret ;
19591980
1960- if (nvme_tcp_tls (ctrl ) && !ctrl -> tls_key ) {
1981+ if (nvme_tcp_tls_configured (ctrl ) && !ctrl -> tls_pskid ) {
19611982 dev_err (ctrl -> device , "no PSK negotiated\n" );
19621983 return - ENOKEY ;
19631984 }
1985+
19641986 for (i = 1 ; i < ctrl -> queue_count ; i ++ ) {
19651987 ret = nvme_tcp_alloc_queue (ctrl , i ,
1966- key_serial ( ctrl -> tls_key ) );
1988+ ctrl -> tls_pskid );
19671989 if (ret )
19681990 goto out_free_queues ;
19691991 }
@@ -2144,6 +2166,11 @@ static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
21442166 if (remove )
21452167 nvme_unquiesce_admin_queue (ctrl );
21462168 nvme_tcp_destroy_admin_queue (ctrl , remove );
2169+ if (ctrl -> tls_pskid ) {
2170+ dev_dbg (ctrl -> device , "Wipe negotiated TLS_PSK %08x\n" ,
2171+ ctrl -> tls_pskid );
2172+ ctrl -> tls_pskid = 0 ;
2173+ }
21472174}
21482175
21492176static void nvme_tcp_teardown_io_queues (struct nvme_ctrl * ctrl ,
0 commit comments