xref: /linux/drivers/nvme/target/fabrics-cmd-auth.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NVMe over Fabrics DH-HMAC-CHAP authentication command handling.
4  * Copyright (c) 2020 Hannes Reinecke, SUSE Software Solutions.
5  * All rights reserved.
6  */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/blkdev.h>
9 #include <linux/jiffies.h>
10 #include <linux/random.h>
11 #include <linux/nvme-auth.h>
12 #include <crypto/kpp.h>
13 #include <crypto/utils.h>
14 #include "nvmet.h"
15 
16 static void nvmet_auth_expired_work(struct work_struct *work)
17 {
18 	struct nvmet_sq *sq = container_of(to_delayed_work(work),
19 			struct nvmet_sq, auth_expired_work);
20 
21 	mutex_lock(&sq->auth_lock);
22 	/* A command may have cancelled or rearmed this timeout. */
23 	if (!sq->auth_expiry_active ||
24 	    delayed_work_pending(&sq->auth_expired_work))
25 		goto out_unlock;
26 	sq->auth_expiry_active = false;
27 
28 	pr_debug("%s: ctrl %d qid %d transaction %u expired, resetting\n",
29 		 __func__, sq->ctrl->cntlid, sq->qid, sq->dhchap_tid);
30 	sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE;
31 	sq->dhchap_tid = -1;
32 out_unlock:
33 	mutex_unlock(&sq->auth_lock);
34 }
35 
36 void nvmet_auth_sq_init(struct nvmet_sq *sq)
37 {
38 	/* Initialize in-band authentication */
39 	mutex_init(&sq->auth_lock);
40 	INIT_DELAYED_WORK(&sq->auth_expired_work, nvmet_auth_expired_work);
41 	sq->auth_expiry_active = false;
42 	sq->authenticated = false;
43 	sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE;
44 }
45 
46 static u8 nvmet_auth_negotiate(struct nvmet_req *req, void *d, u32 tl)
47 {
48 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
49 	struct nvmf_auth_dhchap_negotiate_data *data = d;
50 	int i, hash_id = 0, fallback_hash_id = 0, dhgid, fallback_dhgid;
51 
52 	if (tl < sizeof(*data) +
53 			sizeof(struct nvmf_auth_dhchap_protocol_descriptor))
54 		return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
55 
56 	pr_debug("%s: ctrl %d qid %d: data sc_d %d napd %d authid %d halen %d dhlen %d\n",
57 		 __func__, ctrl->cntlid, req->sq->qid,
58 		 data->sc_c, data->napd, data->auth_protocol[0].dhchap.authid,
59 		 data->auth_protocol[0].dhchap.halen,
60 		 data->auth_protocol[0].dhchap.dhlen);
61 	req->sq->dhchap_tid = le16_to_cpu(data->t_id);
62 	req->sq->sc_c = data->sc_c;
63 	if (data->sc_c != NVME_AUTH_SECP_NOSC) {
64 		if (!IS_ENABLED(CONFIG_NVME_TARGET_TCP_TLS))
65 			return NVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH;
66 		/* Secure concatenation can only be enabled on the admin queue */
67 		if (req->sq->qid)
68 			return NVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH;
69 		switch (data->sc_c) {
70 		case NVME_AUTH_SECP_NEWTLSPSK:
71 			if (nvmet_queue_tls_keyid(req->sq))
72 				return NVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH;
73 			break;
74 		case NVME_AUTH_SECP_REPLACETLSPSK:
75 			if (!nvmet_queue_tls_keyid(req->sq))
76 				return NVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH;
77 			break;
78 		default:
79 			return NVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH;
80 		}
81 		ctrl->concat = true;
82 	}
83 
84 	if (data->napd != 1)
85 		return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE;
86 
87 	if (data->auth_protocol[0].dhchap.authid !=
88 	    NVME_AUTH_DHCHAP_AUTH_ID)
89 		return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
90 
91 	if (data->auth_protocol[0].dhchap.dhlen > NVME_AUTH_DHCHAP_MAX_DH_IDS ||
92 	    data->auth_protocol[0].dhchap.halen > NVME_AUTH_DHCHAP_MAX_HASH_IDS)
93 		return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
94 
95 	for (i = 0; i < data->auth_protocol[0].dhchap.halen; i++) {
96 		u8 host_hmac_id = data->auth_protocol[0].dhchap.idlist[i];
97 
98 		if (!fallback_hash_id && nvme_auth_hmac_hash_len(host_hmac_id))
99 			fallback_hash_id = host_hmac_id;
100 		if (ctrl->shash_id != host_hmac_id)
101 			continue;
102 		hash_id = ctrl->shash_id;
103 		break;
104 	}
105 	if (hash_id == 0) {
106 		if (fallback_hash_id == 0) {
107 			pr_debug("%s: ctrl %d qid %d: no usable hash found\n",
108 				 __func__, ctrl->cntlid, req->sq->qid);
109 			return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE;
110 		}
111 		pr_debug("%s: ctrl %d qid %d: no usable hash found, falling back to %s\n",
112 			 __func__, ctrl->cntlid, req->sq->qid,
113 			 nvme_auth_hmac_name(fallback_hash_id));
114 		ctrl->shash_id = fallback_hash_id;
115 	}
116 
117 	dhgid = -1;
118 	fallback_dhgid = -1;
119 	for (i = 0; i < data->auth_protocol[0].dhchap.dhlen; i++) {
120 		int tmp_dhgid = data->auth_protocol[0].dhchap.idlist[i + 30];
121 
122 		if (tmp_dhgid != ctrl->dh_gid) {
123 			dhgid = tmp_dhgid;
124 			break;
125 		}
126 		if (fallback_dhgid < 0) {
127 			const char *kpp = nvme_auth_dhgroup_kpp(tmp_dhgid);
128 
129 			if (crypto_has_kpp(kpp, 0, 0))
130 				fallback_dhgid = tmp_dhgid;
131 		}
132 	}
133 	if (dhgid < 0) {
134 		if (fallback_dhgid < 0) {
135 			pr_debug("%s: ctrl %d qid %d: no usable DH group found\n",
136 				 __func__, ctrl->cntlid, req->sq->qid);
137 			return NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE;
138 		}
139 		pr_debug("%s: ctrl %d qid %d: configured DH group %s not found\n",
140 			 __func__, ctrl->cntlid, req->sq->qid,
141 			 nvme_auth_dhgroup_name(fallback_dhgid));
142 		ctrl->dh_gid = fallback_dhgid;
143 	}
144 	if (ctrl->dh_gid == NVME_AUTH_DHGROUP_NULL && ctrl->concat) {
145 		pr_debug("%s: ctrl %d qid %d: NULL DH group invalid "
146 			 "for secure channel concatenation\n", __func__,
147 			 ctrl->cntlid, req->sq->qid);
148 		return NVME_AUTH_DHCHAP_FAILURE_CONCAT_MISMATCH;
149 	}
150 	pr_debug("%s: ctrl %d qid %d: selected DH group %s (%d)\n",
151 		 __func__, ctrl->cntlid, req->sq->qid,
152 		 nvme_auth_dhgroup_name(ctrl->dh_gid), ctrl->dh_gid);
153 	return 0;
154 }
155 
156 static u8 nvmet_auth_reply(struct nvmet_req *req, void *d, u32 tl)
157 {
158 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
159 	struct nvmf_auth_dhchap_reply_data *data = d;
160 	u16 dhvlen;
161 	u8 *response;
162 
163 	if (tl < sizeof(*data))
164 		return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
165 
166 	dhvlen = le16_to_cpu(data->dhvlen);
167 
168 	/* Validate that hl and dhvlen fit within the transfer length */
169 	if (sizeof(*data) + 2 * (size_t)data->hl + dhvlen > tl)
170 		return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
171 
172 	pr_debug("%s: ctrl %d qid %d: data hl %d cvalid %d dhvlen %u\n",
173 		 __func__, ctrl->cntlid, req->sq->qid,
174 		 data->hl, data->cvalid, dhvlen);
175 
176 	if (dhvlen) {
177 		if (!ctrl->dh_tfm)
178 			return NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
179 		if (nvmet_auth_ctrl_sesskey(req, data->rval + 2 * data->hl,
180 					    dhvlen) < 0)
181 			return NVME_AUTH_DHCHAP_FAILURE_DHGROUP_UNUSABLE;
182 	}
183 
184 	response = kmalloc(data->hl, GFP_KERNEL);
185 	if (!response)
186 		return NVME_AUTH_DHCHAP_FAILURE_FAILED;
187 
188 	if (!ctrl->host_key) {
189 		pr_warn("ctrl %d qid %d no host key\n",
190 			ctrl->cntlid, req->sq->qid);
191 		kfree(response);
192 		return NVME_AUTH_DHCHAP_FAILURE_FAILED;
193 	}
194 	if (nvmet_auth_host_hash(req, response, data->hl) < 0) {
195 		pr_debug("ctrl %d qid %d host hash failed\n",
196 			 ctrl->cntlid, req->sq->qid);
197 		kfree(response);
198 		return NVME_AUTH_DHCHAP_FAILURE_FAILED;
199 	}
200 
201 	if (crypto_memneq(data->rval, response, data->hl)) {
202 		pr_info("ctrl %d qid %d host response mismatch\n",
203 			ctrl->cntlid, req->sq->qid);
204 		pr_debug("ctrl %d qid %d rval %*ph\n",
205 			 ctrl->cntlid, req->sq->qid, data->hl, data->rval);
206 		pr_debug("ctrl %d qid %d response %*ph\n",
207 			 ctrl->cntlid, req->sq->qid, data->hl, response);
208 		kfree(response);
209 		return NVME_AUTH_DHCHAP_FAILURE_FAILED;
210 	}
211 	kfree(response);
212 	pr_debug("%s: ctrl %d qid %d host authenticated\n",
213 		 __func__, ctrl->cntlid, req->sq->qid);
214 	if (!data->cvalid && ctrl->concat) {
215 		pr_debug("%s: ctrl %d qid %d invalid challenge\n",
216 			 __func__, ctrl->cntlid, req->sq->qid);
217 		return NVME_AUTH_DHCHAP_FAILURE_FAILED;
218 	}
219 	req->sq->dhchap_s2 = le32_to_cpu(data->seqnum);
220 	if (data->cvalid) {
221 		req->sq->dhchap_c2 = kmemdup(data->rval + data->hl, data->hl,
222 					     GFP_KERNEL);
223 		if (!req->sq->dhchap_c2)
224 			return NVME_AUTH_DHCHAP_FAILURE_FAILED;
225 
226 		pr_debug("%s: ctrl %d qid %d challenge %*ph\n",
227 			 __func__, ctrl->cntlid, req->sq->qid, data->hl,
228 			 req->sq->dhchap_c2);
229 	}
230 	/*
231 	 * NVMe Base Spec 2.2 section 8.3.4.5.4: DH-HMAC-CHAP_Reply message
232 	 * Sequence Number (SEQNUM): [ .. ]
233 	 * The value 0h is used to indicate that bidirectional authentication
234 	 * is not performed, but a challenge value C2 is carried in order to
235 	 * generate a pre-shared key (PSK) for subsequent establishment of a
236 	 * secure channel.
237 	 */
238 	if (req->sq->dhchap_s2 == 0) {
239 		if (ctrl->concat)
240 			nvmet_auth_insert_psk(req->sq);
241 		req->sq->authenticated = true;
242 		kfree(req->sq->dhchap_c2);
243 		req->sq->dhchap_c2 = NULL;
244 	} else if (!data->cvalid)
245 		req->sq->authenticated = true;
246 
247 	return 0;
248 }
249 
250 static u8 nvmet_auth_failure2(void *d)
251 {
252 	struct nvmf_auth_dhchap_failure_data *data = d;
253 
254 	return data->rescode_exp;
255 }
256 
257 u32 nvmet_auth_send_data_len(struct nvmet_req *req)
258 {
259 	return le32_to_cpu(req->cmd->auth_send.tl);
260 }
261 
262 void nvmet_execute_auth_send(struct nvmet_req *req)
263 {
264 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
265 	struct nvmf_auth_dhchap_success2_data *data;
266 	void *d;
267 	u32 tl;
268 	u16 status = 0;
269 	u8 dhchap_status;
270 
271 	mutex_lock(&req->sq->auth_lock);
272 
273 	if (req->cmd->auth_send.secp != NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) {
274 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
275 		req->error_loc =
276 			offsetof(struct nvmf_auth_send_command, secp);
277 		goto done;
278 	}
279 	if (req->cmd->auth_send.spsp0 != 0x01) {
280 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
281 		req->error_loc =
282 			offsetof(struct nvmf_auth_send_command, spsp0);
283 		goto done;
284 	}
285 	if (req->cmd->auth_send.spsp1 != 0x01) {
286 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
287 		req->error_loc =
288 			offsetof(struct nvmf_auth_send_command, spsp1);
289 		goto done;
290 	}
291 	tl = nvmet_auth_send_data_len(req);
292 	if (!tl) {
293 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
294 		req->error_loc =
295 			offsetof(struct nvmf_auth_send_command, tl);
296 		goto done;
297 	}
298 	if (unlikely(tl != req->transfer_len)) {
299 		pr_debug("%s: transfer length mismatch (%u)\n", __func__, tl);
300 		mutex_unlock(&req->sq->auth_lock);
301 		nvmet_check_transfer_len(req, tl);
302 		return;
303 	}
304 	if (tl < sizeof(struct nvmf_auth_dhchap_failure_data)) {
305 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
306 		req->error_loc =
307 			offsetof(struct nvmf_auth_send_command, tl);
308 		goto done;
309 	}
310 
311 	d = kmalloc(tl, GFP_KERNEL);
312 	if (!d) {
313 		status = NVME_SC_INTERNAL;
314 		goto done;
315 	}
316 
317 	status = nvmet_copy_from_sgl(req, 0, d, tl);
318 	if (status)
319 		goto done_kfree;
320 
321 	data = d;
322 	pr_debug("%s: ctrl %d qid %d type %d id %d step %x\n", __func__,
323 		 ctrl->cntlid, req->sq->qid, data->auth_type, data->auth_id,
324 		 req->sq->dhchap_step);
325 	if (data->auth_type != NVME_AUTH_COMMON_MESSAGES &&
326 	    data->auth_type != NVME_AUTH_DHCHAP_MESSAGES)
327 		goto done_failure1;
328 	if (data->auth_type == NVME_AUTH_COMMON_MESSAGES) {
329 		if (data->auth_id == NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE) {
330 			/* Restart negotiation */
331 			pr_debug("%s: ctrl %d qid %d reset negotiation\n",
332 				 __func__, ctrl->cntlid, req->sq->qid);
333 			nvmet_auth_sq_free(req->sq);
334 			if (!req->sq->qid) {
335 				dhchap_status = nvmet_setup_auth(ctrl, req->sq,
336 								 true);
337 				if (dhchap_status) {
338 					pr_err("ctrl %d qid 0 failed to setup re-authentication\n",
339 					       ctrl->cntlid);
340 					req->sq->dhchap_status = dhchap_status;
341 					req->sq->dhchap_step =
342 						NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
343 					goto done_kfree;
344 				}
345 			}
346 			req->sq->dhchap_step =
347 				NVME_AUTH_DHCHAP_MESSAGE_NEGOTIATE;
348 		} else {
349 			goto done_failure1;
350 		}
351 		/* Validate negotiation parameters */
352 		dhchap_status = nvmet_auth_negotiate(req, d, tl);
353 		if (dhchap_status == 0)
354 			req->sq->dhchap_step =
355 				NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE;
356 		else {
357 			req->sq->dhchap_step =
358 				NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
359 			req->sq->dhchap_status = dhchap_status;
360 		}
361 		goto done_kfree;
362 	}
363 	if (data->auth_id != req->sq->dhchap_step) {
364 		pr_debug("%s: ctrl %d qid %d step mismatch (%d != %d)\n",
365 			 __func__, ctrl->cntlid, req->sq->qid,
366 			 data->auth_id, req->sq->dhchap_step);
367 		goto done_failure1;
368 	}
369 	if (le16_to_cpu(data->t_id) != req->sq->dhchap_tid) {
370 		pr_debug("%s: ctrl %d qid %d invalid transaction %d (expected %d)\n",
371 			 __func__, ctrl->cntlid, req->sq->qid,
372 			 le16_to_cpu(data->t_id),
373 			 req->sq->dhchap_tid);
374 		req->sq->dhchap_step =
375 			NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
376 		req->sq->dhchap_status =
377 			NVME_AUTH_DHCHAP_FAILURE_INCORRECT_PAYLOAD;
378 		goto done_kfree;
379 	}
380 
381 	switch (data->auth_id) {
382 	case NVME_AUTH_DHCHAP_MESSAGE_REPLY:
383 		dhchap_status = nvmet_auth_reply(req, d, tl);
384 		if (dhchap_status == 0)
385 			req->sq->dhchap_step =
386 				NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1;
387 		else {
388 			req->sq->dhchap_step =
389 				NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
390 			req->sq->dhchap_status = dhchap_status;
391 		}
392 		goto done_kfree;
393 	case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2:
394 		if (ctrl->concat)
395 			nvmet_auth_insert_psk(req->sq);
396 		req->sq->authenticated = true;
397 		pr_debug("%s: ctrl %d qid %d ctrl authenticated\n",
398 			 __func__, ctrl->cntlid, req->sq->qid);
399 		goto done_kfree;
400 	case NVME_AUTH_DHCHAP_MESSAGE_FAILURE2:
401 		dhchap_status = nvmet_auth_failure2(d);
402 		if (dhchap_status) {
403 			pr_warn("ctrl %d qid %d: authentication failed (%d)\n",
404 				ctrl->cntlid, req->sq->qid, dhchap_status);
405 			req->sq->dhchap_status = dhchap_status;
406 			req->sq->authenticated = false;
407 		}
408 		goto done_kfree;
409 	default:
410 		req->sq->dhchap_status =
411 			NVME_AUTH_DHCHAP_FAILURE_INCORRECT_MESSAGE;
412 		req->sq->dhchap_step =
413 			NVME_AUTH_DHCHAP_MESSAGE_FAILURE2;
414 		req->sq->authenticated = false;
415 		goto done_kfree;
416 	}
417 done_failure1:
418 	req->sq->dhchap_status = NVME_AUTH_DHCHAP_FAILURE_INCORRECT_MESSAGE;
419 	req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_FAILURE2;
420 
421 done_kfree:
422 	kfree(d);
423 done:
424 	pr_debug("%s: ctrl %d qid %d dhchap status %x step %x\n", __func__,
425 		 ctrl->cntlid, req->sq->qid,
426 		 req->sq->dhchap_status, req->sq->dhchap_step);
427 	if (status)
428 		pr_debug("%s: ctrl %d qid %d nvme status %x error loc %d\n",
429 			 __func__, ctrl->cntlid, req->sq->qid,
430 			 status, req->error_loc);
431 	if (req->sq->dhchap_step != NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2 &&
432 	    req->sq->dhchap_step != NVME_AUTH_DHCHAP_MESSAGE_FAILURE2) {
433 		unsigned long auth_expire = min_t(u64, MAX_JIFFY_OFFSET,
434 			(u64)(ctrl->kato ? ctrl->kato : 120) * HZ);
435 
436 		req->sq->auth_expiry_active = true;
437 		mod_delayed_work(system_percpu_wq, &req->sq->auth_expired_work,
438 				 auth_expire);
439 		goto complete;
440 	}
441 	/* Final states, clear up variables */
442 	nvmet_auth_sq_free(req->sq);
443 	if (req->sq->dhchap_step == NVME_AUTH_DHCHAP_MESSAGE_FAILURE2)
444 		nvmet_ctrl_fatal_error(ctrl);
445 
446 complete:
447 	mutex_unlock(&req->sq->auth_lock);
448 	nvmet_req_complete(req, status);
449 }
450 
451 static int nvmet_auth_challenge(struct nvmet_req *req, void *d, u32 al)
452 {
453 	struct nvmf_auth_dhchap_challenge_data *data = d;
454 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
455 	int ret = 0;
456 	int hash_len = nvme_auth_hmac_hash_len(ctrl->shash_id);
457 	size_t data_size = sizeof(*data) + hash_len;
458 
459 	if (ctrl->dh_tfm)
460 		data_size += ctrl->dh_keysize;
461 	if (al < data_size) {
462 		pr_debug("%s: buffer too small (al %u need %zu)\n", __func__,
463 			 al, data_size);
464 		return -EINVAL;
465 	}
466 	memset(data, 0, data_size);
467 	req->sq->dhchap_s1 = nvme_auth_get_seqnum();
468 	data->auth_type = NVME_AUTH_DHCHAP_MESSAGES;
469 	data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE;
470 	data->t_id = cpu_to_le16(req->sq->dhchap_tid);
471 	data->hashid = ctrl->shash_id;
472 	data->hl = hash_len;
473 	data->seqnum = cpu_to_le32(req->sq->dhchap_s1);
474 	req->sq->dhchap_c1 = kmalloc(data->hl, GFP_KERNEL);
475 	if (!req->sq->dhchap_c1)
476 		return -ENOMEM;
477 	get_random_bytes(req->sq->dhchap_c1, data->hl);
478 	memcpy(data->cval, req->sq->dhchap_c1, data->hl);
479 	if (ctrl->dh_tfm) {
480 		data->dhgid = ctrl->dh_gid;
481 		data->dhvlen = cpu_to_le16(ctrl->dh_keysize);
482 		ret = nvmet_auth_ctrl_exponential(req, data->cval + data->hl,
483 						  ctrl->dh_keysize);
484 	}
485 	pr_debug("%s: ctrl %d qid %d seq %d transaction %d hl %d dhvlen %zu\n",
486 		 __func__, ctrl->cntlid, req->sq->qid, req->sq->dhchap_s1,
487 		 req->sq->dhchap_tid, data->hl, ctrl->dh_keysize);
488 	return ret;
489 }
490 
491 static int nvmet_auth_success1(struct nvmet_req *req, void *d, u32 al)
492 {
493 	struct nvmf_auth_dhchap_success1_data *data = d;
494 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
495 	int hash_len = nvme_auth_hmac_hash_len(ctrl->shash_id);
496 
497 	WARN_ON(al < sizeof(*data));
498 	memset(data, 0, sizeof(*data));
499 	data->auth_type = NVME_AUTH_DHCHAP_MESSAGES;
500 	data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1;
501 	data->t_id = cpu_to_le16(req->sq->dhchap_tid);
502 	data->hl = hash_len;
503 	if (req->sq->dhchap_c2) {
504 		if (!ctrl->ctrl_key) {
505 			pr_warn("ctrl %d qid %d no ctrl key\n",
506 				ctrl->cntlid, req->sq->qid);
507 			return NVME_AUTH_DHCHAP_FAILURE_FAILED;
508 		}
509 		if (nvmet_auth_ctrl_hash(req, data->rval, data->hl))
510 			return NVME_AUTH_DHCHAP_FAILURE_HASH_UNUSABLE;
511 		data->rvalid = 1;
512 		pr_debug("ctrl %d qid %d response %*ph\n",
513 			 ctrl->cntlid, req->sq->qid, data->hl, data->rval);
514 	}
515 	return 0;
516 }
517 
518 static void nvmet_auth_failure1(struct nvmet_req *req, void *d, int al)
519 {
520 	struct nvmf_auth_dhchap_failure_data *data = d;
521 
522 	WARN_ON(al < sizeof(*data));
523 	data->auth_type = NVME_AUTH_COMMON_MESSAGES;
524 	data->auth_id = NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
525 	data->t_id = cpu_to_le16(req->sq->dhchap_tid);
526 	data->rescode = NVME_AUTH_DHCHAP_FAILURE_REASON_FAILED;
527 	data->rescode_exp = req->sq->dhchap_status;
528 }
529 
530 u32 nvmet_auth_receive_data_len(struct nvmet_req *req)
531 {
532 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
533 	u32 al = le32_to_cpu(req->cmd->auth_receive.al);
534 	u32 min_len;
535 
536 	/*
537 	 * Reject too-short al before kmalloc(al), since the SUCCESS1 and
538 	 * FAILURE1/default builders write fixed response headers into it.
539 	 */
540 	switch (req->sq->dhchap_step) {
541 	case NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE:
542 		return al;
543 	case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1:
544 		min_len = sizeof(struct nvmf_auth_dhchap_success1_data);
545 		if (req->sq->dhchap_c2)
546 			min_len += nvme_auth_hmac_hash_len(ctrl->shash_id);
547 		break;
548 	default:
549 		min_len = sizeof(struct nvmf_auth_dhchap_failure_data);
550 		break;
551 	}
552 
553 	if (al < min_len)
554 		return 0;
555 
556 	return al;
557 }
558 
559 void nvmet_execute_auth_receive(struct nvmet_req *req)
560 {
561 	struct nvmet_ctrl *ctrl = req->sq->ctrl;
562 	void *d;
563 	u32 al;
564 	u16 status = 0;
565 
566 	mutex_lock(&req->sq->auth_lock);
567 
568 	if (req->cmd->auth_receive.secp != NVME_AUTH_DHCHAP_PROTOCOL_IDENTIFIER) {
569 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
570 		req->error_loc =
571 			offsetof(struct nvmf_auth_receive_command, secp);
572 		goto done;
573 	}
574 	if (req->cmd->auth_receive.spsp0 != 0x01) {
575 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
576 		req->error_loc =
577 			offsetof(struct nvmf_auth_receive_command, spsp0);
578 		goto done;
579 	}
580 	if (req->cmd->auth_receive.spsp1 != 0x01) {
581 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
582 		req->error_loc =
583 			offsetof(struct nvmf_auth_receive_command, spsp1);
584 		goto done;
585 	}
586 	al = nvmet_auth_receive_data_len(req);
587 	if (!al) {
588 		status = NVME_SC_INVALID_FIELD | NVME_STATUS_DNR;
589 		req->error_loc =
590 			offsetof(struct nvmf_auth_receive_command, al);
591 		goto done;
592 	}
593 	if (unlikely(al != req->transfer_len)) {
594 		pr_debug("%s: transfer length mismatch (%u)\n", __func__, al);
595 		mutex_unlock(&req->sq->auth_lock);
596 		nvmet_check_transfer_len(req, al);
597 		return;
598 	}
599 
600 	d = kzalloc(al, GFP_KERNEL);
601 	if (!d) {
602 		status = NVME_SC_INTERNAL;
603 		goto done;
604 	}
605 	pr_debug("%s: ctrl %d qid %d step %x\n", __func__,
606 		 ctrl->cntlid, req->sq->qid, req->sq->dhchap_step);
607 	switch (req->sq->dhchap_step) {
608 	case NVME_AUTH_DHCHAP_MESSAGE_CHALLENGE:
609 		if (nvmet_auth_challenge(req, d, al) < 0) {
610 			pr_warn("ctrl %d qid %d: challenge error\n",
611 				ctrl->cntlid, req->sq->qid);
612 			status = NVME_SC_INTERNAL;
613 			kfree(d);
614 			goto done;
615 		}
616 		req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_REPLY;
617 		break;
618 	case NVME_AUTH_DHCHAP_MESSAGE_SUCCESS1:
619 		status = nvmet_auth_success1(req, d, al);
620 		if (status) {
621 			req->sq->dhchap_status = status;
622 			req->sq->authenticated = false;
623 			nvmet_auth_failure1(req, d, al);
624 			pr_warn("ctrl %d qid %d: success1 status (%x)\n",
625 				ctrl->cntlid, req->sq->qid,
626 				req->sq->dhchap_status);
627 			break;
628 		}
629 		req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2;
630 		break;
631 	case NVME_AUTH_DHCHAP_MESSAGE_FAILURE1:
632 		req->sq->authenticated = false;
633 		nvmet_auth_failure1(req, d, al);
634 		pr_warn("ctrl %d qid %d failure1 (%x)\n",
635 			ctrl->cntlid, req->sq->qid, req->sq->dhchap_status);
636 		break;
637 	default:
638 		pr_warn("ctrl %d qid %d unhandled step (%d)\n",
639 			ctrl->cntlid, req->sq->qid, req->sq->dhchap_step);
640 		req->sq->dhchap_step = NVME_AUTH_DHCHAP_MESSAGE_FAILURE1;
641 		req->sq->dhchap_status = NVME_AUTH_DHCHAP_FAILURE_FAILED;
642 		nvmet_auth_failure1(req, d, al);
643 		status = 0;
644 		break;
645 	}
646 
647 	status = nvmet_copy_to_sgl(req, 0, d, al);
648 	kfree(d);
649 done:
650 	if (req->sq->dhchap_step == NVME_AUTH_DHCHAP_MESSAGE_SUCCESS2)
651 		nvmet_auth_sq_free(req->sq);
652 	else if (req->sq->dhchap_step == NVME_AUTH_DHCHAP_MESSAGE_FAILURE1) {
653 		nvmet_auth_sq_free(req->sq);
654 		nvmet_ctrl_fatal_error(ctrl);
655 	}
656 	mutex_unlock(&req->sq->auth_lock);
657 	nvmet_req_complete(req, status);
658 }
659