1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e48354ceSNicholas Bellinger /*******************************************************************************
3e48354ceSNicholas Bellinger  * This file contains main functions related to iSCSI Parameter negotiation.
4e48354ceSNicholas Bellinger  *
54c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
6e48354ceSNicholas Bellinger  *
7e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8e48354ceSNicholas Bellinger  *
9e48354ceSNicholas Bellinger  ******************************************************************************/
10e48354ceSNicholas Bellinger 
11e48354ceSNicholas Bellinger #include <linux/ctype.h>
12e5419865SNicholas Bellinger #include <linux/kthread.h>
138dcf07beSBart Van Assche #include <linux/slab.h>
143f07c014SIngo Molnar #include <linux/sched/signal.h>
158dcf07beSBart Van Assche #include <net/sock.h>
16e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
17e48354ceSNicholas Bellinger #include <target/target_core_base.h>
18c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
19baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
20e48354ceSNicholas Bellinger 
2167f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
22e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
23e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
24e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
25e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
26e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
27e48354ceSNicholas Bellinger #include "iscsi_target.h"
28e48354ceSNicholas Bellinger #include "iscsi_target_auth.h"
29e48354ceSNicholas Bellinger 
30e48354ceSNicholas Bellinger #define MAX_LOGIN_PDUS  7
31e48354ceSNicholas Bellinger 
32e48354ceSNicholas Bellinger void convert_null_to_semi(char *buf, int len)
33e48354ceSNicholas Bellinger {
34e48354ceSNicholas Bellinger 	int i;
35e48354ceSNicholas Bellinger 
36e48354ceSNicholas Bellinger 	for (i = 0; i < len; i++)
37e48354ceSNicholas Bellinger 		if (buf[i] == '\0')
38e48354ceSNicholas Bellinger 			buf[i] = ';';
39e48354ceSNicholas Bellinger }
40e48354ceSNicholas Bellinger 
41fceb5bc7SChristoph Hellwig static int strlen_semi(char *buf)
42e48354ceSNicholas Bellinger {
43e48354ceSNicholas Bellinger 	int i = 0;
44e48354ceSNicholas Bellinger 
45e48354ceSNicholas Bellinger 	while (buf[i] != '\0') {
46e48354ceSNicholas Bellinger 		if (buf[i] == ';')
47e48354ceSNicholas Bellinger 			return i;
48e48354ceSNicholas Bellinger 		i++;
49e48354ceSNicholas Bellinger 	}
50e48354ceSNicholas Bellinger 
51e48354ceSNicholas Bellinger 	return -1;
52e48354ceSNicholas Bellinger }
53e48354ceSNicholas Bellinger 
54e48354ceSNicholas Bellinger int extract_param(
55e48354ceSNicholas Bellinger 	const char *in_buf,
56e48354ceSNicholas Bellinger 	const char *pattern,
57e48354ceSNicholas Bellinger 	unsigned int max_length,
58e48354ceSNicholas Bellinger 	char *out_buf,
59e48354ceSNicholas Bellinger 	unsigned char *type)
60e48354ceSNicholas Bellinger {
61e48354ceSNicholas Bellinger 	char *ptr;
62e48354ceSNicholas Bellinger 	int len;
63e48354ceSNicholas Bellinger 
64e48354ceSNicholas Bellinger 	if (!in_buf || !pattern || !out_buf || !type)
65e48354ceSNicholas Bellinger 		return -1;
66e48354ceSNicholas Bellinger 
67e48354ceSNicholas Bellinger 	ptr = strstr(in_buf, pattern);
68e48354ceSNicholas Bellinger 	if (!ptr)
69e48354ceSNicholas Bellinger 		return -1;
70e48354ceSNicholas Bellinger 
71e48354ceSNicholas Bellinger 	ptr = strstr(ptr, "=");
72e48354ceSNicholas Bellinger 	if (!ptr)
73e48354ceSNicholas Bellinger 		return -1;
74e48354ceSNicholas Bellinger 
75e48354ceSNicholas Bellinger 	ptr += 1;
76e48354ceSNicholas Bellinger 	if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
77e48354ceSNicholas Bellinger 		ptr += 2; /* skip 0x */
78e48354ceSNicholas Bellinger 		*type = HEX;
79e48354ceSNicholas Bellinger 	} else
80e48354ceSNicholas Bellinger 		*type = DECIMAL;
81e48354ceSNicholas Bellinger 
82e48354ceSNicholas Bellinger 	len = strlen_semi(ptr);
83e48354ceSNicholas Bellinger 	if (len < 0)
84e48354ceSNicholas Bellinger 		return -1;
85e48354ceSNicholas Bellinger 
86369653e4SEric Seppanen 	if (len >= max_length) {
875e58b029SMasanari Iida 		pr_err("Length of input: %d exceeds max_length:"
88e48354ceSNicholas Bellinger 			" %d\n", len, max_length);
89e48354ceSNicholas Bellinger 		return -1;
90e48354ceSNicholas Bellinger 	}
91e48354ceSNicholas Bellinger 	memcpy(out_buf, ptr, len);
92e48354ceSNicholas Bellinger 	out_buf[len] = '\0';
93e48354ceSNicholas Bellinger 
94e48354ceSNicholas Bellinger 	return 0;
95e48354ceSNicholas Bellinger }
96e48354ceSNicholas Bellinger 
97e48354ceSNicholas Bellinger static u32 iscsi_handle_authentication(
98e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
99e48354ceSNicholas Bellinger 	char *in_buf,
100e48354ceSNicholas Bellinger 	char *out_buf,
101e48354ceSNicholas Bellinger 	int in_length,
102e48354ceSNicholas Bellinger 	int *out_length,
103e48354ceSNicholas Bellinger 	unsigned char *authtype)
104e48354ceSNicholas Bellinger {
105e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
106e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth;
107e48354ceSNicholas Bellinger 	struct iscsi_node_acl *iscsi_nacl;
108c3e51442SNicholas Bellinger 	struct iscsi_portal_group *iscsi_tpg;
109e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
110e48354ceSNicholas Bellinger 
111e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType) {
112e48354ceSNicholas Bellinger 		/*
113e48354ceSNicholas Bellinger 		 * For SessionType=Normal
114e48354ceSNicholas Bellinger 		 */
115e48354ceSNicholas Bellinger 		se_nacl = conn->sess->se_sess->se_node_acl;
116e48354ceSNicholas Bellinger 		if (!se_nacl) {
117e48354ceSNicholas Bellinger 			pr_err("Unable to locate struct se_node_acl for"
118e48354ceSNicholas Bellinger 					" CHAP auth\n");
119e48354ceSNicholas Bellinger 			return -1;
120e48354ceSNicholas Bellinger 		}
121e48354ceSNicholas Bellinger 
122c3e51442SNicholas Bellinger 		if (se_nacl->dynamic_node_acl) {
123c3e51442SNicholas Bellinger 			iscsi_tpg = container_of(se_nacl->se_tpg,
124c3e51442SNicholas Bellinger 					struct iscsi_portal_group, tpg_se_tpg);
125c3e51442SNicholas Bellinger 
126c3e51442SNicholas Bellinger 			auth = &iscsi_tpg->tpg_demo_auth;
127c3e51442SNicholas Bellinger 		} else {
128c3e51442SNicholas Bellinger 			iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
129c3e51442SNicholas Bellinger 						  se_node_acl);
130c3e51442SNicholas Bellinger 
131b7eec2cdSAndy Grover 			auth = &iscsi_nacl->node_auth;
132c3e51442SNicholas Bellinger 		}
133e48354ceSNicholas Bellinger 	} else {
134e48354ceSNicholas Bellinger 		/*
135e48354ceSNicholas Bellinger 		 * For SessionType=Discovery
136e48354ceSNicholas Bellinger 		 */
137e48354ceSNicholas Bellinger 		auth = &iscsit_global->discovery_acl.node_auth;
138e48354ceSNicholas Bellinger 	}
139e48354ceSNicholas Bellinger 
140e48354ceSNicholas Bellinger 	if (strstr("CHAP", authtype))
141e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, "CHAP");
142e48354ceSNicholas Bellinger 	else
143e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, NONE);
144e48354ceSNicholas Bellinger 
145e48354ceSNicholas Bellinger 	if (strstr("None", authtype))
146e48354ceSNicholas Bellinger 		return 1;
147e48354ceSNicholas Bellinger 	else if (strstr("CHAP", authtype))
148e48354ceSNicholas Bellinger 		return chap_main_loop(conn, auth, in_buf, out_buf,
149e48354ceSNicholas Bellinger 				&in_length, out_length);
1508a914f32SHariprasad Kelam 	/* SRP, SPKM1, SPKM2 and KRB5 are unsupported */
151e48354ceSNicholas Bellinger 	return 2;
152e48354ceSNicholas Bellinger }
153e48354ceSNicholas Bellinger 
154e48354ceSNicholas Bellinger static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
155e48354ceSNicholas Bellinger {
156e48354ceSNicholas Bellinger 	kfree(conn->auth_protocol);
157e48354ceSNicholas Bellinger }
158e48354ceSNicholas Bellinger 
159baa4d64bSNicholas Bellinger int iscsi_target_check_login_request(
160e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
161e48354ceSNicholas Bellinger 	struct iscsi_login *login)
162e48354ceSNicholas Bellinger {
16328168905SJörn Engel 	int req_csg, req_nsg;
164e48354ceSNicholas Bellinger 	u32 payload_length;
165e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
166e48354ceSNicholas Bellinger 
167e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
168e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
169e48354ceSNicholas Bellinger 
170e48354ceSNicholas Bellinger 	switch (login_req->opcode & ISCSI_OPCODE_MASK) {
171e48354ceSNicholas Bellinger 	case ISCSI_OP_LOGIN:
172e48354ceSNicholas Bellinger 		break;
173e48354ceSNicholas Bellinger 	default:
174e48354ceSNicholas Bellinger 		pr_err("Received unknown opcode 0x%02x.\n",
175e48354ceSNicholas Bellinger 				login_req->opcode & ISCSI_OPCODE_MASK);
176e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
177e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
178e48354ceSNicholas Bellinger 		return -1;
179e48354ceSNicholas Bellinger 	}
180e48354ceSNicholas Bellinger 
181e48354ceSNicholas Bellinger 	if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
182e48354ceSNicholas Bellinger 	    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
183e48354ceSNicholas Bellinger 		pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
184e48354ceSNicholas Bellinger 			" and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
185e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
186e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
187e48354ceSNicholas Bellinger 		return -1;
188e48354ceSNicholas Bellinger 	}
189e48354ceSNicholas Bellinger 
1905d358065SAndy Grover 	req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1915d358065SAndy Grover 	req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
192e48354ceSNicholas Bellinger 
193e48354ceSNicholas Bellinger 	if (req_csg != login->current_stage) {
194e48354ceSNicholas Bellinger 		pr_err("Initiator unexpectedly changed login stage"
195e48354ceSNicholas Bellinger 			" from %d to %d, login failed.\n", login->current_stage,
196e48354ceSNicholas Bellinger 			req_csg);
197e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
198e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
199e48354ceSNicholas Bellinger 		return -1;
200e48354ceSNicholas Bellinger 	}
201e48354ceSNicholas Bellinger 
202e48354ceSNicholas Bellinger 	if ((req_nsg == 2) || (req_csg >= 2) ||
203e48354ceSNicholas Bellinger 	   ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
204e48354ceSNicholas Bellinger 	    (req_nsg <= req_csg))) {
205e48354ceSNicholas Bellinger 		pr_err("Illegal login_req->flags Combination, CSG: %d,"
206e48354ceSNicholas Bellinger 			" NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
207e48354ceSNicholas Bellinger 			req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
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 
213e48354ceSNicholas Bellinger 	if ((login_req->max_version != login->version_max) ||
214e48354ceSNicholas Bellinger 	    (login_req->min_version != login->version_min)) {
215e48354ceSNicholas Bellinger 		pr_err("Login request changed Version Max/Nin"
216e48354ceSNicholas Bellinger 			" unexpectedly to 0x%02x/0x%02x, protocol error\n",
217e48354ceSNicholas Bellinger 			login_req->max_version, login_req->min_version);
218e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
219e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
220e48354ceSNicholas Bellinger 		return -1;
221e48354ceSNicholas Bellinger 	}
222e48354ceSNicholas Bellinger 
223e48354ceSNicholas Bellinger 	if (memcmp(login_req->isid, login->isid, 6) != 0) {
224e48354ceSNicholas Bellinger 		pr_err("Login request changed ISID unexpectedly,"
225e48354ceSNicholas Bellinger 				" protocol error.\n");
226e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
227e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
228e48354ceSNicholas Bellinger 		return -1;
229e48354ceSNicholas Bellinger 	}
230e48354ceSNicholas Bellinger 
231e48354ceSNicholas Bellinger 	if (login_req->itt != login->init_task_tag) {
232e48354ceSNicholas Bellinger 		pr_err("Login request changed ITT unexpectedly to"
233e48354ceSNicholas Bellinger 			" 0x%08x, protocol error.\n", login_req->itt);
234e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
235e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
236e48354ceSNicholas Bellinger 		return -1;
237e48354ceSNicholas Bellinger 	}
238e48354ceSNicholas Bellinger 
239e48354ceSNicholas Bellinger 	if (payload_length > MAX_KEY_VALUE_PAIRS) {
240e48354ceSNicholas Bellinger 		pr_err("Login request payload exceeds default"
241e48354ceSNicholas Bellinger 			" MaxRecvDataSegmentLength: %u, protocol error.\n",
242e48354ceSNicholas Bellinger 				MAX_KEY_VALUE_PAIRS);
243e48354ceSNicholas Bellinger 		return -1;
244e48354ceSNicholas Bellinger 	}
245e48354ceSNicholas Bellinger 
246e48354ceSNicholas Bellinger 	return 0;
247e48354ceSNicholas Bellinger }
248d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_target_check_login_request);
249e48354ceSNicholas Bellinger 
250e48354ceSNicholas Bellinger static int iscsi_target_check_first_request(
251e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
252e48354ceSNicholas Bellinger 	struct iscsi_login *login)
253e48354ceSNicholas Bellinger {
254e48354ceSNicholas Bellinger 	struct iscsi_param *param = NULL;
255e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
256e48354ceSNicholas Bellinger 
257e48354ceSNicholas Bellinger 	login->first_request = 0;
258e48354ceSNicholas Bellinger 
259e48354ceSNicholas Bellinger 	list_for_each_entry(param, &conn->param_list->param_list, p_list) {
260e48354ceSNicholas Bellinger 		if (!strncmp(param->name, SESSIONTYPE, 11)) {
261e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
262e48354ceSNicholas Bellinger 				pr_err("SessionType key not received"
263e48354ceSNicholas Bellinger 					" in first login request.\n");
264e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
265e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
266e48354ceSNicholas Bellinger 				return -1;
267e48354ceSNicholas Bellinger 			}
268e48354ceSNicholas Bellinger 			if (!strncmp(param->value, DISCOVERY, 9))
269e48354ceSNicholas Bellinger 				return 0;
270e48354ceSNicholas Bellinger 		}
271e48354ceSNicholas Bellinger 
272e48354ceSNicholas Bellinger 		if (!strncmp(param->name, INITIATORNAME, 13)) {
273e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
274e48354ceSNicholas Bellinger 				if (!login->leading_connection)
275e48354ceSNicholas Bellinger 					continue;
276e48354ceSNicholas Bellinger 
277e48354ceSNicholas Bellinger 				pr_err("InitiatorName key not received"
278e48354ceSNicholas Bellinger 					" in first login request.\n");
279e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
280e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
281e48354ceSNicholas Bellinger 				return -1;
282e48354ceSNicholas Bellinger 			}
283e48354ceSNicholas Bellinger 
284e48354ceSNicholas Bellinger 			/*
285e48354ceSNicholas Bellinger 			 * For non-leading connections, double check that the
286e48354ceSNicholas Bellinger 			 * received InitiatorName matches the existing session's
287e48354ceSNicholas Bellinger 			 * struct iscsi_node_acl.
288e48354ceSNicholas Bellinger 			 */
289e48354ceSNicholas Bellinger 			if (!login->leading_connection) {
290e48354ceSNicholas Bellinger 				se_nacl = conn->sess->se_sess->se_node_acl;
291e48354ceSNicholas Bellinger 				if (!se_nacl) {
292e48354ceSNicholas Bellinger 					pr_err("Unable to locate"
293e48354ceSNicholas Bellinger 						" struct se_node_acl\n");
294e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
295e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
296e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
297e48354ceSNicholas Bellinger 					return -1;
298e48354ceSNicholas Bellinger 				}
299e48354ceSNicholas Bellinger 
300e48354ceSNicholas Bellinger 				if (strcmp(param->value,
301e48354ceSNicholas Bellinger 						se_nacl->initiatorname)) {
302e48354ceSNicholas Bellinger 					pr_err("Incorrect"
303e48354ceSNicholas Bellinger 						" InitiatorName: %s for this"
304e48354ceSNicholas Bellinger 						" iSCSI Initiator Node.\n",
305e48354ceSNicholas Bellinger 						param->value);
306e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
307e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
308e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
309e48354ceSNicholas Bellinger 					return -1;
310e48354ceSNicholas Bellinger 				}
311e48354ceSNicholas Bellinger 			}
312e48354ceSNicholas Bellinger 		}
313e48354ceSNicholas Bellinger 	}
314e48354ceSNicholas Bellinger 
315e48354ceSNicholas Bellinger 	return 0;
316e48354ceSNicholas Bellinger }
317e48354ceSNicholas Bellinger 
318e48354ceSNicholas Bellinger static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
319e48354ceSNicholas Bellinger {
320e48354ceSNicholas Bellinger 	u32 padding = 0;
321e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
322e48354ceSNicholas Bellinger 
323e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
324e48354ceSNicholas Bellinger 
325e48354ceSNicholas Bellinger 	login_rsp->opcode		= ISCSI_OP_LOGIN_RSP;
326e48354ceSNicholas Bellinger 	hton24(login_rsp->dlength, login->rsp_length);
327e48354ceSNicholas Bellinger 	memcpy(login_rsp->isid, login->isid, 6);
328e48354ceSNicholas Bellinger 	login_rsp->tsih			= cpu_to_be16(login->tsih);
32966c7db68SChristoph Hellwig 	login_rsp->itt			= login->init_task_tag;
330e48354ceSNicholas Bellinger 	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
331e48354ceSNicholas Bellinger 	login_rsp->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
332109e2381SRoland Dreier 	login_rsp->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
333e48354ceSNicholas Bellinger 
334e48354ceSNicholas Bellinger 	pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
335e48354ceSNicholas Bellinger 		" ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
33666c7db68SChristoph Hellwig 		" %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
337e48354ceSNicholas Bellinger 		ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
338e48354ceSNicholas Bellinger 		ntohl(login_rsp->statsn), login->rsp_length);
339e48354ceSNicholas Bellinger 
340e48354ceSNicholas Bellinger 	padding = ((-login->rsp_length) & 3);
341e5419865SNicholas Bellinger 	/*
342e5419865SNicholas Bellinger 	 * Before sending the last login response containing the transition
343e5419865SNicholas Bellinger 	 * bit for full-feature-phase, go ahead and start up TX/RX threads
344e5419865SNicholas Bellinger 	 * now to avoid potential resource allocation failures after the
345e5419865SNicholas Bellinger 	 * final login response has been sent.
346e5419865SNicholas Bellinger 	 */
347e5419865SNicholas Bellinger 	if (login->login_complete) {
348e5419865SNicholas Bellinger 		int rc = iscsit_start_kthreads(conn);
349e5419865SNicholas Bellinger 		if (rc) {
350e5419865SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
351e5419865SNicholas Bellinger 					    ISCSI_LOGIN_STATUS_NO_RESOURCES);
352e5419865SNicholas Bellinger 			return -1;
353e5419865SNicholas Bellinger 		}
354e5419865SNicholas Bellinger 	}
355e48354ceSNicholas Bellinger 
356baa4d64bSNicholas Bellinger 	if (conn->conn_transport->iscsit_put_login_tx(conn, login,
357e48354ceSNicholas Bellinger 					login->rsp_length + padding) < 0)
358e5419865SNicholas Bellinger 		goto err;
359e48354ceSNicholas Bellinger 
360e48354ceSNicholas Bellinger 	login->rsp_length		= 0;
361e48354ceSNicholas Bellinger 
362e48354ceSNicholas Bellinger 	return 0;
363e5419865SNicholas Bellinger 
364e5419865SNicholas Bellinger err:
365e5419865SNicholas Bellinger 	if (login->login_complete) {
366e5419865SNicholas Bellinger 		if (conn->rx_thread && conn->rx_thread_active) {
367e5419865SNicholas Bellinger 			send_sig(SIGINT, conn->rx_thread, 1);
368ca82c2bdSNicholas Bellinger 			complete(&conn->rx_login_comp);
369e5419865SNicholas Bellinger 			kthread_stop(conn->rx_thread);
370e5419865SNicholas Bellinger 		}
371e5419865SNicholas Bellinger 		if (conn->tx_thread && conn->tx_thread_active) {
372e5419865SNicholas Bellinger 			send_sig(SIGINT, conn->tx_thread, 1);
373e5419865SNicholas Bellinger 			kthread_stop(conn->tx_thread);
374e5419865SNicholas Bellinger 		}
375e5419865SNicholas Bellinger 		spin_lock(&iscsit_global->ts_bitmap_lock);
376e5419865SNicholas Bellinger 		bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
377e5419865SNicholas Bellinger 				      get_order(1));
378e5419865SNicholas Bellinger 		spin_unlock(&iscsit_global->ts_bitmap_lock);
379e5419865SNicholas Bellinger 	}
380e5419865SNicholas Bellinger 	return -1;
381e48354ceSNicholas Bellinger }
382e48354ceSNicholas Bellinger 
383676d2369SDavid S. Miller static void iscsi_target_sk_data_ready(struct sock *sk)
384d381a801SNicholas Bellinger {
385d381a801SNicholas Bellinger 	struct iscsi_conn *conn = sk->sk_user_data;
386d381a801SNicholas Bellinger 	bool rc;
387d381a801SNicholas Bellinger 
388d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
389d381a801SNicholas Bellinger 
390d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
391d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
392d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
393d381a801SNicholas Bellinger 		return;
394d381a801SNicholas Bellinger 	}
395d381a801SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
396d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
397d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
398d381a801SNicholas Bellinger 		return;
399d381a801SNicholas Bellinger 	}
400d381a801SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
401d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
402d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
403d381a801SNicholas Bellinger 		return;
404d381a801SNicholas Bellinger 	}
405d381a801SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
406d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
407d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
4081c130ae0SFlorian Westphal 		if (iscsi_target_sk_data_ready == conn->orig_data_ready)
4091c130ae0SFlorian Westphal 			return;
4101c130ae0SFlorian Westphal 		conn->orig_data_ready(sk);
411d381a801SNicholas Bellinger 		return;
412d381a801SNicholas Bellinger 	}
413d381a801SNicholas Bellinger 
414d381a801SNicholas Bellinger 	rc = schedule_delayed_work(&conn->login_work, 0);
4150bcc297eSChristophe Vu-Brugier 	if (!rc) {
416d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
417d381a801SNicholas Bellinger 			 " got false\n");
418d381a801SNicholas Bellinger 	}
419d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
420d381a801SNicholas Bellinger }
421d381a801SNicholas Bellinger 
422bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *);
423bb048357SNicholas Bellinger 
424d381a801SNicholas Bellinger static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
425d381a801SNicholas Bellinger {
426d381a801SNicholas Bellinger 	struct sock *sk;
427d381a801SNicholas Bellinger 
428d381a801SNicholas Bellinger 	if (!conn->sock)
429d381a801SNicholas Bellinger 		return;
430d381a801SNicholas Bellinger 
431d381a801SNicholas Bellinger 	sk = conn->sock->sk;
432d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
433d381a801SNicholas Bellinger 
434d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
435d381a801SNicholas Bellinger 	sk->sk_user_data = conn;
436d381a801SNicholas Bellinger 	conn->orig_data_ready = sk->sk_data_ready;
437bb048357SNicholas Bellinger 	conn->orig_state_change = sk->sk_state_change;
438d381a801SNicholas Bellinger 	sk->sk_data_ready = iscsi_target_sk_data_ready;
439bb048357SNicholas Bellinger 	sk->sk_state_change = iscsi_target_sk_state_change;
440d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
441bb048357SNicholas Bellinger 
442bb048357SNicholas Bellinger 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
443bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
444d381a801SNicholas Bellinger }
445d381a801SNicholas Bellinger 
446d381a801SNicholas Bellinger static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
447d381a801SNicholas Bellinger {
448d381a801SNicholas Bellinger 	struct sock *sk;
449d381a801SNicholas Bellinger 
450d381a801SNicholas Bellinger 	if (!conn->sock)
451d381a801SNicholas Bellinger 		return;
452d381a801SNicholas Bellinger 
453d381a801SNicholas Bellinger 	sk = conn->sock->sk;
454d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
455d381a801SNicholas Bellinger 
456d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
457d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
458d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
459d381a801SNicholas Bellinger 		return;
460d381a801SNicholas Bellinger 	}
461d381a801SNicholas Bellinger 	sk->sk_user_data = NULL;
462d381a801SNicholas Bellinger 	sk->sk_data_ready = conn->orig_data_ready;
463bb048357SNicholas Bellinger 	sk->sk_state_change = conn->orig_state_change;
464d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
465bb048357SNicholas Bellinger 
466bb048357SNicholas Bellinger 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
467bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
468d381a801SNicholas Bellinger }
469d381a801SNicholas Bellinger 
470d381a801SNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
471d381a801SNicholas Bellinger 
47225cdda95SNicholas Bellinger static bool __iscsi_target_sk_check_close(struct sock *sk)
473d381a801SNicholas Bellinger {
474d381a801SNicholas Bellinger 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
47525cdda95SNicholas Bellinger 		pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
476df2de6f2SHou Pu 			"returning TRUE\n");
47725cdda95SNicholas Bellinger 		return true;
47825cdda95SNicholas Bellinger 	}
479d381a801SNicholas Bellinger 	return false;
480d381a801SNicholas Bellinger }
48125cdda95SNicholas Bellinger 
48225cdda95SNicholas Bellinger static bool iscsi_target_sk_check_close(struct iscsi_conn *conn)
48325cdda95SNicholas Bellinger {
48425cdda95SNicholas Bellinger 	bool state = false;
48525cdda95SNicholas Bellinger 
48625cdda95SNicholas Bellinger 	if (conn->sock) {
48725cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
48825cdda95SNicholas Bellinger 
48925cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
49025cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
49125cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
49225cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
49325cdda95SNicholas Bellinger 	}
49425cdda95SNicholas Bellinger 	return state;
49525cdda95SNicholas Bellinger }
49625cdda95SNicholas Bellinger 
49725cdda95SNicholas Bellinger static bool iscsi_target_sk_check_flag(struct iscsi_conn *conn, unsigned int flag)
49825cdda95SNicholas Bellinger {
49925cdda95SNicholas Bellinger 	bool state = false;
50025cdda95SNicholas Bellinger 
50125cdda95SNicholas Bellinger 	if (conn->sock) {
50225cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
50325cdda95SNicholas Bellinger 
50425cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
50525cdda95SNicholas Bellinger 		state = test_bit(flag, &conn->login_flags);
50625cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
50725cdda95SNicholas Bellinger 	}
50825cdda95SNicholas Bellinger 	return state;
50925cdda95SNicholas Bellinger }
51025cdda95SNicholas Bellinger 
51125cdda95SNicholas Bellinger static bool iscsi_target_sk_check_and_clear(struct iscsi_conn *conn, unsigned int flag)
51225cdda95SNicholas Bellinger {
51325cdda95SNicholas Bellinger 	bool state = false;
51425cdda95SNicholas Bellinger 
51525cdda95SNicholas Bellinger 	if (conn->sock) {
51625cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
51725cdda95SNicholas Bellinger 
51825cdda95SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
51925cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
52025cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
52125cdda95SNicholas Bellinger 		if (!state)
52225cdda95SNicholas Bellinger 			clear_bit(flag, &conn->login_flags);
52325cdda95SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
52425cdda95SNicholas Bellinger 	}
52525cdda95SNicholas Bellinger 	return state;
526d381a801SNicholas Bellinger }
527d381a801SNicholas Bellinger 
528d381a801SNicholas Bellinger static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
529d381a801SNicholas Bellinger {
530d381a801SNicholas Bellinger 	bool zero_tsih = login->zero_tsih;
531d381a801SNicholas Bellinger 
532d381a801SNicholas Bellinger 	iscsi_remove_failed_auth_entry(conn);
533d381a801SNicholas Bellinger 	iscsi_target_nego_release(conn);
534ed43ffeaSHou Pu 	iscsi_target_login_sess_out(conn, zero_tsih, true);
535d381a801SNicholas Bellinger }
536d381a801SNicholas Bellinger 
537f7c9564aSKees Cook struct conn_timeout {
538f7c9564aSKees Cook 	struct timer_list timer;
539f7c9564aSKees Cook 	struct iscsi_conn *conn;
540f7c9564aSKees Cook };
541f7c9564aSKees Cook 
542f7c9564aSKees Cook static void iscsi_target_login_timeout(struct timer_list *t)
543d381a801SNicholas Bellinger {
544f7c9564aSKees Cook 	struct conn_timeout *timeout = from_timer(timeout, t, timer);
545f7c9564aSKees Cook 	struct iscsi_conn *conn = timeout->conn;
546d381a801SNicholas Bellinger 
547d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
548d381a801SNicholas Bellinger 
549d381a801SNicholas Bellinger 	if (conn->login_kworker) {
550d381a801SNicholas Bellinger 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
551d381a801SNicholas Bellinger 			 conn->login_kworker->comm, conn->login_kworker->pid);
552d381a801SNicholas Bellinger 		send_sig(SIGINT, conn->login_kworker, 1);
553d381a801SNicholas Bellinger 	}
554d381a801SNicholas Bellinger }
555d381a801SNicholas Bellinger 
556d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work)
557d381a801SNicholas Bellinger {
558d381a801SNicholas Bellinger 	struct iscsi_conn *conn = container_of(work,
559d381a801SNicholas Bellinger 				struct iscsi_conn, login_work.work);
560d381a801SNicholas Bellinger 	struct iscsi_login *login = conn->login;
561d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
562d381a801SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
563d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
564f7c9564aSKees Cook 	struct conn_timeout timeout;
565d381a801SNicholas Bellinger 	int rc, zero_tsih = login->zero_tsih;
566d381a801SNicholas Bellinger 	bool state;
567d381a801SNicholas Bellinger 
568d381a801SNicholas Bellinger 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
569d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
57025cdda95SNicholas Bellinger 	/*
57125cdda95SNicholas Bellinger 	 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
57225cdda95SNicholas Bellinger 	 * before initial PDU processing in iscsi_target_start_negotiation()
57325cdda95SNicholas Bellinger 	 * has completed, go ahead and retry until it's cleared.
57425cdda95SNicholas Bellinger 	 *
57525cdda95SNicholas Bellinger 	 * Otherwise if the TCP connection drops while this is occuring,
57625cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() will detect the failure, call
57725cdda95SNicholas Bellinger 	 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
57825cdda95SNicholas Bellinger 	 * remaining iscsi connection resources from iscsi_np process context.
57925cdda95SNicholas Bellinger 	 */
58025cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
58125cdda95SNicholas Bellinger 		schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
58225cdda95SNicholas Bellinger 		return;
58325cdda95SNicholas Bellinger 	}
584d381a801SNicholas Bellinger 
585d381a801SNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
586d381a801SNicholas Bellinger 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
587d381a801SNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
588d381a801SNicholas Bellinger 
5890bcc297eSChristophe Vu-Brugier 	if (!state) {
590d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
59125cdda95SNicholas Bellinger 		goto err;
592d381a801SNicholas Bellinger 	}
593d381a801SNicholas Bellinger 
59425cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_close(conn)) {
595d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
59625cdda95SNicholas Bellinger 		goto err;
597d381a801SNicholas Bellinger 	}
598d381a801SNicholas Bellinger 
599d381a801SNicholas Bellinger 	conn->login_kworker = current;
600d381a801SNicholas Bellinger 	allow_signal(SIGINT);
601d381a801SNicholas Bellinger 
602f7c9564aSKees Cook 	timeout.conn = conn;
603f7c9564aSKees Cook 	timer_setup_on_stack(&timeout.timer, iscsi_target_login_timeout, 0);
604f7c9564aSKees Cook 	mod_timer(&timeout.timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
605f7c9564aSKees Cook 	pr_debug("Starting login timer for %s/%d\n", current->comm, current->pid);
606d381a801SNicholas Bellinger 
607d381a801SNicholas Bellinger 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
608f7c9564aSKees Cook 	del_timer_sync(&timeout.timer);
609f7c9564aSKees Cook 	destroy_timer_on_stack(&timeout.timer);
610d381a801SNicholas Bellinger 	flush_signals(current);
611d381a801SNicholas Bellinger 	conn->login_kworker = NULL;
612d381a801SNicholas Bellinger 
61325cdda95SNicholas Bellinger 	if (rc < 0)
61425cdda95SNicholas Bellinger 		goto err;
615d381a801SNicholas Bellinger 
616d381a801SNicholas Bellinger 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
617d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
618d381a801SNicholas Bellinger 
6194e108d4fSHou Pu 	/*
6204e108d4fSHou Pu 	 * LOGIN_FLAGS_READ_ACTIVE is cleared so that sk_data_ready
6214e108d4fSHou Pu 	 * could be triggered again after this.
6224e108d4fSHou Pu 	 *
6234e108d4fSHou Pu 	 * LOGIN_FLAGS_WRITE_ACTIVE is cleared after we successfully
6244e108d4fSHou Pu 	 * process a login PDU, so that sk_state_chage can do login
6254e108d4fSHou Pu 	 * cleanup as needed if the socket is closed. If a delayed work is
6264e108d4fSHou Pu 	 * ongoing (LOGIN_FLAGS_WRITE_ACTIVE or LOGIN_FLAGS_READ_ACTIVE),
6274e108d4fSHou Pu 	 * sk_state_change will leave the cleanup to the delayed work or
6284e108d4fSHou Pu 	 * it will schedule a delayed work to do cleanup.
6294e108d4fSHou Pu 	 */
6304e108d4fSHou Pu 	if (conn->sock) {
6314e108d4fSHou Pu 		struct sock *sk = conn->sock->sk;
6324e108d4fSHou Pu 
6334e108d4fSHou Pu 		write_lock_bh(&sk->sk_callback_lock);
6344e108d4fSHou Pu 		if (!test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags)) {
6354e108d4fSHou Pu 			clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
6364e108d4fSHou Pu 			set_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags);
6374e108d4fSHou Pu 		}
6384e108d4fSHou Pu 		write_unlock_bh(&sk->sk_callback_lock);
6394e108d4fSHou Pu 	}
6404e108d4fSHou Pu 
641d381a801SNicholas Bellinger 	rc = iscsi_target_do_login(conn, login);
642d381a801SNicholas Bellinger 	if (rc < 0) {
64325cdda95SNicholas Bellinger 		goto err;
644d381a801SNicholas Bellinger 	} else if (!rc) {
6454e108d4fSHou Pu 		if (iscsi_target_sk_check_and_clear(conn,
6464e108d4fSHou Pu 						    LOGIN_FLAGS_WRITE_ACTIVE))
64725cdda95SNicholas Bellinger 			goto err;
648d381a801SNicholas Bellinger 	} else if (rc == 1) {
6494e108d4fSHou Pu 		cancel_delayed_work(&conn->login_work);
650d381a801SNicholas Bellinger 		iscsi_target_nego_release(conn);
651d381a801SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
652d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
653d381a801SNicholas Bellinger 	}
65425cdda95SNicholas Bellinger 	return;
65525cdda95SNicholas Bellinger 
65625cdda95SNicholas Bellinger err:
65725cdda95SNicholas Bellinger 	iscsi_target_restore_sock_callbacks(conn);
6584e108d4fSHou Pu 	cancel_delayed_work(&conn->login_work);
65925cdda95SNicholas Bellinger 	iscsi_target_login_drop(conn, login);
66025cdda95SNicholas Bellinger 	iscsit_deaccess_np(np, tpg, tpg_np);
661d381a801SNicholas Bellinger }
662d381a801SNicholas Bellinger 
663bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk)
664bb048357SNicholas Bellinger {
665bb048357SNicholas Bellinger 	struct iscsi_conn *conn;
666bb048357SNicholas Bellinger 	void (*orig_state_change)(struct sock *);
667bb048357SNicholas Bellinger 	bool state;
668bb048357SNicholas Bellinger 
669bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_state_change\n");
670bb048357SNicholas Bellinger 
671bb048357SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
672bb048357SNicholas Bellinger 	conn = sk->sk_user_data;
673bb048357SNicholas Bellinger 	if (!conn) {
674bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
675bb048357SNicholas Bellinger 		return;
676bb048357SNicholas Bellinger 	}
677bb048357SNicholas Bellinger 	orig_state_change = conn->orig_state_change;
678bb048357SNicholas Bellinger 
679bb048357SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
680bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
681bb048357SNicholas Bellinger 			 conn);
682bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
683bb048357SNicholas Bellinger 		orig_state_change(sk);
684bb048357SNicholas Bellinger 		return;
685bb048357SNicholas Bellinger 	}
68625cdda95SNicholas Bellinger 	state = __iscsi_target_sk_check_close(sk);
68725cdda95SNicholas Bellinger 	pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
68825cdda95SNicholas Bellinger 
6894e108d4fSHou Pu 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags) ||
6904e108d4fSHou Pu 	    test_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags)) {
6914e108d4fSHou Pu 		pr_debug("Got LOGIN_FLAGS_{READ|WRITE}_ACTIVE=1"
6924e108d4fSHou Pu 			 " sk_state_change conn: %p\n", conn);
69325cdda95SNicholas Bellinger 		if (state)
69425cdda95SNicholas Bellinger 			set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
695bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
696bb048357SNicholas Bellinger 		orig_state_change(sk);
697bb048357SNicholas Bellinger 		return;
698bb048357SNicholas Bellinger 	}
69925cdda95SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
700bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
701bb048357SNicholas Bellinger 			 conn);
702bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
703bb048357SNicholas Bellinger 		orig_state_change(sk);
704bb048357SNicholas Bellinger 		return;
705bb048357SNicholas Bellinger 	}
70625cdda95SNicholas Bellinger 	/*
70725cdda95SNicholas Bellinger 	 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
70825cdda95SNicholas Bellinger 	 * but only queue conn->login_work -> iscsi_target_do_login_rx()
70925cdda95SNicholas Bellinger 	 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
71025cdda95SNicholas Bellinger 	 *
71125cdda95SNicholas Bellinger 	 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
71225cdda95SNicholas Bellinger 	 * will detect the dropped TCP connection from delayed workqueue context.
71325cdda95SNicholas Bellinger 	 *
71425cdda95SNicholas Bellinger 	 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
71525cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
71625cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
71725cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
71825cdda95SNicholas Bellinger 	 * dropped TCP connection in iscsi_np process context, and cleaning up
71925cdda95SNicholas Bellinger 	 * the remaining iscsi connection resources.
72025cdda95SNicholas Bellinger 	 */
72125cdda95SNicholas Bellinger 	if (state) {
72225cdda95SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_change got failed state\n");
72325cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
72425cdda95SNicholas Bellinger 		state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
725bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
726bb048357SNicholas Bellinger 
72725cdda95SNicholas Bellinger 		orig_state_change(sk);
728bb048357SNicholas Bellinger 
72925cdda95SNicholas Bellinger 		if (!state)
73025cdda95SNicholas Bellinger 			schedule_delayed_work(&conn->login_work, 0);
731bb048357SNicholas Bellinger 		return;
732bb048357SNicholas Bellinger 	}
73325cdda95SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
73425cdda95SNicholas Bellinger 
735bb048357SNicholas Bellinger 	orig_state_change(sk);
736bb048357SNicholas Bellinger }
737bb048357SNicholas Bellinger 
738e48354ceSNicholas Bellinger /*
739e48354ceSNicholas Bellinger  *	NOTE: We check for existing sessions or connections AFTER the initiator
740e48354ceSNicholas Bellinger  *	has been successfully authenticated in order to protect against faked
741e48354ceSNicholas Bellinger  *	ISID/TSIH combinations.
742e48354ceSNicholas Bellinger  */
743e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances(
744e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
745e48354ceSNicholas Bellinger 	struct iscsi_login *login)
746e48354ceSNicholas Bellinger {
747e48354ceSNicholas Bellinger 	if (login->checked_for_existing)
748e48354ceSNicholas Bellinger 		return 0;
749e48354ceSNicholas Bellinger 
750e48354ceSNicholas Bellinger 	login->checked_for_existing = 1;
751e48354ceSNicholas Bellinger 
752e48354ceSNicholas Bellinger 	if (!login->tsih)
753e48354ceSNicholas Bellinger 		return iscsi_check_for_session_reinstatement(conn);
754e48354ceSNicholas Bellinger 	else
755e48354ceSNicholas Bellinger 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
756e48354ceSNicholas Bellinger 				login->initial_exp_statsn);
757e48354ceSNicholas Bellinger }
758e48354ceSNicholas Bellinger 
759e48354ceSNicholas Bellinger static int iscsi_target_do_authentication(
760e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
761e48354ceSNicholas Bellinger 	struct iscsi_login *login)
762e48354ceSNicholas Bellinger {
763e48354ceSNicholas Bellinger 	int authret;
764e48354ceSNicholas Bellinger 	u32 payload_length;
765e48354ceSNicholas Bellinger 	struct iscsi_param *param;
766e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
767e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
768e48354ceSNicholas Bellinger 
769e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
770e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
771e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
772e48354ceSNicholas Bellinger 
773e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
774e48354ceSNicholas Bellinger 	if (!param)
775e48354ceSNicholas Bellinger 		return -1;
776e48354ceSNicholas Bellinger 
777e48354ceSNicholas Bellinger 	authret = iscsi_handle_authentication(
778e48354ceSNicholas Bellinger 			conn,
779e48354ceSNicholas Bellinger 			login->req_buf,
780e48354ceSNicholas Bellinger 			login->rsp_buf,
781e48354ceSNicholas Bellinger 			payload_length,
782e48354ceSNicholas Bellinger 			&login->rsp_length,
783e48354ceSNicholas Bellinger 			param->value);
784e48354ceSNicholas Bellinger 	switch (authret) {
785e48354ceSNicholas Bellinger 	case 0:
786e48354ceSNicholas Bellinger 		pr_debug("Received OK response"
787e48354ceSNicholas Bellinger 		" from LIO Authentication, continuing.\n");
788e48354ceSNicholas Bellinger 		break;
789e48354ceSNicholas Bellinger 	case 1:
790e48354ceSNicholas Bellinger 		pr_debug("iSCSI security negotiation"
791bfb9035cSJoe Perches 			" completed successfully.\n");
792e48354ceSNicholas Bellinger 		login->auth_complete = 1;
793e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
794e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
795e48354ceSNicholas Bellinger 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
796e48354ceSNicholas Bellinger 					     ISCSI_FLAG_LOGIN_TRANSIT);
797e48354ceSNicholas Bellinger 			login->current_stage = 1;
798e48354ceSNicholas Bellinger 		}
799e48354ceSNicholas Bellinger 		return iscsi_target_check_for_existing_instances(
800e48354ceSNicholas Bellinger 				conn, login);
801e48354ceSNicholas Bellinger 	case 2:
802e48354ceSNicholas Bellinger 		pr_err("Security negotiation"
803e48354ceSNicholas Bellinger 			" failed.\n");
804e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
805e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
806e48354ceSNicholas Bellinger 		return -1;
807e48354ceSNicholas Bellinger 	default:
808e48354ceSNicholas Bellinger 		pr_err("Received unknown error %d from LIO"
809e48354ceSNicholas Bellinger 				" Authentication\n", authret);
810e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
811e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
812e48354ceSNicholas Bellinger 		return -1;
813e48354ceSNicholas Bellinger 	}
814e48354ceSNicholas Bellinger 
815e48354ceSNicholas Bellinger 	return 0;
816e48354ceSNicholas Bellinger }
817e48354ceSNicholas Bellinger 
818e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero(
819e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
820e48354ceSNicholas Bellinger 	struct iscsi_login *login)
821e48354ceSNicholas Bellinger {
822e48354ceSNicholas Bellinger 	int ret;
823e48354ceSNicholas Bellinger 	u32 payload_length;
824e48354ceSNicholas Bellinger 	struct iscsi_param *param;
825e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
826e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
827e48354ceSNicholas Bellinger 
828e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
829e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
830e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
831e48354ceSNicholas Bellinger 
832e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
833e48354ceSNicholas Bellinger 	if (!param)
834e48354ceSNicholas Bellinger 		return -1;
835e48354ceSNicholas Bellinger 
836e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
837e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
838e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
839e48354ceSNicholas Bellinger 			login->req_buf,
840e48354ceSNicholas Bellinger 			payload_length,
8419977bb18SNicholas Bellinger 			conn);
842e48354ceSNicholas Bellinger 	if (ret < 0)
843e48354ceSNicholas Bellinger 		return -1;
844e48354ceSNicholas Bellinger 
845e48354ceSNicholas Bellinger 	if (ret > 0) {
846e48354ceSNicholas Bellinger 		if (login->auth_complete) {
847e48354ceSNicholas Bellinger 			pr_err("Initiator has already been"
848e48354ceSNicholas Bellinger 				" successfully authenticated, but is still"
849e48354ceSNicholas Bellinger 				" sending %s keys.\n", param->value);
850e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
851e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_INIT_ERR);
852e48354ceSNicholas Bellinger 			return -1;
853e48354ceSNicholas Bellinger 		}
854e48354ceSNicholas Bellinger 
855e48354ceSNicholas Bellinger 		goto do_auth;
85691f0abfdSNicholas Bellinger 	} else if (!payload_length) {
85791f0abfdSNicholas Bellinger 		pr_err("Initiator sent zero length security payload,"
85891f0abfdSNicholas Bellinger 		       " login failed\n");
85991f0abfdSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
86091f0abfdSNicholas Bellinger 				    ISCSI_LOGIN_STATUS_AUTH_FAILED);
86191f0abfdSNicholas Bellinger 		return -1;
862e48354ceSNicholas Bellinger 	}
863e48354ceSNicholas Bellinger 
864e48354ceSNicholas Bellinger 	if (login->first_request)
865e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
866e48354ceSNicholas Bellinger 			return -1;
867e48354ceSNicholas Bellinger 
868e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
869e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
870e48354ceSNicholas Bellinger 			SENDER_TARGET,
871e48354ceSNicholas Bellinger 			login->rsp_buf,
872e48354ceSNicholas Bellinger 			&login->rsp_length,
873138d351eSNicholas Bellinger 			conn->param_list,
874138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
875e48354ceSNicholas Bellinger 	if (ret < 0)
876e48354ceSNicholas Bellinger 		return -1;
877e48354ceSNicholas Bellinger 
878e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
87960bfcf8eSAndy Grover 		if (conn->tpg->tpg_attrib.authentication &&
880e48354ceSNicholas Bellinger 		    !strncmp(param->value, NONE, 4)) {
881e48354ceSNicholas Bellinger 			pr_err("Initiator sent AuthMethod=None but"
882e48354ceSNicholas Bellinger 				" Target is enforcing iSCSI Authentication,"
883e48354ceSNicholas Bellinger 					" login failed.\n");
884e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
885e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_AUTH_FAILED);
886e48354ceSNicholas Bellinger 			return -1;
887e48354ceSNicholas Bellinger 		}
888e48354ceSNicholas Bellinger 
88960bfcf8eSAndy Grover 		if (conn->tpg->tpg_attrib.authentication &&
890e48354ceSNicholas Bellinger 		    !login->auth_complete)
891e48354ceSNicholas Bellinger 			return 0;
892e48354ceSNicholas Bellinger 
893e48354ceSNicholas Bellinger 		if (strncmp(param->value, NONE, 4) && !login->auth_complete)
894e48354ceSNicholas Bellinger 			return 0;
895e48354ceSNicholas Bellinger 
896e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
897e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
898e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
899e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
900e48354ceSNicholas Bellinger 			login->current_stage = 1;
901e48354ceSNicholas Bellinger 		}
902e48354ceSNicholas Bellinger 	}
903e48354ceSNicholas Bellinger 
904e48354ceSNicholas Bellinger 	return 0;
905e48354ceSNicholas Bellinger do_auth:
906e48354ceSNicholas Bellinger 	return iscsi_target_do_authentication(conn, login);
907e48354ceSNicholas Bellinger }
908e48354ceSNicholas Bellinger 
909e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
910e48354ceSNicholas Bellinger {
911e48354ceSNicholas Bellinger 	int ret;
912e48354ceSNicholas Bellinger 	u32 payload_length;
913e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
914e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
915e48354ceSNicholas Bellinger 
916e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
917e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
918e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
919e48354ceSNicholas Bellinger 
920e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
921e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
922e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
923e48354ceSNicholas Bellinger 			login->req_buf,
924e48354ceSNicholas Bellinger 			payload_length,
9259977bb18SNicholas Bellinger 			conn);
9261c5c12c6SRoland Dreier 	if (ret < 0) {
9271c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
9281c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
929e48354ceSNicholas Bellinger 		return -1;
9301c5c12c6SRoland Dreier 	}
931e48354ceSNicholas Bellinger 
932e48354ceSNicholas Bellinger 	if (login->first_request)
933e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
934e48354ceSNicholas Bellinger 			return -1;
935e48354ceSNicholas Bellinger 
936e48354ceSNicholas Bellinger 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
937e48354ceSNicholas Bellinger 		return -1;
938e48354ceSNicholas Bellinger 
939e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
940e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
941e48354ceSNicholas Bellinger 			SENDER_TARGET,
942e48354ceSNicholas Bellinger 			login->rsp_buf,
943e48354ceSNicholas Bellinger 			&login->rsp_length,
944138d351eSNicholas Bellinger 			conn->param_list,
945138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
9461c5c12c6SRoland Dreier 	if (ret < 0) {
9471c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
9481c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
949e48354ceSNicholas Bellinger 		return -1;
9501c5c12c6SRoland Dreier 	}
951e48354ceSNicholas Bellinger 
952e48354ceSNicholas Bellinger 	if (!login->auth_complete &&
95360bfcf8eSAndy Grover 	     conn->tpg->tpg_attrib.authentication) {
954e48354ceSNicholas Bellinger 		pr_err("Initiator is requesting CSG: 1, has not been"
955e48354ceSNicholas Bellinger 			 " successfully authenticated, and the Target is"
956e48354ceSNicholas Bellinger 			" enforcing iSCSI Authentication, login failed.\n");
957e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
958e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
959e48354ceSNicholas Bellinger 		return -1;
960e48354ceSNicholas Bellinger 	}
961e48354ceSNicholas Bellinger 
962e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list))
963e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
964e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
965e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
966e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
967e48354ceSNicholas Bellinger 
968e48354ceSNicholas Bellinger 	return 0;
969e48354ceSNicholas Bellinger }
970e48354ceSNicholas Bellinger 
971e48354ceSNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
972e48354ceSNicholas Bellinger {
973e48354ceSNicholas Bellinger 	int pdu_count = 0;
974e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
975e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
976e48354ceSNicholas Bellinger 
977e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
978e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
979e48354ceSNicholas Bellinger 
980e48354ceSNicholas Bellinger 	while (1) {
981e48354ceSNicholas Bellinger 		if (++pdu_count > MAX_LOGIN_PDUS) {
982e48354ceSNicholas Bellinger 			pr_err("MAX_LOGIN_PDUS count reached.\n");
983e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
984e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
985e48354ceSNicholas Bellinger 			return -1;
986e48354ceSNicholas Bellinger 		}
987e48354ceSNicholas Bellinger 
9885d358065SAndy Grover 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
989e48354ceSNicholas Bellinger 		case 0:
9905d358065SAndy Grover 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
991e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
992e48354ceSNicholas Bellinger 				return -1;
993e48354ceSNicholas Bellinger 			break;
994e48354ceSNicholas Bellinger 		case 1:
995e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
996e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_one(conn, login) < 0)
997e48354ceSNicholas Bellinger 				return -1;
998e48354ceSNicholas Bellinger 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
99925cdda95SNicholas Bellinger 				/*
100025cdda95SNicholas Bellinger 				 * Check to make sure the TCP connection has not
100125cdda95SNicholas Bellinger 				 * dropped asynchronously while session reinstatement
100225cdda95SNicholas Bellinger 				 * was occuring in this kthread context, before
100325cdda95SNicholas Bellinger 				 * transitioning to full feature phase operation.
100425cdda95SNicholas Bellinger 				 */
100525cdda95SNicholas Bellinger 				if (iscsi_target_sk_check_close(conn))
100625cdda95SNicholas Bellinger 					return -1;
100725cdda95SNicholas Bellinger 
1008e48354ceSNicholas Bellinger 				login->tsih = conn->sess->tsih;
1009baa4d64bSNicholas Bellinger 				login->login_complete = 1;
1010d381a801SNicholas Bellinger 				iscsi_target_restore_sock_callbacks(conn);
1011e48354ceSNicholas Bellinger 				if (iscsi_target_do_tx_login_io(conn,
1012e48354ceSNicholas Bellinger 						login) < 0)
1013e48354ceSNicholas Bellinger 					return -1;
1014d381a801SNicholas Bellinger 				return 1;
1015e48354ceSNicholas Bellinger 			}
1016e48354ceSNicholas Bellinger 			break;
1017e48354ceSNicholas Bellinger 		default:
1018e48354ceSNicholas Bellinger 			pr_err("Illegal CSG: %d received from"
1019e48354ceSNicholas Bellinger 				" Initiator, protocol error.\n",
10205d358065SAndy Grover 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
1021e48354ceSNicholas Bellinger 			break;
1022e48354ceSNicholas Bellinger 		}
1023e48354ceSNicholas Bellinger 
1024ea3a179aSNicholas Bellinger 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
1025e48354ceSNicholas Bellinger 			return -1;
1026e48354ceSNicholas Bellinger 
1027e48354ceSNicholas Bellinger 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1028e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1029e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1030e48354ceSNicholas Bellinger 		}
1031d381a801SNicholas Bellinger 		break;
1032e48354ceSNicholas Bellinger 	}
1033e48354ceSNicholas Bellinger 
1034e48354ceSNicholas Bellinger 	return 0;
1035e48354ceSNicholas Bellinger }
1036e48354ceSNicholas Bellinger 
1037e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower(
1038e48354ceSNicholas Bellinger 	char *param_buf)
1039e48354ceSNicholas Bellinger {
1040e48354ceSNicholas Bellinger 	char *c;
1041e48354ceSNicholas Bellinger 	u32 iqn_size = strlen(param_buf), i;
1042e48354ceSNicholas Bellinger 
1043e48354ceSNicholas Bellinger 	for (i = 0; i < iqn_size; i++) {
10448359cf43SJörn Engel 		c = &param_buf[i];
1045e48354ceSNicholas Bellinger 		if (!isupper(*c))
1046e48354ceSNicholas Bellinger 			continue;
1047e48354ceSNicholas Bellinger 
1048e48354ceSNicholas Bellinger 		*c = tolower(*c);
1049e48354ceSNicholas Bellinger 	}
1050e48354ceSNicholas Bellinger }
1051e48354ceSNicholas Bellinger 
1052e48354ceSNicholas Bellinger /*
1053e48354ceSNicholas Bellinger  * Processes the first Login Request..
1054e48354ceSNicholas Bellinger  */
1055baa4d64bSNicholas Bellinger int iscsi_target_locate_portal(
1056e48354ceSNicholas Bellinger 	struct iscsi_np *np,
1057e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
1058e48354ceSNicholas Bellinger 	struct iscsi_login *login)
1059e48354ceSNicholas Bellinger {
1060e48354ceSNicholas Bellinger 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1061e48354ceSNicholas Bellinger 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1062e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
1063e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1064d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = NULL;
1065e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
1066988e3a85SNicholas Bellinger 	struct se_node_acl *se_nacl;
1067988e3a85SNicholas Bellinger 	u32 payload_length, queue_depth = 0;
1068988e3a85SNicholas Bellinger 	int sessiontype = 0, ret = 0, tag_num, tag_size;
1069e48354ceSNicholas Bellinger 
1070d381a801SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1071d381a801SNicholas Bellinger 	iscsi_target_set_sock_callbacks(conn);
1072d381a801SNicholas Bellinger 
1073d381a801SNicholas Bellinger 	login->np = np;
1074d381a801SNicholas Bellinger 
1075e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1076e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
1077e48354ceSNicholas Bellinger 
10786235bef6SYang Yingliang 	tmpbuf = kmemdup_nul(login->req_buf, payload_length, GFP_KERNEL);
1079e48354ceSNicholas Bellinger 	if (!tmpbuf) {
1080e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for tmpbuf.\n");
1081e48354ceSNicholas Bellinger 		return -1;
1082e48354ceSNicholas Bellinger 	}
1083e48354ceSNicholas Bellinger 
1084e48354ceSNicholas Bellinger 	start = tmpbuf;
1085e48354ceSNicholas Bellinger 	end = (start + payload_length);
1086e48354ceSNicholas Bellinger 
1087e48354ceSNicholas Bellinger 	/*
1088e48354ceSNicholas Bellinger 	 * Locate the initial keys expected from the Initiator node in
1089e48354ceSNicholas Bellinger 	 * the first login request in order to progress with the login phase.
1090e48354ceSNicholas Bellinger 	 */
1091e48354ceSNicholas Bellinger 	while (start < end) {
1092e48354ceSNicholas Bellinger 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1093e48354ceSNicholas Bellinger 			ret = -1;
1094e48354ceSNicholas Bellinger 			goto out;
1095e48354ceSNicholas Bellinger 		}
1096e48354ceSNicholas Bellinger 
1097e48354ceSNicholas Bellinger 		if (!strncmp(key, "InitiatorName", 13))
1098e48354ceSNicholas Bellinger 			i_buf = value;
1099e48354ceSNicholas Bellinger 		else if (!strncmp(key, "SessionType", 11))
1100e48354ceSNicholas Bellinger 			s_buf = value;
1101e48354ceSNicholas Bellinger 		else if (!strncmp(key, "TargetName", 10))
1102e48354ceSNicholas Bellinger 			t_buf = value;
1103e48354ceSNicholas Bellinger 
1104e48354ceSNicholas Bellinger 		start += strlen(key) + strlen(value) + 2;
1105e48354ceSNicholas Bellinger 	}
1106e48354ceSNicholas Bellinger 	/*
1107e48354ceSNicholas Bellinger 	 * See 5.3.  Login Phase.
1108e48354ceSNicholas Bellinger 	 */
1109e48354ceSNicholas Bellinger 	if (!i_buf) {
1110e48354ceSNicholas Bellinger 		pr_err("InitiatorName key not received"
1111e48354ceSNicholas Bellinger 			" in first login request.\n");
1112e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1113e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1114e48354ceSNicholas Bellinger 		ret = -1;
1115e48354ceSNicholas Bellinger 		goto out;
1116e48354ceSNicholas Bellinger 	}
1117e48354ceSNicholas Bellinger 	/*
1118e48354ceSNicholas Bellinger 	 * Convert the incoming InitiatorName to lowercase following
1119e48354ceSNicholas Bellinger 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1120e48354ceSNicholas Bellinger 	 * are NOT case sensitive.
1121e48354ceSNicholas Bellinger 	 */
1122e48354ceSNicholas Bellinger 	iscsi_initiatorname_tolower(i_buf);
1123e48354ceSNicholas Bellinger 
1124e48354ceSNicholas Bellinger 	if (!s_buf) {
1125e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1126e48354ceSNicholas Bellinger 			goto get_target;
1127e48354ceSNicholas Bellinger 
1128e48354ceSNicholas Bellinger 		pr_err("SessionType key not received"
1129e48354ceSNicholas Bellinger 			" in first login request.\n");
1130e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1131e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1132e48354ceSNicholas Bellinger 		ret = -1;
1133e48354ceSNicholas Bellinger 		goto out;
1134e48354ceSNicholas Bellinger 	}
1135e48354ceSNicholas Bellinger 
1136e48354ceSNicholas Bellinger 	/*
1137e48354ceSNicholas Bellinger 	 * Use default portal group for discovery sessions.
1138e48354ceSNicholas Bellinger 	 */
1139e48354ceSNicholas Bellinger 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1140e48354ceSNicholas Bellinger 	if (!sessiontype) {
1141e48354ceSNicholas Bellinger 		conn->tpg = iscsit_global->discovery_tpg;
1142e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1143e48354ceSNicholas Bellinger 			goto get_target;
1144e48354ceSNicholas Bellinger 
1145e48354ceSNicholas Bellinger 		sess->sess_ops->SessionType = 1;
1146e48354ceSNicholas Bellinger 		/*
1147e48354ceSNicholas Bellinger 		 * Setup crc32c modules from libcrypto
1148e48354ceSNicholas Bellinger 		 */
1149e48354ceSNicholas Bellinger 		if (iscsi_login_setup_crypto(conn) < 0) {
1150e48354ceSNicholas Bellinger 			pr_err("iscsi_login_setup_crypto() failed\n");
1151e48354ceSNicholas Bellinger 			ret = -1;
1152e48354ceSNicholas Bellinger 			goto out;
1153e48354ceSNicholas Bellinger 		}
1154e48354ceSNicholas Bellinger 		/*
1155e48354ceSNicholas Bellinger 		 * Serialize access across the discovery struct iscsi_portal_group to
1156e48354ceSNicholas Bellinger 		 * process login attempt.
1157e48354ceSNicholas Bellinger 		 */
1158e48354ceSNicholas Bellinger 		if (iscsit_access_np(np, conn->tpg) < 0) {
1159e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1160e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1161e48354ceSNicholas Bellinger 			ret = -1;
1162e48354ceSNicholas Bellinger 			goto out;
1163e48354ceSNicholas Bellinger 		}
1164e48354ceSNicholas Bellinger 		ret = 0;
1165988e3a85SNicholas Bellinger 		goto alloc_tags;
1166e48354ceSNicholas Bellinger 	}
1167e48354ceSNicholas Bellinger 
1168e48354ceSNicholas Bellinger get_target:
1169e48354ceSNicholas Bellinger 	if (!t_buf) {
1170e48354ceSNicholas Bellinger 		pr_err("TargetName key not received"
1171e48354ceSNicholas Bellinger 			" in first login request while"
1172e48354ceSNicholas Bellinger 			" SessionType=Normal.\n");
1173e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1174e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1175e48354ceSNicholas Bellinger 		ret = -1;
1176e48354ceSNicholas Bellinger 		goto out;
1177e48354ceSNicholas Bellinger 	}
1178e48354ceSNicholas Bellinger 
1179e48354ceSNicholas Bellinger 	/*
1180e48354ceSNicholas Bellinger 	 * Locate Target IQN from Storage Node.
1181e48354ceSNicholas Bellinger 	 */
1182e48354ceSNicholas Bellinger 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1183e48354ceSNicholas Bellinger 	if (!tiqn) {
1184e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target IQN: %s in"
1185e48354ceSNicholas Bellinger 			" Storage Node\n", t_buf);
1186e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1187e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1188e48354ceSNicholas Bellinger 		ret = -1;
1189e48354ceSNicholas Bellinger 		goto out;
1190e48354ceSNicholas Bellinger 	}
1191e48354ceSNicholas Bellinger 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1192e48354ceSNicholas Bellinger 
1193e48354ceSNicholas Bellinger 	/*
1194e48354ceSNicholas Bellinger 	 * Locate Target Portal Group from Storage Node.
1195e48354ceSNicholas Bellinger 	 */
1196d381a801SNicholas Bellinger 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1197e48354ceSNicholas Bellinger 	if (!conn->tpg) {
1198e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target Portal Group"
1199e48354ceSNicholas Bellinger 				" on %s\n", tiqn->tiqn);
1200e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1201e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1202e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1203e48354ceSNicholas Bellinger 		ret = -1;
1204e48354ceSNicholas Bellinger 		goto out;
1205e48354ceSNicholas Bellinger 	}
1206d381a801SNicholas Bellinger 	conn->tpg_np = tpg_np;
1207e48354ceSNicholas Bellinger 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1208e48354ceSNicholas Bellinger 	/*
1209e48354ceSNicholas Bellinger 	 * Setup crc32c modules from libcrypto
1210e48354ceSNicholas Bellinger 	 */
1211e48354ceSNicholas Bellinger 	if (iscsi_login_setup_crypto(conn) < 0) {
1212e48354ceSNicholas Bellinger 		pr_err("iscsi_login_setup_crypto() failed\n");
1213d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1214d381a801SNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1215d381a801SNicholas Bellinger 		conn->tpg = NULL;
1216e48354ceSNicholas Bellinger 		ret = -1;
1217e48354ceSNicholas Bellinger 		goto out;
1218e48354ceSNicholas Bellinger 	}
1219e48354ceSNicholas Bellinger 	/*
1220e48354ceSNicholas Bellinger 	 * Serialize access across the struct iscsi_portal_group to
1221e48354ceSNicholas Bellinger 	 * process login attempt.
1222e48354ceSNicholas Bellinger 	 */
1223e48354ceSNicholas Bellinger 	if (iscsit_access_np(np, conn->tpg) < 0) {
1224d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1225e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1226e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1227e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1228e48354ceSNicholas Bellinger 		conn->tpg = NULL;
1229d381a801SNicholas Bellinger 		ret = -1;
1230e48354ceSNicholas Bellinger 		goto out;
1231e48354ceSNicholas Bellinger 	}
1232e48354ceSNicholas Bellinger 
1233e48354ceSNicholas Bellinger 	/*
1234e48354ceSNicholas Bellinger 	 * conn->sess->node_acl will be set when the referenced
1235e48354ceSNicholas Bellinger 	 * struct iscsi_session is located from received ISID+TSIH in
1236e48354ceSNicholas Bellinger 	 * iscsi_login_non_zero_tsih_s2().
1237e48354ceSNicholas Bellinger 	 */
1238e48354ceSNicholas Bellinger 	if (!login->leading_connection) {
1239e48354ceSNicholas Bellinger 		ret = 0;
1240e48354ceSNicholas Bellinger 		goto out;
1241e48354ceSNicholas Bellinger 	}
1242e48354ceSNicholas Bellinger 
1243e48354ceSNicholas Bellinger 	/*
1244e48354ceSNicholas Bellinger 	 * This value is required in iscsi_login_zero_tsih_s2()
1245e48354ceSNicholas Bellinger 	 */
1246e48354ceSNicholas Bellinger 	sess->sess_ops->SessionType = 0;
1247e48354ceSNicholas Bellinger 
1248e48354ceSNicholas Bellinger 	/*
1249e48354ceSNicholas Bellinger 	 * Locate incoming Initiator IQN reference from Storage Node.
1250e48354ceSNicholas Bellinger 	 */
1251e48354ceSNicholas Bellinger 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1252e48354ceSNicholas Bellinger 			&conn->tpg->tpg_se_tpg, i_buf);
1253e48354ceSNicholas Bellinger 	if (!sess->se_sess->se_node_acl) {
1254e48354ceSNicholas Bellinger 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1255e48354ceSNicholas Bellinger 			" access iSCSI target portal group: %hu.\n",
1256e48354ceSNicholas Bellinger 				i_buf, conn->tpg->tpgt);
1257e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1258e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1259e48354ceSNicholas Bellinger 		ret = -1;
1260e48354ceSNicholas Bellinger 		goto out;
1261e48354ceSNicholas Bellinger 	}
1262988e3a85SNicholas Bellinger 	se_nacl = sess->se_sess->se_node_acl;
1263988e3a85SNicholas Bellinger 	queue_depth = se_nacl->queue_depth;
1264988e3a85SNicholas Bellinger 	/*
1265988e3a85SNicholas Bellinger 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1266988e3a85SNicholas Bellinger 	 * depth for non immediate commands, plus extra tags for immediate
1267988e3a85SNicholas Bellinger 	 * commands.
1268988e3a85SNicholas Bellinger 	 *
1269988e3a85SNicholas Bellinger 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1270988e3a85SNicholas Bellinger 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1271988e3a85SNicholas Bellinger 	 */
1272988e3a85SNicholas Bellinger alloc_tags:
1273988e3a85SNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
12744a4caa29SNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1275*66cd9d4eSMax Gurtovoy 	tag_size = sizeof(struct iscsit_cmd) + conn->conn_transport->priv_size;
1276e48354ceSNicholas Bellinger 
1277988e3a85SNicholas Bellinger 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1278988e3a85SNicholas Bellinger 	if (ret < 0) {
1279988e3a85SNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1280988e3a85SNicholas Bellinger 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1281988e3a85SNicholas Bellinger 		ret = -1;
1282988e3a85SNicholas Bellinger 	}
1283e48354ceSNicholas Bellinger out:
1284e48354ceSNicholas Bellinger 	kfree(tmpbuf);
1285e48354ceSNicholas Bellinger 	return ret;
1286e48354ceSNicholas Bellinger }
1287e48354ceSNicholas Bellinger 
1288e48354ceSNicholas Bellinger int iscsi_target_start_negotiation(
1289e48354ceSNicholas Bellinger 	struct iscsi_login *login,
1290e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
1291e48354ceSNicholas Bellinger {
1292baa4d64bSNicholas Bellinger 	int ret;
1293e48354ceSNicholas Bellinger 
1294d381a801SNicholas Bellinger 	if (conn->sock) {
1295d381a801SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
1296e48354ceSNicholas Bellinger 
1297d381a801SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
1298d381a801SNicholas Bellinger 		set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
129925cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1300d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
1301d381a801SNicholas Bellinger 	}
130225cdda95SNicholas Bellinger 	/*
130325cdda95SNicholas Bellinger 	 * If iscsi_target_do_login returns zero to signal more PDU
130425cdda95SNicholas Bellinger 	 * exchanges are required to complete the login, go ahead and
130525cdda95SNicholas Bellinger 	 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
130625cdda95SNicholas Bellinger 	 * is still active.
130725cdda95SNicholas Bellinger 	 *
130825cdda95SNicholas Bellinger 	 * Otherwise if TCP connection dropped asynchronously, go ahead
130925cdda95SNicholas Bellinger 	 * and perform connection cleanup now.
131025cdda95SNicholas Bellinger 	 */
13118f0dfb3dSNicholas Bellinger 	ret = iscsi_target_do_login(conn, login);
131225cdda95SNicholas Bellinger 	if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
131325cdda95SNicholas Bellinger 		ret = -1;
131425cdda95SNicholas Bellinger 
13158f0dfb3dSNicholas Bellinger 	if (ret < 0) {
1316d381a801SNicholas Bellinger 		cancel_delayed_work_sync(&conn->login_work);
1317d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
1318d381a801SNicholas Bellinger 		iscsi_remove_failed_auth_entry(conn);
1319d381a801SNicholas Bellinger 	}
1320d381a801SNicholas Bellinger 	if (ret != 0)
1321baa4d64bSNicholas Bellinger 		iscsi_target_nego_release(conn);
1322d381a801SNicholas Bellinger 
1323e48354ceSNicholas Bellinger 	return ret;
1324e48354ceSNicholas Bellinger }
1325e48354ceSNicholas Bellinger 
1326baa4d64bSNicholas Bellinger void iscsi_target_nego_release(struct iscsi_conn *conn)
1327e48354ceSNicholas Bellinger {
1328baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1329baa4d64bSNicholas Bellinger 
1330baa4d64bSNicholas Bellinger 	if (!login)
1331baa4d64bSNicholas Bellinger 		return;
1332baa4d64bSNicholas Bellinger 
1333e48354ceSNicholas Bellinger 	kfree(login->req_buf);
1334e48354ceSNicholas Bellinger 	kfree(login->rsp_buf);
1335e48354ceSNicholas Bellinger 	kfree(login);
1336baa4d64bSNicholas Bellinger 
1337baa4d64bSNicholas Bellinger 	conn->conn_login = NULL;
1338e48354ceSNicholas Bellinger }
1339