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>
25baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
26e48354ceSNicholas Bellinger 
27e48354ceSNicholas Bellinger #include "iscsi_target_core.h"
28e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
29e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
30e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
31e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
32e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
33e48354ceSNicholas Bellinger #include "iscsi_target.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_auth.h"
35e48354ceSNicholas Bellinger 
36e48354ceSNicholas Bellinger #define MAX_LOGIN_PDUS  7
37e48354ceSNicholas Bellinger #define TEXT_LEN	4096
38e48354ceSNicholas Bellinger 
39e48354ceSNicholas Bellinger void convert_null_to_semi(char *buf, int len)
40e48354ceSNicholas Bellinger {
41e48354ceSNicholas Bellinger 	int i;
42e48354ceSNicholas Bellinger 
43e48354ceSNicholas Bellinger 	for (i = 0; i < len; i++)
44e48354ceSNicholas Bellinger 		if (buf[i] == '\0')
45e48354ceSNicholas Bellinger 			buf[i] = ';';
46e48354ceSNicholas Bellinger }
47e48354ceSNicholas Bellinger 
48fceb5bc7SChristoph Hellwig static int strlen_semi(char *buf)
49e48354ceSNicholas Bellinger {
50e48354ceSNicholas Bellinger 	int i = 0;
51e48354ceSNicholas Bellinger 
52e48354ceSNicholas Bellinger 	while (buf[i] != '\0') {
53e48354ceSNicholas Bellinger 		if (buf[i] == ';')
54e48354ceSNicholas Bellinger 			return i;
55e48354ceSNicholas Bellinger 		i++;
56e48354ceSNicholas Bellinger 	}
57e48354ceSNicholas Bellinger 
58e48354ceSNicholas Bellinger 	return -1;
59e48354ceSNicholas Bellinger }
60e48354ceSNicholas Bellinger 
61e48354ceSNicholas Bellinger int extract_param(
62e48354ceSNicholas Bellinger 	const char *in_buf,
63e48354ceSNicholas Bellinger 	const char *pattern,
64e48354ceSNicholas Bellinger 	unsigned int max_length,
65e48354ceSNicholas Bellinger 	char *out_buf,
66e48354ceSNicholas Bellinger 	unsigned char *type)
67e48354ceSNicholas Bellinger {
68e48354ceSNicholas Bellinger 	char *ptr;
69e48354ceSNicholas Bellinger 	int len;
70e48354ceSNicholas Bellinger 
71e48354ceSNicholas Bellinger 	if (!in_buf || !pattern || !out_buf || !type)
72e48354ceSNicholas Bellinger 		return -1;
73e48354ceSNicholas Bellinger 
74e48354ceSNicholas Bellinger 	ptr = strstr(in_buf, pattern);
75e48354ceSNicholas Bellinger 	if (!ptr)
76e48354ceSNicholas Bellinger 		return -1;
77e48354ceSNicholas Bellinger 
78e48354ceSNicholas Bellinger 	ptr = strstr(ptr, "=");
79e48354ceSNicholas Bellinger 	if (!ptr)
80e48354ceSNicholas Bellinger 		return -1;
81e48354ceSNicholas Bellinger 
82e48354ceSNicholas Bellinger 	ptr += 1;
83e48354ceSNicholas Bellinger 	if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
84e48354ceSNicholas Bellinger 		ptr += 2; /* skip 0x */
85e48354ceSNicholas Bellinger 		*type = HEX;
86e48354ceSNicholas Bellinger 	} else
87e48354ceSNicholas Bellinger 		*type = DECIMAL;
88e48354ceSNicholas Bellinger 
89e48354ceSNicholas Bellinger 	len = strlen_semi(ptr);
90e48354ceSNicholas Bellinger 	if (len < 0)
91e48354ceSNicholas Bellinger 		return -1;
92e48354ceSNicholas Bellinger 
93e48354ceSNicholas Bellinger 	if (len > max_length) {
945e58b029SMasanari Iida 		pr_err("Length of input: %d exceeds max_length:"
95e48354ceSNicholas Bellinger 			" %d\n", len, max_length);
96e48354ceSNicholas Bellinger 		return -1;
97e48354ceSNicholas Bellinger 	}
98e48354ceSNicholas Bellinger 	memcpy(out_buf, ptr, len);
99e48354ceSNicholas Bellinger 	out_buf[len] = '\0';
100e48354ceSNicholas Bellinger 
101e48354ceSNicholas Bellinger 	return 0;
102e48354ceSNicholas Bellinger }
103e48354ceSNicholas Bellinger 
104e48354ceSNicholas Bellinger static u32 iscsi_handle_authentication(
105e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
106e48354ceSNicholas Bellinger 	char *in_buf,
107e48354ceSNicholas Bellinger 	char *out_buf,
108e48354ceSNicholas Bellinger 	int in_length,
109e48354ceSNicholas Bellinger 	int *out_length,
110e48354ceSNicholas Bellinger 	unsigned char *authtype)
111e48354ceSNicholas Bellinger {
112e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
113e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth;
114e48354ceSNicholas Bellinger 	struct iscsi_node_acl *iscsi_nacl;
115c3e51442SNicholas Bellinger 	struct iscsi_portal_group *iscsi_tpg;
116e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
117e48354ceSNicholas Bellinger 
118e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType) {
119e48354ceSNicholas Bellinger 		/*
120e48354ceSNicholas Bellinger 		 * For SessionType=Normal
121e48354ceSNicholas Bellinger 		 */
122e48354ceSNicholas Bellinger 		se_nacl = conn->sess->se_sess->se_node_acl;
123e48354ceSNicholas Bellinger 		if (!se_nacl) {
124e48354ceSNicholas Bellinger 			pr_err("Unable to locate struct se_node_acl for"
125e48354ceSNicholas Bellinger 					" CHAP auth\n");
126e48354ceSNicholas Bellinger 			return -1;
127e48354ceSNicholas Bellinger 		}
128e48354ceSNicholas Bellinger 		iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
129e48354ceSNicholas Bellinger 				se_node_acl);
130e48354ceSNicholas Bellinger 		if (!iscsi_nacl) {
131e48354ceSNicholas Bellinger 			pr_err("Unable to locate struct iscsi_node_acl for"
132e48354ceSNicholas Bellinger 					" CHAP auth\n");
133e48354ceSNicholas Bellinger 			return -1;
134e48354ceSNicholas Bellinger 		}
135e48354ceSNicholas Bellinger 
136c3e51442SNicholas Bellinger 		if (se_nacl->dynamic_node_acl) {
137c3e51442SNicholas Bellinger 			iscsi_tpg = container_of(se_nacl->se_tpg,
138c3e51442SNicholas Bellinger 					struct iscsi_portal_group, tpg_se_tpg);
139c3e51442SNicholas Bellinger 
140c3e51442SNicholas Bellinger 			auth = &iscsi_tpg->tpg_demo_auth;
141c3e51442SNicholas Bellinger 		} else {
142c3e51442SNicholas Bellinger 			iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
143c3e51442SNicholas Bellinger 						  se_node_acl);
144c3e51442SNicholas Bellinger 
145e48354ceSNicholas Bellinger 			auth = ISCSI_NODE_AUTH(iscsi_nacl);
146c3e51442SNicholas Bellinger 		}
147e48354ceSNicholas Bellinger 	} else {
148e48354ceSNicholas Bellinger 		/*
149e48354ceSNicholas Bellinger 		 * For SessionType=Discovery
150e48354ceSNicholas Bellinger 		 */
151e48354ceSNicholas Bellinger 		auth = &iscsit_global->discovery_acl.node_auth;
152e48354ceSNicholas Bellinger 	}
153e48354ceSNicholas Bellinger 
154e48354ceSNicholas Bellinger 	if (strstr("CHAP", authtype))
155e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, "CHAP");
156e48354ceSNicholas Bellinger 	else
157e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, NONE);
158e48354ceSNicholas Bellinger 
159e48354ceSNicholas Bellinger 	if (strstr("None", authtype))
160e48354ceSNicholas Bellinger 		return 1;
161e48354ceSNicholas Bellinger #ifdef CANSRP
162e48354ceSNicholas Bellinger 	else if (strstr("SRP", authtype))
163e48354ceSNicholas Bellinger 		return srp_main_loop(conn, auth, in_buf, out_buf,
164e48354ceSNicholas Bellinger 				&in_length, out_length);
165e48354ceSNicholas Bellinger #endif
166e48354ceSNicholas Bellinger 	else if (strstr("CHAP", authtype))
167e48354ceSNicholas Bellinger 		return chap_main_loop(conn, auth, in_buf, out_buf,
168e48354ceSNicholas Bellinger 				&in_length, out_length);
169e48354ceSNicholas Bellinger 	else if (strstr("SPKM1", authtype))
170e48354ceSNicholas Bellinger 		return 2;
171e48354ceSNicholas Bellinger 	else if (strstr("SPKM2", authtype))
172e48354ceSNicholas Bellinger 		return 2;
173e48354ceSNicholas Bellinger 	else if (strstr("KRB5", authtype))
174e48354ceSNicholas Bellinger 		return 2;
175e48354ceSNicholas Bellinger 	else
176e48354ceSNicholas Bellinger 		return 2;
177e48354ceSNicholas Bellinger }
178e48354ceSNicholas Bellinger 
179e48354ceSNicholas Bellinger static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
180e48354ceSNicholas Bellinger {
181e48354ceSNicholas Bellinger 	kfree(conn->auth_protocol);
182e48354ceSNicholas Bellinger }
183e48354ceSNicholas Bellinger 
184baa4d64bSNicholas Bellinger int iscsi_target_check_login_request(
185e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
186e48354ceSNicholas Bellinger 	struct iscsi_login *login)
187e48354ceSNicholas Bellinger {
18828168905SJörn Engel 	int req_csg, req_nsg;
189e48354ceSNicholas Bellinger 	u32 payload_length;
190e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
191e48354ceSNicholas Bellinger 
192e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
193e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
194e48354ceSNicholas Bellinger 
195e48354ceSNicholas Bellinger 	switch (login_req->opcode & ISCSI_OPCODE_MASK) {
196e48354ceSNicholas Bellinger 	case ISCSI_OP_LOGIN:
197e48354ceSNicholas Bellinger 		break;
198e48354ceSNicholas Bellinger 	default:
199e48354ceSNicholas Bellinger 		pr_err("Received unknown opcode 0x%02x.\n",
200e48354ceSNicholas Bellinger 				login_req->opcode & ISCSI_OPCODE_MASK);
201e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
202e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
203e48354ceSNicholas Bellinger 		return -1;
204e48354ceSNicholas Bellinger 	}
205e48354ceSNicholas Bellinger 
206e48354ceSNicholas Bellinger 	if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
207e48354ceSNicholas Bellinger 	    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
208e48354ceSNicholas Bellinger 		pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
209e48354ceSNicholas Bellinger 			" and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
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 
2155d358065SAndy Grover 	req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
2165d358065SAndy Grover 	req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
217e48354ceSNicholas Bellinger 
218e48354ceSNicholas Bellinger 	if (req_csg != login->current_stage) {
219e48354ceSNicholas Bellinger 		pr_err("Initiator unexpectedly changed login stage"
220e48354ceSNicholas Bellinger 			" from %d to %d, login failed.\n", login->current_stage,
221e48354ceSNicholas Bellinger 			req_csg);
222e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
223e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
224e48354ceSNicholas Bellinger 		return -1;
225e48354ceSNicholas Bellinger 	}
226e48354ceSNicholas Bellinger 
227e48354ceSNicholas Bellinger 	if ((req_nsg == 2) || (req_csg >= 2) ||
228e48354ceSNicholas Bellinger 	   ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
229e48354ceSNicholas Bellinger 	    (req_nsg <= req_csg))) {
230e48354ceSNicholas Bellinger 		pr_err("Illegal login_req->flags Combination, CSG: %d,"
231e48354ceSNicholas Bellinger 			" NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
232e48354ceSNicholas Bellinger 			req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
233e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
234e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
235e48354ceSNicholas Bellinger 		return -1;
236e48354ceSNicholas Bellinger 	}
237e48354ceSNicholas Bellinger 
238e48354ceSNicholas Bellinger 	if ((login_req->max_version != login->version_max) ||
239e48354ceSNicholas Bellinger 	    (login_req->min_version != login->version_min)) {
240e48354ceSNicholas Bellinger 		pr_err("Login request changed Version Max/Nin"
241e48354ceSNicholas Bellinger 			" unexpectedly to 0x%02x/0x%02x, protocol error\n",
242e48354ceSNicholas Bellinger 			login_req->max_version, login_req->min_version);
243e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
244e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
245e48354ceSNicholas Bellinger 		return -1;
246e48354ceSNicholas Bellinger 	}
247e48354ceSNicholas Bellinger 
248e48354ceSNicholas Bellinger 	if (memcmp(login_req->isid, login->isid, 6) != 0) {
249e48354ceSNicholas Bellinger 		pr_err("Login request changed ISID unexpectedly,"
250e48354ceSNicholas Bellinger 				" protocol error.\n");
251e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
252e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
253e48354ceSNicholas Bellinger 		return -1;
254e48354ceSNicholas Bellinger 	}
255e48354ceSNicholas Bellinger 
256e48354ceSNicholas Bellinger 	if (login_req->itt != login->init_task_tag) {
257e48354ceSNicholas Bellinger 		pr_err("Login request changed ITT unexpectedly to"
258e48354ceSNicholas Bellinger 			" 0x%08x, protocol error.\n", login_req->itt);
259e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
260e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
261e48354ceSNicholas Bellinger 		return -1;
262e48354ceSNicholas Bellinger 	}
263e48354ceSNicholas Bellinger 
264e48354ceSNicholas Bellinger 	if (payload_length > MAX_KEY_VALUE_PAIRS) {
265e48354ceSNicholas Bellinger 		pr_err("Login request payload exceeds default"
266e48354ceSNicholas Bellinger 			" MaxRecvDataSegmentLength: %u, protocol error.\n",
267e48354ceSNicholas Bellinger 				MAX_KEY_VALUE_PAIRS);
268e48354ceSNicholas Bellinger 		return -1;
269e48354ceSNicholas Bellinger 	}
270e48354ceSNicholas Bellinger 
271e48354ceSNicholas Bellinger 	return 0;
272e48354ceSNicholas Bellinger }
273e48354ceSNicholas Bellinger 
274e48354ceSNicholas Bellinger static int iscsi_target_check_first_request(
275e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
276e48354ceSNicholas Bellinger 	struct iscsi_login *login)
277e48354ceSNicholas Bellinger {
278e48354ceSNicholas Bellinger 	struct iscsi_param *param = NULL;
279e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
280e48354ceSNicholas Bellinger 
281e48354ceSNicholas Bellinger 	login->first_request = 0;
282e48354ceSNicholas Bellinger 
283e48354ceSNicholas Bellinger 	list_for_each_entry(param, &conn->param_list->param_list, p_list) {
284e48354ceSNicholas Bellinger 		if (!strncmp(param->name, SESSIONTYPE, 11)) {
285e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
286e48354ceSNicholas Bellinger 				pr_err("SessionType key not received"
287e48354ceSNicholas Bellinger 					" in first login request.\n");
288e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
289e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
290e48354ceSNicholas Bellinger 				return -1;
291e48354ceSNicholas Bellinger 			}
292e48354ceSNicholas Bellinger 			if (!strncmp(param->value, DISCOVERY, 9))
293e48354ceSNicholas Bellinger 				return 0;
294e48354ceSNicholas Bellinger 		}
295e48354ceSNicholas Bellinger 
296e48354ceSNicholas Bellinger 		if (!strncmp(param->name, INITIATORNAME, 13)) {
297e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
298e48354ceSNicholas Bellinger 				if (!login->leading_connection)
299e48354ceSNicholas Bellinger 					continue;
300e48354ceSNicholas Bellinger 
301e48354ceSNicholas Bellinger 				pr_err("InitiatorName key not received"
302e48354ceSNicholas Bellinger 					" in first login request.\n");
303e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
304e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
305e48354ceSNicholas Bellinger 				return -1;
306e48354ceSNicholas Bellinger 			}
307e48354ceSNicholas Bellinger 
308e48354ceSNicholas Bellinger 			/*
309e48354ceSNicholas Bellinger 			 * For non-leading connections, double check that the
310e48354ceSNicholas Bellinger 			 * received InitiatorName matches the existing session's
311e48354ceSNicholas Bellinger 			 * struct iscsi_node_acl.
312e48354ceSNicholas Bellinger 			 */
313e48354ceSNicholas Bellinger 			if (!login->leading_connection) {
314e48354ceSNicholas Bellinger 				se_nacl = conn->sess->se_sess->se_node_acl;
315e48354ceSNicholas Bellinger 				if (!se_nacl) {
316e48354ceSNicholas Bellinger 					pr_err("Unable to locate"
317e48354ceSNicholas Bellinger 						" struct se_node_acl\n");
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 				if (strcmp(param->value,
325e48354ceSNicholas Bellinger 						se_nacl->initiatorname)) {
326e48354ceSNicholas Bellinger 					pr_err("Incorrect"
327e48354ceSNicholas Bellinger 						" InitiatorName: %s for this"
328e48354ceSNicholas Bellinger 						" iSCSI Initiator Node.\n",
329e48354ceSNicholas Bellinger 						param->value);
330e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
331e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
332e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
333e48354ceSNicholas Bellinger 					return -1;
334e48354ceSNicholas Bellinger 				}
335e48354ceSNicholas Bellinger 			}
336e48354ceSNicholas Bellinger 		}
337e48354ceSNicholas Bellinger 	}
338e48354ceSNicholas Bellinger 
339e48354ceSNicholas Bellinger 	return 0;
340e48354ceSNicholas Bellinger }
341e48354ceSNicholas Bellinger 
342e48354ceSNicholas Bellinger static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
343e48354ceSNicholas Bellinger {
344e48354ceSNicholas Bellinger 	u32 padding = 0;
345e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
346e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
347e48354ceSNicholas Bellinger 
348e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
349e48354ceSNicholas Bellinger 
350e48354ceSNicholas Bellinger 	login_rsp->opcode		= ISCSI_OP_LOGIN_RSP;
351e48354ceSNicholas Bellinger 	hton24(login_rsp->dlength, login->rsp_length);
352e48354ceSNicholas Bellinger 	memcpy(login_rsp->isid, login->isid, 6);
353e48354ceSNicholas Bellinger 	login_rsp->tsih			= cpu_to_be16(login->tsih);
35466c7db68SChristoph Hellwig 	login_rsp->itt			= login->init_task_tag;
355e48354ceSNicholas Bellinger 	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
356e48354ceSNicholas Bellinger 	login_rsp->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
357e48354ceSNicholas Bellinger 	login_rsp->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
358e48354ceSNicholas Bellinger 
359e48354ceSNicholas Bellinger 	pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
360e48354ceSNicholas Bellinger 		" ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
36166c7db68SChristoph Hellwig 		" %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
362e48354ceSNicholas Bellinger 		ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
363e48354ceSNicholas Bellinger 		ntohl(login_rsp->statsn), login->rsp_length);
364e48354ceSNicholas Bellinger 
365e48354ceSNicholas Bellinger 	padding = ((-login->rsp_length) & 3);
366e48354ceSNicholas Bellinger 
367baa4d64bSNicholas Bellinger 	if (conn->conn_transport->iscsit_put_login_tx(conn, login,
368e48354ceSNicholas Bellinger 					login->rsp_length + padding) < 0)
369e48354ceSNicholas Bellinger 		return -1;
370e48354ceSNicholas Bellinger 
371e48354ceSNicholas Bellinger 	login->rsp_length		= 0;
372e48354ceSNicholas Bellinger 	mutex_lock(&sess->cmdsn_mutex);
37350e5c87dSChristoph Hellwig 	login_rsp->exp_cmdsn		= cpu_to_be32(sess->exp_cmd_sn);
37450e5c87dSChristoph Hellwig 	login_rsp->max_cmdsn		= cpu_to_be32(sess->max_cmd_sn);
375e48354ceSNicholas Bellinger 	mutex_unlock(&sess->cmdsn_mutex);
376e48354ceSNicholas Bellinger 
377e48354ceSNicholas Bellinger 	return 0;
378e48354ceSNicholas Bellinger }
379e48354ceSNicholas Bellinger 
380d381a801SNicholas Bellinger static void iscsi_target_sk_data_ready(struct sock *sk, int count)
381d381a801SNicholas Bellinger {
382d381a801SNicholas Bellinger 	struct iscsi_conn *conn = sk->sk_user_data;
383d381a801SNicholas Bellinger 	bool rc;
384d381a801SNicholas Bellinger 
385d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
386d381a801SNicholas Bellinger 
387d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
388d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
389d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
390d381a801SNicholas Bellinger 		return;
391d381a801SNicholas Bellinger 	}
392d381a801SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
393d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
394d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
395d381a801SNicholas Bellinger 		return;
396d381a801SNicholas Bellinger 	}
397d381a801SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
398d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
399d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
400d381a801SNicholas Bellinger 		return;
401d381a801SNicholas Bellinger 	}
402d381a801SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
403d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
404d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
405d381a801SNicholas Bellinger 		return;
406d381a801SNicholas Bellinger 	}
407d381a801SNicholas Bellinger 
408d381a801SNicholas Bellinger 	rc = schedule_delayed_work(&conn->login_work, 0);
409d381a801SNicholas Bellinger 	if (rc == false) {
410d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
411d381a801SNicholas Bellinger 			 " got false\n");
412d381a801SNicholas Bellinger 	}
413d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
414d381a801SNicholas Bellinger }
415d381a801SNicholas Bellinger 
416bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *);
417bb048357SNicholas Bellinger 
418d381a801SNicholas Bellinger static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
419d381a801SNicholas Bellinger {
420d381a801SNicholas Bellinger 	struct sock *sk;
421d381a801SNicholas Bellinger 
422d381a801SNicholas Bellinger 	if (!conn->sock)
423d381a801SNicholas Bellinger 		return;
424d381a801SNicholas Bellinger 
425d381a801SNicholas Bellinger 	sk = conn->sock->sk;
426d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
427d381a801SNicholas Bellinger 
428d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
429d381a801SNicholas Bellinger 	sk->sk_user_data = conn;
430d381a801SNicholas Bellinger 	conn->orig_data_ready = sk->sk_data_ready;
431bb048357SNicholas Bellinger 	conn->orig_state_change = sk->sk_state_change;
432d381a801SNicholas Bellinger 	sk->sk_data_ready = iscsi_target_sk_data_ready;
433bb048357SNicholas Bellinger 	sk->sk_state_change = iscsi_target_sk_state_change;
434d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
435bb048357SNicholas Bellinger 
436bb048357SNicholas Bellinger 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
437bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
438d381a801SNicholas Bellinger }
439d381a801SNicholas Bellinger 
440d381a801SNicholas Bellinger static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
441d381a801SNicholas Bellinger {
442d381a801SNicholas Bellinger 	struct sock *sk;
443d381a801SNicholas Bellinger 
444d381a801SNicholas Bellinger 	if (!conn->sock)
445d381a801SNicholas Bellinger 		return;
446d381a801SNicholas Bellinger 
447d381a801SNicholas Bellinger 	sk = conn->sock->sk;
448d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
449d381a801SNicholas Bellinger 
450d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
451d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
452d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
453d381a801SNicholas Bellinger 		return;
454d381a801SNicholas Bellinger 	}
455d381a801SNicholas Bellinger 	sk->sk_user_data = NULL;
456d381a801SNicholas Bellinger 	sk->sk_data_ready = conn->orig_data_ready;
457bb048357SNicholas Bellinger 	sk->sk_state_change = conn->orig_state_change;
458d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
459bb048357SNicholas Bellinger 
460bb048357SNicholas Bellinger 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
461bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
462d381a801SNicholas Bellinger }
463d381a801SNicholas Bellinger 
464d381a801SNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
465d381a801SNicholas Bellinger 
466d381a801SNicholas Bellinger static bool iscsi_target_sk_state_check(struct sock *sk)
467d381a801SNicholas Bellinger {
468d381a801SNicholas Bellinger 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
469d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
470d381a801SNicholas Bellinger 			"returning FALSE\n");
471d381a801SNicholas Bellinger 		return false;
472d381a801SNicholas Bellinger 	}
473d381a801SNicholas Bellinger 	return true;
474d381a801SNicholas Bellinger }
475d381a801SNicholas Bellinger 
476d381a801SNicholas Bellinger static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
477d381a801SNicholas Bellinger {
478d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
479d381a801SNicholas Bellinger 	bool zero_tsih = login->zero_tsih;
480d381a801SNicholas Bellinger 
481d381a801SNicholas Bellinger 	iscsi_remove_failed_auth_entry(conn);
482d381a801SNicholas Bellinger 	iscsi_target_nego_release(conn);
483d381a801SNicholas Bellinger 	iscsi_target_login_sess_out(conn, np, zero_tsih, true);
484d381a801SNicholas Bellinger }
485d381a801SNicholas Bellinger 
486d381a801SNicholas Bellinger static void iscsi_target_login_timeout(unsigned long data)
487d381a801SNicholas Bellinger {
488d381a801SNicholas Bellinger 	struct iscsi_conn *conn = (struct iscsi_conn *)data;
489d381a801SNicholas Bellinger 
490d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
491d381a801SNicholas Bellinger 
492d381a801SNicholas Bellinger 	if (conn->login_kworker) {
493d381a801SNicholas Bellinger 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
494d381a801SNicholas Bellinger 			 conn->login_kworker->comm, conn->login_kworker->pid);
495d381a801SNicholas Bellinger 		send_sig(SIGINT, conn->login_kworker, 1);
496d381a801SNicholas Bellinger 	}
497d381a801SNicholas Bellinger }
498d381a801SNicholas Bellinger 
499d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work)
500d381a801SNicholas Bellinger {
501d381a801SNicholas Bellinger 	struct iscsi_conn *conn = container_of(work,
502d381a801SNicholas Bellinger 				struct iscsi_conn, login_work.work);
503d381a801SNicholas Bellinger 	struct iscsi_login *login = conn->login;
504d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
505d381a801SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
506d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
507d381a801SNicholas Bellinger 	struct timer_list login_timer;
508d381a801SNicholas Bellinger 	int rc, zero_tsih = login->zero_tsih;
509d381a801SNicholas Bellinger 	bool state;
510d381a801SNicholas Bellinger 
511d381a801SNicholas Bellinger 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
512d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
513d381a801SNicholas Bellinger 
514d381a801SNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
515d381a801SNicholas Bellinger 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
516d381a801SNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
517d381a801SNicholas Bellinger 
518d381a801SNicholas Bellinger 	if (state == false) {
519d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
520d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
521d381a801SNicholas Bellinger 		iscsi_target_login_drop(conn, login);
522d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
523d381a801SNicholas Bellinger 		return;
524d381a801SNicholas Bellinger 	}
525d381a801SNicholas Bellinger 
526d381a801SNicholas Bellinger 	if (conn->sock) {
527d381a801SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
528d381a801SNicholas Bellinger 
529d381a801SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
530d381a801SNicholas Bellinger 		state = iscsi_target_sk_state_check(sk);
531d381a801SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
532d381a801SNicholas Bellinger 
533d381a801SNicholas Bellinger 		if (state == false) {
534d381a801SNicholas Bellinger 			pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
535d381a801SNicholas Bellinger 			iscsi_target_restore_sock_callbacks(conn);
536d381a801SNicholas Bellinger 			iscsi_target_login_drop(conn, login);
537d381a801SNicholas Bellinger 			iscsit_deaccess_np(np, tpg, tpg_np);
538d381a801SNicholas Bellinger 			return;
539d381a801SNicholas Bellinger 		}
540d381a801SNicholas Bellinger 	}
541d381a801SNicholas Bellinger 
542d381a801SNicholas Bellinger 	conn->login_kworker = current;
543d381a801SNicholas Bellinger 	allow_signal(SIGINT);
544d381a801SNicholas Bellinger 
545d381a801SNicholas Bellinger 	init_timer(&login_timer);
546d381a801SNicholas Bellinger 	login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
547d381a801SNicholas Bellinger 	login_timer.data = (unsigned long)conn;
548d381a801SNicholas Bellinger 	login_timer.function = iscsi_target_login_timeout;
549d381a801SNicholas Bellinger 	add_timer(&login_timer);
550d381a801SNicholas Bellinger 	pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
551d381a801SNicholas Bellinger 
552d381a801SNicholas Bellinger 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
553d381a801SNicholas Bellinger 	del_timer_sync(&login_timer);
554d381a801SNicholas Bellinger 	flush_signals(current);
555d381a801SNicholas Bellinger 	conn->login_kworker = NULL;
556d381a801SNicholas Bellinger 
557d381a801SNicholas Bellinger 	if (rc < 0) {
558d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
559d381a801SNicholas Bellinger 		iscsi_target_login_drop(conn, login);
560d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
561d381a801SNicholas Bellinger 		return;
562d381a801SNicholas Bellinger 	}
563d381a801SNicholas Bellinger 
564d381a801SNicholas Bellinger 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
565d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
566d381a801SNicholas Bellinger 
567d381a801SNicholas Bellinger 	rc = iscsi_target_do_login(conn, login);
568d381a801SNicholas Bellinger 	if (rc < 0) {
569d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
570d381a801SNicholas Bellinger 		iscsi_target_login_drop(conn, login);
571d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
572d381a801SNicholas Bellinger 	} else if (!rc) {
573d381a801SNicholas Bellinger 		if (conn->sock) {
574d381a801SNicholas Bellinger 			struct sock *sk = conn->sock->sk;
575d381a801SNicholas Bellinger 
576d381a801SNicholas Bellinger 			write_lock_bh(&sk->sk_callback_lock);
577d381a801SNicholas Bellinger 			clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
578d381a801SNicholas Bellinger 			write_unlock_bh(&sk->sk_callback_lock);
579d381a801SNicholas Bellinger 		}
580d381a801SNicholas Bellinger 	} else if (rc == 1) {
581d381a801SNicholas Bellinger 		iscsi_target_nego_release(conn);
582d381a801SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
583d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
584d381a801SNicholas Bellinger 	}
585d381a801SNicholas Bellinger }
586d381a801SNicholas Bellinger 
587bb048357SNicholas Bellinger static void iscsi_target_do_cleanup(struct work_struct *work)
588bb048357SNicholas Bellinger {
589bb048357SNicholas Bellinger 	struct iscsi_conn *conn = container_of(work,
590bb048357SNicholas Bellinger 				struct iscsi_conn, login_cleanup_work.work);
591bb048357SNicholas Bellinger 	struct sock *sk = conn->sock->sk;
592bb048357SNicholas Bellinger 	struct iscsi_login *login = conn->login;
593bb048357SNicholas Bellinger 	struct iscsi_np *np = login->np;
594bb048357SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
595bb048357SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
596bb048357SNicholas Bellinger 
597bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_do_cleanup\n");
598bb048357SNicholas Bellinger 
599bb048357SNicholas Bellinger 	cancel_delayed_work_sync(&conn->login_work);
600bb048357SNicholas Bellinger 	conn->orig_state_change(sk);
601bb048357SNicholas Bellinger 
602bb048357SNicholas Bellinger 	iscsi_target_restore_sock_callbacks(conn);
603bb048357SNicholas Bellinger 	iscsi_target_login_drop(conn, login);
604bb048357SNicholas Bellinger 	iscsit_deaccess_np(np, tpg, tpg_np);
605bb048357SNicholas Bellinger 
606bb048357SNicholas Bellinger 	pr_debug("iscsi_target_do_cleanup done()\n");
607bb048357SNicholas Bellinger }
608bb048357SNicholas Bellinger 
609bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk)
610bb048357SNicholas Bellinger {
611bb048357SNicholas Bellinger 	struct iscsi_conn *conn;
612bb048357SNicholas Bellinger 	void (*orig_state_change)(struct sock *);
613bb048357SNicholas Bellinger 	bool state;
614bb048357SNicholas Bellinger 
615bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_state_change\n");
616bb048357SNicholas Bellinger 
617bb048357SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
618bb048357SNicholas Bellinger 	conn = sk->sk_user_data;
619bb048357SNicholas Bellinger 	if (!conn) {
620bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
621bb048357SNicholas Bellinger 		return;
622bb048357SNicholas Bellinger 	}
623bb048357SNicholas Bellinger 	orig_state_change = conn->orig_state_change;
624bb048357SNicholas Bellinger 
625bb048357SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
626bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
627bb048357SNicholas Bellinger 			 conn);
628bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
629bb048357SNicholas Bellinger 		orig_state_change(sk);
630bb048357SNicholas Bellinger 		return;
631bb048357SNicholas Bellinger 	}
632bb048357SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
633bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
634bb048357SNicholas Bellinger 			 " conn: %p\n", conn);
635bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
636bb048357SNicholas Bellinger 		orig_state_change(sk);
637bb048357SNicholas Bellinger 		return;
638bb048357SNicholas Bellinger 	}
639bb048357SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
640bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
641bb048357SNicholas Bellinger 			 conn);
642bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
643bb048357SNicholas Bellinger 		orig_state_change(sk);
644bb048357SNicholas Bellinger 		return;
645bb048357SNicholas Bellinger 	}
646bb048357SNicholas Bellinger 
647bb048357SNicholas Bellinger 	state = iscsi_target_sk_state_check(sk);
648bb048357SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
649bb048357SNicholas Bellinger 
650bb048357SNicholas Bellinger 	pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
651bb048357SNicholas Bellinger 
652bb048357SNicholas Bellinger 	if (!state) {
653bb048357SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_change got failed state\n");
654bb048357SNicholas Bellinger 		schedule_delayed_work(&conn->login_cleanup_work, 0);
655bb048357SNicholas Bellinger 		return;
656bb048357SNicholas Bellinger 	}
657bb048357SNicholas Bellinger 	orig_state_change(sk);
658bb048357SNicholas Bellinger }
659bb048357SNicholas Bellinger 
660e48354ceSNicholas Bellinger /*
661e48354ceSNicholas Bellinger  *	NOTE: We check for existing sessions or connections AFTER the initiator
662e48354ceSNicholas Bellinger  *	has been successfully authenticated in order to protect against faked
663e48354ceSNicholas Bellinger  *	ISID/TSIH combinations.
664e48354ceSNicholas Bellinger  */
665e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances(
666e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
667e48354ceSNicholas Bellinger 	struct iscsi_login *login)
668e48354ceSNicholas Bellinger {
669e48354ceSNicholas Bellinger 	if (login->checked_for_existing)
670e48354ceSNicholas Bellinger 		return 0;
671e48354ceSNicholas Bellinger 
672e48354ceSNicholas Bellinger 	login->checked_for_existing = 1;
673e48354ceSNicholas Bellinger 
674e48354ceSNicholas Bellinger 	if (!login->tsih)
675e48354ceSNicholas Bellinger 		return iscsi_check_for_session_reinstatement(conn);
676e48354ceSNicholas Bellinger 	else
677e48354ceSNicholas Bellinger 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
678e48354ceSNicholas Bellinger 				login->initial_exp_statsn);
679e48354ceSNicholas Bellinger }
680e48354ceSNicholas Bellinger 
681e48354ceSNicholas Bellinger static int iscsi_target_do_authentication(
682e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
683e48354ceSNicholas Bellinger 	struct iscsi_login *login)
684e48354ceSNicholas Bellinger {
685e48354ceSNicholas Bellinger 	int authret;
686e48354ceSNicholas Bellinger 	u32 payload_length;
687e48354ceSNicholas Bellinger 	struct iscsi_param *param;
688e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
689e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
690e48354ceSNicholas Bellinger 
691e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
692e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
693e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
694e48354ceSNicholas Bellinger 
695e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
696e48354ceSNicholas Bellinger 	if (!param)
697e48354ceSNicholas Bellinger 		return -1;
698e48354ceSNicholas Bellinger 
699e48354ceSNicholas Bellinger 	authret = iscsi_handle_authentication(
700e48354ceSNicholas Bellinger 			conn,
701e48354ceSNicholas Bellinger 			login->req_buf,
702e48354ceSNicholas Bellinger 			login->rsp_buf,
703e48354ceSNicholas Bellinger 			payload_length,
704e48354ceSNicholas Bellinger 			&login->rsp_length,
705e48354ceSNicholas Bellinger 			param->value);
706e48354ceSNicholas Bellinger 	switch (authret) {
707e48354ceSNicholas Bellinger 	case 0:
708e48354ceSNicholas Bellinger 		pr_debug("Received OK response"
709e48354ceSNicholas Bellinger 		" from LIO Authentication, continuing.\n");
710e48354ceSNicholas Bellinger 		break;
711e48354ceSNicholas Bellinger 	case 1:
712e48354ceSNicholas Bellinger 		pr_debug("iSCSI security negotiation"
713bfb9035cSJoe Perches 			" completed successfully.\n");
714e48354ceSNicholas Bellinger 		login->auth_complete = 1;
715e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
716e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
717e48354ceSNicholas Bellinger 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
718e48354ceSNicholas Bellinger 					     ISCSI_FLAG_LOGIN_TRANSIT);
719e48354ceSNicholas Bellinger 			login->current_stage = 1;
720e48354ceSNicholas Bellinger 		}
721e48354ceSNicholas Bellinger 		return iscsi_target_check_for_existing_instances(
722e48354ceSNicholas Bellinger 				conn, login);
723e48354ceSNicholas Bellinger 	case 2:
724e48354ceSNicholas Bellinger 		pr_err("Security negotiation"
725e48354ceSNicholas Bellinger 			" failed.\n");
726e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
727e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
728e48354ceSNicholas Bellinger 		return -1;
729e48354ceSNicholas Bellinger 	default:
730e48354ceSNicholas Bellinger 		pr_err("Received unknown error %d from LIO"
731e48354ceSNicholas Bellinger 				" Authentication\n", authret);
732e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
733e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
734e48354ceSNicholas Bellinger 		return -1;
735e48354ceSNicholas Bellinger 	}
736e48354ceSNicholas Bellinger 
737e48354ceSNicholas Bellinger 	return 0;
738e48354ceSNicholas Bellinger }
739e48354ceSNicholas Bellinger 
740e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero(
741e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
742e48354ceSNicholas Bellinger 	struct iscsi_login *login)
743e48354ceSNicholas Bellinger {
744e48354ceSNicholas Bellinger 	int ret;
745e48354ceSNicholas Bellinger 	u32 payload_length;
746e48354ceSNicholas Bellinger 	struct iscsi_param *param;
747e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
748e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
749e48354ceSNicholas Bellinger 
750e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
751e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
752e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
753e48354ceSNicholas Bellinger 
754e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
755e48354ceSNicholas Bellinger 	if (!param)
756e48354ceSNicholas Bellinger 		return -1;
757e48354ceSNicholas Bellinger 
758e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
759e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
760e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
761e48354ceSNicholas Bellinger 			login->req_buf,
762e48354ceSNicholas Bellinger 			payload_length,
7639977bb18SNicholas Bellinger 			conn);
764e48354ceSNicholas Bellinger 	if (ret < 0)
765e48354ceSNicholas Bellinger 		return -1;
766e48354ceSNicholas Bellinger 
767e48354ceSNicholas Bellinger 	if (ret > 0) {
768e48354ceSNicholas Bellinger 		if (login->auth_complete) {
769e48354ceSNicholas Bellinger 			pr_err("Initiator has already been"
770e48354ceSNicholas Bellinger 				" successfully authenticated, but is still"
771e48354ceSNicholas Bellinger 				" sending %s keys.\n", param->value);
772e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
773e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_INIT_ERR);
774e48354ceSNicholas Bellinger 			return -1;
775e48354ceSNicholas Bellinger 		}
776e48354ceSNicholas Bellinger 
777e48354ceSNicholas Bellinger 		goto do_auth;
778e48354ceSNicholas Bellinger 	}
779e48354ceSNicholas Bellinger 
780e48354ceSNicholas Bellinger 	if (login->first_request)
781e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
782e48354ceSNicholas Bellinger 			return -1;
783e48354ceSNicholas Bellinger 
784e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
785e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
786e48354ceSNicholas Bellinger 			SENDER_TARGET,
787e48354ceSNicholas Bellinger 			login->rsp_buf,
788e48354ceSNicholas Bellinger 			&login->rsp_length,
789e48354ceSNicholas Bellinger 			conn->param_list);
790e48354ceSNicholas Bellinger 	if (ret < 0)
791e48354ceSNicholas Bellinger 		return -1;
792e48354ceSNicholas Bellinger 
793e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
794e48354ceSNicholas Bellinger 		if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
795e48354ceSNicholas Bellinger 		    !strncmp(param->value, NONE, 4)) {
796e48354ceSNicholas Bellinger 			pr_err("Initiator sent AuthMethod=None but"
797e48354ceSNicholas Bellinger 				" Target is enforcing iSCSI Authentication,"
798e48354ceSNicholas Bellinger 					" login failed.\n");
799e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
800e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_AUTH_FAILED);
801e48354ceSNicholas Bellinger 			return -1;
802e48354ceSNicholas Bellinger 		}
803e48354ceSNicholas Bellinger 
804e48354ceSNicholas Bellinger 		if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
805e48354ceSNicholas Bellinger 		    !login->auth_complete)
806e48354ceSNicholas Bellinger 			return 0;
807e48354ceSNicholas Bellinger 
808e48354ceSNicholas Bellinger 		if (strncmp(param->value, NONE, 4) && !login->auth_complete)
809e48354ceSNicholas Bellinger 			return 0;
810e48354ceSNicholas Bellinger 
811e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
812e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
813e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
814e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
815e48354ceSNicholas Bellinger 			login->current_stage = 1;
816e48354ceSNicholas Bellinger 		}
817e48354ceSNicholas Bellinger 	}
818e48354ceSNicholas Bellinger 
819e48354ceSNicholas Bellinger 	return 0;
820e48354ceSNicholas Bellinger do_auth:
821e48354ceSNicholas Bellinger 	return iscsi_target_do_authentication(conn, login);
822e48354ceSNicholas Bellinger }
823e48354ceSNicholas Bellinger 
824e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
825e48354ceSNicholas Bellinger {
826e48354ceSNicholas Bellinger 	int ret;
827e48354ceSNicholas Bellinger 	u32 payload_length;
828e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
829e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
830e48354ceSNicholas Bellinger 
831e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
832e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
833e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
834e48354ceSNicholas Bellinger 
835e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
836e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
837e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
838e48354ceSNicholas Bellinger 			login->req_buf,
839e48354ceSNicholas Bellinger 			payload_length,
8409977bb18SNicholas Bellinger 			conn);
8411c5c12c6SRoland Dreier 	if (ret < 0) {
8421c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
8431c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
844e48354ceSNicholas Bellinger 		return -1;
8451c5c12c6SRoland Dreier 	}
846e48354ceSNicholas Bellinger 
847e48354ceSNicholas Bellinger 	if (login->first_request)
848e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
849e48354ceSNicholas Bellinger 			return -1;
850e48354ceSNicholas Bellinger 
851e48354ceSNicholas Bellinger 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
852e48354ceSNicholas Bellinger 		return -1;
853e48354ceSNicholas Bellinger 
854e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
855e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
856e48354ceSNicholas Bellinger 			SENDER_TARGET,
857e48354ceSNicholas Bellinger 			login->rsp_buf,
858e48354ceSNicholas Bellinger 			&login->rsp_length,
859e48354ceSNicholas Bellinger 			conn->param_list);
8601c5c12c6SRoland Dreier 	if (ret < 0) {
8611c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
8621c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
863e48354ceSNicholas Bellinger 		return -1;
8641c5c12c6SRoland Dreier 	}
865e48354ceSNicholas Bellinger 
866e48354ceSNicholas Bellinger 	if (!login->auth_complete &&
867e48354ceSNicholas Bellinger 	     ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) {
868e48354ceSNicholas Bellinger 		pr_err("Initiator is requesting CSG: 1, has not been"
869e48354ceSNicholas Bellinger 			 " successfully authenticated, and the Target is"
870e48354ceSNicholas Bellinger 			" enforcing iSCSI Authentication, login failed.\n");
871e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
872e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
873e48354ceSNicholas Bellinger 		return -1;
874e48354ceSNicholas Bellinger 	}
875e48354ceSNicholas Bellinger 
876e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list))
877e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
878e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
879e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
880e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
881e48354ceSNicholas Bellinger 
882e48354ceSNicholas Bellinger 	return 0;
883e48354ceSNicholas Bellinger }
884e48354ceSNicholas Bellinger 
885e48354ceSNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
886e48354ceSNicholas Bellinger {
887e48354ceSNicholas Bellinger 	int pdu_count = 0;
888e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
889e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
890e48354ceSNicholas Bellinger 
891e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
892e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
893e48354ceSNicholas Bellinger 
894e48354ceSNicholas Bellinger 	while (1) {
895e48354ceSNicholas Bellinger 		if (++pdu_count > MAX_LOGIN_PDUS) {
896e48354ceSNicholas Bellinger 			pr_err("MAX_LOGIN_PDUS count reached.\n");
897e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
898e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
899e48354ceSNicholas Bellinger 			return -1;
900e48354ceSNicholas Bellinger 		}
901e48354ceSNicholas Bellinger 
9025d358065SAndy Grover 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
903e48354ceSNicholas Bellinger 		case 0:
9045d358065SAndy Grover 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
905e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
906e48354ceSNicholas Bellinger 				return -1;
907e48354ceSNicholas Bellinger 			break;
908e48354ceSNicholas Bellinger 		case 1:
909e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
910e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_one(conn, login) < 0)
911e48354ceSNicholas Bellinger 				return -1;
912e48354ceSNicholas Bellinger 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
913e48354ceSNicholas Bellinger 				login->tsih = conn->sess->tsih;
914baa4d64bSNicholas Bellinger 				login->login_complete = 1;
915d381a801SNicholas Bellinger 				iscsi_target_restore_sock_callbacks(conn);
916e48354ceSNicholas Bellinger 				if (iscsi_target_do_tx_login_io(conn,
917e48354ceSNicholas Bellinger 						login) < 0)
918e48354ceSNicholas Bellinger 					return -1;
919d381a801SNicholas Bellinger 				return 1;
920e48354ceSNicholas Bellinger 			}
921e48354ceSNicholas Bellinger 			break;
922e48354ceSNicholas Bellinger 		default:
923e48354ceSNicholas Bellinger 			pr_err("Illegal CSG: %d received from"
924e48354ceSNicholas Bellinger 				" Initiator, protocol error.\n",
9255d358065SAndy Grover 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
926e48354ceSNicholas Bellinger 			break;
927e48354ceSNicholas Bellinger 		}
928e48354ceSNicholas Bellinger 
929ea3a179aSNicholas Bellinger 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
930e48354ceSNicholas Bellinger 			return -1;
931e48354ceSNicholas Bellinger 
932e48354ceSNicholas Bellinger 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
933e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
934e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
935e48354ceSNicholas Bellinger 		}
936d381a801SNicholas Bellinger 		break;
937e48354ceSNicholas Bellinger 	}
938e48354ceSNicholas Bellinger 
939bb048357SNicholas Bellinger 	if (conn->sock) {
940bb048357SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
941bb048357SNicholas Bellinger 		bool state;
942bb048357SNicholas Bellinger 
943bb048357SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
944bb048357SNicholas Bellinger 		state = iscsi_target_sk_state_check(sk);
945bb048357SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
946bb048357SNicholas Bellinger 
947bb048357SNicholas Bellinger 		if (!state) {
948bb048357SNicholas Bellinger 			pr_debug("iscsi_target_do_login() failed state for"
949bb048357SNicholas Bellinger 				 " conn: %p\n", conn);
950bb048357SNicholas Bellinger 			return -1;
951bb048357SNicholas Bellinger 		}
952bb048357SNicholas Bellinger 	}
953bb048357SNicholas Bellinger 
954e48354ceSNicholas Bellinger 	return 0;
955e48354ceSNicholas Bellinger }
956e48354ceSNicholas Bellinger 
957e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower(
958e48354ceSNicholas Bellinger 	char *param_buf)
959e48354ceSNicholas Bellinger {
960e48354ceSNicholas Bellinger 	char *c;
961e48354ceSNicholas Bellinger 	u32 iqn_size = strlen(param_buf), i;
962e48354ceSNicholas Bellinger 
963e48354ceSNicholas Bellinger 	for (i = 0; i < iqn_size; i++) {
9648359cf43SJörn Engel 		c = &param_buf[i];
965e48354ceSNicholas Bellinger 		if (!isupper(*c))
966e48354ceSNicholas Bellinger 			continue;
967e48354ceSNicholas Bellinger 
968e48354ceSNicholas Bellinger 		*c = tolower(*c);
969e48354ceSNicholas Bellinger 	}
970e48354ceSNicholas Bellinger }
971e48354ceSNicholas Bellinger 
972e48354ceSNicholas Bellinger /*
973e48354ceSNicholas Bellinger  * Processes the first Login Request..
974e48354ceSNicholas Bellinger  */
975baa4d64bSNicholas Bellinger int iscsi_target_locate_portal(
976e48354ceSNicholas Bellinger 	struct iscsi_np *np,
977e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
978e48354ceSNicholas Bellinger 	struct iscsi_login *login)
979e48354ceSNicholas Bellinger {
980e48354ceSNicholas Bellinger 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
981e48354ceSNicholas Bellinger 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
982e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
983e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
984d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = NULL;
985e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
986988e3a85SNicholas Bellinger 	struct se_node_acl *se_nacl;
987988e3a85SNicholas Bellinger 	u32 payload_length, queue_depth = 0;
988988e3a85SNicholas Bellinger 	int sessiontype = 0, ret = 0, tag_num, tag_size;
989e48354ceSNicholas Bellinger 
990d381a801SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
991bb048357SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
992d381a801SNicholas Bellinger 	iscsi_target_set_sock_callbacks(conn);
993d381a801SNicholas Bellinger 
994d381a801SNicholas Bellinger 	login->np = np;
995d381a801SNicholas Bellinger 
996e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
997e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
998e48354ceSNicholas Bellinger 
999e48354ceSNicholas Bellinger 	tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1000e48354ceSNicholas Bellinger 	if (!tmpbuf) {
1001e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for tmpbuf.\n");
1002e48354ceSNicholas Bellinger 		return -1;
1003e48354ceSNicholas Bellinger 	}
1004e48354ceSNicholas Bellinger 
1005e48354ceSNicholas Bellinger 	memcpy(tmpbuf, login->req_buf, payload_length);
1006e48354ceSNicholas Bellinger 	tmpbuf[payload_length] = '\0';
1007e48354ceSNicholas Bellinger 	start = tmpbuf;
1008e48354ceSNicholas Bellinger 	end = (start + payload_length);
1009e48354ceSNicholas Bellinger 
1010e48354ceSNicholas Bellinger 	/*
1011e48354ceSNicholas Bellinger 	 * Locate the initial keys expected from the Initiator node in
1012e48354ceSNicholas Bellinger 	 * the first login request in order to progress with the login phase.
1013e48354ceSNicholas Bellinger 	 */
1014e48354ceSNicholas Bellinger 	while (start < end) {
1015e48354ceSNicholas Bellinger 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1016e48354ceSNicholas Bellinger 			ret = -1;
1017e48354ceSNicholas Bellinger 			goto out;
1018e48354ceSNicholas Bellinger 		}
1019e48354ceSNicholas Bellinger 
1020e48354ceSNicholas Bellinger 		if (!strncmp(key, "InitiatorName", 13))
1021e48354ceSNicholas Bellinger 			i_buf = value;
1022e48354ceSNicholas Bellinger 		else if (!strncmp(key, "SessionType", 11))
1023e48354ceSNicholas Bellinger 			s_buf = value;
1024e48354ceSNicholas Bellinger 		else if (!strncmp(key, "TargetName", 10))
1025e48354ceSNicholas Bellinger 			t_buf = value;
1026e48354ceSNicholas Bellinger 
1027e48354ceSNicholas Bellinger 		start += strlen(key) + strlen(value) + 2;
1028e48354ceSNicholas Bellinger 	}
1029e48354ceSNicholas Bellinger 	/*
1030e48354ceSNicholas Bellinger 	 * See 5.3.  Login Phase.
1031e48354ceSNicholas Bellinger 	 */
1032e48354ceSNicholas Bellinger 	if (!i_buf) {
1033e48354ceSNicholas Bellinger 		pr_err("InitiatorName key not received"
1034e48354ceSNicholas Bellinger 			" in first login request.\n");
1035e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1036e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1037e48354ceSNicholas Bellinger 		ret = -1;
1038e48354ceSNicholas Bellinger 		goto out;
1039e48354ceSNicholas Bellinger 	}
1040e48354ceSNicholas Bellinger 	/*
1041e48354ceSNicholas Bellinger 	 * Convert the incoming InitiatorName to lowercase following
1042e48354ceSNicholas Bellinger 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1043e48354ceSNicholas Bellinger 	 * are NOT case sensitive.
1044e48354ceSNicholas Bellinger 	 */
1045e48354ceSNicholas Bellinger 	iscsi_initiatorname_tolower(i_buf);
1046e48354ceSNicholas Bellinger 
1047e48354ceSNicholas Bellinger 	if (!s_buf) {
1048e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1049e48354ceSNicholas Bellinger 			goto get_target;
1050e48354ceSNicholas Bellinger 
1051e48354ceSNicholas Bellinger 		pr_err("SessionType key not received"
1052e48354ceSNicholas Bellinger 			" in first login request.\n");
1053e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1054e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1055e48354ceSNicholas Bellinger 		ret = -1;
1056e48354ceSNicholas Bellinger 		goto out;
1057e48354ceSNicholas Bellinger 	}
1058e48354ceSNicholas Bellinger 
1059e48354ceSNicholas Bellinger 	/*
1060e48354ceSNicholas Bellinger 	 * Use default portal group for discovery sessions.
1061e48354ceSNicholas Bellinger 	 */
1062e48354ceSNicholas Bellinger 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1063e48354ceSNicholas Bellinger 	if (!sessiontype) {
1064e48354ceSNicholas Bellinger 		conn->tpg = iscsit_global->discovery_tpg;
1065e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1066e48354ceSNicholas Bellinger 			goto get_target;
1067e48354ceSNicholas Bellinger 
1068e48354ceSNicholas Bellinger 		sess->sess_ops->SessionType = 1;
1069e48354ceSNicholas Bellinger 		/*
1070e48354ceSNicholas Bellinger 		 * Setup crc32c modules from libcrypto
1071e48354ceSNicholas Bellinger 		 */
1072e48354ceSNicholas Bellinger 		if (iscsi_login_setup_crypto(conn) < 0) {
1073e48354ceSNicholas Bellinger 			pr_err("iscsi_login_setup_crypto() failed\n");
1074e48354ceSNicholas Bellinger 			ret = -1;
1075e48354ceSNicholas Bellinger 			goto out;
1076e48354ceSNicholas Bellinger 		}
1077e48354ceSNicholas Bellinger 		/*
1078e48354ceSNicholas Bellinger 		 * Serialize access across the discovery struct iscsi_portal_group to
1079e48354ceSNicholas Bellinger 		 * process login attempt.
1080e48354ceSNicholas Bellinger 		 */
1081e48354ceSNicholas Bellinger 		if (iscsit_access_np(np, conn->tpg) < 0) {
1082e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1083e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1084e48354ceSNicholas Bellinger 			ret = -1;
1085e48354ceSNicholas Bellinger 			goto out;
1086e48354ceSNicholas Bellinger 		}
1087e48354ceSNicholas Bellinger 		ret = 0;
1088988e3a85SNicholas Bellinger 		goto alloc_tags;
1089e48354ceSNicholas Bellinger 	}
1090e48354ceSNicholas Bellinger 
1091e48354ceSNicholas Bellinger get_target:
1092e48354ceSNicholas Bellinger 	if (!t_buf) {
1093e48354ceSNicholas Bellinger 		pr_err("TargetName key not received"
1094e48354ceSNicholas Bellinger 			" in first login request while"
1095e48354ceSNicholas Bellinger 			" SessionType=Normal.\n");
1096e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1097e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1098e48354ceSNicholas Bellinger 		ret = -1;
1099e48354ceSNicholas Bellinger 		goto out;
1100e48354ceSNicholas Bellinger 	}
1101e48354ceSNicholas Bellinger 
1102e48354ceSNicholas Bellinger 	/*
1103e48354ceSNicholas Bellinger 	 * Locate Target IQN from Storage Node.
1104e48354ceSNicholas Bellinger 	 */
1105e48354ceSNicholas Bellinger 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1106e48354ceSNicholas Bellinger 	if (!tiqn) {
1107e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target IQN: %s in"
1108e48354ceSNicholas Bellinger 			" Storage Node\n", t_buf);
1109e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1110e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1111e48354ceSNicholas Bellinger 		ret = -1;
1112e48354ceSNicholas Bellinger 		goto out;
1113e48354ceSNicholas Bellinger 	}
1114e48354ceSNicholas Bellinger 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1115e48354ceSNicholas Bellinger 
1116e48354ceSNicholas Bellinger 	/*
1117e48354ceSNicholas Bellinger 	 * Locate Target Portal Group from Storage Node.
1118e48354ceSNicholas Bellinger 	 */
1119d381a801SNicholas Bellinger 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1120e48354ceSNicholas Bellinger 	if (!conn->tpg) {
1121e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target Portal Group"
1122e48354ceSNicholas Bellinger 				" on %s\n", tiqn->tiqn);
1123e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1124e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1125e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1126e48354ceSNicholas Bellinger 		ret = -1;
1127e48354ceSNicholas Bellinger 		goto out;
1128e48354ceSNicholas Bellinger 	}
1129d381a801SNicholas Bellinger 	conn->tpg_np = tpg_np;
1130e48354ceSNicholas Bellinger 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1131e48354ceSNicholas Bellinger 	/*
1132e48354ceSNicholas Bellinger 	 * Setup crc32c modules from libcrypto
1133e48354ceSNicholas Bellinger 	 */
1134e48354ceSNicholas Bellinger 	if (iscsi_login_setup_crypto(conn) < 0) {
1135e48354ceSNicholas Bellinger 		pr_err("iscsi_login_setup_crypto() failed\n");
1136d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1137d381a801SNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1138d381a801SNicholas Bellinger 		conn->tpg = NULL;
1139e48354ceSNicholas Bellinger 		ret = -1;
1140e48354ceSNicholas Bellinger 		goto out;
1141e48354ceSNicholas Bellinger 	}
1142e48354ceSNicholas Bellinger 	/*
1143e48354ceSNicholas Bellinger 	 * Serialize access across the struct iscsi_portal_group to
1144e48354ceSNicholas Bellinger 	 * process login attempt.
1145e48354ceSNicholas Bellinger 	 */
1146e48354ceSNicholas Bellinger 	if (iscsit_access_np(np, conn->tpg) < 0) {
1147d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1148e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1149e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1150e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1151e48354ceSNicholas Bellinger 		conn->tpg = NULL;
1152d381a801SNicholas Bellinger 		ret = -1;
1153e48354ceSNicholas Bellinger 		goto out;
1154e48354ceSNicholas Bellinger 	}
1155e48354ceSNicholas Bellinger 
1156e48354ceSNicholas Bellinger 	/*
1157e48354ceSNicholas Bellinger 	 * conn->sess->node_acl will be set when the referenced
1158e48354ceSNicholas Bellinger 	 * struct iscsi_session is located from received ISID+TSIH in
1159e48354ceSNicholas Bellinger 	 * iscsi_login_non_zero_tsih_s2().
1160e48354ceSNicholas Bellinger 	 */
1161e48354ceSNicholas Bellinger 	if (!login->leading_connection) {
1162e48354ceSNicholas Bellinger 		ret = 0;
1163e48354ceSNicholas Bellinger 		goto out;
1164e48354ceSNicholas Bellinger 	}
1165e48354ceSNicholas Bellinger 
1166e48354ceSNicholas Bellinger 	/*
1167e48354ceSNicholas Bellinger 	 * This value is required in iscsi_login_zero_tsih_s2()
1168e48354ceSNicholas Bellinger 	 */
1169e48354ceSNicholas Bellinger 	sess->sess_ops->SessionType = 0;
1170e48354ceSNicholas Bellinger 
1171e48354ceSNicholas Bellinger 	/*
1172e48354ceSNicholas Bellinger 	 * Locate incoming Initiator IQN reference from Storage Node.
1173e48354ceSNicholas Bellinger 	 */
1174e48354ceSNicholas Bellinger 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1175e48354ceSNicholas Bellinger 			&conn->tpg->tpg_se_tpg, i_buf);
1176e48354ceSNicholas Bellinger 	if (!sess->se_sess->se_node_acl) {
1177e48354ceSNicholas Bellinger 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1178e48354ceSNicholas Bellinger 			" access iSCSI target portal group: %hu.\n",
1179e48354ceSNicholas Bellinger 				i_buf, conn->tpg->tpgt);
1180e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1181e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1182e48354ceSNicholas Bellinger 		ret = -1;
1183e48354ceSNicholas Bellinger 		goto out;
1184e48354ceSNicholas Bellinger 	}
1185988e3a85SNicholas Bellinger 	se_nacl = sess->se_sess->se_node_acl;
1186988e3a85SNicholas Bellinger 	queue_depth = se_nacl->queue_depth;
1187988e3a85SNicholas Bellinger 	/*
1188988e3a85SNicholas Bellinger 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1189988e3a85SNicholas Bellinger 	 * depth for non immediate commands, plus extra tags for immediate
1190988e3a85SNicholas Bellinger 	 * commands.
1191988e3a85SNicholas Bellinger 	 *
1192988e3a85SNicholas Bellinger 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1193988e3a85SNicholas Bellinger 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1194988e3a85SNicholas Bellinger 	 */
1195988e3a85SNicholas Bellinger alloc_tags:
1196988e3a85SNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
1197988e3a85SNicholas Bellinger 	tag_num += ISCSIT_EXTRA_TAGS;
1198988e3a85SNicholas Bellinger 	tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1199e48354ceSNicholas Bellinger 
1200988e3a85SNicholas Bellinger 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1201988e3a85SNicholas Bellinger 	if (ret < 0) {
1202988e3a85SNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1203988e3a85SNicholas Bellinger 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1204988e3a85SNicholas Bellinger 		ret = -1;
1205988e3a85SNicholas Bellinger 	}
1206e48354ceSNicholas Bellinger out:
1207e48354ceSNicholas Bellinger 	kfree(tmpbuf);
1208e48354ceSNicholas Bellinger 	return ret;
1209e48354ceSNicholas Bellinger }
1210e48354ceSNicholas Bellinger 
1211e48354ceSNicholas Bellinger int iscsi_target_start_negotiation(
1212e48354ceSNicholas Bellinger 	struct iscsi_login *login,
1213e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
1214e48354ceSNicholas Bellinger {
1215baa4d64bSNicholas Bellinger 	int ret;
1216e48354ceSNicholas Bellinger 
1217e48354ceSNicholas Bellinger 	ret = iscsi_target_do_login(conn, login);
1218d381a801SNicholas Bellinger 	if (!ret) {
1219d381a801SNicholas Bellinger 		if (conn->sock) {
1220d381a801SNicholas Bellinger 			struct sock *sk = conn->sock->sk;
1221e48354ceSNicholas Bellinger 
1222d381a801SNicholas Bellinger 			write_lock_bh(&sk->sk_callback_lock);
1223d381a801SNicholas Bellinger 			set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1224d381a801SNicholas Bellinger 			write_unlock_bh(&sk->sk_callback_lock);
1225d381a801SNicholas Bellinger 		}
1226d381a801SNicholas Bellinger 	} else if (ret < 0) {
1227d381a801SNicholas Bellinger 		cancel_delayed_work_sync(&conn->login_work);
1228bb048357SNicholas Bellinger 		cancel_delayed_work_sync(&conn->login_cleanup_work);
1229d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
1230d381a801SNicholas Bellinger 		iscsi_remove_failed_auth_entry(conn);
1231d381a801SNicholas Bellinger 	}
1232d381a801SNicholas Bellinger 	if (ret != 0)
1233baa4d64bSNicholas Bellinger 		iscsi_target_nego_release(conn);
1234d381a801SNicholas Bellinger 
1235e48354ceSNicholas Bellinger 	return ret;
1236e48354ceSNicholas Bellinger }
1237e48354ceSNicholas Bellinger 
1238baa4d64bSNicholas Bellinger void iscsi_target_nego_release(struct iscsi_conn *conn)
1239e48354ceSNicholas Bellinger {
1240baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1241baa4d64bSNicholas Bellinger 
1242baa4d64bSNicholas Bellinger 	if (!login)
1243baa4d64bSNicholas Bellinger 		return;
1244baa4d64bSNicholas Bellinger 
1245e48354ceSNicholas Bellinger 	kfree(login->req_buf);
1246e48354ceSNicholas Bellinger 	kfree(login->rsp_buf);
1247e48354ceSNicholas Bellinger 	kfree(login);
1248baa4d64bSNicholas Bellinger 
1249baa4d64bSNicholas Bellinger 	conn->conn_login = NULL;
1250e48354ceSNicholas Bellinger }
1251