Skip to content

Commit 44e3c25

Browse files
Hannes Reineckekeithbusch
authored andcommitted
nvmet: return DHCHAP status codes from nvmet_setup_auth()
A failure in nvmet_setup_auth() does not mean that the NVMe authentication command failed, so we should rather return a protocol error with a 'failure1' response than an NVMe status. Also update the type used for dhchap_step and dhchap_status to u8 to avoid confusions with nvme status. Furthermore, split dhchap_status and nvme status so we don't accidentally mix these return values. Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Hannes Reinecke <[email protected]> [dwagner: - use u8 as type for dhchap_{step|status} - separate nvme status from dhcap_status] Signed-off-by: Daniel Wagner <[email protected]> Signed-off-by: Keith Busch <[email protected]>
1 parent 213cbad commit 44e3c25

File tree

4 files changed

+43
-45
lines changed

4 files changed

+43
-45
lines changed

drivers/nvme/target/auth.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,11 @@ int nvmet_setup_dhgroup(struct nvmet_ctrl *ctrl, u8 dhgroup_id)
126126
return ret;
127127
}
128128

129-
int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
129+
u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl)
130130
{
131131
int ret = 0;
132132
struct nvmet_host_link *p;
133133
struct nvmet_host *host = NULL;
134-
const char *hash_name;
135134

136135
down_read(&nvmet_config_sem);
137136
if (nvmet_is_disc_subsys(ctrl->subsys))
@@ -149,13 +148,16 @@ int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
149148
}
150149
if (!host) {
151150
pr_debug("host %s not found\n", ctrl->hostnqn);
152-
ret = -EPERM;
151+
ret = NVME_AUTH_DHCHAP_FAILURE_FAILED;
153152
goto out_unlock;
154153
}
155154

156155
ret = nvmet_setup_dhgroup(ctrl, host->dhchap_dhgroup_id);
157-
if (ret < 0)
156+
if (ret < 0) {
158157
pr_warn("Failed to setup DH group");
158+
ret = NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE;
159+
goto out_unlock;
160+
}
159161

160162
if (!host->dhchap_secret) {
161163
pr_debug("No authentication provided\n");
@@ -166,12 +168,6 @@ int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
166168
pr_debug("Re-use existing hash ID %d\n",
167169
ctrl->shash_id);
168170
} else {
169-
hash_name = nvme_auth_hmac_name(host->dhchap_hash_id);
170-
if (!hash_name) {
171-
pr_warn("Hash ID %d invalid\n", host->dhchap_hash_id);
172-
ret = -EINVAL;
173-
goto out_unlock;
174-
}
175171
ctrl->shash_id = host->dhchap_hash_id;
176172
}
177173

@@ -180,7 +176,7 @@ int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
180176
ctrl->host_key = nvme_auth_extract_key(host->dhchap_secret + 10,
181177
host->dhchap_key_hash);
182178
if (IS_ERR(ctrl->host_key)) {
183-
ret = PTR_ERR(ctrl->host_key);
179+
ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
184180
ctrl->host_key = NULL;
185181
goto out_free_hash;
186182
}
@@ -198,7 +194,7 @@ int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
198194
ctrl->ctrl_key = nvme_auth_extract_key(host->dhchap_ctrl_secret + 10,
199195
host->dhchap_ctrl_key_hash);
200196
if (IS_ERR(ctrl->ctrl_key)) {
201-
ret = PTR_ERR(ctrl->ctrl_key);
197+
ret = NVME_AUTH_DHCHAP_FAILURE_NOT_USABLE;
202198
ctrl->ctrl_key = NULL;
203199
goto out_free_hash;
204200
}

drivers/nvme/target/fabrics-cmd-auth.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void nvmet_auth_sq_init(struct nvmet_sq *sq)
3131
sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE;
3232
}
3333

