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