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>
16*40e0b090SPeilin Ye #include <trace/events/sock.h>
17e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
18e48354ceSNicholas Bellinger #include <target/target_core_base.h>
19c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
20baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
21e48354ceSNicholas Bellinger 
2267f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
23e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
24e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
25e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
26e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
27e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
28e48354ceSNicholas Bellinger #include "iscsi_target.h"
29e48354ceSNicholas Bellinger #include "iscsi_target_auth.h"
30e48354ceSNicholas Bellinger 
31e48354ceSNicholas Bellinger #define MAX_LOGIN_PDUS  7
32e48354ceSNicholas Bellinger 
33e48354ceSNicholas Bellinger void convert_null_to_semi(char *buf, int len)
34e48354ceSNicholas Bellinger {
35e48354ceSNicholas Bellinger 	int i;
36e48354ceSNicholas Bellinger 
37e48354ceSNicholas Bellinger 	for (i = 0; i < len; i++)
38e48354ceSNicholas Bellinger 		if (buf[i] == '\0')
39e48354ceSNicholas Bellinger 			buf[i] = ';';
40e48354ceSNicholas Bellinger }
41e48354ceSNicholas Bellinger 
42fceb5bc7SChristoph Hellwig static int strlen_semi(char *buf)
43e48354ceSNicholas Bellinger {
44e48354ceSNicholas Bellinger 	int i = 0;
45e48354ceSNicholas Bellinger 
46e48354ceSNicholas Bellinger 	while (buf[i] != '\0') {
47e48354ceSNicholas Bellinger 		if (buf[i] == ';')
48e48354ceSNicholas Bellinger 			return i;
49e48354ceSNicholas Bellinger 		i++;
50e48354ceSNicholas Bellinger 	}
51e48354ceSNicholas Bellinger 
52e48354ceSNicholas Bellinger 	return -1;
53e48354ceSNicholas Bellinger }
54e48354ceSNicholas Bellinger 
55e48354ceSNicholas Bellinger int extract_param(
56e48354ceSNicholas Bellinger 	const char *in_buf,
57e48354ceSNicholas Bellinger 	const char *pattern,
58e48354ceSNicholas Bellinger 	unsigned int max_length,
59e48354ceSNicholas Bellinger 	char *out_buf,
60e48354ceSNicholas Bellinger 	unsigned char *type)
61e48354ceSNicholas Bellinger {
62e48354ceSNicholas Bellinger 	char *ptr;
63e48354ceSNicholas Bellinger 	int len;
64e48354ceSNicholas Bellinger 
65e48354ceSNicholas Bellinger 	if (!in_buf || !pattern || !out_buf || !type)
66292cef5eSDmitry Bogdanov 		return -EINVAL;
67e48354ceSNicholas Bellinger 
68e48354ceSNicholas Bellinger 	ptr = strstr(in_buf, pattern);
69e48354ceSNicholas Bellinger 	if (!ptr)
70292cef5eSDmitry Bogdanov 		return -ENOENT;
71e48354ceSNicholas Bellinger 
72e48354ceSNicholas Bellinger 	ptr = strstr(ptr, "=");
73e48354ceSNicholas Bellinger 	if (!ptr)
74292cef5eSDmitry Bogdanov 		return -EINVAL;
75e48354ceSNicholas Bellinger 
76e48354ceSNicholas Bellinger 	ptr += 1;
77e48354ceSNicholas Bellinger 	if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
78e48354ceSNicholas Bellinger 		ptr += 2; /* skip 0x */
79e48354ceSNicholas Bellinger 		*type = HEX;
801e573388SDmitry Bogdanov 	} else if (*ptr == '0' && (*(ptr+1) == 'b' || *(ptr+1) == 'B')) {
811e573388SDmitry Bogdanov 		ptr += 2; /* skip 0b */
821e573388SDmitry Bogdanov 		*type = BASE64;
83e48354ceSNicholas Bellinger 	} else
84e48354ceSNicholas Bellinger 		*type = DECIMAL;
85e48354ceSNicholas Bellinger 
86e48354ceSNicholas Bellinger 	len = strlen_semi(ptr);
87e48354ceSNicholas Bellinger 	if (len < 0)
88292cef5eSDmitry Bogdanov 		return -EINVAL;
89e48354ceSNicholas Bellinger 
90369653e4SEric Seppanen 	if (len >= max_length) {
915e58b029SMasanari Iida 		pr_err("Length of input: %d exceeds max_length:"
92e48354ceSNicholas Bellinger 			" %d\n", len, max_length);
93292cef5eSDmitry Bogdanov 		return -EINVAL;
94e48354ceSNicholas Bellinger 	}
95e48354ceSNicholas Bellinger 	memcpy(out_buf, ptr, len);
96e48354ceSNicholas Bellinger 	out_buf[len] = '\0';
97e48354ceSNicholas Bellinger 
98e48354ceSNicholas Bellinger 	return 0;
99e48354ceSNicholas Bellinger }
100e48354ceSNicholas Bellinger 
101a75fcb09SDmitry Bogdanov static struct iscsi_node_auth *iscsi_get_node_auth(struct iscsit_conn *conn)
102a75fcb09SDmitry Bogdanov {
103a75fcb09SDmitry Bogdanov 	struct iscsi_portal_group *tpg;
104a75fcb09SDmitry Bogdanov 	struct iscsi_node_acl *nacl;
105a75fcb09SDmitry Bogdanov 	struct se_node_acl *se_nacl;
106a75fcb09SDmitry Bogdanov 
107a75fcb09SDmitry Bogdanov 	if (conn->sess->sess_ops->SessionType)
108a75fcb09SDmitry Bogdanov 		return &iscsit_global->discovery_acl.node_auth;
109a75fcb09SDmitry Bogdanov 
110a75fcb09SDmitry Bogdanov 	se_nacl = conn->sess->se_sess->se_node_acl;
111a75fcb09SDmitry Bogdanov 	if (!se_nacl) {
112a75fcb09SDmitry Bogdanov 		pr_err("Unable to locate struct se_node_acl for CHAP auth\n");
113a75fcb09SDmitry Bogdanov 		return NULL;
114a75fcb09SDmitry Bogdanov 	}
115a75fcb09SDmitry Bogdanov 
116a75fcb09SDmitry Bogdanov 	if (se_nacl->dynamic_node_acl) {
117a75fcb09SDmitry Bogdanov 		tpg = to_iscsi_tpg(se_nacl->se_tpg);
118a75fcb09SDmitry Bogdanov 		return &tpg->tpg_demo_auth;
119a75fcb09SDmitry Bogdanov 	}
120a75fcb09SDmitry Bogdanov 
121a75fcb09SDmitry Bogdanov 	nacl = to_iscsi_nacl(se_nacl);
122a75fcb09SDmitry Bogdanov 
123a75fcb09SDmitry Bogdanov 	return &nacl->node_auth;
124a75fcb09SDmitry Bogdanov }
125a75fcb09SDmitry Bogdanov 
126e48354ceSNicholas Bellinger static u32 iscsi_handle_authentication(
127be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
128e48354ceSNicholas Bellinger 	char *in_buf,
129e48354ceSNicholas Bellinger 	char *out_buf,
130e48354ceSNicholas Bellinger 	int in_length,
131e48354ceSNicholas Bellinger 	int *out_length,
132e48354ceSNicholas Bellinger 	unsigned char *authtype)
133e48354ceSNicholas Bellinger {
134e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth;
135e48354ceSNicholas Bellinger 
136a75fcb09SDmitry Bogdanov 	auth = iscsi_get_node_auth(conn);
137a75fcb09SDmitry Bogdanov 	if (!auth)
138e48354ceSNicholas Bellinger 		return -1;
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 
154be36d683SMax Gurtovoy static void iscsi_remove_failed_auth_entry(struct iscsit_conn *conn)
155e48354ceSNicholas Bellinger {
156e48354ceSNicholas Bellinger 	kfree(conn->auth_protocol);
157e48354ceSNicholas Bellinger }
158e48354ceSNicholas Bellinger 
159baa4d64bSNicholas Bellinger int iscsi_target_check_login_request(
160be36d683SMax Gurtovoy 	struct iscsit_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(
251be36d683SMax Gurtovoy 	struct iscsit_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 
318be36d683SMax Gurtovoy static int iscsi_target_do_tx_login_io(struct iscsit_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 {
385be36d683SMax Gurtovoy 	struct iscsit_conn *conn = sk->sk_user_data;
386d381a801SNicholas Bellinger 	bool rc;
387d381a801SNicholas Bellinger 
388*40e0b090SPeilin Ye 	trace_sk_data_ready(sk);
389d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
390d381a801SNicholas Bellinger 
391d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
392d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
393d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
394d381a801SNicholas Bellinger 		return;
395d381a801SNicholas Bellinger 	}
396d381a801SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
397d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
398d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
399d381a801SNicholas Bellinger 		return;
400d381a801SNicholas Bellinger 	}
401d381a801SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
402d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
403d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
404d381a801SNicholas Bellinger 		return;
405d381a801SNicholas Bellinger 	}
406d381a801SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
407d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
408d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
4091c130ae0SFlorian Westphal 		if (iscsi_target_sk_data_ready == conn->orig_data_ready)
4101c130ae0SFlorian Westphal 			return;
4111c130ae0SFlorian Westphal 		conn->orig_data_ready(sk);
412d381a801SNicholas Bellinger 		return;
413d381a801SNicholas Bellinger 	}
414d381a801SNicholas Bellinger 
415d381a801SNicholas Bellinger 	rc = schedule_delayed_work(&conn->login_work, 0);
4160bcc297eSChristophe Vu-Brugier 	if (!rc) {
417d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
418d381a801SNicholas Bellinger 			 " got false\n");
419d381a801SNicholas Bellinger 	}
420d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
421d381a801SNicholas Bellinger }
422d381a801SNicholas Bellinger 
423bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *);
424bb048357SNicholas Bellinger 
425be36d683SMax Gurtovoy static void iscsi_target_set_sock_callbacks(struct iscsit_conn *conn)
426d381a801SNicholas Bellinger {
427d381a801SNicholas Bellinger 	struct sock *sk;
428d381a801SNicholas Bellinger 
429d381a801SNicholas Bellinger 	if (!conn->sock)
430d381a801SNicholas Bellinger 		return;
431d381a801SNicholas Bellinger 
432d381a801SNicholas Bellinger 	sk = conn->sock->sk;
433d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
434d381a801SNicholas Bellinger 
435d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
436d381a801SNicholas Bellinger 	sk->sk_user_data = conn;
437d381a801SNicholas Bellinger 	conn->orig_data_ready = sk->sk_data_ready;
438bb048357SNicholas Bellinger 	conn->orig_state_change = sk->sk_state_change;
439d381a801SNicholas Bellinger 	sk->sk_data_ready = iscsi_target_sk_data_ready;
440bb048357SNicholas Bellinger 	sk->sk_state_change = iscsi_target_sk_state_change;
441d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
442bb048357SNicholas Bellinger 
443bb048357SNicholas Bellinger 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
444bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
445d381a801SNicholas Bellinger }
446d381a801SNicholas Bellinger 
447be36d683SMax Gurtovoy static void iscsi_target_restore_sock_callbacks(struct iscsit_conn *conn)
448d381a801SNicholas Bellinger {
449d381a801SNicholas Bellinger 	struct sock *sk;
450d381a801SNicholas Bellinger 
451d381a801SNicholas Bellinger 	if (!conn->sock)
452d381a801SNicholas Bellinger 		return;
453d381a801SNicholas Bellinger 
454d381a801SNicholas Bellinger 	sk = conn->sock->sk;
455d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
456d381a801SNicholas Bellinger 
457d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
458d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
459d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
460d381a801SNicholas Bellinger 		return;
461d381a801SNicholas Bellinger 	}
462d381a801SNicholas Bellinger 	sk->sk_user_data = NULL;
463d381a801SNicholas Bellinger 	sk->sk_data_ready = conn->orig_data_ready;
464bb048357SNicholas Bellinger 	sk->sk_state_change = conn->orig_state_change;
465d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
466bb048357SNicholas Bellinger 
467bb048357SNicholas Bellinger 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
468bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
469d381a801SNicholas Bellinger }
470d381a801SNicholas Bellinger 
471be36d683SMax Gurtovoy static int iscsi_target_do_login(struct iscsit_conn *, struct iscsi_login *);
472d381a801SNicholas Bellinger 
47325cdda95SNicholas Bellinger static bool __iscsi_target_sk_check_close(struct sock *sk)
474d381a801SNicholas Bellinger {
475d381a801SNicholas Bellinger 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
47625cdda95SNicholas Bellinger 		pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
477df2de6f2SHou Pu 			"returning TRUE\n");
47825cdda95SNicholas Bellinger 		return true;
47925cdda95SNicholas Bellinger 	}
480d381a801SNicholas Bellinger 	return false;
481d381a801SNicholas Bellinger }
48225cdda95SNicholas Bellinger 
483be36d683SMax Gurtovoy static bool iscsi_target_sk_check_close(struct iscsit_conn *conn)
48425cdda95SNicholas Bellinger {
48525cdda95SNicholas Bellinger 	bool state = false;
48625cdda95SNicholas Bellinger 
48725cdda95SNicholas Bellinger 	if (conn->sock) {
48825cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
48925cdda95SNicholas Bellinger 
49025cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
49125cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
49225cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
49325cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
49425cdda95SNicholas Bellinger 	}
49525cdda95SNicholas Bellinger 	return state;
49625cdda95SNicholas Bellinger }
49725cdda95SNicholas Bellinger 
498be36d683SMax Gurtovoy static bool iscsi_target_sk_check_flag(struct iscsit_conn *conn, unsigned int flag)
49925cdda95SNicholas Bellinger {
50025cdda95SNicholas Bellinger 	bool state = false;
50125cdda95SNicholas Bellinger 
50225cdda95SNicholas Bellinger 	if (conn->sock) {
50325cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
50425cdda95SNicholas Bellinger 
50525cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
50625cdda95SNicholas Bellinger 		state = test_bit(flag, &conn->login_flags);
50725cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
50825cdda95SNicholas Bellinger 	}
50925cdda95SNicholas Bellinger 	return state;
51025cdda95SNicholas Bellinger }
51125cdda95SNicholas Bellinger 
512be36d683SMax Gurtovoy static bool iscsi_target_sk_check_and_clear(struct iscsit_conn *conn, unsigned int flag)
51325cdda95SNicholas Bellinger {
51425cdda95SNicholas Bellinger 	bool state = false;
51525cdda95SNicholas Bellinger 
51625cdda95SNicholas Bellinger 	if (conn->sock) {
51725cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
51825cdda95SNicholas Bellinger 
51925cdda95SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
52025cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
52125cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
52225cdda95SNicholas Bellinger 		if (!state)
52325cdda95SNicholas Bellinger 			clear_bit(flag, &conn->login_flags);
52425cdda95SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
52525cdda95SNicholas Bellinger 	}
52625cdda95SNicholas Bellinger 	return state;
527d381a801SNicholas Bellinger }
528d381a801SNicholas Bellinger 
529be36d683SMax Gurtovoy static void iscsi_target_login_drop(struct iscsit_conn *conn, struct iscsi_login *login)
530d381a801SNicholas Bellinger {
531d381a801SNicholas Bellinger 	bool zero_tsih = login->zero_tsih;
532d381a801SNicholas Bellinger 
533d381a801SNicholas Bellinger 	iscsi_remove_failed_auth_entry(conn);
534d381a801SNicholas Bellinger 	iscsi_target_nego_release(conn);
535ed43ffeaSHou Pu 	iscsi_target_login_sess_out(conn, zero_tsih, true);
536d381a801SNicholas Bellinger }
537d381a801SNicholas Bellinger 
538f7c9564aSKees Cook struct conn_timeout {
539f7c9564aSKees Cook 	struct timer_list timer;
540be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
541f7c9564aSKees Cook };
542f7c9564aSKees Cook 
543f7c9564aSKees Cook static void iscsi_target_login_timeout(struct timer_list *t)
544d381a801SNicholas Bellinger {
545f7c9564aSKees Cook 	struct conn_timeout *timeout = from_timer(timeout, t, timer);
546be36d683SMax Gurtovoy 	struct iscsit_conn *conn = timeout->conn;
547d381a801SNicholas Bellinger 
548d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
549d381a801SNicholas Bellinger 
550d381a801SNicholas Bellinger 	if (conn->login_kworker) {
551d381a801SNicholas Bellinger 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
552d381a801SNicholas Bellinger 			 conn->login_kworker->comm, conn->login_kworker->pid);
553d381a801SNicholas Bellinger 		send_sig(SIGINT, conn->login_kworker, 1);
554d381a801SNicholas Bellinger 	}
555d381a801SNicholas Bellinger }
556d381a801SNicholas Bellinger 
557d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work)
558d381a801SNicholas Bellinger {
559be36d683SMax Gurtovoy 	struct iscsit_conn *conn = container_of(work,
560be36d683SMax Gurtovoy 				struct iscsit_conn, login_work.work);
561d381a801SNicholas Bellinger 	struct iscsi_login *login = conn->login;
562d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
563d381a801SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
564d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
565f7c9564aSKees Cook 	struct conn_timeout timeout;
566d381a801SNicholas Bellinger 	int rc, zero_tsih = login->zero_tsih;
567d381a801SNicholas Bellinger 	bool state;
568d381a801SNicholas Bellinger 
569d381a801SNicholas Bellinger 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
570d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
57125cdda95SNicholas Bellinger 	/*
57225cdda95SNicholas Bellinger 	 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
57325cdda95SNicholas Bellinger 	 * before initial PDU processing in iscsi_target_start_negotiation()
57425cdda95SNicholas Bellinger 	 * has completed, go ahead and retry until it's cleared.
57525cdda95SNicholas Bellinger 	 *
57625cdda95SNicholas Bellinger 	 * Otherwise if the TCP connection drops while this is occuring,
57725cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() will detect the failure, call
57825cdda95SNicholas Bellinger 	 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
57925cdda95SNicholas Bellinger 	 * remaining iscsi connection resources from iscsi_np process context.
58025cdda95SNicholas Bellinger 	 */
58125cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
58225cdda95SNicholas Bellinger 		schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
58325cdda95SNicholas Bellinger 		return;
58425cdda95SNicholas Bellinger 	}
585d381a801SNicholas Bellinger 
586d381a801SNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
587d381a801SNicholas Bellinger 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
588d381a801SNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
589d381a801SNicholas Bellinger 
5900bcc297eSChristophe Vu-Brugier 	if (!state) {
591d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
59225cdda95SNicholas Bellinger 		goto err;
593d381a801SNicholas Bellinger 	}
594d381a801SNicholas Bellinger 
59525cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_close(conn)) {
596d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
59725cdda95SNicholas Bellinger 		goto err;
598d381a801SNicholas Bellinger 	}
599d381a801SNicholas Bellinger 
600d381a801SNicholas Bellinger 	conn->login_kworker = current;
601d381a801SNicholas Bellinger 	allow_signal(SIGINT);
602d381a801SNicholas Bellinger 
603f7c9564aSKees Cook 	timeout.conn = conn;
604f7c9564aSKees Cook 	timer_setup_on_stack(&timeout.timer, iscsi_target_login_timeout, 0);
605f7c9564aSKees Cook 	mod_timer(&timeout.timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
606f7c9564aSKees Cook 	pr_debug("Starting login timer for %s/%d\n", current->comm, current->pid);
607d381a801SNicholas Bellinger 
608d381a801SNicholas Bellinger 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
609f7c9564aSKees Cook 	del_timer_sync(&timeout.timer);
610f7c9564aSKees Cook 	destroy_timer_on_stack(&timeout.timer);
611d381a801SNicholas Bellinger 	flush_signals(current);
612d381a801SNicholas Bellinger 	conn->login_kworker = NULL;
613d381a801SNicholas Bellinger 
61425cdda95SNicholas Bellinger 	if (rc < 0)
61525cdda95SNicholas Bellinger 		goto err;
616d381a801SNicholas Bellinger 
617d381a801SNicholas Bellinger 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
618d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
619d381a801SNicholas Bellinger 
6204e108d4fSHou Pu 	/*
6214e108d4fSHou Pu 	 * LOGIN_FLAGS_READ_ACTIVE is cleared so that sk_data_ready
6224e108d4fSHou Pu 	 * could be triggered again after this.
6234e108d4fSHou Pu 	 *
6244e108d4fSHou Pu 	 * LOGIN_FLAGS_WRITE_ACTIVE is cleared after we successfully
6254e108d4fSHou Pu 	 * process a login PDU, so that sk_state_chage can do login
6264e108d4fSHou Pu 	 * cleanup as needed if the socket is closed. If a delayed work is
6274e108d4fSHou Pu 	 * ongoing (LOGIN_FLAGS_WRITE_ACTIVE or LOGIN_FLAGS_READ_ACTIVE),
6284e108d4fSHou Pu 	 * sk_state_change will leave the cleanup to the delayed work or
6294e108d4fSHou Pu 	 * it will schedule a delayed work to do cleanup.
6304e108d4fSHou Pu 	 */
6314e108d4fSHou Pu 	if (conn->sock) {
6324e108d4fSHou Pu 		struct sock *sk = conn->sock->sk;
6334e108d4fSHou Pu 
6344e108d4fSHou Pu 		write_lock_bh(&sk->sk_callback_lock);
6354e108d4fSHou Pu 		if (!test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags)) {
6364e108d4fSHou Pu 			clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
6374e108d4fSHou Pu 			set_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags);
6384e108d4fSHou Pu 		}
6394e108d4fSHou Pu 		write_unlock_bh(&sk->sk_callback_lock);
6404e108d4fSHou Pu 	}
6414e108d4fSHou Pu 
642d381a801SNicholas Bellinger 	rc = iscsi_target_do_login(conn, login);
643d381a801SNicholas Bellinger 	if (rc < 0) {
64425cdda95SNicholas Bellinger 		goto err;
645d381a801SNicholas Bellinger 	} else if (!rc) {
6464e108d4fSHou Pu 		if (iscsi_target_sk_check_and_clear(conn,
6474e108d4fSHou Pu 						    LOGIN_FLAGS_WRITE_ACTIVE))
64825cdda95SNicholas Bellinger 			goto err;
649d381a801SNicholas Bellinger 	} else if (rc == 1) {
6504e108d4fSHou Pu 		cancel_delayed_work(&conn->login_work);
651d381a801SNicholas Bellinger 		iscsi_target_nego_release(conn);
652d381a801SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
653d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
654d381a801SNicholas Bellinger 	}
65525cdda95SNicholas Bellinger 	return;
65625cdda95SNicholas Bellinger 
65725cdda95SNicholas Bellinger err:
65825cdda95SNicholas Bellinger 	iscsi_target_restore_sock_callbacks(conn);
6594e108d4fSHou Pu 	cancel_delayed_work(&conn->login_work);
66025cdda95SNicholas Bellinger 	iscsi_target_login_drop(conn, login);
66125cdda95SNicholas Bellinger 	iscsit_deaccess_np(np, tpg, tpg_np);
662d381a801SNicholas Bellinger }
663d381a801SNicholas Bellinger 
664bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk)
665bb048357SNicholas Bellinger {
666be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
667bb048357SNicholas Bellinger 	void (*orig_state_change)(struct sock *);
668bb048357SNicholas Bellinger 	bool state;
669bb048357SNicholas Bellinger 
670bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_state_change\n");
671bb048357SNicholas Bellinger 
672bb048357SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
673bb048357SNicholas Bellinger 	conn = sk->sk_user_data;
674bb048357SNicholas Bellinger 	if (!conn) {
675bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
676bb048357SNicholas Bellinger 		return;
677bb048357SNicholas Bellinger 	}
678bb048357SNicholas Bellinger 	orig_state_change = conn->orig_state_change;
679bb048357SNicholas Bellinger 
680bb048357SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
681bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
682bb048357SNicholas Bellinger 			 conn);
683bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
684bb048357SNicholas Bellinger 		orig_state_change(sk);
685bb048357SNicholas Bellinger 		return;
686bb048357SNicholas Bellinger 	}
68725cdda95SNicholas Bellinger 	state = __iscsi_target_sk_check_close(sk);
68825cdda95SNicholas Bellinger 	pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
68925cdda95SNicholas Bellinger 
6904e108d4fSHou Pu 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags) ||
6914e108d4fSHou Pu 	    test_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags)) {
6924e108d4fSHou Pu 		pr_debug("Got LOGIN_FLAGS_{READ|WRITE}_ACTIVE=1"
6934e108d4fSHou Pu 			 " sk_state_change conn: %p\n", conn);
69425cdda95SNicholas Bellinger 		if (state)
69525cdda95SNicholas Bellinger 			set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
696bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
697bb048357SNicholas Bellinger 		orig_state_change(sk);
698bb048357SNicholas Bellinger 		return;
699bb048357SNicholas Bellinger 	}
70025cdda95SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
701bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
702bb048357SNicholas Bellinger 			 conn);
703bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
704bb048357SNicholas Bellinger 		orig_state_change(sk);
705bb048357SNicholas Bellinger 		return;
706bb048357SNicholas Bellinger 	}
70725cdda95SNicholas Bellinger 	/*
70825cdda95SNicholas Bellinger 	 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
70925cdda95SNicholas Bellinger 	 * but only queue conn->login_work -> iscsi_target_do_login_rx()
71025cdda95SNicholas Bellinger 	 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
71125cdda95SNicholas Bellinger 	 *
71225cdda95SNicholas Bellinger 	 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
71325cdda95SNicholas Bellinger 	 * will detect the dropped TCP connection from delayed workqueue context.
71425cdda95SNicholas Bellinger 	 *
71525cdda95SNicholas Bellinger 	 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
71625cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
71725cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
71825cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
71925cdda95SNicholas Bellinger 	 * dropped TCP connection in iscsi_np process context, and cleaning up
72025cdda95SNicholas Bellinger 	 * the remaining iscsi connection resources.
72125cdda95SNicholas Bellinger 	 */
72225cdda95SNicholas Bellinger 	if (state) {
72325cdda95SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_change got failed state\n");
72425cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
72525cdda95SNicholas Bellinger 		state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
726bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
727bb048357SNicholas Bellinger 
72825cdda95SNicholas Bellinger 		orig_state_change(sk);
729bb048357SNicholas Bellinger 
73025cdda95SNicholas Bellinger 		if (!state)
73125cdda95SNicholas Bellinger 			schedule_delayed_work(&conn->login_work, 0);
732bb048357SNicholas Bellinger 		return;
733bb048357SNicholas Bellinger 	}
73425cdda95SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
73525cdda95SNicholas Bellinger 
736bb048357SNicholas Bellinger 	orig_state_change(sk);
737bb048357SNicholas Bellinger }
738bb048357SNicholas Bellinger 
739e48354ceSNicholas Bellinger /*
740e48354ceSNicholas Bellinger  *	NOTE: We check for existing sessions or connections AFTER the initiator
741e48354ceSNicholas Bellinger  *	has been successfully authenticated in order to protect against faked
742e48354ceSNicholas Bellinger  *	ISID/TSIH combinations.
743e48354ceSNicholas Bellinger  */
744e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances(
745be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
746e48354ceSNicholas Bellinger 	struct iscsi_login *login)
747e48354ceSNicholas Bellinger {
748e48354ceSNicholas Bellinger 	if (login->checked_for_existing)
749e48354ceSNicholas Bellinger 		return 0;
750e48354ceSNicholas Bellinger 
751e48354ceSNicholas Bellinger 	login->checked_for_existing = 1;
752e48354ceSNicholas Bellinger 
753e48354ceSNicholas Bellinger 	if (!login->tsih)
754e48354ceSNicholas Bellinger 		return iscsi_check_for_session_reinstatement(conn);
755e48354ceSNicholas Bellinger 	else
756e48354ceSNicholas Bellinger 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
757e48354ceSNicholas Bellinger 				login->initial_exp_statsn);
758e48354ceSNicholas Bellinger }
759e48354ceSNicholas Bellinger 
760e48354ceSNicholas Bellinger static int iscsi_target_do_authentication(
761be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
762e48354ceSNicholas Bellinger 	struct iscsi_login *login)
763e48354ceSNicholas Bellinger {
764e48354ceSNicholas Bellinger 	int authret;
765e48354ceSNicholas Bellinger 	u32 payload_length;
766e48354ceSNicholas Bellinger 	struct iscsi_param *param;
767e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
768e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
769e48354ceSNicholas Bellinger 
770e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
771e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
772e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
773e48354ceSNicholas Bellinger 
774e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
775e48354ceSNicholas Bellinger 	if (!param)
776e48354ceSNicholas Bellinger 		return -1;
777e48354ceSNicholas Bellinger 
778e48354ceSNicholas Bellinger 	authret = iscsi_handle_authentication(
779e48354ceSNicholas Bellinger 			conn,
780e48354ceSNicholas Bellinger 			login->req_buf,
781e48354ceSNicholas Bellinger 			login->rsp_buf,
782e48354ceSNicholas Bellinger 			payload_length,
783e48354ceSNicholas Bellinger 			&login->rsp_length,
784e48354ceSNicholas Bellinger 			param->value);
785e48354ceSNicholas Bellinger 	switch (authret) {
786e48354ceSNicholas Bellinger 	case 0:
787e48354ceSNicholas Bellinger 		pr_debug("Received OK response"
788e48354ceSNicholas Bellinger 		" from LIO Authentication, continuing.\n");
789e48354ceSNicholas Bellinger 		break;
790e48354ceSNicholas Bellinger 	case 1:
791e48354ceSNicholas Bellinger 		pr_debug("iSCSI security negotiation"
792bfb9035cSJoe Perches 			" completed successfully.\n");
793e48354ceSNicholas Bellinger 		login->auth_complete = 1;
794e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
795e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
796e48354ceSNicholas Bellinger 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
797e48354ceSNicholas Bellinger 					     ISCSI_FLAG_LOGIN_TRANSIT);
798e48354ceSNicholas Bellinger 			login->current_stage = 1;
799e48354ceSNicholas Bellinger 		}
800e48354ceSNicholas Bellinger 		return iscsi_target_check_for_existing_instances(
801e48354ceSNicholas Bellinger 				conn, login);
802e48354ceSNicholas Bellinger 	case 2:
803e48354ceSNicholas Bellinger 		pr_err("Security negotiation"
804e48354ceSNicholas Bellinger 			" failed.\n");
805e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
806e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
807e48354ceSNicholas Bellinger 		return -1;
808e48354ceSNicholas Bellinger 	default:
809e48354ceSNicholas Bellinger 		pr_err("Received unknown error %d from LIO"
810e48354ceSNicholas Bellinger 				" Authentication\n", authret);
811e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
812e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
813e48354ceSNicholas Bellinger 		return -1;
814e48354ceSNicholas Bellinger 	}
815e48354ceSNicholas Bellinger 
816e48354ceSNicholas Bellinger 	return 0;
817e48354ceSNicholas Bellinger }
818e48354ceSNicholas Bellinger 
819e52b904bSDmitry Bogdanov bool iscsi_conn_auth_required(struct iscsit_conn *conn)
820a75fcb09SDmitry Bogdanov {
821a6e0d179SDmitry Bogdanov 	struct iscsi_node_acl *nacl;
822a75fcb09SDmitry Bogdanov 	struct se_node_acl *se_nacl;
823a75fcb09SDmitry Bogdanov 
824a75fcb09SDmitry Bogdanov 	if (conn->sess->sess_ops->SessionType) {
825a75fcb09SDmitry Bogdanov 		/*
826a75fcb09SDmitry Bogdanov 		 * For SessionType=Discovery
827a75fcb09SDmitry Bogdanov 		 */
828a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
829a75fcb09SDmitry Bogdanov 	}
830a75fcb09SDmitry Bogdanov 	/*
831a75fcb09SDmitry Bogdanov 	 * For SessionType=Normal
832a75fcb09SDmitry Bogdanov 	 */
833a75fcb09SDmitry Bogdanov 	se_nacl = conn->sess->se_sess->se_node_acl;
834a75fcb09SDmitry Bogdanov 	if (!se_nacl) {
83535bf020bSYang Li 		pr_debug("Unknown ACL is trying to connect\n");
836a75fcb09SDmitry Bogdanov 		return true;
837a75fcb09SDmitry Bogdanov 	}
838a75fcb09SDmitry Bogdanov 
839a75fcb09SDmitry Bogdanov 	if (se_nacl->dynamic_node_acl) {
840a75fcb09SDmitry Bogdanov 		pr_debug("Dynamic ACL %s is trying to connect\n",
841a75fcb09SDmitry Bogdanov 			 se_nacl->initiatorname);
842a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
843a75fcb09SDmitry Bogdanov 	}
844a75fcb09SDmitry Bogdanov 
845a75fcb09SDmitry Bogdanov 	pr_debug("Known ACL %s is trying to connect\n",
846a75fcb09SDmitry Bogdanov 		 se_nacl->initiatorname);
847a6e0d179SDmitry Bogdanov 
848a6e0d179SDmitry Bogdanov 	nacl = to_iscsi_nacl(se_nacl);
849a6e0d179SDmitry Bogdanov 	if (nacl->node_attrib.authentication == NA_AUTHENTICATION_INHERITED)
850a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
851a6e0d179SDmitry Bogdanov 
852a6e0d179SDmitry Bogdanov 	return nacl->node_attrib.authentication;
853a75fcb09SDmitry Bogdanov }
854a75fcb09SDmitry Bogdanov 
855e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero(
856be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
857e48354ceSNicholas Bellinger 	struct iscsi_login *login)
858e48354ceSNicholas Bellinger {
859e48354ceSNicholas Bellinger 	int ret;
860e48354ceSNicholas Bellinger 	u32 payload_length;
861e48354ceSNicholas Bellinger 	struct iscsi_param *param;
862e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
863e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
864e48354ceSNicholas Bellinger 
865e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
866e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
867e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
868e48354ceSNicholas Bellinger 
869e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
870e48354ceSNicholas Bellinger 	if (!param)
871e48354ceSNicholas Bellinger 		return -1;
872e48354ceSNicholas Bellinger 
873e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
874e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
875e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
876e48354ceSNicholas Bellinger 			login->req_buf,
877e48354ceSNicholas Bellinger 			payload_length,
8789977bb18SNicholas Bellinger 			conn);
879e48354ceSNicholas Bellinger 	if (ret < 0)
880e48354ceSNicholas Bellinger 		return -1;
881e48354ceSNicholas Bellinger 
882e48354ceSNicholas Bellinger 	if (ret > 0) {
883e48354ceSNicholas Bellinger 		if (login->auth_complete) {
884e48354ceSNicholas Bellinger 			pr_err("Initiator has already been"
885e48354ceSNicholas Bellinger 				" successfully authenticated, but is still"
886e48354ceSNicholas Bellinger 				" sending %s keys.\n", param->value);
887e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
888e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_INIT_ERR);
889e48354ceSNicholas Bellinger 			return -1;
890e48354ceSNicholas Bellinger 		}
891e48354ceSNicholas Bellinger 
892e48354ceSNicholas Bellinger 		goto do_auth;
89391f0abfdSNicholas Bellinger 	} else if (!payload_length) {
89491f0abfdSNicholas Bellinger 		pr_err("Initiator sent zero length security payload,"
89591f0abfdSNicholas Bellinger 		       " login failed\n");
89691f0abfdSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
89791f0abfdSNicholas Bellinger 				    ISCSI_LOGIN_STATUS_AUTH_FAILED);
89891f0abfdSNicholas Bellinger 		return -1;
899e48354ceSNicholas Bellinger 	}
900e48354ceSNicholas Bellinger 
901e48354ceSNicholas Bellinger 	if (login->first_request)
902e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
903e48354ceSNicholas Bellinger 			return -1;
904e48354ceSNicholas Bellinger 
905e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
906e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
907e48354ceSNicholas Bellinger 			SENDER_TARGET,
908e48354ceSNicholas Bellinger 			login->rsp_buf,
909e48354ceSNicholas Bellinger 			&login->rsp_length,
910138d351eSNicholas Bellinger 			conn->param_list,
911138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
912e48354ceSNicholas Bellinger 	if (ret < 0)
913e48354ceSNicholas Bellinger 		return -1;
914e48354ceSNicholas Bellinger 
915e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
916a75fcb09SDmitry Bogdanov 		bool auth_required = iscsi_conn_auth_required(conn);
917a75fcb09SDmitry Bogdanov 
918a75fcb09SDmitry Bogdanov 		if (auth_required) {
919a75fcb09SDmitry Bogdanov 			if (!strncmp(param->value, NONE, 4)) {
920e48354ceSNicholas Bellinger 				pr_err("Initiator sent AuthMethod=None but"
921e48354ceSNicholas Bellinger 				       " Target is enforcing iSCSI Authentication,"
922e48354ceSNicholas Bellinger 				       " login failed.\n");
923a75fcb09SDmitry Bogdanov 				iscsit_tx_login_rsp(conn,
924a75fcb09SDmitry Bogdanov 						ISCSI_STATUS_CLS_INITIATOR_ERR,
925e48354ceSNicholas Bellinger 						ISCSI_LOGIN_STATUS_AUTH_FAILED);
926e48354ceSNicholas Bellinger 				return -1;
927e48354ceSNicholas Bellinger 			}
928e48354ceSNicholas Bellinger 
929a75fcb09SDmitry Bogdanov 			if (!login->auth_complete)
930e48354ceSNicholas Bellinger 				return 0;
931e48354ceSNicholas Bellinger 
932a75fcb09SDmitry Bogdanov 			if (strncmp(param->value, NONE, 4) &&
933a75fcb09SDmitry Bogdanov 			    !login->auth_complete)
934e48354ceSNicholas Bellinger 				return 0;
935a75fcb09SDmitry Bogdanov 		}
936e48354ceSNicholas Bellinger 
937e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
938e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
939e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
940e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
941e48354ceSNicholas Bellinger 			login->current_stage = 1;
942e48354ceSNicholas Bellinger 		}
943e48354ceSNicholas Bellinger 	}
944e48354ceSNicholas Bellinger 
945e48354ceSNicholas Bellinger 	return 0;
946e48354ceSNicholas Bellinger do_auth:
947e48354ceSNicholas Bellinger 	return iscsi_target_do_authentication(conn, login);
948e48354ceSNicholas Bellinger }
949e48354ceSNicholas Bellinger 
950a75fcb09SDmitry Bogdanov static bool iscsi_conn_authenticated(struct iscsit_conn *conn,
951a75fcb09SDmitry Bogdanov 				     struct iscsi_login *login)
952a75fcb09SDmitry Bogdanov {
953a75fcb09SDmitry Bogdanov 	if (!iscsi_conn_auth_required(conn))
954a75fcb09SDmitry Bogdanov 		return true;
955a75fcb09SDmitry Bogdanov 
956a75fcb09SDmitry Bogdanov 	if (login->auth_complete)
957a75fcb09SDmitry Bogdanov 		return true;
958a75fcb09SDmitry Bogdanov 
959a75fcb09SDmitry Bogdanov 	return false;
960a75fcb09SDmitry Bogdanov }
961a75fcb09SDmitry Bogdanov 
962be36d683SMax Gurtovoy static int iscsi_target_handle_csg_one(struct iscsit_conn *conn, struct iscsi_login *login)
963e48354ceSNicholas Bellinger {
964e48354ceSNicholas Bellinger 	int ret;
965e48354ceSNicholas Bellinger 	u32 payload_length;
966e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
967e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
968e48354ceSNicholas Bellinger 
969e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
970e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
971e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
972e48354ceSNicholas Bellinger 
973e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
974e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
975e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
976e48354ceSNicholas Bellinger 			login->req_buf,
977e48354ceSNicholas Bellinger 			payload_length,
9789977bb18SNicholas Bellinger 			conn);
9791c5c12c6SRoland Dreier 	if (ret < 0) {
9801c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
9811c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
982e48354ceSNicholas Bellinger 		return -1;
9831c5c12c6SRoland Dreier 	}
984e48354ceSNicholas Bellinger 
985e48354ceSNicholas Bellinger 	if (login->first_request)
986e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
987e48354ceSNicholas Bellinger 			return -1;
988e48354ceSNicholas Bellinger 
989e48354ceSNicholas Bellinger 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
990e48354ceSNicholas Bellinger 		return -1;
991e48354ceSNicholas Bellinger 
992e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
993e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
994e48354ceSNicholas Bellinger 			SENDER_TARGET,
995e48354ceSNicholas Bellinger 			login->rsp_buf,
996e48354ceSNicholas Bellinger 			&login->rsp_length,
997138d351eSNicholas Bellinger 			conn->param_list,
998138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
9991c5c12c6SRoland Dreier 	if (ret < 0) {
10001c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
10011c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
1002e48354ceSNicholas Bellinger 		return -1;
10031c5c12c6SRoland Dreier 	}
1004e48354ceSNicholas Bellinger 
1005a75fcb09SDmitry Bogdanov 	if (!iscsi_conn_authenticated(conn, login)) {
1006e48354ceSNicholas Bellinger 		pr_err("Initiator is requesting CSG: 1, has not been"
1007e48354ceSNicholas Bellinger 		       " successfully authenticated, and the Target is"
1008e48354ceSNicholas Bellinger 		       " enforcing iSCSI Authentication, login failed.\n");
1009e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1010e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
1011e48354ceSNicholas Bellinger 		return -1;
1012e48354ceSNicholas Bellinger 	}
1013e48354ceSNicholas Bellinger 
1014e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list))
1015e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
1016e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
1017e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
1018e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
1019e48354ceSNicholas Bellinger 
1020e48354ceSNicholas Bellinger 	return 0;
1021e48354ceSNicholas Bellinger }
1022e48354ceSNicholas Bellinger 
1023fec1b2faSMaurizio Lombardi /*
1024fec1b2faSMaurizio Lombardi  * RETURN VALUE:
1025fec1b2faSMaurizio Lombardi  *
1026fec1b2faSMaurizio Lombardi  *  1 = Login successful
1027fec1b2faSMaurizio Lombardi  * -1 = Login failed
1028fec1b2faSMaurizio Lombardi  *  0 = More PDU exchanges required
1029fec1b2faSMaurizio Lombardi  */
1030be36d683SMax Gurtovoy static int iscsi_target_do_login(struct iscsit_conn *conn, struct iscsi_login *login)
1031e48354ceSNicholas Bellinger {
1032e48354ceSNicholas Bellinger 	int pdu_count = 0;
1033e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
1034e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
1035e48354ceSNicholas Bellinger 
1036e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1037e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
1038e48354ceSNicholas Bellinger 
1039e48354ceSNicholas Bellinger 	while (1) {
1040e48354ceSNicholas Bellinger 		if (++pdu_count > MAX_LOGIN_PDUS) {
1041e48354ceSNicholas Bellinger 			pr_err("MAX_LOGIN_PDUS count reached.\n");
1042e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1043e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
1044e48354ceSNicholas Bellinger 			return -1;
1045e48354ceSNicholas Bellinger 		}
1046e48354ceSNicholas Bellinger 
10475d358065SAndy Grover 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
1048e48354ceSNicholas Bellinger 		case 0:
10495d358065SAndy Grover 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
1050e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
1051e48354ceSNicholas Bellinger 				return -1;
1052e48354ceSNicholas Bellinger 			break;
1053e48354ceSNicholas Bellinger 		case 1:
1054e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
1055e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_one(conn, login) < 0)
1056e48354ceSNicholas Bellinger 				return -1;
1057e48354ceSNicholas Bellinger 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
105825cdda95SNicholas Bellinger 				/*
105925cdda95SNicholas Bellinger 				 * Check to make sure the TCP connection has not
106025cdda95SNicholas Bellinger 				 * dropped asynchronously while session reinstatement
106125cdda95SNicholas Bellinger 				 * was occuring in this kthread context, before
106225cdda95SNicholas Bellinger 				 * transitioning to full feature phase operation.
106325cdda95SNicholas Bellinger 				 */
106425cdda95SNicholas Bellinger 				if (iscsi_target_sk_check_close(conn))
106525cdda95SNicholas Bellinger 					return -1;
106625cdda95SNicholas Bellinger 
1067e48354ceSNicholas Bellinger 				login->tsih = conn->sess->tsih;
1068baa4d64bSNicholas Bellinger 				login->login_complete = 1;
1069d381a801SNicholas Bellinger 				iscsi_target_restore_sock_callbacks(conn);
1070e48354ceSNicholas Bellinger 				if (iscsi_target_do_tx_login_io(conn,
1071e48354ceSNicholas Bellinger 						login) < 0)
1072e48354ceSNicholas Bellinger 					return -1;
1073d381a801SNicholas Bellinger 				return 1;
1074e48354ceSNicholas Bellinger 			}
1075e48354ceSNicholas Bellinger 			break;
1076e48354ceSNicholas Bellinger 		default:
1077e48354ceSNicholas Bellinger 			pr_err("Illegal CSG: %d received from"
1078e48354ceSNicholas Bellinger 				" Initiator, protocol error.\n",
10795d358065SAndy Grover 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
1080e48354ceSNicholas Bellinger 			break;
1081e48354ceSNicholas Bellinger 		}
1082e48354ceSNicholas Bellinger 
1083ea3a179aSNicholas Bellinger 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
1084e48354ceSNicholas Bellinger 			return -1;
1085e48354ceSNicholas Bellinger 
1086e48354ceSNicholas Bellinger 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1087e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1088e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1089e48354ceSNicholas Bellinger 		}
1090d381a801SNicholas Bellinger 		break;
1091e48354ceSNicholas Bellinger 	}
1092e48354ceSNicholas Bellinger 
1093e48354ceSNicholas Bellinger 	return 0;
1094e48354ceSNicholas Bellinger }
1095e48354ceSNicholas Bellinger 
1096e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower(
1097e48354ceSNicholas Bellinger 	char *param_buf)
1098e48354ceSNicholas Bellinger {
1099e48354ceSNicholas Bellinger 	char *c;
1100e48354ceSNicholas Bellinger 	u32 iqn_size = strlen(param_buf), i;
1101e48354ceSNicholas Bellinger 
1102e48354ceSNicholas Bellinger 	for (i = 0; i < iqn_size; i++) {
11038359cf43SJörn Engel 		c = &param_buf[i];
1104e48354ceSNicholas Bellinger 		if (!isupper(*c))
1105e48354ceSNicholas Bellinger 			continue;
1106e48354ceSNicholas Bellinger 
1107e48354ceSNicholas Bellinger 		*c = tolower(*c);
1108e48354ceSNicholas Bellinger 	}
1109e48354ceSNicholas Bellinger }
1110e48354ceSNicholas Bellinger 
1111e48354ceSNicholas Bellinger /*
1112e48354ceSNicholas Bellinger  * Processes the first Login Request..
1113e48354ceSNicholas Bellinger  */
1114baa4d64bSNicholas Bellinger int iscsi_target_locate_portal(
1115e48354ceSNicholas Bellinger 	struct iscsi_np *np,
1116be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
1117e48354ceSNicholas Bellinger 	struct iscsi_login *login)
1118e48354ceSNicholas Bellinger {
1119e48354ceSNicholas Bellinger 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1120e48354ceSNicholas Bellinger 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
11210873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
1122e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1123d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = NULL;
1124e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
1125988e3a85SNicholas Bellinger 	struct se_node_acl *se_nacl;
1126988e3a85SNicholas Bellinger 	u32 payload_length, queue_depth = 0;
1127988e3a85SNicholas Bellinger 	int sessiontype = 0, ret = 0, tag_num, tag_size;
1128e48354ceSNicholas Bellinger 
1129d381a801SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1130d381a801SNicholas Bellinger 	iscsi_target_set_sock_callbacks(conn);
1131d381a801SNicholas Bellinger 
1132d381a801SNicholas Bellinger 	login->np = np;
1133d381a801SNicholas Bellinger 
1134e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1135e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
1136e48354ceSNicholas Bellinger 
11376235bef6SYang Yingliang 	tmpbuf = kmemdup_nul(login->req_buf, payload_length, GFP_KERNEL);
1138e48354ceSNicholas Bellinger 	if (!tmpbuf) {
1139e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for tmpbuf.\n");
1140e48354ceSNicholas Bellinger 		return -1;
1141e48354ceSNicholas Bellinger 	}
1142e48354ceSNicholas Bellinger 
1143e48354ceSNicholas Bellinger 	start = tmpbuf;
1144e48354ceSNicholas Bellinger 	end = (start + payload_length);
1145e48354ceSNicholas Bellinger 
1146e48354ceSNicholas Bellinger 	/*
1147e48354ceSNicholas Bellinger 	 * Locate the initial keys expected from the Initiator node in
1148e48354ceSNicholas Bellinger 	 * the first login request in order to progress with the login phase.
1149e48354ceSNicholas Bellinger 	 */
1150e48354ceSNicholas Bellinger 	while (start < end) {
1151e48354ceSNicholas Bellinger 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1152e48354ceSNicholas Bellinger 			ret = -1;
1153e48354ceSNicholas Bellinger 			goto out;
1154e48354ceSNicholas Bellinger 		}
1155e48354ceSNicholas Bellinger 
1156e48354ceSNicholas Bellinger 		if (!strncmp(key, "InitiatorName", 13))
1157e48354ceSNicholas Bellinger 			i_buf = value;
1158e48354ceSNicholas Bellinger 		else if (!strncmp(key, "SessionType", 11))
1159e48354ceSNicholas Bellinger 			s_buf = value;
1160e48354ceSNicholas Bellinger 		else if (!strncmp(key, "TargetName", 10))
1161e48354ceSNicholas Bellinger 			t_buf = value;
1162e48354ceSNicholas Bellinger 
1163e48354ceSNicholas Bellinger 		start += strlen(key) + strlen(value) + 2;
1164e48354ceSNicholas Bellinger 	}
1165e48354ceSNicholas Bellinger 	/*
1166e48354ceSNicholas Bellinger 	 * See 5.3.  Login Phase.
1167e48354ceSNicholas Bellinger 	 */
1168e48354ceSNicholas Bellinger 	if (!i_buf) {
1169e48354ceSNicholas Bellinger 		pr_err("InitiatorName key not received"
1170e48354ceSNicholas Bellinger 			" in first login request.\n");
1171e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1172e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1173e48354ceSNicholas Bellinger 		ret = -1;
1174e48354ceSNicholas Bellinger 		goto out;
1175e48354ceSNicholas Bellinger 	}
1176e48354ceSNicholas Bellinger 	/*
1177e48354ceSNicholas Bellinger 	 * Convert the incoming InitiatorName to lowercase following
1178e48354ceSNicholas Bellinger 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1179e48354ceSNicholas Bellinger 	 * are NOT case sensitive.
1180e48354ceSNicholas Bellinger 	 */
1181e48354ceSNicholas Bellinger 	iscsi_initiatorname_tolower(i_buf);
1182e48354ceSNicholas Bellinger 
1183e48354ceSNicholas Bellinger 	if (!s_buf) {
1184e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1185e48354ceSNicholas Bellinger 			goto get_target;
1186e48354ceSNicholas Bellinger 
1187e48354ceSNicholas Bellinger 		pr_err("SessionType key not received"
1188e48354ceSNicholas Bellinger 			" in first login request.\n");
1189e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1190e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1191e48354ceSNicholas Bellinger 		ret = -1;
1192e48354ceSNicholas Bellinger 		goto out;
1193e48354ceSNicholas Bellinger 	}
1194e48354ceSNicholas Bellinger 
1195e48354ceSNicholas Bellinger 	/*
1196e48354ceSNicholas Bellinger 	 * Use default portal group for discovery sessions.
1197e48354ceSNicholas Bellinger 	 */
1198e48354ceSNicholas Bellinger 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1199e48354ceSNicholas Bellinger 	if (!sessiontype) {
1200e48354ceSNicholas Bellinger 		conn->tpg = iscsit_global->discovery_tpg;
1201e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1202e48354ceSNicholas Bellinger 			goto get_target;
1203e48354ceSNicholas Bellinger 
1204e48354ceSNicholas Bellinger 		sess->sess_ops->SessionType = 1;
1205e48354ceSNicholas Bellinger 		/*
1206e48354ceSNicholas Bellinger 		 * Setup crc32c modules from libcrypto
1207e48354ceSNicholas Bellinger 		 */
1208e48354ceSNicholas Bellinger 		if (iscsi_login_setup_crypto(conn) < 0) {
1209e48354ceSNicholas Bellinger 			pr_err("iscsi_login_setup_crypto() failed\n");
1210e48354ceSNicholas Bellinger 			ret = -1;
1211e48354ceSNicholas Bellinger 			goto out;
1212e48354ceSNicholas Bellinger 		}
1213e48354ceSNicholas Bellinger 		/*
1214e48354ceSNicholas Bellinger 		 * Serialize access across the discovery struct iscsi_portal_group to
1215e48354ceSNicholas Bellinger 		 * process login attempt.
1216e48354ceSNicholas Bellinger 		 */
1217e48354ceSNicholas Bellinger 		if (iscsit_access_np(np, conn->tpg) < 0) {
1218e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1219e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1220e48354ceSNicholas Bellinger 			ret = -1;
1221e48354ceSNicholas Bellinger 			goto out;
1222e48354ceSNicholas Bellinger 		}
1223e48354ceSNicholas Bellinger 		ret = 0;
1224988e3a85SNicholas Bellinger 		goto alloc_tags;
1225e48354ceSNicholas Bellinger 	}
1226e48354ceSNicholas Bellinger 
1227e48354ceSNicholas Bellinger get_target:
1228e48354ceSNicholas Bellinger 	if (!t_buf) {
1229e48354ceSNicholas Bellinger 		pr_err("TargetName key not received"
1230e48354ceSNicholas Bellinger 			" in first login request while"
1231e48354ceSNicholas Bellinger 			" SessionType=Normal.\n");
1232e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1233e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1234e48354ceSNicholas Bellinger 		ret = -1;
1235e48354ceSNicholas Bellinger 		goto out;
1236e48354ceSNicholas Bellinger 	}
1237e48354ceSNicholas Bellinger 
1238e48354ceSNicholas Bellinger 	/*
1239e48354ceSNicholas Bellinger 	 * Locate Target IQN from Storage Node.
1240e48354ceSNicholas Bellinger 	 */
1241e48354ceSNicholas Bellinger 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1242e48354ceSNicholas Bellinger 	if (!tiqn) {
1243e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target IQN: %s in"
1244e48354ceSNicholas Bellinger 			" Storage Node\n", t_buf);
1245e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1246e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1247e48354ceSNicholas Bellinger 		ret = -1;
1248e48354ceSNicholas Bellinger 		goto out;
1249e48354ceSNicholas Bellinger 	}
1250e48354ceSNicholas Bellinger 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1251e48354ceSNicholas Bellinger 
1252e48354ceSNicholas Bellinger 	/*
1253e48354ceSNicholas Bellinger 	 * Locate Target Portal Group from Storage Node.
1254e48354ceSNicholas Bellinger 	 */
1255d381a801SNicholas Bellinger 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1256e48354ceSNicholas Bellinger 	if (!conn->tpg) {
1257e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target Portal Group"
1258e48354ceSNicholas Bellinger 				" on %s\n", tiqn->tiqn);
1259e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1260e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1261e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1262e48354ceSNicholas Bellinger 		ret = -1;
1263e48354ceSNicholas Bellinger 		goto out;
1264e48354ceSNicholas Bellinger 	}
1265d381a801SNicholas Bellinger 	conn->tpg_np = tpg_np;
1266e48354ceSNicholas Bellinger 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1267e48354ceSNicholas Bellinger 	/*
1268e48354ceSNicholas Bellinger 	 * Setup crc32c modules from libcrypto
1269e48354ceSNicholas Bellinger 	 */
1270e48354ceSNicholas Bellinger 	if (iscsi_login_setup_crypto(conn) < 0) {
1271e48354ceSNicholas Bellinger 		pr_err("iscsi_login_setup_crypto() failed\n");
1272d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1273d381a801SNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1274d381a801SNicholas Bellinger 		conn->tpg = NULL;
1275e48354ceSNicholas Bellinger 		ret = -1;
1276e48354ceSNicholas Bellinger 		goto out;
1277e48354ceSNicholas Bellinger 	}
1278e48354ceSNicholas Bellinger 	/*
1279e48354ceSNicholas Bellinger 	 * Serialize access across the struct iscsi_portal_group to
1280e48354ceSNicholas Bellinger 	 * process login attempt.
1281e48354ceSNicholas Bellinger 	 */
1282e48354ceSNicholas Bellinger 	if (iscsit_access_np(np, conn->tpg) < 0) {
1283d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1284e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1285e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1286e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1287e48354ceSNicholas Bellinger 		conn->tpg = NULL;
1288d381a801SNicholas Bellinger 		ret = -1;
1289e48354ceSNicholas Bellinger 		goto out;
1290e48354ceSNicholas Bellinger 	}
1291e48354ceSNicholas Bellinger 
1292e48354ceSNicholas Bellinger 	/*
1293e48354ceSNicholas Bellinger 	 * conn->sess->node_acl will be set when the referenced
12940873fe44SMax Gurtovoy 	 * struct iscsit_session is located from received ISID+TSIH in
1295e48354ceSNicholas Bellinger 	 * iscsi_login_non_zero_tsih_s2().
1296e48354ceSNicholas Bellinger 	 */
1297e48354ceSNicholas Bellinger 	if (!login->leading_connection) {
1298e48354ceSNicholas Bellinger 		ret = 0;
1299e48354ceSNicholas Bellinger 		goto out;
1300e48354ceSNicholas Bellinger 	}
1301e48354ceSNicholas Bellinger 
1302e48354ceSNicholas Bellinger 	/*
1303e48354ceSNicholas Bellinger 	 * This value is required in iscsi_login_zero_tsih_s2()
1304e48354ceSNicholas Bellinger 	 */
1305e48354ceSNicholas Bellinger 	sess->sess_ops->SessionType = 0;
1306e48354ceSNicholas Bellinger 
1307e48354ceSNicholas Bellinger 	/*
1308e48354ceSNicholas Bellinger 	 * Locate incoming Initiator IQN reference from Storage Node.
1309e48354ceSNicholas Bellinger 	 */
1310e48354ceSNicholas Bellinger 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1311e48354ceSNicholas Bellinger 			&conn->tpg->tpg_se_tpg, i_buf);
1312e48354ceSNicholas Bellinger 	if (!sess->se_sess->se_node_acl) {
1313e48354ceSNicholas Bellinger 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1314e48354ceSNicholas Bellinger 			" access iSCSI target portal group: %hu.\n",
1315e48354ceSNicholas Bellinger 				i_buf, conn->tpg->tpgt);
1316e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1317e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1318e48354ceSNicholas Bellinger 		ret = -1;
1319e48354ceSNicholas Bellinger 		goto out;
1320e48354ceSNicholas Bellinger 	}
1321988e3a85SNicholas Bellinger 	se_nacl = sess->se_sess->se_node_acl;
1322988e3a85SNicholas Bellinger 	queue_depth = se_nacl->queue_depth;
1323988e3a85SNicholas Bellinger 	/*
1324988e3a85SNicholas Bellinger 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1325988e3a85SNicholas Bellinger 	 * depth for non immediate commands, plus extra tags for immediate
1326988e3a85SNicholas Bellinger 	 * commands.
1327988e3a85SNicholas Bellinger 	 *
1328988e3a85SNicholas Bellinger 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1329988e3a85SNicholas Bellinger 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1330988e3a85SNicholas Bellinger 	 */
1331988e3a85SNicholas Bellinger alloc_tags:
1332988e3a85SNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
13334a4caa29SNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
133466cd9d4eSMax Gurtovoy 	tag_size = sizeof(struct iscsit_cmd) + conn->conn_transport->priv_size;
1335e48354ceSNicholas Bellinger 
1336988e3a85SNicholas Bellinger 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1337988e3a85SNicholas Bellinger 	if (ret < 0) {
1338988e3a85SNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1339988e3a85SNicholas Bellinger 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1340988e3a85SNicholas Bellinger 		ret = -1;
1341988e3a85SNicholas Bellinger 	}
1342e48354ceSNicholas Bellinger out:
1343e48354ceSNicholas Bellinger 	kfree(tmpbuf);
1344e48354ceSNicholas Bellinger 	return ret;
1345e48354ceSNicholas Bellinger }
1346e48354ceSNicholas Bellinger 
1347e48354ceSNicholas Bellinger int iscsi_target_start_negotiation(
1348e48354ceSNicholas Bellinger 	struct iscsi_login *login,
1349be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
1350e48354ceSNicholas Bellinger {
1351baa4d64bSNicholas Bellinger 	int ret;
1352e48354ceSNicholas Bellinger 
1353d381a801SNicholas Bellinger 	if (conn->sock) {
1354d381a801SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
1355e48354ceSNicholas Bellinger 
1356d381a801SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
1357d381a801SNicholas Bellinger 		set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
135825cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1359d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
1360d381a801SNicholas Bellinger 	}
136125cdda95SNicholas Bellinger 	/*
136225cdda95SNicholas Bellinger 	 * If iscsi_target_do_login returns zero to signal more PDU
136325cdda95SNicholas Bellinger 	 * exchanges are required to complete the login, go ahead and
136425cdda95SNicholas Bellinger 	 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
136525cdda95SNicholas Bellinger 	 * is still active.
136625cdda95SNicholas Bellinger 	 *
136725cdda95SNicholas Bellinger 	 * Otherwise if TCP connection dropped asynchronously, go ahead
136825cdda95SNicholas Bellinger 	 * and perform connection cleanup now.
136925cdda95SNicholas Bellinger 	 */
13708f0dfb3dSNicholas Bellinger 	ret = iscsi_target_do_login(conn, login);
137125cdda95SNicholas Bellinger 	if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
137225cdda95SNicholas Bellinger 		ret = -1;
137325cdda95SNicholas Bellinger 
13748f0dfb3dSNicholas Bellinger 	if (ret < 0) {
1375d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
1376d381a801SNicholas Bellinger 		iscsi_remove_failed_auth_entry(conn);
1377d381a801SNicholas Bellinger 	}
1378fec1b2faSMaurizio Lombardi 	if (ret != 0) {
1379fec1b2faSMaurizio Lombardi 		cancel_delayed_work_sync(&conn->login_work);
1380baa4d64bSNicholas Bellinger 		iscsi_target_nego_release(conn);
1381fec1b2faSMaurizio Lombardi 	}
1382d381a801SNicholas Bellinger 
1383e48354ceSNicholas Bellinger 	return ret;
1384e48354ceSNicholas Bellinger }
1385e48354ceSNicholas Bellinger 
1386be36d683SMax Gurtovoy void iscsi_target_nego_release(struct iscsit_conn *conn)
1387e48354ceSNicholas Bellinger {
1388baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1389baa4d64bSNicholas Bellinger 
1390baa4d64bSNicholas Bellinger 	if (!login)
1391baa4d64bSNicholas Bellinger 		return;
1392baa4d64bSNicholas Bellinger 
1393e48354ceSNicholas Bellinger 	kfree(login->req_buf);
1394e48354ceSNicholas Bellinger 	kfree(login->rsp_buf);
1395e48354ceSNicholas Bellinger 	kfree(login);
1396baa4d64bSNicholas Bellinger 
1397baa4d64bSNicholas Bellinger 	conn->conn_login = NULL;
1398e48354ceSNicholas Bellinger }
1399