34-
static u16 nvmet_auth_negotiate(struct nvmet_req *req, void *d)
34+
static u8 nvmet_auth_negotiate(struct nvmet_req *req, void *d)
3535
{
3636
struct nvmet_ctrl *ctrl = req->sq->ctrl;
3737
struct nvmf_auth_dhchap_negotiate_data *data = d;
@@ -109,7 +109,7 @@ static u16 nvmet_auth_negotiate(struct nvmet_req *req, void *d)
109109
return 0;
110110
}
111111

112-
static u16 nvmet_auth_reply(struct nvmet_req *req, void *d)
112+
static u8 nvmet_auth_reply(struct nvmet_req *req, void *d)
113113
{
114114
struct nvmet_ctrl *ctrl = req->sq->ctrl;
115115
struct nvmf_auth_dhchap_reply_data *data = d;
@@ -172,7 +172,7 @@ static u16 nvmet_auth_reply(struct nvmet_req *req, void *d)
172172
return 0;
173173
}
174174

175-
static u16 nvmet_auth_failure2(void *d)
175+
static u8 nvmet_auth_failure2(void *d)
176176
{
177177
struct nvmf_auth_dhchap_failure_data *data = d;
178178

@@ -186,6 +186,7 @@ void nvmet_execute_auth_send(struct nvmet_req *req)
186186
void *d;
187187
u32 tl;
188188
u16 status = 0;
189+
u8 dhchap_status;
189190

190191
if (req->cmd->auth_send.secp != NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) {
191192
status = NVME_SC_INVALID_FIELD | NVME_SC_DNR;
@@ -237,30 +238,32 @@ void nvmet_execute_auth_send(struct nvmet_req *req)
237238
if (data->auth_type == NVME_AUTH_COMMON_MESSAGES) {
238239
if (data->auth_id == NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE) {
239240
/* Restart negotiation */
240-
pr_debug("%s: ctrl %d qid %d reset negotiation\n", __func__,
241-
ctrl->cntlid, req->sq->qid);
241+
pr_debug("%s: ctrl %d qid %d reset negotiation\n",
242+
__func__, ctrl->cntlid, req->sq->qid);
242243
if (!req->sq->qid) {
243-
if (nvmet_setup_auth(ctrl) < 0) {
244-
status = NVME_SC_INTERNAL;
245-
pr_err("ctrl %d qid 0 failed to setup"
246-
"re-authentication",
244+
dhchap_status = nvmet_setup_auth(ctrl);
245+
if (dhchap_status) {
246+
pr_err("ctrl %d qid 0 failed to setup re-authentication\n",
247247
ctrl->cntlid);
248-
goto done_failure1;
248+
req->sq->dhchap_status = dhchap_status;
249+
req->sq->dhchap_step =
250+
NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
251+
goto done_kfree;
249252
}
250253
}
251-
req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE;
254+
req->sq->dhchap_step =
255+
NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE;
252256
} else if (data->auth_id != req->sq->dhchap_step)
253257
goto done_failure1;
254258
/* Validate negotiation parameters */
255-
status = nvmet_auth_negotiate(req, d);
256-
if (status == 0)
259+
dhchap_status = nvmet_auth_negotiate(req, d);
260+
if (dhchap_status == 0)
257261
req->sq->dhchap_step =
258262
NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE;
259263
else {
260264
req->sq->dhchap_step =
261265
NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
262-
req->sq->dhchap_status = status;
263-
status = 0;
266+
req->sq->dhchap_status = dhchap_status;
264267
}
265268
goto done_kfree;
266269
}
@@ -284,15 +287,14 @@ void nvmet_execute_auth_send(struct nvmet_req *req)
284287

285288
switch (data->auth_id) {
286289
case NVME_AUTH_DHCHAP_MESSAGE_REPLY:
287-
status = nvmet_auth_reply(req, d);
288-
if (status == 0)
290+
dhchap_status = nvmet_auth_reply(req, d);
291+
if (dhchap_status == 0)
289292
req->sq->dhchap_step =
290293
NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1;
291294
else {
292295
req->sq->dhchap_step =
293296
NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
294-
req->sq->dhchap_status = status;
295-
status = 0;
297+
req->sq->dhchap_status = dhchap_status;
296298
}
297299
goto done_kfree;
298300
case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2:
@@ -301,13 +303,12 @@ void nvmet_execute_auth_send(struct nvmet_req *req)
301303
__func__, ctrl->cntlid, req->sq->qid);
302304
goto done_kfree;
303305
case NVME_AUTH_DHCHAP_MESSAGE_FAILURE2:
304-
status = nvmet_auth_failure2(d);
305-
if (status) {
306+
dhchap_status = nvmet_auth_failure2(d);
307+
if (dhchap_status) {
306308
pr_warn("ctrl %d qid %d: authentication failed (%d)\n",
307-
ctrl->cntlid, req->sq->qid, status);
308-
req->sq->dhchap_status = status;
309+
ctrl->cntlid, req->sq->qid, dhchap_status);
310+
req->sq->dhchap_status = dhchap_status;
309311
req->sq->authenticated = false;
310-
status = 0;
311312
}
312313
goto done_kfree;
313314
default:

drivers/nvme/target/fabrics-cmd.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
211211
struct nvmf_connect_data *d;
212212
struct nvmet_ctrl *ctrl = NULL;
213213
u16 status;
214-
int ret;
214+
u8 dhchap_status;
215215

216216
if (!nvmet_check_transfer_len(req, sizeof(struct nvmf_connect_data)))
217217
return;
@@ -254,11 +254,12 @@ static void nvmet_execute_admin_connect(struct nvmet_req *req)
254254

255255
uuid_copy(&ctrl->hostid, &d->hostid);
256256

257-
ret = nvmet_setup_auth(ctrl);
258-
if (ret < 0) {
259-
pr_err("Failed to setup authentication, error %d\n", ret);
257+
dhchap_status = nvmet_setup_auth(ctrl);
258+
if (dhchap_status) {
259+
pr_err("Failed to setup authentication, dhchap status %u\n",
260+
dhchap_status);
260261
nvmet_ctrl_put(ctrl);
261-
if (ret == -EPERM)
262+
if (dhchap_status == NVME_AUTH_DHCHAP_FAILURE_FAILED)
262263
status = (NVME_SC_CONNECT_INVALID_HOST | NVME_SC_DNR);
263264
else
264265
status = NVME_SC_INTERNAL;

drivers/nvme/target/nvmet.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ struct nvmet_sq {
113113
bool authenticated;
114114
struct delayed_work auth_expired_work;
115115
u16 dhchap_tid;
116-
u16 dhchap_status;
117-
int dhchap_step;
116+
u8 dhchap_status;
117+
u8 dhchap_step;
118118
u8 *dhchap_c1;
119119
u8 *dhchap_c2;
120120
u32 dhchap_s1;
@@ -713,7 +713,7 @@ void nvmet_execute_auth_receive(struct nvmet_req *req);
713713
int nvmet_auth_set_key(struct nvmet_host *host, const char *secret,
714714
bool set_ctrl);
715715
int nvmet_auth_set_host_hash(struct nvmet_host *host, const char *hash);
716-
int nvmet_setup_auth(struct nvmet_ctrl *ctrl);
716+
u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl);
717717
void nvmet_auth_sq_init(struct nvmet_sq *sq);
718718
void nvmet_destroy_auth(struct nvmet_ctrl *ctrl);
719719
void nvmet_auth_sq_free(struct nvmet_sq *sq);
@@ -732,7 +732,7 @@ int nvmet_auth_ctrl_exponential(struct nvmet_req *req,
732732
int nvmet_auth_ctrl_sesskey(struct nvmet_req *req,
733733
u8 *buf, int buf_size);
734734
#else
735-
static inline int nvmet_setup_auth(struct nvmet_ctrl *ctrl)
735+
static inline u8 nvmet_setup_auth(struct nvmet_ctrl *ctrl)
736736
{
737737
return 0;
738738
}

0 commit comments

Comments
 (0)