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