1 /*******************************************************************************
2  * This file contains main functions related to iSCSI Parameter negotiation.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20 
21 #include <linux/ctype.h>
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_fabric.h>
25 
26 #include "iscsi_target_core.h"
27 #include "iscsi_target_parameters.h"
28 #include "iscsi_target_login.h"
29 #include "iscsi_target_nego.h"
30 #include "iscsi_target_tpg.h"
31 #include "iscsi_target_util.h"
32 #include "iscsi_target.h"
33 #include "iscsi_target_auth.h"
34 
35 #define MAX_LOGIN_PDUS  7
36 #define TEXT_LEN	4096
37 
38 void convert_null_to_semi(char *buf, int len)
39 {
40 	int i;
41 
42 	for (i = 0; i < len; i++)
43 		if (buf[i] == '\0')
44 			buf[i] = ';';
45 }
46 
47 static int strlen_semi(char *buf)
48 {
49 	int i = 0;
50 
51 	while (buf[i] != '\0') {
52 		if (buf[i] == ';')
53 			return i;
54 		i++;
55 	}
56 
57 	return -1;
58 }
59 
60 int extract_param(
61 	const char *in_buf,
62 	const char *pattern,
63 	unsigned int max_length,
64 	char *out_buf,
65 	unsigned char *type)
66 {
67 	char *ptr;
68 	int len;
69 
70 	if (!in_buf || !pattern || !out_buf || !type)
71 		return -1;
72 
73 	ptr = strstr(in_buf, pattern);
74 	if (!ptr)
75 		return -1;
76 
77 	ptr = strstr(ptr, "=");
78 	if (!ptr)
79 		return -1;
80 
81 	ptr += 1;
82 	if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
83 		ptr += 2; /* skip 0x */
84 		*type = HEX;
85 	} else
86 		*type = DECIMAL;
87 
88 	len = strlen_semi(ptr);
89 	if (len < 0)
90 		return -1;
91 
92 	if (len > max_length) {
93 		pr_err("Length of input: %d exceeds max_length:"
94 			" %d\n", len, max_length);
95 		return -1;
96 	}
97 	memcpy(out_buf, ptr, len);
98 	out_buf[len] = '\0';
99 
100 	return 0;
101 }
102 
103 static u32 iscsi_handle_authentication(
104 	struct iscsi_conn *conn,
105 	char *in_buf,
106 	char *out_buf,
107 	int in_length,
108 	int *out_length,
109 	unsigned char *authtype)
110 {
111 	struct iscsi_session *sess = conn->sess;
112 	struct iscsi_node_auth *auth;
113 	struct iscsi_node_acl *iscsi_nacl;
114 	struct se_node_acl *se_nacl;
115 
116 	if (!sess->sess_ops->SessionType) {
117 		/*
118 		 * For SessionType=Normal
119 		 */
120 		se_nacl = conn->sess->se_sess->se_node_acl;
121 		if (!se_nacl) {
122 			pr_err("Unable to locate struct se_node_acl for"
123 					" CHAP auth\n");
124 			return -1;
125 		}
126 		iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
127 				se_node_acl);
128 		if (!iscsi_nacl) {
129 			pr_err("Unable to locate struct iscsi_node_acl for"
130 					" CHAP auth\n");
131 			return -1;
132 		}
133 
134 		auth = ISCSI_NODE_AUTH(iscsi_nacl);
135 	} else {
136 		/*
137 		 * For SessionType=Discovery
138 		 */
139 		auth = &iscsit_global->discovery_acl.node_auth;
140 	}
141 
142 	if (strstr("CHAP", authtype))
143 		strcpy(conn->sess->auth_type, "CHAP");
144 	else
145 		strcpy(conn->sess->auth_type, NONE);
146 
147 	if (strstr("None", authtype))
148 		return 1;
149 #ifdef CANSRP
150 	else if (strstr("SRP", authtype))
151 		return srp_main_loop(conn, auth, in_buf, out_buf,
152 				&in_length, out_length);
153 #endif
154 	else if (strstr("CHAP", authtype))
155 		return chap_main_loop(conn, auth, in_buf, out_buf,
156 				&in_length, out_length);
157 	else if (strstr("SPKM1", authtype))
158 		return 2;
159 	else if (strstr("SPKM2", authtype))
160 		return 2;
161 	else if (strstr("KRB5", authtype))
162 		return 2;
163 	else
164 		return 2;
165 }
166 
167 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
168 {
169 	kfree(conn->auth_protocol);
170 }
171 
172 static int iscsi_target_check_login_request(
173 	struct iscsi_conn *conn,
174 	struct iscsi_login *login)
175 {
176 	int req_csg, req_nsg;
177 	u32 payload_length;
178 	struct iscsi_login_req *login_req;
179 
180 	login_req = (struct iscsi_login_req *) login->req;
181 	payload_length = ntoh24(login_req->dlength);
182 
183 	switch (login_req->opcode & ISCSI_OPCODE_MASK) {
184 	case ISCSI_OP_LOGIN:
185 		break;
186 	default:
187 		pr_err("Received unknown opcode 0x%02x.\n",
188 				login_req->opcode & ISCSI_OPCODE_MASK);
189 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
190 				ISCSI_LOGIN_STATUS_INIT_ERR);
191 		return -1;
192 	}
193 
194 	if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
195 	    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
196 		pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
197 			" and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
198 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
199 				ISCSI_LOGIN_STATUS_INIT_ERR);
200 		return -1;
201 	}
202 
203 	req_csg = (login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
204 	req_nsg = (login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK);
205 
206 	if (req_csg != login->current_stage) {
207 		pr_err("Initiator unexpectedly changed login stage"
208 			" from %d to %d, login failed.\n", login->current_stage,
209 			req_csg);
210 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
211 				ISCSI_LOGIN_STATUS_INIT_ERR);
212 		return -1;
213 	}
214 
215 	if ((req_nsg == 2) || (req_csg >= 2) ||
216 	   ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
217 	    (req_nsg <= req_csg))) {
218 		pr_err("Illegal login_req->flags Combination, CSG: %d,"
219 			" NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
220 			req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
221 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
222 				ISCSI_LOGIN_STATUS_INIT_ERR);
223 		return -1;
224 	}
225 
226 	if ((login_req->max_version != login->version_max) ||
227 	    (login_req->min_version != login->version_min)) {
228 		pr_err("Login request changed Version Max/Nin"
229 			" unexpectedly to 0x%02x/0x%02x, protocol error\n",
230 			login_req->max_version, login_req->min_version);
231 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
232 				ISCSI_LOGIN_STATUS_INIT_ERR);
233 		return -1;
234 	}
235 
236 	if (memcmp(login_req->isid, login->isid, 6) != 0) {
237 		pr_err("Login request changed ISID unexpectedly,"
238 				" protocol error.\n");
239 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
240 				ISCSI_LOGIN_STATUS_INIT_ERR);
241 		return -1;
242 	}
243 
244 	if (login_req->itt != login->init_task_tag) {
245 		pr_err("Login request changed ITT unexpectedly to"
246 			" 0x%08x, protocol error.\n", login_req->itt);
247 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
248 				ISCSI_LOGIN_STATUS_INIT_ERR);
249 		return -1;
250 	}
251 
252 	if (payload_length > MAX_KEY_VALUE_PAIRS) {
253 		pr_err("Login request payload exceeds default"
254 			" MaxRecvDataSegmentLength: %u, protocol error.\n",
255 				MAX_KEY_VALUE_PAIRS);
256 		return -1;
257 	}
258 
259 	return 0;
260 }
261 
262 static int iscsi_target_check_first_request(
263 	struct iscsi_conn *conn,
264 	struct iscsi_login *login)
265 {
266 	struct iscsi_param *param = NULL;
267 	struct se_node_acl *se_nacl;
268 
269 	login->first_request = 0;
270 
271 	list_for_each_entry(param, &conn->param_list->param_list, p_list) {
272 		if (!strncmp(param->name, SESSIONTYPE, 11)) {
273 			if (!IS_PSTATE_ACCEPTOR(param)) {
274 				pr_err("SessionType key not received"
275 					" in first login request.\n");
276 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
277 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
278 				return -1;
279 			}
280 			if (!strncmp(param->value, DISCOVERY, 9))
281 				return 0;
282 		}
283 
284 		if (!strncmp(param->name, INITIATORNAME, 13)) {
285 			if (!IS_PSTATE_ACCEPTOR(param)) {
286 				if (!login->leading_connection)
287 					continue;
288 
289 				pr_err("InitiatorName key not received"
290 					" in first login request.\n");
291 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
292 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
293 				return -1;
294 			}
295 
296 			/*
297 			 * For non-leading connections, double check that the
298 			 * received InitiatorName matches the existing session's
299 			 * struct iscsi_node_acl.
300 			 */
301 			if (!login->leading_connection) {
302 				se_nacl = conn->sess->se_sess->se_node_acl;
303 				if (!se_nacl) {
304 					pr_err("Unable to locate"
305 						" struct se_node_acl\n");
306 					iscsit_tx_login_rsp(conn,
307 							ISCSI_STATUS_CLS_INITIATOR_ERR,
308 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
309 					return -1;
310 				}
311 
312 				if (strcmp(param->value,
313 						se_nacl->initiatorname)) {
314 					pr_err("Incorrect"
315 						" InitiatorName: %s for this"
316 						" iSCSI Initiator Node.\n",
317 						param->value);
318 					iscsit_tx_login_rsp(conn,
319 							ISCSI_STATUS_CLS_INITIATOR_ERR,
320 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
321 					return -1;
322 				}
323 			}
324 		}
325 	}
326 
327 	return 0;
328 }
329 
330 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
331 {
332 	u32 padding = 0;
333 	struct iscsi_session *sess = conn->sess;
334 	struct iscsi_login_rsp *login_rsp;
335 
336 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
337 
338 	login_rsp->opcode		= ISCSI_OP_LOGIN_RSP;
339 	hton24(login_rsp->dlength, login->rsp_length);
340 	memcpy(login_rsp->isid, login->isid, 6);
341 	login_rsp->tsih			= cpu_to_be16(login->tsih);
342 	login_rsp->itt			= login->init_task_tag;
343 	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
344 	login_rsp->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
345 	login_rsp->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
346 
347 	pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
348 		" ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
349 		" %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
350 		ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
351 		ntohl(login_rsp->statsn), login->rsp_length);
352 
353 	padding = ((-login->rsp_length) & 3);
354 
355 	if (iscsi_login_tx_data(
356 			conn,
357 			login->rsp,
358 			login->rsp_buf,
359 			login->rsp_length + padding) < 0)
360 		return -1;
361 
362 	login->rsp_length		= 0;
363 	mutex_lock(&sess->cmdsn_mutex);
364 	login_rsp->exp_cmdsn		= cpu_to_be32(sess->exp_cmd_sn);
365 	login_rsp->max_cmdsn		= cpu_to_be32(sess->max_cmd_sn);
366 	mutex_unlock(&sess->cmdsn_mutex);
367 
368 	return 0;
369 }
370 
371 static int iscsi_target_do_rx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
372 {
373 	u32 padding = 0, payload_length;
374 	struct iscsi_login_req *login_req;
375 
376 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
377 		return -1;
378 
379 	login_req = (struct iscsi_login_req *) login->req;
380 	payload_length			= ntoh24(login_req->dlength);
381 
382 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
383 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
384 		 login_req->flags, login_req->itt, login_req->cmdsn,
385 		 login_req->exp_statsn, login_req->cid, payload_length);
386 
387 	if (iscsi_target_check_login_request(conn, login) < 0)
388 		return -1;
389 
390 	padding = ((-payload_length) & 3);
391 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
392 
393 	if (iscsi_login_rx_data(
394 			conn,
395 			login->req_buf,
396 			payload_length + padding) < 0)
397 		return -1;
398 
399 	return 0;
400 }
401 
402 static int iscsi_target_do_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
403 {
404 	if (iscsi_target_do_tx_login_io(conn, login) < 0)
405 		return -1;
406 
407 	if (iscsi_target_do_rx_login_io(conn, login) < 0)
408 		return -1;
409 
410 	return 0;
411 }
412 
413 static int iscsi_target_get_initial_payload(
414 	struct iscsi_conn *conn,
415 	struct iscsi_login *login)
416 {
417 	u32 padding = 0, payload_length;
418 	struct iscsi_login_req *login_req;
419 
420 	login_req = (struct iscsi_login_req *) login->req;
421 	payload_length = ntoh24(login_req->dlength);
422 
423 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
424 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, Length: %u\n",
425 		login_req->flags, login_req->itt, login_req->cmdsn,
426 		login_req->exp_statsn, payload_length);
427 
428 	if (iscsi_target_check_login_request(conn, login) < 0)
429 		return -1;
430 
431 	padding = ((-payload_length) & 3);
432 
433 	if (iscsi_login_rx_data(
434 			conn,
435 			login->req_buf,
436 			payload_length + padding) < 0)
437 		return -1;
438 
439 	return 0;
440 }
441 
442 /*
443  *	NOTE: We check for existing sessions or connections AFTER the initiator
444  *	has been successfully authenticated in order to protect against faked
445  *	ISID/TSIH combinations.
446  */
447 static int iscsi_target_check_for_existing_instances(
448 	struct iscsi_conn *conn,
449 	struct iscsi_login *login)
450 {
451 	if (login->checked_for_existing)
452 		return 0;
453 
454 	login->checked_for_existing = 1;
455 
456 	if (!login->tsih)
457 		return iscsi_check_for_session_reinstatement(conn);
458 	else
459 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
460 				login->initial_exp_statsn);
461 }
462 
463 static int iscsi_target_do_authentication(
464 	struct iscsi_conn *conn,
465 	struct iscsi_login *login)
466 {
467 	int authret;
468 	u32 payload_length;
469 	struct iscsi_param *param;
470 	struct iscsi_login_req *login_req;
471 	struct iscsi_login_rsp *login_rsp;
472 
473 	login_req = (struct iscsi_login_req *) login->req;
474 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
475 	payload_length = ntoh24(login_req->dlength);
476 
477 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
478 	if (!param)
479 		return -1;
480 
481 	authret = iscsi_handle_authentication(
482 			conn,
483 			login->req_buf,
484 			login->rsp_buf,
485 			payload_length,
486 			&login->rsp_length,
487 			param->value);
488 	switch (authret) {
489 	case 0:
490 		pr_debug("Received OK response"
491 		" from LIO Authentication, continuing.\n");
492 		break;
493 	case 1:
494 		pr_debug("iSCSI security negotiation"
495 			" completed successfully.\n");
496 		login->auth_complete = 1;
497 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
498 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
499 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
500 					     ISCSI_FLAG_LOGIN_TRANSIT);
501 			login->current_stage = 1;
502 		}
503 		return iscsi_target_check_for_existing_instances(
504 				conn, login);
505 	case 2:
506 		pr_err("Security negotiation"
507 			" failed.\n");
508 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
509 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
510 		return -1;
511 	default:
512 		pr_err("Received unknown error %d from LIO"
513 				" Authentication\n", authret);
514 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
515 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
516 		return -1;
517 	}
518 
519 	return 0;
520 }
521 
522 static int iscsi_target_handle_csg_zero(
523 	struct iscsi_conn *conn,
524 	struct iscsi_login *login)
525 {
526 	int ret;
527 	u32 payload_length;
528 	struct iscsi_param *param;
529 	struct iscsi_login_req *login_req;
530 	struct iscsi_login_rsp *login_rsp;
531 
532 	login_req = (struct iscsi_login_req *) login->req;
533 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
534 	payload_length = ntoh24(login_req->dlength);
535 
536 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
537 	if (!param)
538 		return -1;
539 
540 	ret = iscsi_decode_text_input(
541 			PHASE_SECURITY|PHASE_DECLARATIVE,
542 			SENDER_INITIATOR|SENDER_RECEIVER,
543 			login->req_buf,
544 			payload_length,
545 			conn);
546 	if (ret < 0)
547 		return -1;
548 
549 	if (ret > 0) {
550 		if (login->auth_complete) {
551 			pr_err("Initiator has already been"
552 				" successfully authenticated, but is still"
553 				" sending %s keys.\n", param->value);
554 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
555 					ISCSI_LOGIN_STATUS_INIT_ERR);
556 			return -1;
557 		}
558 
559 		goto do_auth;
560 	}
561 
562 	if (login->first_request)
563 		if (iscsi_target_check_first_request(conn, login) < 0)
564 			return -1;
565 
566 	ret = iscsi_encode_text_output(
567 			PHASE_SECURITY|PHASE_DECLARATIVE,
568 			SENDER_TARGET,
569 			login->rsp_buf,
570 			&login->rsp_length,
571 			conn->param_list);
572 	if (ret < 0)
573 		return -1;
574 
575 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
576 		if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
577 		    !strncmp(param->value, NONE, 4)) {
578 			pr_err("Initiator sent AuthMethod=None but"
579 				" Target is enforcing iSCSI Authentication,"
580 					" login failed.\n");
581 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
582 					ISCSI_LOGIN_STATUS_AUTH_FAILED);
583 			return -1;
584 		}
585 
586 		if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
587 		    !login->auth_complete)
588 			return 0;
589 
590 		if (strncmp(param->value, NONE, 4) && !login->auth_complete)
591 			return 0;
592 
593 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
594 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
595 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
596 					    ISCSI_FLAG_LOGIN_TRANSIT;
597 			login->current_stage = 1;
598 		}
599 	}
600 
601 	return 0;
602 do_auth:
603 	return iscsi_target_do_authentication(conn, login);
604 }
605 
606 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
607 {
608 	int ret;
609 	u32 payload_length;
610 	struct iscsi_login_req *login_req;
611 	struct iscsi_login_rsp *login_rsp;
612 
613 	login_req = (struct iscsi_login_req *) login->req;
614 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
615 	payload_length = ntoh24(login_req->dlength);
616 
617 	ret = iscsi_decode_text_input(
618 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
619 			SENDER_INITIATOR|SENDER_RECEIVER,
620 			login->req_buf,
621 			payload_length,
622 			conn);
623 	if (ret < 0)
624 		return -1;
625 
626 	if (login->first_request)
627 		if (iscsi_target_check_first_request(conn, login) < 0)
628 			return -1;
629 
630 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
631 		return -1;
632 
633 	ret = iscsi_encode_text_output(
634 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
635 			SENDER_TARGET,
636 			login->rsp_buf,
637 			&login->rsp_length,
638 			conn->param_list);
639 	if (ret < 0)
640 		return -1;
641 
642 	if (!login->auth_complete &&
643 	     ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) {
644 		pr_err("Initiator is requesting CSG: 1, has not been"
645 			 " successfully authenticated, and the Target is"
646 			" enforcing iSCSI Authentication, login failed.\n");
647 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
648 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
649 		return -1;
650 	}
651 
652 	if (!iscsi_check_negotiated_keys(conn->param_list))
653 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
654 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
655 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
656 					    ISCSI_FLAG_LOGIN_TRANSIT;
657 
658 	return 0;
659 }
660 
661 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
662 {
663 	int pdu_count = 0;
664 	struct iscsi_login_req *login_req;
665 	struct iscsi_login_rsp *login_rsp;
666 
667 	login_req = (struct iscsi_login_req *) login->req;
668 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
669 
670 	while (1) {
671 		if (++pdu_count > MAX_LOGIN_PDUS) {
672 			pr_err("MAX_LOGIN_PDUS count reached.\n");
673 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
674 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
675 			return -1;
676 		}
677 
678 		switch ((login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2) {
679 		case 0:
680 			login_rsp->flags |= (0 & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK);
681 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
682 				return -1;
683 			break;
684 		case 1:
685 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
686 			if (iscsi_target_handle_csg_one(conn, login) < 0)
687 				return -1;
688 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
689 				login->tsih = conn->sess->tsih;
690 				if (iscsi_target_do_tx_login_io(conn,
691 						login) < 0)
692 					return -1;
693 				return 0;
694 			}
695 			break;
696 		default:
697 			pr_err("Illegal CSG: %d received from"
698 				" Initiator, protocol error.\n",
699 				(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK)
700 				>> 2);
701 			break;
702 		}
703 
704 		if (iscsi_target_do_login_io(conn, login) < 0)
705 			return -1;
706 
707 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
708 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
709 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
710 		}
711 	}
712 
713 	return 0;
714 }
715 
716 static void iscsi_initiatorname_tolower(
717 	char *param_buf)
718 {
719 	char *c;
720 	u32 iqn_size = strlen(param_buf), i;
721 
722 	for (i = 0; i < iqn_size; i++) {
723 		c = &param_buf[i];
724 		if (!isupper(*c))
725 			continue;
726 
727 		*c = tolower(*c);
728 	}
729 }
730 
731 /*
732  * Processes the first Login Request..
733  */
734 static int iscsi_target_locate_portal(
735 	struct iscsi_np *np,
736 	struct iscsi_conn *conn,
737 	struct iscsi_login *login)
738 {
739 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
740 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
741 	struct iscsi_session *sess = conn->sess;
742 	struct iscsi_tiqn *tiqn;
743 	struct iscsi_login_req *login_req;
744 	u32 payload_length;
745 	int sessiontype = 0, ret = 0;
746 
747 	login_req = (struct iscsi_login_req *) login->req;
748 	payload_length = ntoh24(login_req->dlength);
749 
750 	login->first_request	= 1;
751 	login->leading_connection = (!login_req->tsih) ? 1 : 0;
752 	login->current_stage	=
753 		(login_req->flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2;
754 	login->version_min	= login_req->min_version;
755 	login->version_max	= login_req->max_version;
756 	memcpy(login->isid, login_req->isid, 6);
757 	login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
758 	login->init_task_tag	= login_req->itt;
759 	login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
760 	login->cid		= be16_to_cpu(login_req->cid);
761 	login->tsih		= be16_to_cpu(login_req->tsih);
762 
763 	if (iscsi_target_get_initial_payload(conn, login) < 0)
764 		return -1;
765 
766 	tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
767 	if (!tmpbuf) {
768 		pr_err("Unable to allocate memory for tmpbuf.\n");
769 		return -1;
770 	}
771 
772 	memcpy(tmpbuf, login->req_buf, payload_length);
773 	tmpbuf[payload_length] = '\0';
774 	start = tmpbuf;
775 	end = (start + payload_length);
776 
777 	/*
778 	 * Locate the initial keys expected from the Initiator node in
779 	 * the first login request in order to progress with the login phase.
780 	 */
781 	while (start < end) {
782 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
783 			ret = -1;
784 			goto out;
785 		}
786 
787 		if (!strncmp(key, "InitiatorName", 13))
788 			i_buf = value;
789 		else if (!strncmp(key, "SessionType", 11))
790 			s_buf = value;
791 		else if (!strncmp(key, "TargetName", 10))
792 			t_buf = value;
793 
794 		start += strlen(key) + strlen(value) + 2;
795 	}
796 
797 	/*
798 	 * See 5.3.  Login Phase.
799 	 */
800 	if (!i_buf) {
801 		pr_err("InitiatorName key not received"
802 			" in first login request.\n");
803 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
804 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
805 		ret = -1;
806 		goto out;
807 	}
808 	/*
809 	 * Convert the incoming InitiatorName to lowercase following
810 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
811 	 * are NOT case sensitive.
812 	 */
813 	iscsi_initiatorname_tolower(i_buf);
814 
815 	if (!s_buf) {
816 		if (!login->leading_connection)
817 			goto get_target;
818 
819 		pr_err("SessionType key not received"
820 			" in first login request.\n");
821 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
822 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
823 		ret = -1;
824 		goto out;
825 	}
826 
827 	/*
828 	 * Use default portal group for discovery sessions.
829 	 */
830 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
831 	if (!sessiontype) {
832 		conn->tpg = iscsit_global->discovery_tpg;
833 		if (!login->leading_connection)
834 			goto get_target;
835 
836 		sess->sess_ops->SessionType = 1;
837 		/*
838 		 * Setup crc32c modules from libcrypto
839 		 */
840 		if (iscsi_login_setup_crypto(conn) < 0) {
841 			pr_err("iscsi_login_setup_crypto() failed\n");
842 			ret = -1;
843 			goto out;
844 		}
845 		/*
846 		 * Serialize access across the discovery struct iscsi_portal_group to
847 		 * process login attempt.
848 		 */
849 		if (iscsit_access_np(np, conn->tpg) < 0) {
850 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
851 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
852 			ret = -1;
853 			goto out;
854 		}
855 		ret = 0;
856 		goto out;
857 	}
858 
859 get_target:
860 	if (!t_buf) {
861 		pr_err("TargetName key not received"
862 			" in first login request while"
863 			" SessionType=Normal.\n");
864 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
865 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
866 		ret = -1;
867 		goto out;
868 	}
869 
870 	/*
871 	 * Locate Target IQN from Storage Node.
872 	 */
873 	tiqn = iscsit_get_tiqn_for_login(t_buf);
874 	if (!tiqn) {
875 		pr_err("Unable to locate Target IQN: %s in"
876 			" Storage Node\n", t_buf);
877 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
878 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
879 		ret = -1;
880 		goto out;
881 	}
882 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
883 
884 	/*
885 	 * Locate Target Portal Group from Storage Node.
886 	 */
887 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np);
888 	if (!conn->tpg) {
889 		pr_err("Unable to locate Target Portal Group"
890 				" on %s\n", tiqn->tiqn);
891 		iscsit_put_tiqn_for_login(tiqn);
892 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
893 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
894 		ret = -1;
895 		goto out;
896 	}
897 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
898 	/*
899 	 * Setup crc32c modules from libcrypto
900 	 */
901 	if (iscsi_login_setup_crypto(conn) < 0) {
902 		pr_err("iscsi_login_setup_crypto() failed\n");
903 		ret = -1;
904 		goto out;
905 	}
906 	/*
907 	 * Serialize access across the struct iscsi_portal_group to
908 	 * process login attempt.
909 	 */
910 	if (iscsit_access_np(np, conn->tpg) < 0) {
911 		iscsit_put_tiqn_for_login(tiqn);
912 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
913 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
914 		ret = -1;
915 		conn->tpg = NULL;
916 		goto out;
917 	}
918 
919 	/*
920 	 * conn->sess->node_acl will be set when the referenced
921 	 * struct iscsi_session is located from received ISID+TSIH in
922 	 * iscsi_login_non_zero_tsih_s2().
923 	 */
924 	if (!login->leading_connection) {
925 		ret = 0;
926 		goto out;
927 	}
928 
929 	/*
930 	 * This value is required in iscsi_login_zero_tsih_s2()
931 	 */
932 	sess->sess_ops->SessionType = 0;
933 
934 	/*
935 	 * Locate incoming Initiator IQN reference from Storage Node.
936 	 */
937 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
938 			&conn->tpg->tpg_se_tpg, i_buf);
939 	if (!sess->se_sess->se_node_acl) {
940 		pr_err("iSCSI Initiator Node: %s is not authorized to"
941 			" access iSCSI target portal group: %hu.\n",
942 				i_buf, conn->tpg->tpgt);
943 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
944 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
945 		ret = -1;
946 		goto out;
947 	}
948 
949 	ret = 0;
950 out:
951 	kfree(tmpbuf);
952 	return ret;
953 }
954 
955 struct iscsi_login *iscsi_target_init_negotiation(
956 	struct iscsi_np *np,
957 	struct iscsi_conn *conn,
958 	char *login_pdu)
959 {
960 	struct iscsi_login *login;
961 
962 	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
963 	if (!login) {
964 		pr_err("Unable to allocate memory for struct iscsi_login.\n");
965 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
966 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
967 		return NULL;
968 	}
969 
970 	login->req = kmemdup(login_pdu, ISCSI_HDR_LEN, GFP_KERNEL);
971 	if (!login->req) {
972 		pr_err("Unable to allocate memory for Login Request.\n");
973 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
974 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
975 		goto out;
976 	}
977 
978 	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
979 	if (!login->req_buf) {
980 		pr_err("Unable to allocate memory for response buffer.\n");
981 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
982 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
983 		goto out;
984 	}
985 	/*
986 	 * SessionType: Discovery
987 	 *
988 	 *	Locates Default Portal
989 	 *
990 	 * SessionType: Normal
991 	 *
992 	 *	Locates Target Portal from NP -> Target IQN
993 	 */
994 	if (iscsi_target_locate_portal(np, conn, login) < 0) {
995 		goto out;
996 	}
997 
998 	return login;
999 out:
1000 	kfree(login->req);
1001 	kfree(login->req_buf);
1002 	kfree(login);
1003 
1004 	return NULL;
1005 }
1006 
1007 int iscsi_target_start_negotiation(
1008 	struct iscsi_login *login,
1009 	struct iscsi_conn *conn)
1010 {
1011 	int ret = -1;
1012 
1013 	login->rsp = kzalloc(ISCSI_HDR_LEN, GFP_KERNEL);
1014 	if (!login->rsp) {
1015 		pr_err("Unable to allocate memory for"
1016 				" Login Response.\n");
1017 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1018 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
1019 		ret = -1;
1020 		goto out;
1021 	}
1022 
1023 	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
1024 	if (!login->rsp_buf) {
1025 		pr_err("Unable to allocate memory for"
1026 			" request buffer.\n");
1027 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1028 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
1029 		ret = -1;
1030 		goto out;
1031 	}
1032 
1033 	ret = iscsi_target_do_login(conn, login);
1034 out:
1035 	if (ret != 0)
1036 		iscsi_remove_failed_auth_entry(conn);
1037 
1038 	iscsi_target_nego_release(login, conn);
1039 	return ret;
1040 }
1041 
1042 void iscsi_target_nego_release(
1043 	struct iscsi_login *login,
1044 	struct iscsi_conn *conn)
1045 {
1046 	kfree(login->req);
1047 	kfree(login->rsp);
1048 	kfree(login->req_buf);
1049 	kfree(login->rsp_buf);
1050 	kfree(login);
1051 }
1052