nvme: In-kernel TLS support for TCP
Subsystems (7)
v3 cover nvme: In-kernel TLS support for TCP
Hi all,
finally I've managed to put all things together and enable in-kernel TLS support for NVMe-over-TCP.
The patchset is based on the TLS upcall mechanism from Chuck Lever (cf '[PATCH v7 0/2] Another crack at a handshake upcall mechanism' posted to the linux netdev list), and requires the 'tlshd' userspace daemon (https://github.com/oracle/ktls-utils) for the actual TLS handshake. Changes for nvme-cli are already included in the upstream repository.
Theory of operation:
A dedicated '.nvme' keyring is created to hold the pre-shared keys (PSKs) for the TLS handshake. Keys will have to be provisioned before TLS handshake is attempted; that can be done with the 'nvme gen-tls-key' command for nvme-cli (patches are already merged upstream).
After connection to the remote TCP port the client side will use the 'best' PSK (as inferred from the NVMe TCP spec) or the PSK specified by the '--tls_key' option to nvme-cli and call the TLS userspace daemon to initiate a TLS handshake.
The server side will then invoke the TLS userspace daemon to run the TLS handshake.
If the TLS handshake succeeds the userspace daemon will be activating kTLS on the socket, and control is passed back to the kernel.
To make this work I had to implement the 'read_sock()' functionality for TLS; it seems to be holding up well enough (for me), but it really could do with reviews from persons with more network stack knowledge.
As usual, comments and reviews are welcome.
Changes to v2:
- Included reviews from Sagi
- Removed MSG_SENDPAGE_NOTLAST
- Improved MSG_EOR handling for TLS
- Add config options NVME_TCP_TLS
and NVME_TARGET_TCP_TLS
Changes to the original RFC:
- Add a CONFIG_NVME_TLS config option
- Use a single PSK for the TLS handshake
- Make TLS connections mandatory
- Do not peek messages for the server
- Simplify data_ready callback
- Implement read_sock() for TLS
Hannes Reinecke (18):
nvme-keyring: register '.nvme' keyring
nvme-keyring: define a 'psk' keytype
nvme: add TCP TSAS definitions
nvme-tcp: add definitions for TLS cipher suites
nvme-keyring: implement nvme_tls_psk_default() net/tls: implement ->read_sock()
net/tls: sanitize MSG_EOR handling
nvme-tcp: do not set MSG_SENDPAGE_NOTLAST security/keys: export key_lookup() nvme/tcp: allocate socket file
nvme-tcp: enable TLS handshake upcall
nvme-tcp: control message handling for recvmsg()
nvme-fabrics: parse options 'keyring' and 'tls_key'
nvmet: make TCP sectype settable via configfs
nvmet-tcp: allocate socket file
nvmet-tcp: enable TLS handshake upcall
nvmet-tcp: control messages for recvmsg()
nvmet-tcp: add configfs attribute 'param_keyring'
drivers/nvme/common/Kconfig | 4 + drivers/nvme/common/Makefile | 3 +- drivers/nvme/common/keyring.c | 182 ++++++++++++++++++++++++++++ drivers/nvme/host/Kconfig | 14 +++ drivers/nvme/host/core.c | 33 ++++- drivers/nvme/host/fabrics.c | 77 +++++++++++- drivers/nvme/host/fabrics.h | 9 ++ drivers/nvme/host/nvme.h | 1 + drivers/nvme/host/tcp.c | 175 +++++++++++++++++++++++++-- drivers/nvme/target/Kconfig | 14 +++ drivers/nvme/target/configfs.c | 173 +++++++++++++++++++++++++- drivers/nvme/target/nvmet.h | 1 + drivers/nvme/target/tcp.c | 213 ++++++++++++++++++++++++++++++--- include/linux/nvme-keyring.h | 36 ++++++ include/linux/nvme-tcp.h | 6 + include/linux/nvme.h | 10 ++ net/tls/tls.h | 2 + net/tls/tls_device.c | 10 ++ net/tls/tls_main.c | 2 + net/tls/tls_sw.c | 92 ++++++++++++++ security/keys/key.c | 1 + 21 files changed, 1020 insertions(+), 38 deletions(-) create mode 100644 drivers/nvme/common/keyring.c create mode 100644 include/linux/nvme-keyring.h
Signature · 1 line
2.35.3
PATCH v3 1/18 nvme-keyring: register '.nvme' keyring
Register a '.nvme' keyring to hold keys for TLS and DH-HMAC-CHAP and add a new config option NVME_KEYRING.
We need a separate keyring for NVMe as the configuration is done via individual commands (eg for configfs), and the usual per-session or per-process keyrings can't be used.
--- drivers/nvme/common/Kconfig | 4 ++++ drivers/nvme/common/Makefile | 3 ++- drivers/nvme/common/keyring.c | 40 +++++++++++++++++++++++++++++++++++ drivers/nvme/host/core.c | 10 +++++++-- include/linux/nvme-keyring.h | 28 ++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 drivers/nvme/common/keyring.c create mode 100644 include/linux/nvme-keyring.h
drivers/nvme/common/Kconfig +4 -0
@@ -2,3 +2,7 @@ config NVME_COMMON tristate + +config NVME_KEYRING + bool + select KEYS
drivers/nvme/common/Makefile +2 -1
@@ -4,4 +4,5 @@ ccflags-y += -I$(src) obj-$(CONFIG_NVME_COMMON) += nvme-common.o -nvme-common-y += auth.o +nvme-common-$(CONFIG_NVME_AUTH) += auth.o +nvme-common-$(CONFIG_NVME_KEYRING) += keyring.o
drivers/nvme/common/keyring.c new file +40 -0
@@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (c) 2023 Hannes Reinecke, SUSE Labs +*/ + +#include <linux/module.h> +#include <linux/seq_file.h> +#include <linux/key-type.h> +#include <keys/user-type.h> +#include <linux/nvme.h> + +static struct key *nvme_keyring; + +key_serial_t nvme_keyring_id (void ) +{ +return nvme_keyring->serial; +} +EXPORT_SYMBOL_GPL (nvme_keyring_id); + +int nvme_keyring_init (void ) +{ + nvme_keyring =keyring_alloc (".nvme" , +GLOBAL_ROOT_UID ,GLOBAL_ROOT_GID , +current_cred (), + (KEY_POS_ALL & ~KEY_POS_SETATTR ) | + (KEY_USR_ALL & ~KEY_USR_SETATTR ), +KEY_ALLOC_NOT_IN_QUOTA ,NULL ,NULL ); +if (IS_ERR (nvme_keyring)) +return PTR_ERR (nvme_keyring); + +return 0 ; +} +EXPORT_SYMBOL_GPL (nvme_keyring_init); + +void nvme_keyring_exit (void ) +{ +key_revoke (nvme_keyring); +key_put (nvme_keyring); +} +EXPORT_SYMBOL_GPL (nvme_keyring_exit);
drivers/nvme/host/core.c +8 -2
@@ -25,6 +25,7 @@#include "nvme.h" #include "fabrics.h" #include <linux/nvme-auth.h> +#include <linux/nvme-keyring.h> #define CREATE_TRACE_POINTS #include "trace.h"
@@ -5401,12 +5402,16 @@ static int __init nvme_core_init(void) result =PTR_ERR (nvme_ns_chr_class); goto unregister_generic_ns; } - - result =nvme_init_auth (); + result =nvme_keyring_init ();if (result) goto destroy_ns_chr; + result =nvme_init_auth (); +if (result) + goto keyring_exit;return 0 ; +keyring_exit: +nvme_keyring_exit (); destroy_ns_chr:class_destroy (nvme_ns_chr_class); unregister_generic_ns:
@@ -5430,6 +5435,7 @@ static int __init nvme_core_init(void) static void __exit nvme_core_exit(void) {nvme_exit_auth (); +nvme_keyring_exit ();class_destroy (nvme_ns_chr_class);class_destroy (nvme_subsys_class);class_destroy (nvme_class);
include/linux/nvme-keyring.h new file +28 -0
@@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2023 Hannes Reinecke, SUSE Labs +*/ + +#ifndef _NVME_KEYRING_H +#define _NVME_KEYRING_H + +#ifdef CONFIG_NVME_KEYRING + +key_serial_t nvme_keyring_id (void ); +int nvme_keyring_init (void ); +void nvme_keyring_exit (void ); + +#else + +static inline key_serial_t nvme_keyring_id (void ) +{ +return 0 ; +} +static inline int nvme_keyring_init (void ) +{ +return 0 ; +} +static inline void nvme_keyring_exit (void ) {} + +#endif /* !CONFIG_NVME_KEYRING */ +#endif /* _NVME_KEYRING_H */
Signature · 1 line
2.35.3
PATCH v3 2/18 nvme-keyring: define a 'psk' keytype
In reply to Hannes ReineckeDefine a 'psk' keytype to hold the NVMe TLS PSKs.
--- drivers/nvme/common/keyring.c | 94 +++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+)
drivers/nvme/common/keyring.c +94 -0
@@ -8,6 +8,8 @@#include <linux/key-type.h> #include <keys/user-type.h> #include <linux/nvme.h> +#include <linux/nvme-tcp.h> +#include <linux/nvme-keyring.h> static struct key *nvme_keyring;
@@ -17,8 +19,94 @@ key_serial_t nvme_keyring_id(void) }EXPORT_SYMBOL_GPL (nvme_keyring_id); +static void nvme_tls_psk_describe (const struct key *key,struct seq_file *m) +{ +seq_puts (m, key->description); +seq_printf (m,": %u" , key->datalen); +} + +static bool nvme_tls_psk_match (const struct key *key, +const struct key_match_data *match_data) +{ +const char *match_id; +size_t match_len; + +if (!key->description) { +pr_debug ("%s: no key description\n" , __func__); +return false; + } + match_len =strlen (key->description); +pr_debug ("%s: id %s len %zd\n" , __func__, key->description, match_len); + +if (!match_data->raw_data) { +pr_debug ("%s: no match data\n" , __func__); +return false; + } + match_id = match_data->raw_data; +pr_debug ("%s: match '%s' '%s' len %zd\n" , + __func__, match_id, key->description, match_len); +return !memcmp (key->description, match_id, match_len); +} + +static int nvme_tls_psk_match_preparse (struct key_match_data *match_data) +{ + match_data->lookup_type =KEYRING_SEARCH_LOOKUP_ITERATE ; + match_data->cmp = nvme_tls_psk_match; +return 0 ; +} + +static struct key_type nvme_tls_psk_key_type = { + .name ="psk" , + .flags =KEY_TYPE_NET_DOMAIN , + .preparse = user_preparse, + .free_preparse = user_free_preparse, + .match_preparse = nvme_tls_psk_match_preparse, + .instantiate = generic_key_instantiate, + .revoke = user_revoke, + .destroy = user_destroy, + .describe = nvme_tls_psk_describe, + .read = user_read, +}; + +static struct key *nvme_tls_psk_lookup (struct key *keyring, +const char *hostnqn,const char *subnqn, +int hmac,bool generated) +{ +char *identity; +size_t identity_len = (NVMF_NQN_SIZE ) *2 +11 ; +key_ref_t keyref; +key_serial_t keyring_id; + + identity =kzalloc (identity_len,GFP_KERNEL ); +if (!identity) +return ERR_PTR (-ENOMEM ); + +snprintf (identity, identity_len,"NVMe0%c%02d %s %s" , + generated ?'G' :'R' , hmac, hostnqn, subnqn); + +if (!keyring) + keyring = nvme_keyring; + keyring_id =key_serial (keyring); +pr_debug ("keyring %x lookup tls psk '%s'\n" , + keyring_id, identity); + keyref =keyring_search (make_key_ref (keyring, true), + &nvme_tls_psk_key_type, + identity, false); +if (IS_ERR (keyref)) { +pr_debug ("lookup tls psk '%s' failed, error %ld\n" , + identity,PTR_ERR (keyref)); +kfree (identity); +return ERR_PTR (-ENOKEY ); + } +kfree (identity); + +return key_ref_to_ptr (keyref); +} +int nvme_keyring_init (void ) { +int err; + nvme_keyring = keyring_alloc(".nvme", GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
@@ -28,12 +116,18 @@ int nvme_keyring_init(void)if (IS_ERR (nvme_keyring))return PTR_ERR (nvme_keyring); + err =register_key_type (&nvme_tls_psk_key_type); +if (err) { +key_put (nvme_keyring); +return err; + }return 0 ; }EXPORT_SYMBOL_GPL (nvme_keyring_init);void nvme_keyring_exit (void ) { +unregister_key_type (&nvme_tls_psk_key_type);key_revoke (nvme_keyring);key_put (nvme_keyring); }
Signature · 1 line
2.35.3
PATCH v3 3/18 nvme: add TCP TSAS definitions
In reply to Hannes Reinecke--- include/linux/nvme.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
include/linux/nvme.h +10 -0
@@ -108,6 +108,13 @@ enum { NVMF_RDMA_CMS_RDMA_CM = 1, /* Sockets based endpoint addressing */ }; +/* TSAS SECTYPE for TCP transport */ +enum { +NVMF_TCP_SECTYPE_NONE =0 ,/* No Security */ +NVMF_TCP_SECTYPE_TLS12 =1 ,/* TLSv1.2, NVMe-oF 1.1 and NVMe-TCP 3.6.1.1 */ +NVMF_TCP_SECTYPE_TLS13 =2 ,/* TLSv1.3, NVMe-oF 1.1 and NVMe-TCP 3.6.1.1 */ +}; +#define NVME_AQ_DEPTH 32#define NVME_NR_AEN_COMMANDS 1#define NVME_AQ_BLK_MQ_DEPTH (NVME_AQ_DEPTH - NVME_NR_AEN_COMMANDS)
@@ -1453,6 +1460,9 @@ struct nvmf_disc_rsp_page_entry {__u16 pkey;__u8 resv10[246 ]; } rdma; +struct tcp { +__u8 sectype; + } tcp; } tsas; };
Signature · 1 line
2.35.3
PATCH v3 4/18 nvme-tcp: add definitions for TLS cipher suites
In reply to Hannes Reinecke--- include/linux/nvme-tcp.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/linux/nvme-tcp.h +6 -0
@@ -18,6 +18,12 @@ enum nvme_tcp_pfv { NVME_TCP_PFV_1_0 = 0x0, }; +enum nvme_tcp_tls_cipher { +NVME_TCP_TLS_CIPHER_INVALID =0 , +NVME_TCP_TLS_CIPHER_SHA256 =1 , +NVME_TCP_TLS_CIPHER_SHA384 =2 , +}; +enum nvme_tcp_fatal_error_status {NVME_TCP_FES_INVALID_PDU_HDR =0x01 ,NVME_TCP_FES_PDU_SEQ_ERR =0x02 ,
Signature · 1 line
2.35.3
PATCH v3 6/18 net/tls: implement ->read_sock()
In reply to Hannes ReineckeImplement ->read_sock() function for use with nvme-tcp.
--- net/tls/tls.h | 2 ++ net/tls/tls_main.c | 2 ++ net/tls/tls_sw.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+)
net/tls/tls.h +2 -0
@@ -113,6 +113,8 @@ bool tls_sw_sock_is_readable(struct sock *sk);ssize_t tls_sw_splice_read (struct socket *sock,loff_t *ppos,struct pipe_inode_info *pipe,size_t len,unsigned int flags); +int tls_sw_read_sock (struct sock *sk,read_descriptor_t *desc, +sk_read_actor_t read_actor);int tls_device_sendmsg (struct sock *sk,struct msghdr *msg,size_t size); int tls_device_sendpage(struct sock *sk, struct page *page,
net/tls/tls_main.c +2 -0
@@ -921,9 +921,11 @@ static void build_proto_ops(struct proto_ops ops[TLS_NUM_CONFIG][TLS_NUM_CONFIG] ops[TLS_BASE ][TLS_SW ] = ops[TLS_BASE ][TLS_BASE ]; ops[TLS_BASE ][TLS_SW ].splice_read = tls_sw_splice_read; + ops[TLS_BASE ][TLS_SW ].read_sock = tls_sw_read_sock; ops[TLS_SW ][TLS_SW ] = ops[TLS_SW ][TLS_BASE ]; ops[TLS_SW ][TLS_SW ].splice_read = tls_sw_splice_read; + ops[TLS_SW ][TLS_SW ].read_sock = tls_sw_read_sock;#ifdef CONFIG_TLS_DEVICE ops[TLS_HW ][TLS_BASE ] = ops[TLS_BASE ][TLS_BASE ];
net/tls/tls_sw.c +71 -0
77 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH v3 8/18 nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Hannes ReineckeMSG_SENDPAGE_NOTLAST was introduced with commit 35f9c09fe9c7 ("tcp: tcp_sendpages() should call tcp_push() once") to fix issues with the ->sendfile() call, and the implication seems to be that the flag should be strictly internal to sendfile.
Hence we shouldn't be setting it on nvme-tcp, and anyway doing so will cause TLS sending invalid data.
--- drivers/nvme/host/tcp.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/nvme/host/tcp.c +3 -3
@@ -995,7 +995,7 @@ static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)if (last && !queue->data_digest && !nvme_tcp_queue_more (queue)) flags |=MSG_EOR ;else - flags |=MSG_MORE |MSG_SENDPAGE_NOTLAST ; + flags |=MSG_MORE ;if (sendpage_ok (page)) { ret = kernel_sendpage(queue->sock, page, offset, len,
@@ -1049,7 +1049,7 @@ static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)int ret;if (inline_data ||nvme_tcp_queue_more (queue)) - flags |=MSG_MORE |MSG_SENDPAGE_NOTLAST ; + flags |=MSG_MORE ;else flags |=MSG_EOR ;
@@ -1091,7 +1091,7 @@ static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)if (!req->h2cdata_left) ret =kernel_sendpage (queue->sock,virt_to_page (pdu),offset_in_page (pdu) + req->offset, len, -MSG_DONTWAIT |MSG_MORE |MSG_SENDPAGE_NOTLAST ); +MSG_DONTWAIT |MSG_MORE );else ret = sock_no_sendpage(queue->sock, virt_to_page(pdu), offset_in_page(pdu) + req->offset, len,
Signature · 1 line
2.35.3
PATCH v3 10/18 nvme/tcp: allocate socket file
In reply to Hannes ReineckeWhen using the TLS upcall we need to allocate a socket file such that the userspace daemon is able to use the socket.
--- drivers/nvme/host/tcp.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/nvme/host/tcp.c +12 -2
@@ -1321,7 +1321,9 @@ static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid) } noreclaim_flag =memalloc_noreclaim_save (); -sock_release (queue->sock); +/* ->sock will be released by fput() */ +fput (queue->sock->file); + queue->sock =NULL ;memalloc_noreclaim_restore (noreclaim_flag);kfree (queue->pdu);
@@ -1495,6 +1497,7 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)struct nvme_tcp_ctrl *ctrl =to_tcp_ctrl (nctrl);struct nvme_tcp_queue *queue = &ctrl->queues[qid];int ret, rcv_pdu_size; +struct file *sock_file;mutex_init (&queue->queue_lock); queue->ctrl = ctrl;
@@ -1517,6 +1520,12 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid) goto err_destroy_mutex; } + sock_file =sock_alloc_file (queue->sock,O_CLOEXEC ,NULL ); +if (IS_ERR (sock_file)) { +sock_release (queue->sock); + ret =PTR_ERR (sock_file); + goto err_sock; + }nvme_tcp_reclassify_socket (queue->sock);/* Single syn retry */
@@ -1638,7 +1647,8 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid)if (queue->hdr_digest || queue->data_digest)nvme_tcp_free_crypto (queue); err_sock: -sock_release (queue->sock); +/* ->sock will be released by fput() */ +fput (queue->sock->file); queue->sock =NULL ; err_destroy_mutex:mutex_destroy (&queue->send_mutex);
Signature · 1 line
2.35.3
PATCH v3 11/18 nvme-tcp: enable TLS handshake upcall
In reply to Hannes ReineckeAdd a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute 'tls_key'
--- drivers/nvme/host/Kconfig | 14 ++++ drivers/nvme/host/core.c | 23 ++++++- drivers/nvme/host/fabrics.c | 10 +++ drivers/nvme/host/fabrics.h | 3 + drivers/nvme/host/nvme.h | 1 + drivers/nvme/host/tcp.c | 127 ++++++++++++++++++++++++++++++++++-- 6 files changed, 170 insertions(+), 8 deletions(-)
drivers/nvme/host/Kconfig +14 -0
@@ -92,6 +92,20 @@ config NVME_TCP If unsure, say N. +config NVME_TCP_TLS + bool "NVMe over Fabrics TCP TLS encryption support" + depends on NVME_TCP + select NVME_COMMON + select NVME_KEYRING + select NET_HANDSHAKE + help + Enables TLS encryption for NVMe TCP using the netlink handshake API. + + The TLS handshake daemon is availble at + https://github.com/oracle/ktls-utils. + + If unsure, say N. + config NVME_AUTH bool "NVM Express over Fabrics In-Band Authentication" depends on NVME_CORE
drivers/nvme/host/core.c +21 -2
@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR, nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);#endif +#ifdef CONFIG_NVME_TCP_TLS +static ssize_t tls_key_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct nvme_ctrl *ctrl =dev_get_drvdata (dev); + +if (!ctrl->tls_key) +return 0 ; +return sysfs_emit (buf,"%08x" ,key_serial (ctrl->tls_key)); +} +static DEVICE_ATTR_RO (tls_key); +#endif +static struct attribute *nvme_dev_attrs[] = { &dev_attr_reset_controller.attr, &dev_attr_rescan_controller.attr,
@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = {#ifdef CONFIG_NVME_AUTH &dev_attr_dhchap_secret.attr, &dev_attr_dhchap_ctrl_secret.attr, +#endif +#ifdef CONFIG_NVME_TCP_TLS + &dev_attr_tls_key.attr, #endifNULL };
@@ -3945,7 +3961,10 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)return 0 ;#endif - +#ifdef CONFIG_NVME_TCP_TLS +if (a == &dev_attr_tls_key.attr && !ctrl->opts) +return 0 ; +#endif return a->mode; }
@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev)if (!subsys || ctrl->instance != subsys->instance)ida_free (&nvme_instance_ida, ctrl->instance); - +key_put (ctrl->tls_key);nvme_free_cels (ctrl);nvme_mpath_uninit (ctrl);nvme_auth_stop (ctrl);
drivers/nvme/host/fabrics.c +10 -0
@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };
@@ -632,6 +633,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts, opts->hdr_digest = false; opts->data_digest = false; opts->tos =-1 ;/* < 0 == use transport default */ + opts->tls = false; options = o =kstrdup (buf,GFP_KERNEL );if (!options)
@@ -918,6 +920,14 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,kfree (opts->dhchap_ctrl_secret); opts->dhchap_ctrl_secret = p;break ; +case NVMF_OPT_TLS : +if (!IS_ENABLED (CONFIG_NVME_TCP_TLS )) { +pr_err ("TLS is not supported\n" ); + ret = -EINVAL ; + goto out; + } + opts->tls = true; +break ;default :pr_warn ("unknown parameter or missing value '%s' in ctrl creation request\n" , p);
drivers/nvme/host/fabrics.h +3 -0
@@ -70,6 +70,7 @@ enum {NVMF_OPT_DISCOVERY =1 <<22 ,NVMF_OPT_DHCHAP_SECRET =1 <<23 ,NVMF_OPT_DHCHAP_CTRL_SECRET =1 <<24 , + NVMF_OPT_TLS = 1 << 25, };/**
@@ -102,6 +103,7 @@ enum {* @dhchap_secret: DH-HMAC-CHAP secret * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional * authentication +* @tls: Start TLS encrypted connections (TCP) * @disable_sqflow: disable controller sq flow control * @hdr_digest: generate/verify header digest (TCP) * @data_digest: generate/verify data digest (TCP)
@@ -128,6 +130,7 @@ struct nvmf_ctrl_options {int max_reconnects;char *dhchap_secret;char *dhchap_ctrl_secret; +bool tls;bool disable_sqflow;bool hdr_digest;bool data_digest;
drivers/nvme/host/nvme.h +1 -0
@@ -347,6 +347,7 @@ struct nvme_ctrl {struct nvme_dhchap_key *ctrl_key;u16 transaction;#endif +struct key *tls_key;/* Power saving configuration */ u64 ps_max_latency_us;
drivers/nvme/host/tcp.c +121 -6
182 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH v3 12/18 nvme-tcp: control message handling for recvmsg()
In reply to Hannes ReineckekTLS is sending TLS ALERT messages as control messages for recvmsg(). As we can't do anything sensible with it just abort the connection and let the userspace agent to a re-negotiation.
--- drivers/nvme/host/tcp.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)
drivers/nvme/host/tcp.c +22 -1
@@ -1352,6 +1352,11 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) {struct nvme_tcp_icreq_pdu *icreq;struct nvme_tcp_icresp_pdu *icresp; +#ifdef CONFIG_NVME_TCP_TLS +char cbuf[CMSG_LEN (sizeof (char ))] = {}; +struct cmsghdr *cmsg; +unsigned char ctype; +#endif struct msghdr msg = {};struct kvec iov;bool ctrl_hdgst, ctrl_ddgst;
@@ -1389,11 +1394,27 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)memset (&msg,0 ,sizeof (msg)); iov.iov_base = icresp; iov.iov_len =sizeof (*icresp); +#ifdef CONFIG_NVME_TCP_TLS + msg.msg_control = cbuf; + msg.msg_controllen =sizeof (cbuf); +#endif ret =kernel_recvmsg (queue->sock, &msg, &iov,1 , iov.iov_len, msg.msg_flags);if (ret <0 ) goto free_icresp; - +#ifdef CONFIG_NVME_TCP_TLS + cmsg = (struct cmsghdr *)cbuf; +if (CMSG_OK (&msg, cmsg) && + cmsg->cmsg_level ==SOL_TLS && + cmsg->cmsg_type ==TLS_GET_RECORD_TYPE ) { + ctype = *((unsigned char *)CMSG_DATA (cmsg)); +if (ctype !=TLS_RECORD_TYPE_DATA ) { +pr_err ("queue %d: unhandled TLS record %d\n" , +nvme_tcp_queue_id (queue), ctype); +return -ENOTCONN ; + } + } +#endif ret = -EINVAL ;if (icresp->hdr.type != nvme_tcp_icresp) { pr_err("queue %d: bad type returned %d\n",
Signature · 1 line
2.35.3
PATCH v3 5/18 nvme-keyring: implement nvme_tls_psk_default()
In reply to Hannes ReineckeImplement a function to select the preferred PSK for TLS.
--- drivers/nvme/common/keyring.c | 48 +++++++++++++++++++++++++++++++++++ include/linux/nvme-keyring.h | 8 ++++++ 2 files changed, 56 insertions(+)
drivers/nvme/common/keyring.c +48 -0
@@ -5,6 +5,7 @@#include <linux/module.h> #include <linux/seq_file.h> +#include <linux/key.h> #include <linux/key-type.h> #include <keys/user-type.h> #include <linux/nvme.h>
@@ -103,6 +104,53 @@ static struct key *nvme_tls_psk_lookup(struct key *keyring,return key_ref_to_ptr (keyref); } +/* +* NVMe PSK priority list +* +* 'Retained' PSKs (ie 'generated == false') +* should be preferred to 'generated' PSKs, +* and SHA-384 should be preferred to SHA-256. +*/ +struct nvme_tls_psk_priority_list { +bool generated; +enum nvme_tcp_tls_cipher cipher; +} nvme_tls_psk_prio[] = { + { .generated = false, + .cipher =NVME_TCP_TLS_CIPHER_SHA384 , }, + { .generated = false, + .cipher =NVME_TCP_TLS_CIPHER_SHA256 , }, + { .generated = true, + .cipher =NVME_TCP_TLS_CIPHER_SHA384 , }, + { .generated = true, + .cipher =NVME_TCP_TLS_CIPHER_SHA256 , }, +}; + +/* +* nvme_tls_psk_default - Return the preferred PSK to use for TLS ClientHello +*/ +key_serial_t nvme_tls_psk_default (struct key *keyring, +const char *hostnqn,const char *subnqn) +{ +struct key *tls_key; +key_serial_t tls_key_id; +int prio; + +for (prio =0 ; prio <ARRAY_SIZE (nvme_tls_psk_prio); prio++) { +bool generated = nvme_tls_psk_prio[prio].generated; +enum nvme_tcp_tls_cipher cipher = nvme_tls_psk_prio[prio].cipher; + + tls_key =nvme_tls_psk_lookup (keyring, hostnqn, subnqn, + cipher, generated); +if (!IS_ERR (tls_key)) { + tls_key_id = tls_key->serial; +key_put (tls_key); +return tls_key_id; + } + } +return 0 ; +} +EXPORT_SYMBOL_GPL (nvme_tls_psk_default); +int nvme_keyring_init (void ) {int err;
include/linux/nvme-keyring.h +8 -0
@@ -8,12 +8,20 @@#ifdef CONFIG_NVME_KEYRING +key_serial_t nvme_tls_psk_default (struct key *keyring, +const char *hostnqn,const char *subnqn); +key_serial_t nvme_keyring_id (void );int nvme_keyring_init (void );void nvme_keyring_exit (void );#else +static inline key_serial_t nvme_tls_psk_default (struct key *keyring, +const char *hostnqn,const char *subnqn) +{ +return 0 ; +}static inline key_serial_t nvme_keyring_id (void ) {return 0 ;
Signature · 1 line
2.35.3
PATCH v3 7/18 net/tls: sanitize MSG_EOR handling
In reply to Hannes ReineckeThe TLS stack is using MSG_EOR internally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear the MSG_EOR flag. And blank out MSG_MORE / MSG_SENDPAGE_NOTLAST, too, as they conflict with MSG_EOR anyway.
--- net/tls/tls_device.c | 10 ++++++++++ net/tls/tls_sw.c | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+)
net/tls/tls_device.c +10 -0
@@ -576,6 +576,10 @@ int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)mutex_lock (&tls_ctx->tx_lock);lock_sock (sk); +/* MSG_EOR conflicts with MSG_MORE, so clear both */ +if (msg->msg_flags &MSG_EOR ) + msg->msg_flags &= ~(MSG_EOR |MSG_MORE ); +if (unlikely (msg->msg_controllen)) { rc =tls_process_cmsg (sk, msg, &record_type);if (rc)
@@ -604,6 +608,12 @@ int tls_device_sendpage(struct sock *sk, struct page *page,if (flags &MSG_SENDPAGE_NOTLAST ) flags |=MSG_MORE ; +/* +* MSG_EOR conflicts with MSG_MORE/MSG_SENDPAGE_NOTLAST, +* so clear all of them */ +if (flags &MSG_EOR ) + flags &= ~(MSG_EOR |MSG_SENDPAGE_NOTLAST |MSG_MORE ); +mutex_lock (&tls_ctx->tx_lock);lock_sock (sk);
net/tls/tls_sw.c +21 -0
@@ -952,6 +952,11 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)int ret =0 ;int pending; +if (msg->msg_flags &MSG_EOR ) { + eor = true; +/* MSG_EOR conflicts with MSG_MORE, so clear both */ + msg->msg_flags &= ~(MSG_EOR |MSG_MORE ); + }if (msg->msg_flags & ~(MSG_MORE |MSG_DONTWAIT |MSG_NOSIGNAL |MSG_CMSG_COMPAT ))return -EOPNOTSUPP ;
@@ -1274,6 +1279,14 @@ static int tls_sw_do_sendpage(struct sock *sk, struct page *page,int tls_sw_sendpage_locked (struct sock *sk,struct page *page,int offset,size_t size,int flags) { +/* +* MSG_EOR is invalid for TLS, and conflicts +* with MSG_MORE / MSG_SENDPAGE_NOTLAST. +* So clear all of them. +*/ +if (flags &MSG_EOR ) + flags &= ~(MSG_MORE |MSG_SENDPAGE_NOTLAST |MSG_EOR ); +if (flags & ~(MSG_MORE |MSG_DONTWAIT |MSG_NOSIGNAL |MSG_SENDPAGE_NOTLAST |MSG_SENDPAGE_NOPOLICY |MSG_NO_SHARED_FRAGS ))
@@ -1288,6 +1301,14 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,struct tls_context *tls_ctx =tls_get_ctx (sk);int ret; +/* +* MSG_EOR is invalid for TLS, and conflicts +* with MSG_MORE / MSG_SENDPAGE_NOTLAST. +* So clear all of them. +*/ +if (flags &MSG_EOR ) + flags &= ~(MSG_MORE |MSG_SENDPAGE_NOTLAST |MSG_EOR ); +if (flags & ~(MSG_MORE |MSG_DONTWAIT |MSG_NOSIGNAL |MSG_SENDPAGE_NOTLAST |MSG_SENDPAGE_NOPOLICY ))return -EOPNOTSUPP ;
Signature · 1 line
2.35.3
PATCH v3 13/18 nvme-fabrics: parse options 'keyring' and 'tls_key'
In reply to Hannes ReineckeParse the fabrics options 'keyring' and 'tls_key' and store the referenced keys in the options structure.
--- drivers/nvme/host/fabrics.c | 67 ++++++++++++++++++++++++++++++++++++- drivers/nvme/host/fabrics.h | 6 ++++ drivers/nvme/host/tcp.c | 11 ++++-- 3 files changed, 80 insertions(+), 4 deletions(-)
drivers/nvme/host/fabrics.c +66 -1
@@ -605,6 +605,8 @@ static const match_table_t opt_tokens = { { NVMF_OPT_NR_WRITE_QUEUES, "nr_write_queues=%d" }, { NVMF_OPT_NR_POLL_QUEUES, "nr_poll_queues=%d" }, { NVMF_OPT_TOS, "tos=%d" }, + { NVMF_OPT_KEYRING, "keyring=%d" }, + { NVMF_OPT_TLS_KEY, "tls_key=%d" }, { NVMF_OPT_FAIL_FAST_TMO, "fast_io_fail_tmo=%d" }, { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" },
@@ -620,8 +622,9 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,char *options, *o, *p;int token, ret =0 ;size_t nqnlen =0 ; -int ctrl_loss_tmo =NVMF_DEF_CTRL_LOSS_TMO ; +int ctrl_loss_tmo =NVMF_DEF_CTRL_LOSS_TMO , key_id;uuid_t hostid; +struct key *key =NULL ;/* Set defaults */ opts->queue_size =NVMF_DEF_QUEUE_SIZE ;
@@ -889,6 +892,66 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts, } opts->tos = token;break ; +case NVMF_OPT_KEYRING : +if (!IS_ENABLED (CONFIG_NVME_TCP_TLS )) { +pr_err ("TLS is not supported\n" ); + ret = -EINVAL ; + goto out; + } +if (match_int (args, &key_id)) { + ret = -EINVAL ; + goto out; + } +if (key_id <0 ) { +pr_err ("Invalid keyring id %d\n" , key_id); + ret = -EINVAL ; + goto out; + } +if (!key_id) { +pr_debug ("Using default keyring\n" ); +key_put (opts->keyring); + opts->keyring =NULL ; +break ; + } + key =key_lookup (key_id); +if (!key) { +pr_err ("Keyring id %08x not found\n" , key_id); + ret = -ENOKEY ; + goto out; + } +key_put (opts->keyring); + opts->keyring = key; +break ; +case NVMF_OPT_TLS_KEY : +if (!IS_ENABLED (CONFIG_NVME_TCP_TLS )) { +pr_err ("TLS is not supported\n" ); + ret = -EINVAL ; + goto out; + } +if (match_int (args, &key_id)) { + ret = -EINVAL ; + goto out; + } +if (key_id <0 ) { +pr_err ("Invalid key id %d\n" , key_id); + ret = -EINVAL ; + goto out; + } +if (!key_id) { +pr_debug ("Using 'best' PSK\n" ); +key_put (opts->tls_key); + opts->tls_key =NULL ; +break ; + } + key =key_lookup (key_id); +if (!key) { +pr_err ("Key id %08x not found\n" , key_id); + ret = -ENOKEY ; + goto out; + } +key_put (opts->tls_key); + opts->tls_key = key; +break ;case NVMF_OPT_DISCOVERY : opts->discovery_nqn = true;break ;
@@ -1053,6 +1116,8 @@ static int nvmf_check_allowed_opts(struct nvmf_ctrl_options *opts,void nvmf_free_options (struct nvmf_ctrl_options *opts) {nvmf_host_put (opts->host); +key_put (opts->keyring); +key_put (opts->tls_key);kfree (opts->transport);kfree (opts->traddr);kfree (opts->trsvcid);
drivers/nvme/host/fabrics.h +6 -0
@@ -71,6 +71,8 @@ enum {NVMF_OPT_DHCHAP_SECRET =1 <<23 ,NVMF_OPT_DHCHAP_CTRL_SECRET =1 <<24 ,NVMF_OPT_TLS =1 <<25 , +NVMF_OPT_KEYRING =1 <<26 , + NVMF_OPT_TLS_KEY = 1 << 27, };/**
@@ -103,6 +105,8 @@ enum {* @dhchap_secret: DH-HMAC-CHAP secret * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional * authentication +* @keyring: Keyring to use for key lookups +* @tls_key: TLS key for encrypted connections (TCP) * @tls: Start TLS encrypted connections (TCP) * @disable_sqflow: disable controller sq flow control * @hdr_digest: generate/verify header digest (TCP)
@@ -130,6 +134,8 @@ struct nvmf_ctrl_options {int max_reconnects;char *dhchap_secret;char *dhchap_ctrl_secret; +struct key *keyring; +struct key *tls_key;bool tls;bool disable_sqflow;bool hdr_digest;
drivers/nvme/host/tcp.c +8 -3
@@ -1572,6 +1572,8 @@ static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,dev_dbg (nctrl->device,"queue %d: start TLS with key %x\n" , qid, pskid); +if (nctrl->opts->keyring) + keyring =key_serial (nctrl->opts->keyring); args.ta_sock = queue->sock; args.ta_done = nvme_tcp_tls_done; args.ta_data = queue;
@@ -1880,9 +1882,12 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)key_serial_t psk_id =0 ;if (ctrl->opts->tls) { - psk_id =nvme_tls_psk_default (NULL , - ctrl->opts->host->nqn, - ctrl->opts->subsysnqn); +if (ctrl->opts->tls_key) + psk_id =key_serial (ctrl->opts->tls_key); +else + psk_id =nvme_tls_psk_default (ctrl->opts->keyring, + ctrl->opts->host->nqn, + ctrl->opts->subsysnqn);if (!psk_id) {dev_err (ctrl->device,"no valid PSK found\n" ); ret = -ENOKEY ;
Signature · 1 line
2.35.3
PATCH v3 9/18 security/keys: export key_lookup()
In reply to Hannes ReineckeFor in-kernel consumers one cannot readily assign a user (eg when running from a workqueue), so the normal key search permissions cannot be applied.
This patch exports the 'key_lookup()' function for a simple lookup of keys without checking for permissions.
--- security/keys/key.c | 1 + 1 file changed, 1 insertion(+)
security/keys/key.c +1 -0
@@ -693,6 +693,7 @@ struct key *key_lookup(key_serial_t id)spin_unlock (&key_serial_lock);return key; } +EXPORT_SYMBOL_GPL (key_lookup);/* * Find and lock the specified key type against removal.
Signature · 1 line
2.35.3
PATCH v3 14/18 nvmet: make TCP sectype settable via configfs
In reply to Hannes ReineckeAdd a new configfs attribute 'addr_tsas' to make the TCP sectype settable via configfs.
--- drivers/nvme/target/configfs.c | 65 ++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+)
drivers/nvme/target/configfs.c +65 -0
@@ -303,6 +303,11 @@ static void nvmet_port_init_tsas_rdma(struct nvmet_port *port) port->disc_addr.tsas.rdma.cms =NVMF_RDMA_CMS_RDMA_CM ; } +static void nvmet_port_init_tsas_tcp (struct nvmet_port *port,int sectype) +{ + port->disc_addr.tsas.tcp.sectype = sectype; +} +static ssize_t nvmet_addr_trtype_store (struct config_item *item,const char *page,size_t count) {
@@ -325,11 +330,70 @@ static ssize_t nvmet_addr_trtype_store(struct config_item *item, port->disc_addr.trtype = nvmet_transport[i].type;if (port->disc_addr.trtype ==NVMF_TRTYPE_RDMA )nvmet_port_init_tsas_rdma (port); +else if (port->disc_addr.trtype ==NVMF_TRTYPE_TCP ) +nvmet_port_init_tsas_tcp (port,NVMF_TCP_SECTYPE_NONE );return count; }CONFIGFS_ATTR (nvmet_, addr_trtype); +static const struct nvmet_type_name_map nvmet_addr_tsas_tcp[] = { + {NVMF_TCP_SECTYPE_NONE ,"none" }, + {NVMF_TCP_SECTYPE_TLS13 ,"tls1.3" }, +}; + +static const struct nvmet_type_name_map nvmet_addr_tsas_rdma[] = { + {NVMF_RDMA_QPTYPE_CONNECTED ,"connected" }, + {NVMF_RDMA_QPTYPE_DATAGRAM ,"datagram" }, +}; + +static ssize_t nvmet_addr_tsas_show (struct config_item *item, +char *page) +{ +struct nvmet_port *port =to_nvmet_port (item); +int i; + +if (port->disc_addr.trtype ==NVMF_TRTYPE_TCP ) { +for (i =0 ; i <ARRAY_SIZE (nvmet_addr_tsas_tcp); i++) { +if (port->disc_addr.tsas.tcp.sectype == nvmet_addr_tsas_tcp[i].type) +return sprintf (page,"%s\n" , nvmet_addr_tsas_tcp[i].name); + } + }else if (port->disc_addr.trtype ==NVMF_TRTYPE_RDMA ) { +for (i =0 ; i <ARRAY_SIZE (nvmet_addr_tsas_rdma); i++) { +if (port->disc_addr.tsas.rdma.qptype == nvmet_addr_tsas_rdma[i].type) +return sprintf (page,"%s\n" , nvmet_addr_tsas_rdma[i].name); + } + } +return sprintf (page,"reserved\n" ); +} + +static ssize_t nvmet_addr_tsas_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_port *port =to_nvmet_port (item); +int i; + +if (nvmet_is_port_enabled (port, __func__)) +return -EACCES ; + +if (port->disc_addr.trtype !=NVMF_TRTYPE_TCP ) +return -EINVAL ; + +for (i =0 ; i <ARRAY_SIZE (nvmet_addr_tsas_tcp); i++) { +if (sysfs_streq (page, nvmet_addr_tsas_tcp[i].name)) + goto found; + } + +pr_err ("Invalid value '%s' for tsas\n" , page); +return -EINVAL ; + +found: +nvmet_port_init_tsas_tcp (port, nvmet_addr_tsas_tcp[i].type); +return count; +} + +CONFIGFS_ATTR (nvmet_, addr_tsas); +/* * Namespace structures & file operation functions below */
@@ -1741,6 +1805,7 @@ static struct configfs_attribute *nvmet_port_attrs[] = { &nvmet_attr_addr_traddr, &nvmet_attr_addr_trsvcid, &nvmet_attr_addr_trtype, + &nvmet_attr_addr_tsas, &nvmet_attr_param_inline_data_size, #ifdef CONFIG_BLK_DEV_INTEGRITY &nvmet_attr_param_pi_enable,
Signature · 1 line
2.35.3
PATCH v3 15/18 nvmet-tcp: allocate socket file
In reply to Hannes ReineckeFor the TLS upcall we need to allocate a socket file such that the userspace daemon is able to use the socket.
--- drivers/nvme/target/tcp.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)
drivers/nvme/target/tcp.c +23 -13
@@ -122,6 +122,7 @@ struct nvmet_tcp_cmd {enum nvmet_tcp_queue_state {NVMET_TCP_Q_CONNECTING , +NVMET_TCP_Q_TLS_HANDSHAKE ,NVMET_TCP_Q_LIVE ,NVMET_TCP_Q_DISCONNECTING , };
@@ -1481,12 +1482,12 @@ static void nvmet_tcp_release_queue_work(struct work_struct *w)nvmet_sq_destroy (&queue->nvme_sq);cancel_work_sync (&queue->io_work);nvmet_tcp_free_cmd_data_in_buffers (queue); -sock_release (queue->sock); +/* ->sock will be released by fput() */ +fput (queue->sock->file);nvmet_tcp_free_cmds (queue);if (queue->hdr_digest || queue->data_digest)nvmet_tcp_free_crypto (queue);ida_free (&nvmet_tcp_queue_ida, queue->idx); - page =virt_to_head_page (queue->pf_cache.va);__page_frag_cache_drain (page, queue->pf_cache.pagecnt_bias);kfree (queue);
@@ -1609,15 +1610,16 @@ static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue)return ret; } -static int nvmet_tcp_alloc_queue (struct nvmet_tcp_port *port, +static void nvmet_tcp_alloc_queue (struct nvmet_tcp_port *port,struct socket *newsock) {struct nvmet_tcp_queue *queue; +struct file *sock_file;int ret; queue =kzalloc (sizeof (*queue),GFP_KERNEL );if (!queue) -return -ENOMEM ; +return ;INIT_WORK (&queue->release_work, nvmet_tcp_release_queue_work);INIT_WORK (&queue->io_work, nvmet_tcp_io_work);
@@ -1625,15 +1627,25 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port, queue->port = port; queue->nr_cmds =0 ;spin_lock_init (&queue->state_lock); - queue->state =NVMET_TCP_Q_CONNECTING ; +if (queue->port->nport->disc_addr.tsas.tcp.sectype == +NVMF_TCP_SECTYPE_TLS13 ) + queue->state =NVMET_TCP_Q_TLS_HANDSHAKE ; +else + queue->state =NVMET_TCP_Q_CONNECTING ;INIT_LIST_HEAD (&queue->free_list);init_llist_head (&queue->resp_list);INIT_LIST_HEAD (&queue->resp_send_list); + sock_file =sock_alloc_file (queue->sock,O_CLOEXEC ,NULL ); +if (IS_ERR (sock_file)) { + ret =PTR_ERR (sock_file); + goto out_free_queue; + } + queue->idx =ida_alloc (&nvmet_tcp_queue_ida,GFP_KERNEL );if (queue->idx <0 ) { ret = queue->idx; - goto out_free_queue; + goto out_sock; } ret =nvmet_tcp_alloc_cmd (queue, &queue->connect);
@@ -1654,7 +1666,7 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,if (ret) goto out_destroy_sq; -return 0 ; +return ; out_destroy_sq:mutex_lock (&nvmet_tcp_queue_mutex);list_del_init (&queue->queue_list);
@@ -1664,9 +1676,11 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,nvmet_tcp_free_cmd (&queue->connect); out_ida_remove:ida_free (&nvmet_tcp_queue_ida, queue->idx); +out_sock: +fput (queue->sock->file); out_free_queue:kfree (queue); -return ret; +pr_err ("failed to allocate queue" ); }static void nvmet_tcp_accept_work (struct work_struct *w)
@@ -1683,11 +1697,7 @@ static void nvmet_tcp_accept_work(struct work_struct *w)pr_warn ("failed to accept err=%d\n" , ret);return ; } - ret =nvmet_tcp_alloc_queue (port, newsock); -if (ret) { -pr_err ("failed to allocate queue\n" ); -sock_release (newsock); - } +nvmet_tcp_alloc_queue (port, newsock); } }
Signature · 1 line
2.35.3
PATCH v3 16/18 nvmet-tcp: enable TLS handshake upcall
In reply to Hannes ReineckeAdd functions to start the TLS handshake upcall when the TCP TSAS sectype is set to 'tls1.3' and add a config option NVME_TARGET_TCP_TLS.
--- drivers/nvme/target/Kconfig | 14 ++++ drivers/nvme/target/configfs.c | 63 ++++++++++++++++- drivers/nvme/target/nvmet.h | 1 + drivers/nvme/target/tcp.c | 126 ++++++++++++++++++++++++++++++++- 4 files changed, 199 insertions(+), 5 deletions(-)
drivers/nvme/target/Kconfig +14 -0
@@ -84,6 +84,20 @@ config NVME_TARGET_TCP If unsure, say N. +config NVME_TARGET_TCP_TLS + bool "NVMe over Fabrics TCP target TLS encryption support" + depends on NVME_TARGET_TCP + select NVME_COMMON + select NVME_KEYRING + select NET_HANDSHAKE + help + Enables TLS encryption for the NVMe TCP target using the netlink handshake API. + + The TLS handshake daemon is availble at + https://github.com/oracle/ktls-utils. + + If unsure, say N. + config NVME_TARGET_AUTH bool "NVMe over Fabrics In-band Authentication support" depends on NVME_TARGET
drivers/nvme/target/configfs.c +60 -3
123 lines not on this page · raw message
drivers/nvme/target/nvmet.h +1 -0
@@ -158,6 +158,7 @@ struct nvmet_port {struct config_group ana_groups_group;struct nvmet_ana_group ana_default_group;enum nvme_ana_state *ana_state; +struct key *keyring;void *priv;bool enabled;int inline_data_size;
drivers/nvme/target/tcp.c +124 -2
171 lines not on this page · raw message
Signature · 1 line
2.35.3
PATCH v3 17/18 nvmet-tcp: control messages for recvmsg()
In reply to Hannes ReineckekTLS requires control messages for recvmsg() to relay any out-of-band TLS messages (eg TLS alerts) to the caller.
--- drivers/nvme/target/tcp.c | 51 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-)
drivers/nvme/target/tcp.c +49 -2
@@ -13,6 +13,7 @@#include <linux/nvme-keyring.h> #include <net/sock.h> #include <net/tcp.h> +#include <net/tls.h> #include <net/handshake.h> #include <linux/inet.h> #include <linux/llist.h>
@@ -117,6 +118,7 @@ struct nvmet_tcp_cmd {u32 pdu_len;u32 pdu_recv;int sg_idx; +char recv_cbuf[CMSG_LEN (sizeof (char ))];struct msghdr recv_msg;struct bio_vec *iov;u32 flags;
@@ -1103,12 +1105,39 @@ static inline bool nvmet_tcp_pdu_valid(u8 type)return false; } +static bool nvmet_tcp_tls_record_ok (struct msghdr *msg,char *cbuf) +{ +struct cmsghdr *cmsg = (struct cmsghdr *)cbuf; +unsigned char ctype; + +if (!IS_ENABLED (CONFIG_NVME_TARGET_TCP_TLS )) +return 0 ; + +if (CMSG_OK (msg, cmsg) && + cmsg->cmsg_level ==SOL_TLS && + cmsg->cmsg_type ==TLS_GET_RECORD_TYPE ) { + ctype = *((unsigned char *)CMSG_DATA (cmsg)); +if (ctype !=TLS_RECORD_TYPE_DATA ) { +pr_err ("unhandled TLS record %d\n" , ctype); +return false; + } + } +return true; +} +static int nvmet_tcp_try_recv_pdu (struct nvmet_tcp_queue *queue) {struct nvme_tcp_hdr *hdr = &queue->pdu.cmd.hdr;int len;struct kvec iov; -struct msghdr msg = { .msg_flags =MSG_DONTWAIT }; +char cbuf[CMSG_LEN (sizeof (char ))] = {}; +struct msghdr msg = { +#ifdef CONFIG_NVME_TARGET_TCP_TLS + .msg_control = cbuf, + .msg_controllen =sizeof (cbuf), +#endif + .msg_flags =MSG_DONTWAIT + }; recv: iov.iov_base = (void *)&queue->pdu + queue->offset;
@@ -1117,6 +1146,8 @@ static int nvmet_tcp_try_recv_pdu(struct nvmet_tcp_queue *queue) iov.iov_len, msg.msg_flags);if (unlikely (len <0 ))return len; +if (!nvmet_tcp_tls_record_ok (&msg, cbuf)) +return -ENOTCONN ; queue->offset += len; queue->left -= len;
@@ -1176,6 +1207,9 @@ static int nvmet_tcp_try_recv_data(struct nvmet_tcp_queue *queue) cmd->recv_msg.msg_flags);if (ret <=0 )return ret; +if (!nvmet_tcp_tls_record_ok (&cmd->recv_msg, + cmd->recv_cbuf)) +return -ENOTCONN ; cmd->pdu_recv += ret; cmd->rbytes_done += ret;
@@ -1197,7 +1231,14 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue) {struct nvmet_tcp_cmd *cmd = queue->cmd;int ret; -struct msghdr msg = { .msg_flags =MSG_DONTWAIT }; +char cbuf[CMSG_LEN (sizeof (char ))] = {}; +struct msghdr msg = { +#ifdef CONFIG_NVME_TARGET_TCP_TLS + .msg_control = cbuf, + .msg_controllen =sizeof (cbuf), +#endif + .msg_flags =MSG_DONTWAIT + };struct kvec iov = { .iov_base = (void *)&cmd->recv_ddgst + queue->offset, .iov_len = queue->left
@@ -1207,6 +1248,8 @@ static int nvmet_tcp_try_recv_ddgst(struct nvmet_tcp_queue *queue) iov.iov_len, msg.msg_flags);if (unlikely (ret <0 ))return ret; +if (!nvmet_tcp_tls_record_ok (&msg, cbuf)) +return -ENOTCONN ; queue->offset += ret; queue->left -= ret;
@@ -1376,6 +1419,10 @@ static int nvmet_tcp_alloc_cmd(struct nvmet_tcp_queue *queue,if (!c->r2t_pdu) goto out_free_data; +if (IS_ENABLED (CONFIG_NVME_TARGET_TCP_TLS )) { + c->recv_msg.msg_control = c->recv_cbuf; + c->recv_msg.msg_controllen =sizeof (c->recv_cbuf); + } c->recv_msg.msg_flags =MSG_DONTWAIT |MSG_NOSIGNAL ;list_add_tail (&c->entry, &queue->free_list);
Signature · 1 line
2.35.3
PATCH v3 18/18 nvmet-tcp: add configfs attribute 'param_keyring'
In reply to Hannes ReineckeAdd a configfs attribute to list and change the default keyring.
--- drivers/nvme/target/configfs.c | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
drivers/nvme/target/configfs.c +45 -0
@@ -309,6 +309,47 @@ static ssize_t nvmet_param_pi_enable_store(struct config_item *item,CONFIGFS_ATTR (nvmet_, param_pi_enable);#endif +#ifdef CONFIG_NVME_TARGET_TCP_TLS +static ssize_t nvmet_param_keyring_show (struct config_item *item, +char *page) +{ +struct nvmet_port *port =to_nvmet_port (item); + +if (!port->keyring) +return sprintf (page,"\n" ); +return snprintf (page,PAGE_SIZE ,"%s\n" , + port->keyring->description); +} + +static ssize_t nvmet_param_keyring_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_port *port =to_nvmet_port (item); +struct key *keyring; +unsigned int keyring_id; +int ret; + +if (nvmet_is_port_enabled (port, __func__)) +return -EACCES ; + + ret =kstrtou32 (page,0 , &keyring_id); +if (ret) { +pr_err ("Invalid keyring id '%s'\n" , page); +return ret; + } + keyring =key_lookup (keyring_id); +if (IS_ERR (keyring)) { +pr_err ("Invalid keyring '%08x'\n" , keyring_id); +return PTR_ERR (keyring); + } +key_put (port->keyring); + port->keyring = keyring; +return count; +} + +CONFIGFS_ATTR (nvmet_, param_keyring); +#endif +static ssize_t nvmet_addr_trtype_show (struct config_item *item,char *page) {
@@ -412,6 +453,7 @@ static ssize_t nvmet_addr_tsas_store(struct config_item *item,pr_err ("TLS is not supported\n" );return -EINVAL ; } +if (!port->keyring) {pr_err ("TLS keyring not configured\n" );return -EINVAL ;
@@ -1858,6 +1900,9 @@ static struct configfs_attribute *nvmet_port_attrs[] = { &nvmet_attr_param_inline_data_size,#ifdef CONFIG_BLK_DEV_INTEGRITY &nvmet_attr_param_pi_enable, +#endif +#ifdef CONFIG_NVME_TARGET_TCP_TLS + &nvmet_attr_param_keyring, #endif NULL, };
Signature · 1 line
2.35.3
Sagi Grimberg
maintainerRe: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Hannes Reineckenak from me.
Quoted 4 lines
MSG_SENDPAGE_NOTLASTwas introduced with commit35f9c09fe9c7("tcp:tcp_sendpages()should calltcp_push()once") to fix issues with the ->sendfile() call, and the implication seems to be that the flag should be strictly internal to sendfile.
no, the use case was to allow sendpage consumers to build larger tso packets for large payloads (splice being the primary one), nvme-tcp also benefits from it (as do others), just like sendfile.
Quoted 2 lines
Hence we shouldn't be setting it on nvme-tcp, and anyway doing so will cause TLS sending invalid data.
That is something else. I don't recall the full details on the discussion with Jakub, but it was not my intention to strike it altogether, just remove it if !sendpage_ok.
Sagi Grimberg
maintainerRe: [PATCH 06/18] net/tls: implement ->read_sock()
In reply to Hannes ReineckeQuoted 4 lines
Implement ->read_sock() function for use with nvme-tcp.
Signed-off-by Hannes Reinecke <[email protected]>Cc Boris Pismenny <[email protected]>
This needs a review from Jakub/Boris/John...
Sagi Grimberg
maintainerRe: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
In reply to Hannes ReineckeQuoted 6 lines
The TLS stack is using
MSG_EORinternally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear theMSG_EORflag. And blank outMSG_MORE/MSG_SENDPAGE_NOTLAST, too, as they conflict withMSG_EORanyway.
This looks like a temporary workaround to me.
The networking folks really need to be CC'd on this (same for patch 6).
CC'ing Jakub for starters.
Quoted 80 lines
Signed-off-by Hannes Reinecke <[email protected]>--- net/tls/tls_device.c | 10 ++++++++++ net/tls/tls_sw.c | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+)net/tls/tls_device.c +10
-0@@ -576,6 +576,10 @@ int tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)mutex_lock (&tls_ctx->tx_lock);lock_sock (sk); +/* MSG_EOR conflicts with MSG_MORE, so clear both */ +if (msg->msg_flags &MSG_EOR ) + msg->msg_flags &= ~(MSG_EOR |MSG_MORE ); +if (unlikely (msg->msg_controllen)) { rc =tls_process_cmsg (sk, msg, &record_type);if (rc)@@ -604,6 +608,12 @@ int tls_device_sendpage(struct sock *sk, struct page *page,if (flags &MSG_SENDPAGE_NOTLAST ) flags |=MSG_MORE ; +/* +* MSG_EOR conflicts with MSG_MORE/MSG_SENDPAGE_NOTLAST, +* so clear all of them */ +if (flags &MSG_EOR ) + flags &= ~(MSG_EOR |MSG_SENDPAGE_NOTLAST |MSG_MORE ); +mutex_lock (&tls_ctx->tx_lock);lock_sock (sk);net/tls/tls_sw.c +21
-0@@ -952,6 +952,11 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)int ret =0 ;int pending; +if (msg->msg_flags &MSG_EOR ) { + eor = true; +/* MSG_EOR conflicts with MSG_MORE, so clear both */ + msg->msg_flags &= ~(MSG_EOR |MSG_MORE ); + }if (msg->msg_flags & ~(MSG_MORE |MSG_DONTWAIT |MSG_NOSIGNAL |MSG_CMSG_COMPAT ))return -EOPNOTSUPP ;@@ -1274,6 +1279,14 @@ static int tls_sw_do_sendpage(struct sock *sk, struct page *page,int tls_sw_sendpage_locked (struct sock *sk,struct page *page,int offset,size_t size,int flags) { +/* +* MSG_EOR is invalid for TLS, and conflicts +* with MSG_MORE / MSG_SENDPAGE_NOTLAST. +* So clear all of them. +*/ +if (flags &MSG_EOR ) + flags &= ~(MSG_MORE |MSG_SENDPAGE_NOTLAST |MSG_EOR ); +if (flags & ~(MSG_MORE |MSG_DONTWAIT |MSG_NOSIGNAL |MSG_SENDPAGE_NOTLAST |MSG_SENDPAGE_NOPOLICY |MSG_NO_SHARED_FRAGS ))@@ -1288,6 +1301,14 @@ int tls_sw_sendpage(struct sock *sk, struct page *page,struct tls_context *tls_ctx =tls_get_ctx (sk);int ret; +/* +* MSG_EOR is invalid for TLS, and conflicts +* with MSG_MORE / MSG_SENDPAGE_NOTLAST. +* So clear all of them. +*/ +if (flags &MSG_EOR ) + flags &= ~(MSG_MORE |MSG_SENDPAGE_NOTLAST |MSG_EOR ); +if (flags & ~(MSG_MORE |MSG_DONTWAIT |MSG_NOSIGNAL |MSG_SENDPAGE_NOTLAST |MSG_SENDPAGE_NOPOLICY ))return -EOPNOTSUPP ;
Sagi Grimberg
maintainerRe: [PATCH 12/18] nvme-tcp: control message handling for recvmsg()
In reply to Hannes ReineckeQuoted 15 lines · Hannes Reinecke
kTLS is sending TLS ALERT messages as control messages for
recvmsg(). As we can't do anything sensible with it just abort the connection and let the userspace agent to a re-negotiation.Signed-off-by Hannes Reinecke <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>--- drivers/nvme/host/tcp.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-)drivers/nvme/host/tcp.c +0
-0
drivers/nvme/host/tcp.c static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) · line 1355
Quoted 4 lines
drivers/nvme/host/tcp.c +1
-0@@ -1352,6 +1352,11 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) {struct nvme_tcp_icreq_pdu *icreq;struct nvme_tcp_icresp_pdu *icresp; +#ifdef CONFIG_NVME_TCP_TLS
I don't think you need the ifdef(s) here, right?
Re: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Sagi GrimbergQuoted 11 lines · Sagi Grimberg
nak from me.
Quoted 4 lines
MSG_SENDPAGE_NOTLASTwas introduced with commit35f9c09fe9c7("tcp:tcp_sendpages()should calltcp_push()once") to fix issues with the ->sendfile() call, and the implication seems to be that the flag should be strictly internal to sendfile.no, the use case was to allow sendpage consumers to build larger tso packets for large payloads (splice being the primary one), nvme-tcp also benefits from it (as do others), just like sendfile.
Quoted 7 lines
Quoted 2 lines
Hence we shouldn't be setting it on nvme-tcp, and anyway doing so will cause TLS sending invalid data.
That is something else. I don't recall the full details on the discussion with Jakub, but it was not my intention to strike it altogether, just remove it if !sendpage_ok.
But then we're running afoul with tls_sw_do_sendpage(), which uses MSG_SENDPAGE_NOTLAST as a record indicator. Forwarding MSG_SENDPAGE_NOTLAST to tls results in incorrect TLS records on the wire and a transmission stall.
Took me days to figure that out.
If you have a better idea, I'm all ears.
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
Sagi Grimberg
maintainerRe: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Hannes ReineckeQuoted 338 lines · Hannes Reinecke
Add a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute '
tls_key'Signed-off-by Hannes Reinecke <[email protected]>--- drivers/nvme/host/Kconfig | 14 ++++ drivers/nvme/host/core.c | 23 ++++++- drivers/nvme/host/fabrics.c | 10 +++ drivers/nvme/host/fabrics.h | 3 + drivers/nvme/host/nvme.h | 1 + drivers/nvme/host/tcp.c | 127 ++++++++++++++++++++++++++++++++++-- 6 files changed, 170 insertions(+), 8 deletions(-)drivers/nvme/host/Kconfig +14
-0@@ -92,6 +92,20 @@ config NVME_TCP If unsure, say N. +config NVME_TCP_TLS + bool "NVMe over Fabrics TCP TLS encryption support" + depends on NVME_TCP + select NVME_COMMON + select NVME_KEYRING + select NET_HANDSHAKE + help + Enables TLS encryption for NVMe TCP using the netlink handshake API. + + The TLS handshake daemon is availble at + https://github.com/oracle/ktls-utils. + + If unsure, say N. + config NVME_AUTH bool "NVM Express over Fabrics In-Band Authentication" depends on NVME_COREdrivers/nvme/host/core.c +21
-2@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR, nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store);#endif +#ifdef CONFIG_NVME_TCP_TLS +static ssize_t tls_key_show (struct device *dev, +struct device_attribute *attr,char *buf) +{ +struct nvme_ctrl *ctrl =dev_get_drvdata (dev); + +if (!ctrl->tls_key) +return 0 ; +return sysfs_emit (buf,"%08x" ,key_serial (ctrl->tls_key)); +} +static DEVICE_ATTR_RO (tls_key); +#endif +static struct attribute *nvme_dev_attrs[] = { &dev_attr_reset_controller.attr, &dev_attr_rescan_controller.attr,@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = {#ifdef CONFIG_NVME_AUTH &dev_attr_dhchap_secret.attr, &dev_attr_dhchap_ctrl_secret.attr, +#endif +#ifdef CONFIG_NVME_TCP_TLS + &dev_attr_tls_key.attr, #endifNULL };@@ -3945,7 +3961,10 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj,if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts)return 0 ;#endif - +#ifdef CONFIG_NVME_TCP_TLS +if (a == &dev_attr_tls_key.attr && !ctrl->opts) +return 0 ; +#endif return a->mode; }@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev)if (!subsys || ctrl->instance != subsys->instance)ida_free (&nvme_instance_ida, ctrl->instance); - +key_put (ctrl->tls_key);nvme_free_cels (ctrl);nvme_mpath_uninit (ctrl);nvme_auth_stop (ctrl);drivers/nvme/host/fabrics.c +10
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };@@ -632,6 +633,7 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts, opts->hdr_digest = false; opts->data_digest = false; opts->tos =-1 ;/* < 0 == use transport default */ + opts->tls = false; options = o =kstrdup (buf,GFP_KERNEL );if (!options)@@ -918,6 +920,14 @@ static int nvmf_parse_options(struct nvmf_ctrl_options *opts,kfree (opts->dhchap_ctrl_secret); opts->dhchap_ctrl_secret = p;break ; +case NVMF_OPT_TLS : +if (!IS_ENABLED (CONFIG_NVME_TCP_TLS )) { +pr_err ("TLS is not supported\n" ); + ret = -EINVAL ; + goto out; + } + opts->tls = true; +break ;default :pr_warn ("unknown parameter or missing value '%s' in ctrl creation request\n" , p);drivers/nvme/host/fabrics.h +3
-0@@ -70,6 +70,7 @@ enum {NVMF_OPT_DISCOVERY =1 <<22 ,NVMF_OPT_DHCHAP_SECRET =1 <<23 ,NVMF_OPT_DHCHAP_CTRL_SECRET =1 <<24 , + NVMF_OPT_TLS = 1 << 25, };/** @@ -102,6 +103,7 @@ enum {* @dhchap_secret: DH-HMAC-CHAP secret * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional * authentication +* @tls: Start TLS encrypted connections (TCP) * @disable_sqflow: disable controller sq flow control * @hdr_digest: generate/verify header digest (TCP) * @data_digest: generate/verify data digest (TCP) @@ -128,6 +130,7 @@ struct nvmf_ctrl_options {int max_reconnects;char *dhchap_secret;char *dhchap_ctrl_secret; +bool tls;bool disable_sqflow;bool hdr_digest;bool data_digest;drivers/nvme/host/nvme.h +1
-0@@ -347,6 +347,7 @@ struct nvme_ctrl {struct nvme_dhchap_key *ctrl_key;u16 transaction;#endif +struct key *tls_key;/* Power saving configuration */ u64 ps_max_latency_us;drivers/nvme/host/tcp.c +113
-4@@ -8,9 +8,13 @@#include <linux/init.h> #include <linux/slab.h> #include <linux/err.h> +#include <linux/key.h> #include <linux/nvme-tcp.h> +#include <linux/nvme-keyring.h> #include <net/sock.h> #include <net/tcp.h> +#include <net/tls.h> +#include <net/handshake.h> #include <linux/blk-mq.h> #include <crypto/hash.h> #include <net/busy_poll.h> @@ -31,6 +35,16 @@ static int so_priority;module_param (so_priority, int,0644 );MODULE_PARM_DESC (so_priority,"nvme tcp socket optimize priority" ); +#ifdef CONFIG_NVME_TCP_TLS +/* +* TLS handshake timeout +*/ +static int tls_handshake_timeout =10 ; +module_param (tls_handshake_timeout, int,0644 ); +MODULE_PARM_DESC (tls_handshake_timeout, +"nvme TLS handshake timeout in seconds (default 10)" ); +#endif +#ifdef CONFIG_DEBUG_LOCK_ALLOC /* lockdep can detect a circular dependency of the form * sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock @@ -146,7 +160,10 @@ struct nvme_tcp_queue {struct ahash_request *snd_hash;__le32 exp_ddgst;__le32 recv_ddgst; - +#ifdef CONFIG_NVME_TCP_TLS +struct completion tls_complete; +int tls_err; +#endif struct page_frag_cache pf_cache;void (*state_change)(struct sock *);@@ -1492,7 +1509,79 @@ static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue) queue->io_cpu =cpumask_next_wrap (n -1 , cpu_online_mask,-1 , false); } -static int nvme_tcp_alloc_queue (struct nvme_ctrl *nctrl,int qid) +#ifdef CONFIG_NVME_TCP_TLS +static void nvme_tcp_tls_done (void *data,int status,key_serial_t pskid) +{ +struct nvme_tcp_queue *queue = data; +struct nvme_tcp_ctrl *ctrl = queue->ctrl; +int qid =nvme_tcp_queue_id (queue); +struct key *tls_key; + +dev_dbg (ctrl->ctrl.device,"queue %d: TLS handshake done, key %x, status %d\n" , + qid, pskid, status); + +if (status) { + queue->tls_err = -status; + goto out_complete; + } + + tls_key =key_lookup (pskid); +if (IS_ERR (tls_key)) { +dev_warn (ctrl->ctrl.device,"queue %d: Invalid key %x\n" , + qid, pskid); + queue->tls_err = -ENOKEY ; + }else { + ctrl->ctrl.tls_key = tls_key; + queue->tls_err =0 ; + } + +out_complete: +complete (&queue->tls_complete); +} + +static int nvme_tcp_start_tls (struct nvme_ctrl *nctrl, +struct nvme_tcp_queue *queue, +key_serial_t pskid) +{ +int qid =nvme_tcp_queue_id (queue); +int ret; +struct tls_handshake_args args; +unsigned long tmo = tls_handshake_timeout *HZ ; +key_serial_t keyring =nvme_keyring_id (); + +dev_dbg (nctrl->device,"queue %d: start TLS with key %x\n" , + qid, pskid); + args.ta_sock = queue->sock; + args.ta_done = nvme_tcp_tls_done; + args.ta_data = queue; + args.ta_my_peerids[0 ] = pskid; + args.ta_num_peerids =1 ; + args.ta_keyring = keyring; + args.ta_timeout_ms = tls_handshake_timeout *1000 ; + queue->tls_err = -EOPNOTSUPP ; +init_completion (&queue->tls_complete); + ret =tls_client_hello_psk (&args,GFP_KERNEL ); +if (ret) { +dev_err (nctrl->device,"queue %d: failed to start TLS: %d\n" , + qid, ret); +return ret; + } +if (wait_for_completion_timeout (&queue->tls_complete, tmo) ==0 ) { +dev_err (nctrl->device, +"queue %d: TLS handshake timeout\n" , qid); + ret = -ETIMEDOUT ; + }else { +dev_dbg (nctrl->device, +"queue %d: TLS handshake complete, error %d\n" , + qid, queue->tls_err); + ret = queue->tls_err; + } +return ret; +} +#endif + +static int nvme_tcp_alloc_queue (struct nvme_ctrl *nctrl,int qid, +key_serial_t pskid) {struct nvme_tcp_ctrl *ctrl =to_tcp_ctrl (nctrl);struct nvme_tcp_queue *queue = &ctrl->queues[qid];@@ -1616,6 +1705,14 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid) goto err_rcv_pdu; } +#ifdef CONFIG_NVME_TCP_TLS +/* If PSKs are configured try to start TLS */ +if (pskid) { + ret =nvme_tcp_start_tls (nctrl, queue, pskid); +if (ret) + goto err_init_connect; + } +#endif ret =nvme_tcp_init_connection (queue);if (ret) goto err_init_connect;@@ -1759,10 +1856,22 @@ static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl,static int nvme_tcp_alloc_admin_queue (struct nvme_ctrl *ctrl) {int ret; +key_serial_t psk_id =0 ; + +if (ctrl->opts->tls) { + psk_id =nvme_tls_psk_default (NULL , + ctrl->opts->host->nqn, + ctrl->opts->subsysnqn); +if (!psk_id) { +dev_err (ctrl->device,"no valid PSK found\n" ); + ret = -ENOKEY ; + goto out_free_queue; + } + } - ret =nvme_tcp_alloc_queue (ctrl,0 ); + ret =nvme_tcp_alloc_queue (ctrl,0 , psk_id);if (ret) -return ret; + goto out_free_queue; ret =nvme_tcp_alloc_async_req (to_tcp_ctrl (ctrl));if (ret)
drivers/nvme/host/tcp.c static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) · line 1895
Quoted 9 lines
drivers/nvme/host/tcp.c +5
-0@@ -1778,9 +1887,15 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)static int __nvme_tcp_alloc_io_queues (struct nvme_ctrl *ctrl) {int i, ret; +key_serial_t psk_id; +if (!ctrl->tls_key) { +dev_err (ctrl->device,"no PSK negotiated\n" ); +return -ENOKEY ; + }
is ctrl->tls_key always set? Looks like this is the expectation.
Re: [PATCH 12/18] nvme-tcp: control message handling for recvmsg()
In reply to Sagi GrimbergQuoted 13 lines · Sagi Grimberg
Quoted 10 lines · Hannes Reinecke
kTLS is sending TLS ALERT messages as control messages for
recvmsg(). As we can't do anything sensible with it just abort the connection and let the userspace agent to a re-negotiation.Signed-off-by Hannes Reinecke <[email protected]>Reviewed-by Sagi Grimberg <[email protected]>---
drivers/nvme/host/tcp.c| 23 ++++++++++++++++++++++-1 file changed, 22 insertions(+), 1 deletion(-)
Quoted 13 lines
drivers/nvme/host/tcp.c static int nvme_tcp_init_connection(struct · line 1356
Quoted 10 lines
drivers/nvme/host/tcp.c +1
-0@@ -1352,6 +1352,11 @@ static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue) { struct nvme_tcp_icreq_pdu *icreq; struct nvme_tcp_icresp_pdu *icresp; +#ifdef CONFIG_NVME_TCP_TLS I don't think you need the ifdef(s) here, right?
Leftovers, sorry.
Will be cleaning it up with the next round.
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
Sagi Grimberg
maintainerRe: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Hannes ReineckeQuoted 19 lines
Quoted 12 lines
nak from me.
Quoted 4 lines
MSG_SENDPAGE_NOTLASTwas introduced with commit35f9c09fe9c7("tcp:tcp_sendpages()should calltcp_push()once") to fix issues with the ->sendfile() call, and the implication seems to be that the flag should be strictly internal to sendfile.no, the use case was to allow sendpage consumers to build larger tso packets for large payloads (splice being the primary one), nvme-tcp also benefits from it (as do others), just like sendfile.
Quoted 7 lines
Quoted 2 lines
Hence we shouldn't be setting it on nvme-tcp, and anyway doing so will cause TLS sending invalid data.
That is something else. I don't recall the full details on the discussion with Jakub, but it was not my intention to strike it altogether, just remove it if !sendpage_ok.
Quoted 5 lines
But then we're running afoul with
tls_sw_do_sendpage(), which usesMSG_SENDPAGE_NOTLASTas a record indicator. ForwardingMSG_SENDPAGE_NOTLASTto tls results in incorrect TLS records on the wire and a transmission stall.Took me days to figure that out.
If you have a better idea, I'm all ears.
So sendfile is unsupported over kTLS? Or is there some special case for it in the upper layer?
Jakub?
Re: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Sagi GrimbergQuoted 339 lines · Sagi Grimberg
Quoted 336 lines · Hannes Reinecke
Add a fabrics option 'tls' and start the TLS handshake upcall with the default PSK. When TLS is started the PSK key serial number is displayed in the sysfs attribute '
tls_key'Signed-off-by Hannes Reinecke <[email protected]>---
drivers/nvme/host/Kconfig | 14 ++++ drivers/nvme/host/core.c | 23 ++++++-drivers/nvme/host/fabrics.c| 10 +++drivers/nvme/host/fabrics.h| 3 + drivers/nvme/host/nvme.h | 1 + drivers/nvme/host/tcp.c | 127 ++++++++++++++++++++++++++++++++++--6 files changed, 170 insertions(+), 8 deletions(-)drivers/nvme/host/Kconfig +0
-0@@ -92,6 +92,20 @@ config NVME_TCPIf unsure, say N.
+configNVME_TCP_TLS
+ bool "NVMe over Fabrics TCP TLS encryption support"
+ depends onNVME_TCP
+ selectNVME_COMMON
+ selectNVME_KEYRING
+ selectNET_HANDSHAKE
+ help
+ Enables TLS encryption for NVMe TCP using the netlink handshake API.drivers/nvme/host/Kconfig +6
-0+ + The TLS handshake daemon is availble at + https://github.com/oracle/ktls-utils. + + If unsure, say N. +config
NVME_AUTH
bool "NVM Express over Fabrics In-Band Authentication" depends onNVME_COREdrivers/nvme/host/core.c +16
-0@@ -3889,6 +3889,19 @@ static DEVICE_ATTR(dhchap_ctrl_secret, S_IRUGO | S_IWUSR, nvme_ctrl_dhchap_ctrl_secret_show, nvme_ctrl_dhchap_ctrl_secret_store); #endif +#ifdef CONFIG_NVME_TCP_TLS +static ssize_t tls_key_show (struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct nvme_ctrl *ctrl = dev_get_drvdata(dev); + + if (!ctrl->tls_key) + return 0; + return sysfs_emit(buf, "%08x", key_serial(ctrl->tls_key)); +} +static DEVICE_ATTR_RO (tls_key); +#endif + static struct attribute *nvme_dev_attrs[] = { &dev_attr_reset_controller.attr, &dev_attr_rescan_controller.attr,@@ -3915,6 +3928,9 @@ static struct attribute *nvme_dev_attrs[] = { #ifdef CONFIG_NVME_AUTH &dev_attr_dhchap_secret.attr, &dev_attr_dhchap_ctrl_secret.attr, +#endif +#ifdef CONFIG_NVME_TCP_TLS + &dev_attr_tls_key.attr, #endifNULL
};drivers/nvme/host/core.c +5
-2@@ -3945,7 +3961,10 @@ static umode_t nvme_dev_attrs_are_visible(struct kobject *kobj, if (a == &dev_attr_dhchap_ctrl_secret.attr && !ctrl->opts) return 0; #endif - +#ifdef CONFIG_NVME_TCP_TLS + if (a == &dev_attr_tls_key.attr && !ctrl->opts) + return 0; +#endif return a->mode; }@@ -5080,7 +5099,7 @@ static void nvme_free_ctrl(struct device *dev) if (!subsys || ctrl->instance != subsys->instance) ida_free(&nvme_instance_ida, ctrl->instance); - + key_put(ctrl->tls_key); nvme_free_cels(ctrl); nvme_mpath_uninit(ctrl); nvme_auth_stop(ctrl);drivers/nvme/host/fabrics.c +1
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };@@ -632,6 +633,7 @@ static int nvmf_parse_options(structnvmf_ctrl_options *opts, opts->hdr_digest = false; opts->data_digest = false;
opts->tos= -1; /* < 0 == use transport default */ +opts->tls= false;
options = o =kstrdup(buf, GFP_KERNEL); if (!options)drivers/nvme/host/fabrics.c +8
-0@@ -918,6 +920,14 @@ static int nvmf_parse_options(structnvmf_ctrl_options *opts, kfree(opts->dhchap_ctrl_secret); opts->dhchap_ctrl_secret = p; break; + case NVMF_OPT_TLS: + if (!IS_ENABLED(CONFIG_NVME_TCP_TLS)) { + pr_err("TLS is not supported\n"); + ret = -EINVAL; + goto out; + } + opts->tls = true; + break;default:
pr_warn("unknown parameter or missing value '%s' in ctrl creation request\n", p);drivers/nvme/host/fabrics.h +3
-0@@ -70,6 +70,7 @@ enum { NVMF_OPT_DISCOVERY = 1 << 22, NVMF_OPT_DHCHAP_SECRET = 1 << 23, NVMF_OPT_DHCHAP_CTRL_SECRET = 1 << 24, + NVMF_OPT_TLS = 1 << 25, }; /**@@ -102,6 +103,7 @@ enum { * @dhchap_secret: DH-HMAC-CHAP secret * @dhchap_ctrl_secret: DH-HMAC-CHAP controller secret for bi-directional * authentication + * @tls: Start TLS encrypted connections (TCP) * @disable_sqflow: disable controller sq flow control * @hdr_digest: generate/verify header digest (TCP) * @data_digest: generate/verify data digest (TCP)@@ -128,6 +130,7 @@ struct nvmf_ctrl_options { int max_reconnects; char *dhchap_secret; char *dhchap_ctrl_secret; + bool tls; bool disable_sqflow; bool hdr_digest; bool data_digest;drivers/nvme/host/nvme.h +1
-0@@ -347,6 +347,7 @@ struct nvme_ctrl { struct nvme_dhchap_key *ctrl_key; u16 transaction; #endif + struct key *tls_key; /* Power saving configuration */ u64 ps_max_latency_us;drivers/nvme/host/tcp.c +91
-2@@ -8,9 +8,13 @@ #include <linux/init.h> #include <linux/slab.h> #include <linux/err.h> +#include <linux/key.h> #include <linux/nvme-tcp.h> +#include <linux/nvme-keyring.h> #include <net/sock.h> #include <net/tcp.h> +#include <net/tls.h> +#include <net/handshake.h> #include <linux/blk-mq.h> #include <crypto/hash.h> #include <net/busy_poll.h>@@ -31,6 +35,16 @@ static int so_priority; module_param(so_priority, int, 0644); MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority"); +#ifdef CONFIG_NVME_TCP_TLS +/* +* TLS handshake timeout +*/ +static int tls_handshake_timeout =10 ; +module_param (tls_handshake_timeout, int,0644 ); +MODULE_PARM_DESC (tls_handshake_timeout, + "nvme TLS handshake timeout in seconds (default 10)"); +#endif + #ifdef CONFIG_DEBUG_LOCK_ALLOC /* lockdep can detect a circular dependency of the form* sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock @@ -146,7 +160,10 @@ struct nvme_tcp_queue { struct ahash_request *snd_hash; __le32 exp_ddgst; __le32 recv_ddgst; - +#ifdef CONFIG_NVME_TCP_TLS + struct completion tls_complete; + int tls_err; +#endif struct page_frag_cache pf_cache; void (*state_change)(struct sock *);@@ -1492,7 +1509,79 @@ static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue) queue->io_cpu = cpumask_next_wrap(n - 1, cpu_online_mask, -1, false); } -static int nvme_tcp_alloc_queue (struct nvme_ctrl *nctrl,int qid) +#ifdef CONFIG_NVME_TCP_TLS +static void nvme_tcp_tls_done (void *data,int status,key_serial_t pskid) +{ + struct nvme_tcp_queue *queue = data; + struct nvme_tcp_ctrl *ctrl = queue->ctrl; + int qid = nvme_tcp_queue_id(queue); + struct key *tls_key; + + dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n", + qid, pskid, status); + + if (status) { + queue->tls_err = -status; + goto out_complete; + } + + tls_key = key_lookup(pskid); + if (IS_ERR(tls_key)) { + dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n", + qid, pskid); + queue->tls_err = -ENOKEY; + } else { + ctrl->ctrl.tls_key = tls_key; + queue->tls_err = 0; + } + +out_complete: + complete(&queue->tls_complete); +} + +static int nvme_tcp_start_tls (struct nvme_ctrl *nctrl, + struct nvme_tcp_queue *queue, + key_serial_t pskid) +{ + int qid = nvme_tcp_queue_id(queue); + int ret; + struct tls_handshake_args args; + unsigned long tmo = tls_handshake_timeout * HZ; + key_serial_t keyring = nvme_keyring_id(); + + dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n", + qid, pskid); + args.ta_sock = queue->sock; + args.ta_done = nvme_tcp_tls_done; + args.ta_data = queue; + args.ta_my_peerids[0] = pskid; + args.ta_num_peerids = 1; + args.ta_keyring = keyring; + args.ta_timeout_ms = tls_handshake_timeout * 1000; + queue->tls_err = -EOPNOTSUPP; + init_completion(&queue->tls_complete); + ret = tls_client_hello_psk(&args, GFP_KERNEL); + if (ret) { + dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n", + qid, ret); + return ret; + } + if (wait_for_completion_timeout(&queue->tls_complete, tmo) == 0) { + dev_err(nctrl->device, + "queue %d: TLS handshake timeout\n", qid); + ret = -ETIMEDOUT; + } else { + dev_dbg(nctrl->device, + "queue %d: TLS handshake complete, error %d\n", + qid, queue->tls_err); + ret = queue->tls_err; + } + return ret; +} +#endif + +static int nvme_tcp_alloc_queue (struct nvme_ctrl *nctrl,int qid, + key_serial_t pskid) {struct
nvme_tcp_ctrl*ctrl =to_tcp_ctrl(nctrl); structnvme_tcp_queue*queue = &ctrl->queues[qid];drivers/nvme/host/tcp.c +22
-2@@ -1616,6 +1705,14 @@ static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid) goto err_rcv_pdu; } +#ifdef CONFIG_NVME_TCP_TLS + /* If PSKs are configured try to start TLS */ + if (pskid) { + ret = nvme_tcp_start_tls(nctrl, queue, pskid); + if (ret) + goto err_init_connect; + } +#endif ret = nvme_tcp_init_connection(queue); if (ret) goto err_init_connect;@@ -1759,10 +1856,22 @@ static int nvme_tcp_start_io_queues(structnvme_ctrl *ctrl, static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) { int ret; + key_serial_t psk_id = 0; + + if (ctrl->opts->tls) { + psk_id = nvme_tls_psk_default(NULL, + ctrl->opts->host->nqn, + ctrl->opts->subsysnqn); + if (!psk_id) { + dev_err(ctrl->device, "no valid PSK found\n"); + ret = -ENOKEY; + goto out_free_queue; + } + } - ret = nvme_tcp_alloc_queue(ctrl, 0); + ret = nvme_tcp_alloc_queue(ctrl, 0, psk_id); if (ret) - return ret; + goto out_free_queue; ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl)); if (ret)@@ -1778,9 +1887,15 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) {
Quoted 9 lines
drivers/nvme/host/tcp.c static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) · line 1895
Quoted 6 lines
drivers/nvme/host/tcp.c +5
-0int i, ret; + key_serial_t psk_id; + if (!ctrl->tls_key) { + dev_err(ctrl->device, "no PSK negotiated\n"); + return -ENOKEY; + }is
ctrl->tls_keyalways set? Looks like this is the expectation.
It needs to be set when TLS is enabled for the admin queue. Otherwise no.
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
Sagi Grimberg
maintainerRe: [PATCH 15/18] nvmet-tcp: allocate socket file
In reply to Hannes ReineckeQuoted 13 lines · Hannes Reinecke
For the TLS upcall we need to allocate a socket file such that the userspace daemon is able to use the socket.
Signed-off-by Hannes Reinecke <[email protected]>--- drivers/nvme/target/tcp.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)drivers/nvme/target/tcp.c +0
-0
drivers/nvme/target/tcp.c struct nvmet_tcp_cmd { · line 125
Quoted 4 lines
drivers/nvme/target/tcp.c +1
-0@@ -122,6 +122,7 @@ struct nvmet_tcp_cmd {enum nvmet_tcp_queue_state {NVMET_TCP_Q_CONNECTING , +NVMET_TCP_Q_TLS_HANDSHAKE ,
I think you got some misplaced chunks in the patch.
Quoted 100 lines
drivers/nvme/target/tcp.c +22
-13NVMET_TCP_Q_LIVE , NVMET_TCP_Q_DISCONNECTING, };@@ -1481,12 +1482,12 @@ static void nvmet_tcp_release_queue_work(struct work_struct *w)nvmet_sq_destroy (&queue->nvme_sq);cancel_work_sync (&queue->io_work);nvmet_tcp_free_cmd_data_in_buffers (queue); -sock_release (queue->sock); +/* ->sock will be released by fput() */ +fput (queue->sock->file);nvmet_tcp_free_cmds (queue);if (queue->hdr_digest || queue->data_digest)nvmet_tcp_free_crypto (queue);ida_free (&nvmet_tcp_queue_ida, queue->idx); - page =virt_to_head_page (queue->pf_cache.va);__page_frag_cache_drain (page, queue->pf_cache.pagecnt_bias);kfree (queue);@@ -1609,15 +1610,16 @@ static int nvmet_tcp_set_queue_sock(struct nvmet_tcp_queue *queue)return ret; } -static int nvmet_tcp_alloc_queue (struct nvmet_tcp_port *port, +static void nvmet_tcp_alloc_queue (struct nvmet_tcp_port *port,struct socket *newsock) {struct nvmet_tcp_queue *queue; +struct file *sock_file;int ret; queue =kzalloc (sizeof (*queue),GFP_KERNEL );if (!queue) -return -ENOMEM ; +return ;INIT_WORK (&queue->release_work, nvmet_tcp_release_queue_work);INIT_WORK (&queue->io_work, nvmet_tcp_io_work);@@ -1625,15 +1627,25 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port, queue->port = port; queue->nr_cmds =0 ;spin_lock_init (&queue->state_lock); - queue->state =NVMET_TCP_Q_CONNECTING ; +if (queue->port->nport->disc_addr.tsas.tcp.sectype == +NVMF_TCP_SECTYPE_TLS13 ) + queue->state =NVMET_TCP_Q_TLS_HANDSHAKE ; +else + queue->state =NVMET_TCP_Q_CONNECTING ;INIT_LIST_HEAD (&queue->free_list);init_llist_head (&queue->resp_list);INIT_LIST_HEAD (&queue->resp_send_list); + sock_file =sock_alloc_file (queue->sock,O_CLOEXEC ,NULL ); +if (IS_ERR (sock_file)) { + ret =PTR_ERR (sock_file); + goto out_free_queue; + } + queue->idx =ida_alloc (&nvmet_tcp_queue_ida,GFP_KERNEL );if (queue->idx <0 ) { ret = queue->idx; - goto out_free_queue; + goto out_sock; } ret =nvmet_tcp_alloc_cmd (queue, &queue->connect);@@ -1654,7 +1666,7 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,if (ret) goto out_destroy_sq; -return 0 ; +return ; out_destroy_sq:mutex_lock (&nvmet_tcp_queue_mutex);list_del_init (&queue->queue_list);@@ -1664,9 +1676,11 @@ static int nvmet_tcp_alloc_queue(struct nvmet_tcp_port *port,nvmet_tcp_free_cmd (&queue->connect); out_ida_remove:ida_free (&nvmet_tcp_queue_ida, queue->idx); +out_sock: +fput (queue->sock->file); out_free_queue:kfree (queue); -return ret; +pr_err ("failed to allocate queue" ); }static void nvmet_tcp_accept_work (struct work_struct *w)@@ -1683,11 +1697,7 @@ static void nvmet_tcp_accept_work(struct work_struct *w)pr_warn ("failed to accept err=%d\n" , ret);return ; } - ret =nvmet_tcp_alloc_queue (port, newsock); -if (ret) { -pr_err ("failed to allocate queue\n" ); -sock_release (newsock); - } +nvmet_tcp_alloc_queue (port, newsock); } }
Sagi Grimberg
maintainerRe: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Hannes ReineckeQuoted 37 lines
Quoted 27 lines
Quoted 27 lines
static int
nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl){
int ret;
+key_serial_tpsk_id= 0;
+
+ if (ctrl->opts->tls) {
+psk_id= nvme_tls_psk_default(NULL,
+ctrl->opts->host->nqn,
+ctrl->opts->subsysnqn);
+ if (!psk_id) {
+dev_err(ctrl->device, "no valid PSK found\n"); + ret =-ENOKEY;
+ gotoout_free_queue;
+ }
+ }
- ret =nvme_tcp_alloc_queue(ctrl, 0);
+ ret =nvme_tcp_alloc_queue(ctrl, 0, psk_id);
if (ret)
- return ret;
+ gotoout_free_queue;
ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl)); if (ret)drivers/nvme/host/tcp.c +0
-0@@ -1778,9 +1887,15 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) {Quoted 9 lines
drivers/nvme/host/tcp.c static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) · line 1895
Quoted 6 lines
drivers/nvme/host/tcp.c +5
-0int i, ret; + key_serial_t psk_id; + if (!ctrl->tls_key) { + dev_err(ctrl->device, "no PSK negotiated\n"); + return -ENOKEY; + }is
ctrl->tls_keyalways set? Looks like this is the expectation.
Quoted 2 lines
It needs to be set when TLS is enabled for the admin queue. Otherwise no.
Then aren't you missing also and statement with (ctrl->opts->tls) ?
Re: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Sagi GrimbergQuoted 19 lines · Sagi Grimberg
Quoted 19 lines
Quoted 19 lines
nak from me.
Quoted 4 lines
MSG_SENDPAGE_NOTLASTwas introduced with commit35f9c09fe9c7("tcp:tcp_sendpages()should calltcp_push()once") to fix issues with the ->sendfile() call, and the implication seems to be that the flag should be strictly internal to sendfile.no, the use case was to allow sendpage consumers to build larger tso packets for large payloads (splice being the primary one), nvme-tcp also benefits from it (as do others), just like sendfile.
Quoted 2 lines
Hence we shouldn't be setting it on nvme-tcp, and anyway doing so will cause TLS sending invalid data.
That is something else. I don't recall the full details on the discussion with Jakub, but it was not my intention to strike it altogether, just remove it if !sendpage_ok.
Quoted 9 lines
Quoted 5 lines
But then we're running afoul with
tls_sw_do_sendpage(), which usesMSG_SENDPAGE_NOTLASTas a record indicator. ForwardingMSG_SENDPAGE_NOTLASTto tls results in incorrect TLS records on the wire and a transmission stall.Took me days to figure that out.
If you have a better idea, I'm all ears.
So sendfile is unsupported over kTLS? Or is there some special case for it in the upper layer?
Dunno. Seems to work, at least according to commit d452d48b9f8b ("tls: prevent oversized sendfile() hangs by ignoring MSG_MORE")
Which is also where I got the notion about sendfile() from. If MSG_SENDPAGE_NOTLAST is valid for non-sendfile users then tls_sw_do_sendpage() has a bug.
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
Re: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Sagi GrimbergQuoted 37 lines · Sagi Grimberg
Quoted 37 lines
Quoted 36 lines
Quoted 24 lines
static int
nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl){
int ret;
+key_serial_tpsk_id= 0;
+
+ if (ctrl->opts->tls) {
+psk_id= nvme_tls_psk_default(NULL,
+ctrl->opts->host->nqn,
+ctrl->opts->subsysnqn);
+ if (!psk_id) {
+dev_err(ctrl->device, "no valid PSK found\n"); + ret =-ENOKEY;
+ gotoout_free_queue;
+ }
+ }
- ret =nvme_tcp_alloc_queue(ctrl, 0);
+ ret =nvme_tcp_alloc_queue(ctrl, 0, psk_id);
if (ret)
- return ret;
+ gotoout_free_queue;
ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl)); if (ret)drivers/nvme/host/tcp.c static int nvme_tcp_alloc_admin_queue(struct · line 1895
Quoted 9 lines
drivers/nvme/host/tcp.c +5
-0@@ -1778,9 +1887,15 @@ static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl) static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl) { int i, ret; + key_serial_t psk_id; + if (!ctrl->tls_key) { + dev_err(ctrl->device, "no PSK negotiated\n"); + return -ENOKEY; + }is
ctrl->tls_keyalways set? Looks like this is the expectation.
Quoted 5 lines
Quoted 2 lines
It needs to be set when TLS is enabled for the admin queue. Otherwise no.
Then aren't you missing also and statement with (
ctrl->opts->tls) ?
Indeed, I do. Will be fixing it up.
Cheers,
Hannes
Signature · 5 lines
Dr. Hannes Reinecke Kernel Storage Architect [email protected] +49 911 74053 688 SUSE Software Solutions GmbH, Maxfeldstr. 5, 90409 Nürnberg HRB 36809 (AG Nürnberg), Geschäftsführer: Ivo Totev, Andrew Myers, Andrew McDonald, Martje Boudien Moerman
Jakub Kicinski
maintainerRe: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
In reply to Sagi GrimbergQuoted 9 lines · On Mon, 17 Apr 2023 18:19:12 +0300 Sagi Grimberg wrote:
Quoted 5 lines
The TLS stack is using
MSG_EORinternally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear theMSG_EORflag. And blank outMSG_MORE/MSG_SENDPAGE_NOTLAST, too, as they conflict withMSG_EORanyway.This looks like a temporary workaround to me.
The networking folks really need to be CC'd on this (same for patch 6).
Thanks, when I said "we can support EOR" I obviously meant support not ignore it :( No ack.
Jakub Kicinski
maintainerRe: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Sagi GrimbergQuoted 5 lines · On Mon, 17 Apr 2023 18:28:12 +0300 Sagi Grimberg wrote:
Quoted 5 lines
But then we're running afoul with
tls_sw_do_sendpage(), which usesMSG_SENDPAGE_NOTLASTas a record indicator. ForwardingMSG_SENDPAGE_NOTLASTto tls results in incorrect TLS records on the wire and a transmission stall.
Quoted 8 lines
Quoted 3 lines
Took me days to figure that out.
If you have a better idea, I'm all ears.
So sendfile is unsupported over kTLS? Or is there some special case for it in the upper layer?
Jakub?
First I hear of it. 10 sec look at the code doesn't indicate that the author missed this case completely either.
Hannes, any more details on what's going wrong?
What incorrect TLS record have you seen? What's the sequence of sends? Offload or no offload? Easy to repro or stars need to (mis)align?
Re: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Hannes ReineckeOn Mon, Apr 17, 2023 at 03:02:55PM +0200, Hannes Reinecke wrote:
--- a/drivers/nvme/host/fabrics.c
drivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 614
Quoted 8 lines
+++
b/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c +1
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };
Coming back to your previous discussion about this. 'tls' is always announced but not always supported. This renders the unsupported option filtering added to libnvme useles:
https://github.com/linux-nvme/libnvme/issues/612
Let's assume we go ahead with this patch. The user set explicit the tls option via 'nvme connect ... --tls' and the kernel will return EINVAL, but userland can't really know what it was and drop --tls and retry again (or print some useful information). It could be any other parameter provided by the user.
How is userland supposed to do any feature detection?
Re: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
Quoted 15 lines · Daniel Wagner
On Mon, Apr 17, 2023 at 03:02:55PM +0200, Hannes Reinecke wrote:
---a/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 614
Quoted 8 lines
+++
b/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c +1
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };Coming back to your previous discussion about this. 'tls' is always announced but not always supported. This renders the unsupported option filtering added to libnvme useles:
Quoted 9 lines
https://github.com/linux-nvme/libnvme/issues/612
Let's assume we go ahead with this patch. The user set explicit the tls option via 'nvme connect ... --tls' and the kernel will return EINVAL, but userland can't really know what it was and drop --tls and retry again (or print some useful information). It could be any other parameter provided by the user.
How is userland supposed to do any feature detection?
And that is a good question.
I've just removed most of the #ifdef annotations on request from Sagi. If we were going with your suggestion I would need to bring them back.
So, Sagi: What should we do?
Re-clutter and allow nvme-cli to figure out if TLS is supported? And have it always enabled and invent other means for nvme-cli?
Cheers,
Hannes
Sagi Grimberg
maintainerRe: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
In reply to Jakub KicinskiQuoted 7 lines
Quoted 7 lines
Quoted 6 lines
The TLS stack is using
MSG_EORinternally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear theMSG_EORflag. And blank outMSG_MORE/MSG_SENDPAGE_NOTLAST, too, as they conflict withMSG_EORanyway.
Quoted 6 lines
Quoted 3 lines
This looks like a temporary workaround to me.
The networking folks really need to be CC'd on this (same for patch 6).
Thanks, when I said "we can support EOR" I obviously meant support not ignore it :( No ack.
Obviously neither Hannes or I have sufficient tls knowledge to properly support it... It needs to be done by someone who knows the implementation.
Is this a large scope to add support for it? Because if it is, I'd simply change nvme to clear MSG_EOR when tls is used (despite my preference to not do special things for tls).
Sagi Grimberg
maintainerRe: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Hannes ReineckeQuoted 24 lines
Quoted 15 lines
---
a/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 614
Quoted 8 lines
+++
b/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c +1
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };Coming back to your previous discussion about this. 'tls' is always announced but not always supported. This renders the unsupported option filtering added to libnvme useles:
Quoted 9 lines
https://github.com/linux-nvme/libnvme/issues/612
Let's assume we go ahead with this patch. The user set explicit the tls option via 'nvme connect ... --tls' and the kernel will return EINVAL, but userland can't really know what it was and drop --tls and retry again (or print some useful information). It could be any other parameter provided by the user.
How is userland supposed to do any feature detection?
Quoted 7 lines
And that is a good question.
I've just removed most of the #ifdef annotations on request from Sagi. If we were going with your suggestion I would need to bring them back.So, Sagi: What should we do?
Re-clutter and allow nvme-cli to figure out if TLS is supported? And have it always enabled and invent other means for nvme-cli?
I don't understand the issue.
userspace already has a way to know if a feature is supported by reading the misc device.
Re: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
In reply to Sagi GrimbergQuoted 14 lines · Sagi Grimberg
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The TLS stack is using
MSG_EORinternally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear theMSG_EORflag. And blank outMSG_MORE/MSG_SENDPAGE_NOTLAST, too, as they conflict withMSG_EORanyway.Quoted 6 lines
Quoted 3 lines
This looks like a temporary workaround to me.
The networking folks really need to be CC'd on this (same for patch 6).
Thanks, when I said "we can support EOR" I obviously meant support not ignore it :( No ack.
Quoted 7 lines
Obviously neither Hannes or I have sufficient tls knowledge to properly support it... It needs to be done by someone who knows the implementation.
Is this a large scope to add support for it? Because if it is, I'd simply change nvme to clear
MSG_EORwhen tls is used (despite my preference to not do special things for tls).
There's a rather simple patch for tls_sw. It already paces the data internally by an 'eor' variable, which is currently set to !MSG_MORE. So evaluating MSG_EOR here is really trivial, and doesn't seem to cause any issues.
Or, at least, none which would show up with NVMe-over-TLS :-)
Cheers,
Hannes
Re: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Sagi GrimbergQuoted 28 lines · Sagi Grimberg
Quoted 28 lines
Quoted 15 lines
---
a/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 614
Quoted 8 lines
+++
b/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c +1
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };Coming back to your previous discussion about this. 'tls' is always announced but not always supported. This renders the unsupported option filtering added to libnvme useles:
Quoted 9 lines
https://github.com/linux-nvme/libnvme/issues/612
Let's assume we go ahead with this patch. The user set explicit the tls option via 'nvme connect ... --tls' and the kernel will return EINVAL, but userland can't really know what it was and drop --tls and retry again (or print some useful information). It could be any other parameter provided by the user.
How is userland supposed to do any feature detection?
And that is a good question.
I've just removed most of the #ifdef annotations on request from Sagi. If we were going with your suggestion I would need to bring them back.
Quoted 9 lines
Quoted 3 lines
So, Sagi: What should we do?
Re-clutter and allow nvme-cli to figure out if TLS is supported? And have it always enabled and invent other means for nvme-cli?I don't understand the issue.
userspace already has a way to know if a feature is supported by reading the misc device.
Yes. But the output there is generated by evaluating the fields from the fabrics 'opts' structure.
So if the 'tls' option is present it assumes that TLS is supported.
With my current patchset the _option_ is always present, but the evaluation of the option once set by nvme-cli might return -EINVAL if it's not compiled in.
The request from Daniel is to have the compile option for the fabrics 'tls' option, too, such that it doesn't show up with the misc device. IE revert the changes which you requested to convert all the
'#ifdef CONFIG_NVME_TCP_TLS' conditionals.
Cheers,
Hannes
Sagi Grimberg
maintainerRe: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
Quoted 38 lines · Hannes Reinecke
Quoted 28 lines · Sagi Grimberg
Quoted 28 lines
Quoted 15 lines
---
a/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 614
Quoted 8 lines
+++
b/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c +1
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };Coming back to your previous discussion about this. 'tls' is always announced but not always supported. This renders the unsupported option filtering added to libnvme useles:
Quoted 9 lines
https://github.com/linux-nvme/libnvme/issues/612
Let's assume we go ahead with this patch. The user set explicit the tls option via 'nvme connect ... --tls' and the kernel will return EINVAL, but userland can't really know what it was and drop --tls and retry again (or print some useful information). It could be any other parameter provided by the user.
How is userland supposed to do any feature detection?
And that is a good question.
I've just removed most of the #ifdef annotations on request from Sagi. If we were going with your suggestion I would need to bring them back.Quoted 9 lines
Quoted 3 lines
So, Sagi: What should we do?
Re-clutter and allow nvme-cli to figure out if TLS is supported? And have it always enabled and invent other means for nvme-cli?I don't understand the issue.
userspace already has a way to know if a feature is supported by reading the misc device.
Quoted 8 lines
Yes. But the output there is generated by evaluating the fields from the fabrics 'opts' structure.
So if the 'tls' option is present it assumes that TLS is supported.
With my current patchset the _option_ is always present, but the evaluation of the option once set by nvme-cli might return-EINVALif it's not compiled in.The request from Daniel is to have the compile option for the fabrics 'tls' option, too, such that it doesn't show up with the misc device. IE revert the changes which you requested to convert all the
'#ifdefCONFIG_NVME_TCP_TLS' conditionals.
Thanks for explaining.
Then we can add the ifdef on the opts statement alone, with a comment.
Re: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Jakub KicinskiQuoted 15 lines · Jakub Kicinski
Quoted 5 lines · On Mon, 17 Apr 2023 18:28:12 +0300 Sagi Grimberg wrote:
Quoted 5 lines
But then we're running afoul with
tls_sw_do_sendpage(), which usesMSG_SENDPAGE_NOTLASTas a record indicator. ForwardingMSG_SENDPAGE_NOTLASTto tls results in incorrect TLS records on the wire and a transmission stall.Quoted 8 lines
Quoted 3 lines
Took me days to figure that out.
If you have a better idea, I'm all ears.
So sendfile is unsupported over kTLS? Or is there some special case for it in the upper layer?
Jakub?
Quoted 8 lines
First I hear of it. 10 sec look at the code doesn't indicate that the author missed this case completely either.
Hannes, any more details on what's going wrong?
What incorrect TLS record have you seen? What's the sequence of sends? Offload or no offload? Easy to repro or stars need to (mis)align?
Ah-ha. Found it.
Turns out to be an arguably invalid control flow on our side.
The initial 'connect' command is a command PDU with inline data.
So in our (current) flow we do a 'sendpage()' with the MORE/SENDPAGE_NOTLAST flag set as we have to transfer more data.
Ok so far.
But for transmitting the actual data we evaluate 'sendpage_ok()', which returns _false_. And then we continue to call 'sendmsg()' for the inline data. This confuses the TLS software stack as sendpage() and sendmsg() are two distinct code paths, and we end up with a TX stall.
Solution: check 'sendpage_ok()' when we have inline data and use 'sendmsg' for the command pdu if required.
It always pays to dig deeper if one's not exactly sure what's happening ...
Will be updating the patchset.
Cheers,
Hannes
Re: [PATCH 11/18] nvme-tcp: enable TLS handshake upcall
In reply to Sagi GrimbergQuoted 46 lines · Sagi Grimberg
Quoted 43 lines · Hannes Reinecke
Quoted 28 lines · Sagi Grimberg
Quoted 28 lines
Quoted 15 lines
---
a/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c static const match_table_t opt_tokens = { · line 614
Quoted 8 lines
+++
b/drivers/nvme/host/fabrics.cdrivers/nvme/host/fabrics.c +1
-0@@ -609,6 +609,7 @@ static const match_table_t opt_tokens = { { NVMF_OPT_DISCOVERY, "discovery" }, { NVMF_OPT_DHCHAP_SECRET, "dhchap_secret=%s" }, { NVMF_OPT_DHCHAP_CTRL_SECRET, "dhchap_ctrl_secret=%s" }, + { NVMF_OPT_TLS, "tls" }, { NVMF_OPT_ERR, NULL } };Coming back to your previous discussion about this. 'tls' is always announced but not always supported. This renders the unsupported option filtering added to libnvme useles:
Quoted 9 lines
https://github.com/linux-nvme/libnvme/issues/612
Let's assume we go ahead with this patch. The user set explicit the tls option via 'nvme connect ... --tls' and the kernel will return EINVAL, but userland can't really know what it was and drop --tls and retry again (or print some useful information). It could be any other parameter provided by the user.
How is userland supposed to do any feature detection?
And that is a good question.
I've just removed most of the #ifdef annotations on request from Sagi. If we were going with your suggestion I would need to bring them back.Quoted 9 lines
Quoted 3 lines
So, Sagi: What should we do?
Re-clutter and allow nvme-cli to figure out if TLS is supported? And have it always enabled and invent other means for nvme-cli?I don't understand the issue.
userspace already has a way to know if a feature is supported by reading the misc device.
Yes. But the output there is generated by evaluating the fields from the fabrics 'opts' structure.
So if the 'tls' option is present it assumes that TLS is supported.
With my current patchset the _option_ is always present, but the evaluation of the option once set by nvme-cli might return-EINVALif it's not compiled in.
Quoted 7 lines
Quoted 3 lines
The request from Daniel is to have the compile option for the fabrics 'tls' option, too, such that it doesn't show up with the misc device. IE revert the changes which you requested to convert all the
'#ifdefCONFIG_NVME_TCP_TLS' conditionals.Thanks for explaining.
Then we can add the ifdef on the opts statement alone, with a comment.
Good, will do for the next round.
Cheers,
Hannes
Re: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
In reply to Hannes ReineckeQuoted 19 lines · Hannes Reinecke
Quoted 18 lines · Sagi Grimberg
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The TLS stack is using
MSG_EORinternally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear theMSG_EORflag. And blank outMSG_MORE/MSG_SENDPAGE_NOTLAST, too, as they conflict withMSG_EORanyway.Quoted 6 lines
Quoted 3 lines
This looks like a temporary workaround to me.
The networking folks really need to be CC'd on this (same for patch 6).
Thanks, when I said "we can support EOR" I obviously meant support not ignore it :( No ack.
Obviously neither Hannes or I have sufficient tls knowledge to properly support it... It needs to be done by someone who knows the implementation.
Quoted 9 lines
Quoted 3 lines
Is this a large scope to add support for it? Because if it is, I'd simply change nvme to clear
MSG_EORwhen tls is used (despite my preference to not do special things for tls).There's a rather simple patch for
tls_sw. It already paces the data internally by an 'eor' variable, which is currently set to !MSG_MORE. So evaluatingMSG_EORhere is really trivial, and doesn't seem to cause any issues.Or, at least, none which would show up with NVMe-over-TLS :-)
Suggestion is to change it like this (sendpage follows suit):
net/tls/tls_sw.c +4 -1
@@ -953,9 +953,12 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)int pending;if (msg->msg_flags & ~(MSG_MORE |MSG_DONTWAIT |MSG_NOSIGNAL | -MSG_CMSG_COMPAT )) +MSG_EOR |MSG_CMSG_COMPAT ))return -EOPNOTSUPP ; +if (MSG_EOR ) + eor = true; + ret =mutex_lock_interruptible (&tls_ctx->tx_lock);if (ret)return ret;
Cheers,
Hannes
Sagi Grimberg
maintainerRe: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
Quoted 36 lines · Hannes Reinecke
Quoted 19 lines · Hannes Reinecke
Quoted 18 lines · Sagi Grimberg
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The TLS stack is using
MSG_EORinternally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear theMSG_EORflag. And blank outMSG_MORE/MSG_SENDPAGE_NOTLAST, too, as they conflict withMSG_EORanyway.Quoted 6 lines
Quoted 3 lines
This looks like a temporary workaround to me.
The networking folks really need to be CC'd on this (same for patch 6).
Thanks, when I said "we can support EOR" I obviously meant support not ignore it :( No ack.
Obviously neither Hannes or I have sufficient tls knowledge to properly support it... It needs to be done by someone who knows the implementation.
Quoted 9 lines
Quoted 3 lines
Is this a large scope to add support for it? Because if it is, I'd simply change nvme to clear
MSG_EORwhen tls is used (despite my preference to not do special things for tls).There's a rather simple patch for
tls_sw. It already paces the data internally by an 'eor' variable, which is currently set to !MSG_MORE. So evaluatingMSG_EORhere is really trivial, and doesn't seem to cause any issues.Or, at least, none which would show up with NVMe-over-TLS :-)
Suggestion is to change it like this (sendpage follows suit):
net/tls/tls_sw.c +0
-0
net/tls/tls_sw.c int tls_sw_sendmsg(struct sock *sk, struct msghdr · line 960
Quoted 9 lines
net/tls/tls_sw.c +2
-1@@ -953,9 +953,12 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) int pending; if (msg->msg_flags & ~(MSG_MORE | MSG_DONTWAIT | MSG_NOSIGNAL | - MSG_CMSG_COMPAT)) + MSG_EOR | MSG_CMSG_COMPAT)) return -EOPNOTSUPP; + if (MSG_EOR)
You probably meant if (msg->msg_flags & MSG_EOR)...
Re: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
Quoted 42 lines · Sagi Grimberg
Quoted 39 lines · Hannes Reinecke
Quoted 19 lines · Hannes Reinecke
Quoted 18 lines · Sagi Grimberg
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
The TLS stack is using
MSG_EORinternally, so the flag cannot be set for sendmsg()/sendpage(). But to avoid having the caller to check whether TLS is active modify the code to clear theMSG_EORflag. And blank outMSG_MORE/MSG_SENDPAGE_NOTLAST, too, as they conflict withMSG_EORanyway.Quoted 6 lines
Quoted 3 lines
This looks like a temporary workaround to me.
The networking folks really need to be CC'd on this (same for patch 6).
Thanks, when I said "we can support EOR" I obviously meant support not ignore it :( No ack.
Obviously neither Hannes or I have sufficient tls knowledge to properly support it... It needs to be done by someone who knows the implementation.
Quoted 9 lines
Quoted 3 lines
Is this a large scope to add support for it? Because if it is, I'd simply change nvme to clear
MSG_EORwhen tls is used (despite my preference to not do special things for tls).There's a rather simple patch for
tls_sw. It already paces the data internally by an 'eor' variable, which is currently set to !MSG_MORE. So evaluatingMSG_EORhere is really trivial, and doesn't seem to cause any issues.Or, at least, none which would show up with NVMe-over-TLS :-)
Suggestion is to change it like this (sendpage follows suit):
net/tls/tls_sw.c +0
-0@@ -953,9 +953,12 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) int pending;
Quoted 9 lines
Quoted 6 lines
if (
msg->msg_flags& ~(MSG_MORE |MSG_DONTWAIT|MSG_NOSIGNAL| -MSG_CMSG_COMPAT))
+MSG_EOR|MSG_CMSG_COMPAT)) return-EOPNOTSUPP;+ if (
MSG_EOR)You probably meant if (
msg->msg_flags&MSG_EOR)...
Of course.
Cheers,
Hannes
Jakub Kicinski
maintainerRe: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
In reply to Hannes ReineckeQuoted 14 lines · On Tue, 18 Apr 2023 12:33:03 +0200 Hannes Reinecke wrote:
Ah-ha. Found it.
Turns out to be an arguably invalid control flow on our side.
The initial 'connect' command is a command PDU with inline data. So in our (current) flow we do a '
sendpage()' with the MORE/SENDPAGE_NOTLAST flag set as we have to transfer more data. Ok so far.
But for transmitting the actual data we evaluate 'sendpage_ok()', which returns _false_. And then we continue to call 'sendmsg()' for the inline data.
This confuses the TLS software stack assendpage()andsendmsg()are two distinct code paths, and we end up with a TX stall.
Quoted 6 lines
Solution: check '
sendpage_ok()' when we have inline data and use 'sendmsg' for the command pdu if required.It always pays to dig deeper if one's not exactly sure what's happening ...
Will be updating the patchset.
Hm, yeah, sendpage(NOT_LAST)+sendmsg() could indeed be buggy/untested. It wasn't something we could have tested from user space, we'd need kunit or some such :(
Adding it the backlog. And adding Vadim to CC in case he has cycles to take a look.
Re: [PATCH 08/18] nvme-tcp: do not set MSG_SENDPAGE_NOTLAST
Quoted 20 lines · Jakub Kicinski
Quoted 19 lines · On Tue, 18 Apr 2023 12:33:03 +0200 Hannes Reinecke wrote:
Ah-ha. Found it.
Turns out to be an arguably invalid control flow on our side.
The initial 'connect' command is a command PDU with inline data. So in our (current) flow we do a '
sendpage()' with the MORE/SENDPAGE_NOTLAST flag set as we have to transfer more data. Ok so far.
But for transmitting the actual data we evaluate 'sendpage_ok()', which returns _false_. And then we continue to call 'sendmsg()' for the inline data.
This confuses the TLS software stack assendpage()andsendmsg()are two distinct code paths, and we end up with a TX stall.Solution: check '
sendpage_ok()' when we have inline data and use 'sendmsg' for the command pdu if required.It always pays to dig deeper if one's not exactly sure what's happening ...
Quoted 9 lines
Quoted 1 line
Will be updating the patchset.
Hm, yeah, sendpage(NOT_LAST)+sendmsg() could indeed be buggy/untested. It wasn't something we could have tested from user space, we'd need kunit or some such :(
Adding it the backlog. And adding Vadim to CC in case he has cycles to take a look.
Bear in mind that that distinction will become meaningless once dhowells patches for MSG_SPLICE are in, as that will get rid of sendpage() altogether. (At least to my understanding.)
Cheers,
Hannes
Jakub Kicinski
maintainerRe: [PATCH 07/18] net/tls: sanitize MSG_EOR handling
In reply to Hannes ReineckeQuoted 13 lines · On Tue, 18 Apr 2023 13:02:34 +0200 Hannes Reinecke wrote:
Quoted 13 lines
Quoted 13 lines
net/tls/tls_sw.c +0
-0@@ -953,9 +953,12 @@ int tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size) int pending;if (
msg->msg_flags& ~(MSG_MORE |MSG_DONTWAIT|MSG_NOSIGNAL| -MSG_CMSG_COMPAT))
+MSG_EOR|MSG_CMSG_COMPAT)) return-EOPNOTSUPP;
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
+ if (
MSG_EOR)You probably meant if (
msg->msg_flags&MSG_EOR)...Of course.
Neat. And similarly for sendpage() and in tls_device.c, it should be fairly small changes in all 3 places. You don't need to worry about tls_device itself using EOR, FWIW, it's just to try to make the offload start on a skb boundary. Adding more EORs should not confuse anything.