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 
97a75fcb09SDmitry Bogdanov static struct iscsi_node_auth *iscsi_get_node_auth(struct iscsit_conn *conn)
98a75fcb09SDmitry Bogdanov {
99a75fcb09SDmitry Bogdanov 	struct iscsi_portal_group *tpg;
100a75fcb09SDmitry Bogdanov 	struct iscsi_node_acl *nacl;
101a75fcb09SDmitry Bogdanov 	struct se_node_acl *se_nacl;
102a75fcb09SDmitry Bogdanov 
103a75fcb09SDmitry Bogdanov 	if (conn->sess->sess_ops->SessionType)
104a75fcb09SDmitry Bogdanov 		return &iscsit_global->discovery_acl.node_auth;
105a75fcb09SDmitry Bogdanov 
106a75fcb09SDmitry Bogdanov 	se_nacl = conn->sess->se_sess->se_node_acl;
107a75fcb09SDmitry Bogdanov 	if (!se_nacl) {
108a75fcb09SDmitry Bogdanov 		pr_err("Unable to locate struct se_node_acl for CHAP auth\n");
109a75fcb09SDmitry Bogdanov 		return NULL;
110a75fcb09SDmitry Bogdanov 	}
111a75fcb09SDmitry Bogdanov 
112a75fcb09SDmitry Bogdanov 	if (se_nacl->dynamic_node_acl) {
113a75fcb09SDmitry Bogdanov 		tpg = to_iscsi_tpg(se_nacl->se_tpg);
114a75fcb09SDmitry Bogdanov 		return &tpg->tpg_demo_auth;
115a75fcb09SDmitry Bogdanov 	}
116a75fcb09SDmitry Bogdanov 
117a75fcb09SDmitry Bogdanov 	nacl = to_iscsi_nacl(se_nacl);
118a75fcb09SDmitry Bogdanov 
119a75fcb09SDmitry Bogdanov 	return &nacl->node_auth;
120a75fcb09SDmitry Bogdanov }
121a75fcb09SDmitry Bogdanov 
122e48354ceSNicholas Bellinger static u32 iscsi_handle_authentication(
123be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
124e48354ceSNicholas Bellinger 	char *in_buf,
125e48354ceSNicholas Bellinger 	char *out_buf,
126e48354ceSNicholas Bellinger 	int in_length,
127e48354ceSNicholas Bellinger 	int *out_length,
128e48354ceSNicholas Bellinger 	unsigned char *authtype)
129e48354ceSNicholas Bellinger {
130e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth;
131e48354ceSNicholas Bellinger 
132a75fcb09SDmitry Bogdanov 	auth = iscsi_get_node_auth(conn);
133a75fcb09SDmitry Bogdanov 	if (!auth)
134e48354ceSNicholas Bellinger 		return -1;
135e48354ceSNicholas Bellinger 
136e48354ceSNicholas Bellinger 	if (strstr("CHAP", authtype))
137e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, "CHAP");
138e48354ceSNicholas Bellinger 	else
139e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, NONE);
140e48354ceSNicholas Bellinger 
141e48354ceSNicholas Bellinger 	if (strstr("None", authtype))
142e48354ceSNicholas Bellinger 		return 1;
143e48354ceSNicholas Bellinger 	else if (strstr("CHAP", authtype))
144e48354ceSNicholas Bellinger 		return chap_main_loop(conn, auth, in_buf, out_buf,
145e48354ceSNicholas Bellinger 				&in_length, out_length);
1468a914f32SHariprasad Kelam 	/* SRP, SPKM1, SPKM2 and KRB5 are unsupported */
147e48354ceSNicholas Bellinger 	return 2;
148e48354ceSNicholas Bellinger }
149e48354ceSNicholas Bellinger 
150be36d683SMax Gurtovoy static void iscsi_remove_failed_auth_entry(struct iscsit_conn *conn)
151e48354ceSNicholas Bellinger {
152e48354ceSNicholas Bellinger 	kfree(conn->auth_protocol);
153e48354ceSNicholas Bellinger }
154e48354ceSNicholas Bellinger 
155baa4d64bSNicholas Bellinger int iscsi_target_check_login_request(
156be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
157e48354ceSNicholas Bellinger 	struct iscsi_login *login)
158e48354ceSNicholas Bellinger {
15928168905SJörn Engel 	int req_csg, req_nsg;
160e48354ceSNicholas Bellinger 	u32 payload_length;
161e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
162e48354ceSNicholas Bellinger 
163e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
164e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
165e48354ceSNicholas Bellinger 
166e48354ceSNicholas Bellinger 	switch (login_req->opcode & ISCSI_OPCODE_MASK) {
167e48354ceSNicholas Bellinger 	case ISCSI_OP_LOGIN:
168e48354ceSNicholas Bellinger 		break;
169e48354ceSNicholas Bellinger 	default:
170e48354ceSNicholas Bellinger 		pr_err("Received unknown opcode 0x%02x.\n",
171e48354ceSNicholas Bellinger 				login_req->opcode & ISCSI_OPCODE_MASK);
172e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
173e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
174e48354ceSNicholas Bellinger 		return -1;
175e48354ceSNicholas Bellinger 	}
176e48354ceSNicholas Bellinger 
177e48354ceSNicholas Bellinger 	if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
178e48354ceSNicholas Bellinger 	    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
179e48354ceSNicholas Bellinger 		pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
180e48354ceSNicholas Bellinger 			" and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
181e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
182e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
183e48354ceSNicholas Bellinger 		return -1;
184e48354ceSNicholas Bellinger 	}
185e48354ceSNicholas Bellinger 
1865d358065SAndy Grover 	req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1875d358065SAndy Grover 	req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
188e48354ceSNicholas Bellinger 
189e48354ceSNicholas Bellinger 	if (req_csg != login->current_stage) {
190e48354ceSNicholas Bellinger 		pr_err("Initiator unexpectedly changed login stage"
191e48354ceSNicholas Bellinger 			" from %d to %d, login failed.\n", login->current_stage,
192e48354ceSNicholas Bellinger 			req_csg);
193e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
194e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
195e48354ceSNicholas Bellinger 		return -1;
196e48354ceSNicholas Bellinger 	}
197e48354ceSNicholas Bellinger 
198e48354ceSNicholas Bellinger 	if ((req_nsg == 2) || (req_csg >= 2) ||
199e48354ceSNicholas Bellinger 	   ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
200e48354ceSNicholas Bellinger 	    (req_nsg <= req_csg))) {
201e48354ceSNicholas Bellinger 		pr_err("Illegal login_req->flags Combination, CSG: %d,"
202e48354ceSNicholas Bellinger 			" NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
203e48354ceSNicholas Bellinger 			req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
204e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
205e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
206e48354ceSNicholas Bellinger 		return -1;
207e48354ceSNicholas Bellinger 	}
208e48354ceSNicholas Bellinger 
209e48354ceSNicholas Bellinger 	if ((login_req->max_version != login->version_max) ||
210e48354ceSNicholas Bellinger 	    (login_req->min_version != login->version_min)) {
211e48354ceSNicholas Bellinger 		pr_err("Login request changed Version Max/Nin"
212e48354ceSNicholas Bellinger 			" unexpectedly to 0x%02x/0x%02x, protocol error\n",
213e48354ceSNicholas Bellinger 			login_req->max_version, login_req->min_version);
214e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
215e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
216e48354ceSNicholas Bellinger 		return -1;
217e48354ceSNicholas Bellinger 	}
218e48354ceSNicholas Bellinger 
219e48354ceSNicholas Bellinger 	if (memcmp(login_req->isid, login->isid, 6) != 0) {
220e48354ceSNicholas Bellinger 		pr_err("Login request changed ISID unexpectedly,"
221e48354ceSNicholas Bellinger 				" protocol error.\n");
222e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
223e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
224e48354ceSNicholas Bellinger 		return -1;
225e48354ceSNicholas Bellinger 	}
226e48354ceSNicholas Bellinger 
227e48354ceSNicholas Bellinger 	if (login_req->itt != login->init_task_tag) {
228e48354ceSNicholas Bellinger 		pr_err("Login request changed ITT unexpectedly to"
229e48354ceSNicholas Bellinger 			" 0x%08x, protocol error.\n", login_req->itt);
230e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
231e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
232e48354ceSNicholas Bellinger 		return -1;
233e48354ceSNicholas Bellinger 	}
234e48354ceSNicholas Bellinger 
235e48354ceSNicholas Bellinger 	if (payload_length > MAX_KEY_VALUE_PAIRS) {
236e48354ceSNicholas Bellinger 		pr_err("Login request payload exceeds default"
237e48354ceSNicholas Bellinger 			" MaxRecvDataSegmentLength: %u, protocol error.\n",
238e48354ceSNicholas Bellinger 				MAX_KEY_VALUE_PAIRS);
239e48354ceSNicholas Bellinger 		return -1;
240e48354ceSNicholas Bellinger 	}
241e48354ceSNicholas Bellinger 
242e48354ceSNicholas Bellinger 	return 0;
243e48354ceSNicholas Bellinger }
244d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_target_check_login_request);
245e48354ceSNicholas Bellinger 
246e48354ceSNicholas Bellinger static int iscsi_target_check_first_request(
247be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
248e48354ceSNicholas Bellinger 	struct iscsi_login *login)
249e48354ceSNicholas Bellinger {
250e48354ceSNicholas Bellinger 	struct iscsi_param *param = NULL;
251e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
252e48354ceSNicholas Bellinger 
253e48354ceSNicholas Bellinger 	login->first_request = 0;
254e48354ceSNicholas Bellinger 
255e48354ceSNicholas Bellinger 	list_for_each_entry(param, &conn->param_list->param_list, p_list) {
256e48354ceSNicholas Bellinger 		if (!strncmp(param->name, SESSIONTYPE, 11)) {
257e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
258e48354ceSNicholas Bellinger 				pr_err("SessionType key not received"
259e48354ceSNicholas Bellinger 					" in first login request.\n");
260e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
261e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
262e48354ceSNicholas Bellinger 				return -1;
263e48354ceSNicholas Bellinger 			}
264e48354ceSNicholas Bellinger 			if (!strncmp(param->value, DISCOVERY, 9))
265e48354ceSNicholas Bellinger 				return 0;
266e48354ceSNicholas Bellinger 		}
267e48354ceSNicholas Bellinger 
268e48354ceSNicholas Bellinger 		if (!strncmp(param->name, INITIATORNAME, 13)) {
269e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
270e48354ceSNicholas Bellinger 				if (!login->leading_connection)
271e48354ceSNicholas Bellinger 					continue;
272e48354ceSNicholas Bellinger 
273e48354ceSNicholas Bellinger 				pr_err("InitiatorName key not received"
274e48354ceSNicholas Bellinger 					" in first login request.\n");
275e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
276e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
277e48354ceSNicholas Bellinger 				return -1;
278e48354ceSNicholas Bellinger 			}
279e48354ceSNicholas Bellinger 
280e48354ceSNicholas Bellinger 			/*
281e48354ceSNicholas Bellinger 			 * For non-leading connections, double check that the
282e48354ceSNicholas Bellinger 			 * received InitiatorName matches the existing session's
283e48354ceSNicholas Bellinger 			 * struct iscsi_node_acl.
284e48354ceSNicholas Bellinger 			 */
285e48354ceSNicholas Bellinger 			if (!login->leading_connection) {
286e48354ceSNicholas Bellinger 				se_nacl = conn->sess->se_sess->se_node_acl;
287e48354ceSNicholas Bellinger 				if (!se_nacl) {
288e48354ceSNicholas Bellinger 					pr_err("Unable to locate"
289e48354ceSNicholas Bellinger 						" struct se_node_acl\n");
290e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
291e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
292e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
293e48354ceSNicholas Bellinger 					return -1;
294e48354ceSNicholas Bellinger 				}
295e48354ceSNicholas Bellinger 
296e48354ceSNicholas Bellinger 				if (strcmp(param->value,
297e48354ceSNicholas Bellinger 						se_nacl->initiatorname)) {
298e48354ceSNicholas Bellinger 					pr_err("Incorrect"
299e48354ceSNicholas Bellinger 						" InitiatorName: %s for this"
300e48354ceSNicholas Bellinger 						" iSCSI Initiator Node.\n",
301e48354ceSNicholas Bellinger 						param->value);
302e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
303e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
304e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
305e48354ceSNicholas Bellinger 					return -1;
306e48354ceSNicholas Bellinger 				}
307e48354ceSNicholas Bellinger 			}
308e48354ceSNicholas Bellinger 		}
309e48354ceSNicholas Bellinger 	}
310e48354ceSNicholas Bellinger 
311e48354ceSNicholas Bellinger 	return 0;
312e48354ceSNicholas Bellinger }
313e48354ceSNicholas Bellinger 
314be36d683SMax Gurtovoy static int iscsi_target_do_tx_login_io(struct iscsit_conn *conn, struct iscsi_login *login)
315e48354ceSNicholas Bellinger {
316e48354ceSNicholas Bellinger 	u32 padding = 0;
317e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
318e48354ceSNicholas Bellinger 
319e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
320e48354ceSNicholas Bellinger 
321e48354ceSNicholas Bellinger 	login_rsp->opcode		= ISCSI_OP_LOGIN_RSP;
322e48354ceSNicholas Bellinger 	hton24(login_rsp->dlength, login->rsp_length);
323e48354ceSNicholas Bellinger 	memcpy(login_rsp->isid, login->isid, 6);
324e48354ceSNicholas Bellinger 	login_rsp->tsih			= cpu_to_be16(login->tsih);
32566c7db68SChristoph Hellwig 	login_rsp->itt			= login->init_task_tag;
326e48354ceSNicholas Bellinger 	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
327e48354ceSNicholas Bellinger 	login_rsp->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
328109e2381SRoland Dreier 	login_rsp->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
329e48354ceSNicholas Bellinger 
330e48354ceSNicholas Bellinger 	pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
331e48354ceSNicholas Bellinger 		" ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
33266c7db68SChristoph Hellwig 		" %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
333e48354ceSNicholas Bellinger 		ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
334e48354ceSNicholas Bellinger 		ntohl(login_rsp->statsn), login->rsp_length);
335e48354ceSNicholas Bellinger 
336e48354ceSNicholas Bellinger 	padding = ((-login->rsp_length) & 3);
337e5419865SNicholas Bellinger 	/*
338e5419865SNicholas Bellinger 	 * Before sending the last login response containing the transition
339e5419865SNicholas Bellinger 	 * bit for full-feature-phase, go ahead and start up TX/RX threads
340e5419865SNicholas Bellinger 	 * now to avoid potential resource allocation failures after the
341e5419865SNicholas Bellinger 	 * final login response has been sent.
342e5419865SNicholas Bellinger 	 */
343e5419865SNicholas Bellinger 	if (login->login_complete) {
344e5419865SNicholas Bellinger 		int rc = iscsit_start_kthreads(conn);
345e5419865SNicholas Bellinger 		if (rc) {
346e5419865SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
347e5419865SNicholas Bellinger 					    ISCSI_LOGIN_STATUS_NO_RESOURCES);
348e5419865SNicholas Bellinger 			return -1;
349e5419865SNicholas Bellinger 		}
350e5419865SNicholas Bellinger 	}
351e48354ceSNicholas Bellinger 
352baa4d64bSNicholas Bellinger 	if (conn->conn_transport->iscsit_put_login_tx(conn, login,
353e48354ceSNicholas Bellinger 					login->rsp_length + padding) < 0)
354e5419865SNicholas Bellinger 		goto err;
355e48354ceSNicholas Bellinger 
356e48354ceSNicholas Bellinger 	login->rsp_length		= 0;
357e48354ceSNicholas Bellinger 
358e48354ceSNicholas Bellinger 	return 0;
359e5419865SNicholas Bellinger 
360e5419865SNicholas Bellinger err:
361e5419865SNicholas Bellinger 	if (login->login_complete) {
362e5419865SNicholas Bellinger 		if (conn->rx_thread && conn->rx_thread_active) {
363e5419865SNicholas Bellinger 			send_sig(SIGINT, conn->rx_thread, 1);
364ca82c2bdSNicholas Bellinger 			complete(&conn->rx_login_comp);
365e5419865SNicholas Bellinger 			kthread_stop(conn->rx_thread);
366e5419865SNicholas Bellinger 		}
367e5419865SNicholas Bellinger 		if (conn->tx_thread && conn->tx_thread_active) {
368e5419865SNicholas Bellinger 			send_sig(SIGINT, conn->tx_thread, 1);
369e5419865SNicholas Bellinger 			kthread_stop(conn->tx_thread);
370e5419865SNicholas Bellinger 		}
371e5419865SNicholas Bellinger 		spin_lock(&iscsit_global->ts_bitmap_lock);
372e5419865SNicholas Bellinger 		bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
373e5419865SNicholas Bellinger 				      get_order(1));
374e5419865SNicholas Bellinger 		spin_unlock(&iscsit_global->ts_bitmap_lock);
375e5419865SNicholas Bellinger 	}
376e5419865SNicholas Bellinger 	return -1;
377e48354ceSNicholas Bellinger }
378e48354ceSNicholas Bellinger 
379676d2369SDavid S. Miller static void iscsi_target_sk_data_ready(struct sock *sk)
380d381a801SNicholas Bellinger {
381be36d683SMax Gurtovoy 	struct iscsit_conn *conn = sk->sk_user_data;
382d381a801SNicholas Bellinger 	bool rc;
383d381a801SNicholas Bellinger 
384d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
385d381a801SNicholas Bellinger 
386d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
387d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
388d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
389d381a801SNicholas Bellinger 		return;
390d381a801SNicholas Bellinger 	}
391d381a801SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
392d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
393d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
394d381a801SNicholas Bellinger 		return;
395d381a801SNicholas Bellinger 	}
396d381a801SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
397d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
398d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
399d381a801SNicholas Bellinger 		return;
400d381a801SNicholas Bellinger 	}
401d381a801SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
402d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
403d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
4041c130ae0SFlorian Westphal 		if (iscsi_target_sk_data_ready == conn->orig_data_ready)
4051c130ae0SFlorian Westphal 			return;
4061c130ae0SFlorian Westphal 		conn->orig_data_ready(sk);
407d381a801SNicholas Bellinger 		return;
408d381a801SNicholas Bellinger 	}
409d381a801SNicholas Bellinger 
410d381a801SNicholas Bellinger 	rc = schedule_delayed_work(&conn->login_work, 0);
4110bcc297eSChristophe Vu-Brugier 	if (!rc) {
412d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
413d381a801SNicholas Bellinger 			 " got false\n");
414d381a801SNicholas Bellinger 	}
415d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
416d381a801SNicholas Bellinger }
417d381a801SNicholas Bellinger 
418bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *);
419bb048357SNicholas Bellinger 
420be36d683SMax Gurtovoy static void iscsi_target_set_sock_callbacks(struct iscsit_conn *conn)
421d381a801SNicholas Bellinger {
422d381a801SNicholas Bellinger 	struct sock *sk;
423d381a801SNicholas Bellinger 
424d381a801SNicholas Bellinger 	if (!conn->sock)
425d381a801SNicholas Bellinger 		return;
426d381a801SNicholas Bellinger 
427d381a801SNicholas Bellinger 	sk = conn->sock->sk;
428d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
429d381a801SNicholas Bellinger 
430d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
431d381a801SNicholas Bellinger 	sk->sk_user_data = conn;
432d381a801SNicholas Bellinger 	conn->orig_data_ready = sk->sk_data_ready;
433bb048357SNicholas Bellinger 	conn->orig_state_change = sk->sk_state_change;
434d381a801SNicholas Bellinger 	sk->sk_data_ready = iscsi_target_sk_data_ready;
435bb048357SNicholas Bellinger 	sk->sk_state_change = iscsi_target_sk_state_change;
436d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
437bb048357SNicholas Bellinger 
438bb048357SNicholas Bellinger 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
439bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
440d381a801SNicholas Bellinger }
441d381a801SNicholas Bellinger 
442be36d683SMax Gurtovoy static void iscsi_target_restore_sock_callbacks(struct iscsit_conn *conn)
443d381a801SNicholas Bellinger {
444d381a801SNicholas Bellinger 	struct sock *sk;
445d381a801SNicholas Bellinger 
446d381a801SNicholas Bellinger 	if (!conn->sock)
447d381a801SNicholas Bellinger 		return;
448d381a801SNicholas Bellinger 
449d381a801SNicholas Bellinger 	sk = conn->sock->sk;
450d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
451d381a801SNicholas Bellinger 
452d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
453d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
454d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
455d381a801SNicholas Bellinger 		return;
456d381a801SNicholas Bellinger 	}
457d381a801SNicholas Bellinger 	sk->sk_user_data = NULL;
458d381a801SNicholas Bellinger 	sk->sk_data_ready = conn->orig_data_ready;
459bb048357SNicholas Bellinger 	sk->sk_state_change = conn->orig_state_change;
460d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
461bb048357SNicholas Bellinger 
462bb048357SNicholas Bellinger 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
463bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
464d381a801SNicholas Bellinger }
465d381a801SNicholas Bellinger 
466be36d683SMax Gurtovoy static int iscsi_target_do_login(struct iscsit_conn *, struct iscsi_login *);
467d381a801SNicholas Bellinger 
46825cdda95SNicholas Bellinger static bool __iscsi_target_sk_check_close(struct sock *sk)
469d381a801SNicholas Bellinger {
470d381a801SNicholas Bellinger 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
47125cdda95SNicholas Bellinger 		pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
472df2de6f2SHou Pu 			"returning TRUE\n");
47325cdda95SNicholas Bellinger 		return true;
47425cdda95SNicholas Bellinger 	}
475d381a801SNicholas Bellinger 	return false;
476d381a801SNicholas Bellinger }
47725cdda95SNicholas Bellinger 
478be36d683SMax Gurtovoy static bool iscsi_target_sk_check_close(struct iscsit_conn *conn)
47925cdda95SNicholas Bellinger {
48025cdda95SNicholas Bellinger 	bool state = false;
48125cdda95SNicholas Bellinger 
48225cdda95SNicholas Bellinger 	if (conn->sock) {
48325cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
48425cdda95SNicholas Bellinger 
48525cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
48625cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
48725cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
48825cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
48925cdda95SNicholas Bellinger 	}
49025cdda95SNicholas Bellinger 	return state;
49125cdda95SNicholas Bellinger }
49225cdda95SNicholas Bellinger 
493be36d683SMax Gurtovoy static bool iscsi_target_sk_check_flag(struct iscsit_conn *conn, unsigned int flag)
49425cdda95SNicholas Bellinger {
49525cdda95SNicholas Bellinger 	bool state = false;
49625cdda95SNicholas Bellinger 
49725cdda95SNicholas Bellinger 	if (conn->sock) {
49825cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
49925cdda95SNicholas Bellinger 
50025cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
50125cdda95SNicholas Bellinger 		state = test_bit(flag, &conn->login_flags);
50225cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
50325cdda95SNicholas Bellinger 	}
50425cdda95SNicholas Bellinger 	return state;
50525cdda95SNicholas Bellinger }
50625cdda95SNicholas Bellinger 
507be36d683SMax Gurtovoy static bool iscsi_target_sk_check_and_clear(struct iscsit_conn *conn, unsigned int flag)
50825cdda95SNicholas Bellinger {
50925cdda95SNicholas Bellinger 	bool state = false;
51025cdda95SNicholas Bellinger 
51125cdda95SNicholas Bellinger 	if (conn->sock) {
51225cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
51325cdda95SNicholas Bellinger 
51425cdda95SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
51525cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
51625cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
51725cdda95SNicholas Bellinger 		if (!state)
51825cdda95SNicholas Bellinger 			clear_bit(flag, &conn->login_flags);
51925cdda95SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
52025cdda95SNicholas Bellinger 	}
52125cdda95SNicholas Bellinger 	return state;
522d381a801SNicholas Bellinger }
523d381a801SNicholas Bellinger 
524be36d683SMax Gurtovoy static void iscsi_target_login_drop(struct iscsit_conn *conn, struct iscsi_login *login)
525d381a801SNicholas Bellinger {
526d381a801SNicholas Bellinger 	bool zero_tsih = login->zero_tsih;
527d381a801SNicholas Bellinger 
528d381a801SNicholas Bellinger 	iscsi_remove_failed_auth_entry(conn);
529d381a801SNicholas Bellinger 	iscsi_target_nego_release(conn);
530ed43ffeaSHou Pu 	iscsi_target_login_sess_out(conn, zero_tsih, true);
531d381a801SNicholas Bellinger }
532d381a801SNicholas Bellinger 
533f7c9564aSKees Cook struct conn_timeout {
534f7c9564aSKees Cook 	struct timer_list timer;
535be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
536f7c9564aSKees Cook };
537f7c9564aSKees Cook 
538f7c9564aSKees Cook static void iscsi_target_login_timeout(struct timer_list *t)
539d381a801SNicholas Bellinger {
540f7c9564aSKees Cook 	struct conn_timeout *timeout = from_timer(timeout, t, timer);
541be36d683SMax Gurtovoy 	struct iscsit_conn *conn = timeout->conn;
542d381a801SNicholas Bellinger 
543d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
544d381a801SNicholas Bellinger 
545d381a801SNicholas Bellinger 	if (conn->login_kworker) {
546d381a801SNicholas Bellinger 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
547d381a801SNicholas Bellinger 			 conn->login_kworker->comm, conn->login_kworker->pid);
548d381a801SNicholas Bellinger 		send_sig(SIGINT, conn->login_kworker, 1);
549d381a801SNicholas Bellinger 	}
550d381a801SNicholas Bellinger }
551d381a801SNicholas Bellinger 
552d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work)
553d381a801SNicholas Bellinger {
554be36d683SMax Gurtovoy 	struct iscsit_conn *conn = container_of(work,
555be36d683SMax Gurtovoy 				struct iscsit_conn, login_work.work);
556d381a801SNicholas Bellinger 	struct iscsi_login *login = conn->login;
557d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
558d381a801SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
559d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
560f7c9564aSKees Cook 	struct conn_timeout timeout;
561d381a801SNicholas Bellinger 	int rc, zero_tsih = login->zero_tsih;
562d381a801SNicholas Bellinger 	bool state;
563d381a801SNicholas Bellinger 
564d381a801SNicholas Bellinger 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
565d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
56625cdda95SNicholas Bellinger 	/*
56725cdda95SNicholas Bellinger 	 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
56825cdda95SNicholas Bellinger 	 * before initial PDU processing in iscsi_target_start_negotiation()
56925cdda95SNicholas Bellinger 	 * has completed, go ahead and retry until it's cleared.
57025cdda95SNicholas Bellinger 	 *
57125cdda95SNicholas Bellinger 	 * Otherwise if the TCP connection drops while this is occuring,
57225cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() will detect the failure, call
57325cdda95SNicholas Bellinger 	 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
57425cdda95SNicholas Bellinger 	 * remaining iscsi connection resources from iscsi_np process context.
57525cdda95SNicholas Bellinger 	 */
57625cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
57725cdda95SNicholas Bellinger 		schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
57825cdda95SNicholas Bellinger 		return;
57925cdda95SNicholas Bellinger 	}
580d381a801SNicholas Bellinger 
581d381a801SNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
582d381a801SNicholas Bellinger 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
583d381a801SNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
584d381a801SNicholas Bellinger 
5850bcc297eSChristophe Vu-Brugier 	if (!state) {
586d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
58725cdda95SNicholas Bellinger 		goto err;
588d381a801SNicholas Bellinger 	}
589d381a801SNicholas Bellinger 
59025cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_close(conn)) {
591d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
59225cdda95SNicholas Bellinger 		goto err;
593d381a801SNicholas Bellinger 	}
594d381a801SNicholas Bellinger 
595d381a801SNicholas Bellinger 	conn->login_kworker = current;
596d381a801SNicholas Bellinger 	allow_signal(SIGINT);
597d381a801SNicholas Bellinger 
598f7c9564aSKees Cook 	timeout.conn = conn;
599f7c9564aSKees Cook 	timer_setup_on_stack(&timeout.timer, iscsi_target_login_timeout, 0);
600f7c9564aSKees Cook 	mod_timer(&timeout.timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
601f7c9564aSKees Cook 	pr_debug("Starting login timer for %s/%d\n", current->comm, current->pid);
602d381a801SNicholas Bellinger 
603d381a801SNicholas Bellinger 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
604f7c9564aSKees Cook 	del_timer_sync(&timeout.timer);
605f7c9564aSKees Cook 	destroy_timer_on_stack(&timeout.timer);
606d381a801SNicholas Bellinger 	flush_signals(current);
607d381a801SNicholas Bellinger 	conn->login_kworker = NULL;
608d381a801SNicholas Bellinger 
60925cdda95SNicholas Bellinger 	if (rc < 0)
61025cdda95SNicholas Bellinger 		goto err;
611d381a801SNicholas Bellinger 
612d381a801SNicholas Bellinger 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
613d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
614d381a801SNicholas Bellinger 
6154e108d4fSHou Pu 	/*
6164e108d4fSHou Pu 	 * LOGIN_FLAGS_READ_ACTIVE is cleared so that sk_data_ready
6174e108d4fSHou Pu 	 * could be triggered again after this.
6184e108d4fSHou Pu 	 *
6194e108d4fSHou Pu 	 * LOGIN_FLAGS_WRITE_ACTIVE is cleared after we successfully
6204e108d4fSHou Pu 	 * process a login PDU, so that sk_state_chage can do login
6214e108d4fSHou Pu 	 * cleanup as needed if the socket is closed. If a delayed work is
6224e108d4fSHou Pu 	 * ongoing (LOGIN_FLAGS_WRITE_ACTIVE or LOGIN_FLAGS_READ_ACTIVE),
6234e108d4fSHou Pu 	 * sk_state_change will leave the cleanup to the delayed work or
6244e108d4fSHou Pu 	 * it will schedule a delayed work to do cleanup.
6254e108d4fSHou Pu 	 */
6264e108d4fSHou Pu 	if (conn->sock) {
6274e108d4fSHou Pu 		struct sock *sk = conn->sock->sk;
6284e108d4fSHou Pu 
6294e108d4fSHou Pu 		write_lock_bh(&sk->sk_callback_lock);
6304e108d4fSHou Pu 		if (!test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags)) {
6314e108d4fSHou Pu 			clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
6324e108d4fSHou Pu 			set_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags);
6334e108d4fSHou Pu 		}
6344e108d4fSHou Pu 		write_unlock_bh(&sk->sk_callback_lock);
6354e108d4fSHou Pu 	}
6364e108d4fSHou Pu 
637d381a801SNicholas Bellinger 	rc = iscsi_target_do_login(conn, login);
638d381a801SNicholas Bellinger 	if (rc < 0) {
63925cdda95SNicholas Bellinger 		goto err;
640d381a801SNicholas Bellinger 	} else if (!rc) {
6414e108d4fSHou Pu 		if (iscsi_target_sk_check_and_clear(conn,
6424e108d4fSHou Pu 						    LOGIN_FLAGS_WRITE_ACTIVE))
64325cdda95SNicholas Bellinger 			goto err;
644d381a801SNicholas Bellinger 	} else if (rc == 1) {
6454e108d4fSHou Pu 		cancel_delayed_work(&conn->login_work);
646d381a801SNicholas Bellinger 		iscsi_target_nego_release(conn);
647d381a801SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
648d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
649d381a801SNicholas Bellinger 	}
65025cdda95SNicholas Bellinger 	return;
65125cdda95SNicholas Bellinger 
65225cdda95SNicholas Bellinger err:
65325cdda95SNicholas Bellinger 	iscsi_target_restore_sock_callbacks(conn);
6544e108d4fSHou Pu 	cancel_delayed_work(&conn->login_work);
65525cdda95SNicholas Bellinger 	iscsi_target_login_drop(conn, login);
65625cdda95SNicholas Bellinger 	iscsit_deaccess_np(np, tpg, tpg_np);
657d381a801SNicholas Bellinger }
658d381a801SNicholas Bellinger 
659bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk)
660bb048357SNicholas Bellinger {
661be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
662bb048357SNicholas Bellinger 	void (*orig_state_change)(struct sock *);
663bb048357SNicholas Bellinger 	bool state;
664bb048357SNicholas Bellinger 
665bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_state_change\n");
666bb048357SNicholas Bellinger 
667bb048357SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
668bb048357SNicholas Bellinger 	conn = sk->sk_user_data;
669bb048357SNicholas Bellinger 	if (!conn) {
670bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
671bb048357SNicholas Bellinger 		return;
672bb048357SNicholas Bellinger 	}
673bb048357SNicholas Bellinger 	orig_state_change = conn->orig_state_change;
674bb048357SNicholas Bellinger 
675bb048357SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
676bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
677bb048357SNicholas Bellinger 			 conn);
678bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
679bb048357SNicholas Bellinger 		orig_state_change(sk);
680bb048357SNicholas Bellinger 		return;
681bb048357SNicholas Bellinger 	}
68225cdda95SNicholas Bellinger 	state = __iscsi_target_sk_check_close(sk);
68325cdda95SNicholas Bellinger 	pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
68425cdda95SNicholas Bellinger 
6854e108d4fSHou Pu 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags) ||
6864e108d4fSHou Pu 	    test_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags)) {
6874e108d4fSHou Pu 		pr_debug("Got LOGIN_FLAGS_{READ|WRITE}_ACTIVE=1"
6884e108d4fSHou Pu 			 " sk_state_change conn: %p\n", conn);
68925cdda95SNicholas Bellinger 		if (state)
69025cdda95SNicholas Bellinger 			set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
691bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
692bb048357SNicholas Bellinger 		orig_state_change(sk);
693bb048357SNicholas Bellinger 		return;
694bb048357SNicholas Bellinger 	}
69525cdda95SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
696bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
697bb048357SNicholas Bellinger 			 conn);
698bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
699bb048357SNicholas Bellinger 		orig_state_change(sk);
700bb048357SNicholas Bellinger 		return;
701bb048357SNicholas Bellinger 	}
70225cdda95SNicholas Bellinger 	/*
70325cdda95SNicholas Bellinger 	 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
70425cdda95SNicholas Bellinger 	 * but only queue conn->login_work -> iscsi_target_do_login_rx()
70525cdda95SNicholas Bellinger 	 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
70625cdda95SNicholas Bellinger 	 *
70725cdda95SNicholas Bellinger 	 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
70825cdda95SNicholas Bellinger 	 * will detect the dropped TCP connection from delayed workqueue context.
70925cdda95SNicholas Bellinger 	 *
71025cdda95SNicholas Bellinger 	 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
71125cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
71225cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
71325cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
71425cdda95SNicholas Bellinger 	 * dropped TCP connection in iscsi_np process context, and cleaning up
71525cdda95SNicholas Bellinger 	 * the remaining iscsi connection resources.
71625cdda95SNicholas Bellinger 	 */
71725cdda95SNicholas Bellinger 	if (state) {
71825cdda95SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_change got failed state\n");
71925cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
72025cdda95SNicholas Bellinger 		state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
721bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
722bb048357SNicholas Bellinger 
72325cdda95SNicholas Bellinger 		orig_state_change(sk);
724bb048357SNicholas Bellinger 
72525cdda95SNicholas Bellinger 		if (!state)
72625cdda95SNicholas Bellinger 			schedule_delayed_work(&conn->login_work, 0);
727bb048357SNicholas Bellinger 		return;
728bb048357SNicholas Bellinger 	}
72925cdda95SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
73025cdda95SNicholas Bellinger 
731bb048357SNicholas Bellinger 	orig_state_change(sk);
732bb048357SNicholas Bellinger }
733bb048357SNicholas Bellinger 
734e48354ceSNicholas Bellinger /*
735e48354ceSNicholas Bellinger  *	NOTE: We check for existing sessions or connections AFTER the initiator
736e48354ceSNicholas Bellinger  *	has been successfully authenticated in order to protect against faked
737e48354ceSNicholas Bellinger  *	ISID/TSIH combinations.
738e48354ceSNicholas Bellinger  */
739e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances(
740be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
741e48354ceSNicholas Bellinger 	struct iscsi_login *login)
742e48354ceSNicholas Bellinger {
743e48354ceSNicholas Bellinger 	if (login->checked_for_existing)
744e48354ceSNicholas Bellinger 		return 0;
745e48354ceSNicholas Bellinger 
746e48354ceSNicholas Bellinger 	login->checked_for_existing = 1;
747e48354ceSNicholas Bellinger 
748e48354ceSNicholas Bellinger 	if (!login->tsih)
749e48354ceSNicholas Bellinger 		return iscsi_check_for_session_reinstatement(conn);
750e48354ceSNicholas Bellinger 	else
751e48354ceSNicholas Bellinger 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
752e48354ceSNicholas Bellinger 				login->initial_exp_statsn);
753e48354ceSNicholas Bellinger }
754e48354ceSNicholas Bellinger 
755e48354ceSNicholas Bellinger static int iscsi_target_do_authentication(
756be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
757e48354ceSNicholas Bellinger 	struct iscsi_login *login)
758e48354ceSNicholas Bellinger {
759e48354ceSNicholas Bellinger 	int authret;
760e48354ceSNicholas Bellinger 	u32 payload_length;
761e48354ceSNicholas Bellinger 	struct iscsi_param *param;
762e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
763e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
764e48354ceSNicholas Bellinger 
765e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
766e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
767e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
768e48354ceSNicholas Bellinger 
769e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
770e48354ceSNicholas Bellinger 	if (!param)
771e48354ceSNicholas Bellinger 		return -1;
772e48354ceSNicholas Bellinger 
773e48354ceSNicholas Bellinger 	authret = iscsi_handle_authentication(
774e48354ceSNicholas Bellinger 			conn,
775e48354ceSNicholas Bellinger 			login->req_buf,
776e48354ceSNicholas Bellinger 			login->rsp_buf,
777e48354ceSNicholas Bellinger 			payload_length,
778e48354ceSNicholas Bellinger 			&login->rsp_length,
779e48354ceSNicholas Bellinger 			param->value);
780e48354ceSNicholas Bellinger 	switch (authret) {
781e48354ceSNicholas Bellinger 	case 0:
782e48354ceSNicholas Bellinger 		pr_debug("Received OK response"
783e48354ceSNicholas Bellinger 		" from LIO Authentication, continuing.\n");
784e48354ceSNicholas Bellinger 		break;
785e48354ceSNicholas Bellinger 	case 1:
786e48354ceSNicholas Bellinger 		pr_debug("iSCSI security negotiation"
787bfb9035cSJoe Perches 			" completed successfully.\n");
788e48354ceSNicholas Bellinger 		login->auth_complete = 1;
789e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
790e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
791e48354ceSNicholas Bellinger 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
792e48354ceSNicholas Bellinger 					     ISCSI_FLAG_LOGIN_TRANSIT);
793e48354ceSNicholas Bellinger 			login->current_stage = 1;
794e48354ceSNicholas Bellinger 		}
795e48354ceSNicholas Bellinger 		return iscsi_target_check_for_existing_instances(
796e48354ceSNicholas Bellinger 				conn, login);
797e48354ceSNicholas Bellinger 	case 2:
798e48354ceSNicholas Bellinger 		pr_err("Security negotiation"
799e48354ceSNicholas Bellinger 			" failed.\n");
800e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
801e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
802e48354ceSNicholas Bellinger 		return -1;
803e48354ceSNicholas Bellinger 	default:
804e48354ceSNicholas Bellinger 		pr_err("Received unknown error %d from LIO"
805e48354ceSNicholas Bellinger 				" Authentication\n", authret);
806e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
807e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
808e48354ceSNicholas Bellinger 		return -1;
809e48354ceSNicholas Bellinger 	}
810e48354ceSNicholas Bellinger 
811e48354ceSNicholas Bellinger 	return 0;
812e48354ceSNicholas Bellinger }
813e48354ceSNicholas Bellinger 
814a75fcb09SDmitry Bogdanov static bool iscsi_conn_auth_required(struct iscsit_conn *conn)
815a75fcb09SDmitry Bogdanov {
816*a6e0d179SDmitry Bogdanov 	struct iscsi_node_acl *nacl;
817a75fcb09SDmitry Bogdanov 	struct se_node_acl *se_nacl;
818a75fcb09SDmitry Bogdanov 
819a75fcb09SDmitry Bogdanov 	if (conn->sess->sess_ops->SessionType) {
820a75fcb09SDmitry Bogdanov 		/*
821a75fcb09SDmitry Bogdanov 		 * For SessionType=Discovery
822a75fcb09SDmitry Bogdanov 		 */
823a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
824a75fcb09SDmitry Bogdanov 	}
825a75fcb09SDmitry Bogdanov 	/*
826a75fcb09SDmitry Bogdanov 	 * For SessionType=Normal
827a75fcb09SDmitry Bogdanov 	 */
828a75fcb09SDmitry Bogdanov 	se_nacl = conn->sess->se_sess->se_node_acl;
829a75fcb09SDmitry Bogdanov 	if (!se_nacl) {
830a75fcb09SDmitry Bogdanov 		pr_debug("Unknown ACL %s is trying to connect\n",
831a75fcb09SDmitry Bogdanov 			 se_nacl->initiatorname);
832a75fcb09SDmitry Bogdanov 		return true;
833a75fcb09SDmitry Bogdanov 	}
834a75fcb09SDmitry Bogdanov 
835a75fcb09SDmitry Bogdanov 	if (se_nacl->dynamic_node_acl) {
836a75fcb09SDmitry Bogdanov 		pr_debug("Dynamic ACL %s is trying to connect\n",
837a75fcb09SDmitry Bogdanov 			 se_nacl->initiatorname);
838a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
839a75fcb09SDmitry Bogdanov 	}
840a75fcb09SDmitry Bogdanov 
841a75fcb09SDmitry Bogdanov 	pr_debug("Known ACL %s is trying to connect\n",
842a75fcb09SDmitry Bogdanov 		 se_nacl->initiatorname);
843*a6e0d179SDmitry Bogdanov 
844*a6e0d179SDmitry Bogdanov 	nacl = to_iscsi_nacl(se_nacl);
845*a6e0d179SDmitry Bogdanov 	if (nacl->node_attrib.authentication == NA_AUTHENTICATION_INHERITED)
846a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
847*a6e0d179SDmitry Bogdanov 
848*a6e0d179SDmitry Bogdanov 	return nacl->node_attrib.authentication;
849a75fcb09SDmitry Bogdanov }
850a75fcb09SDmitry Bogdanov 
851e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero(
852be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
853e48354ceSNicholas Bellinger 	struct iscsi_login *login)
854e48354ceSNicholas Bellinger {
855e48354ceSNicholas Bellinger 	int ret;
856e48354ceSNicholas Bellinger 	u32 payload_length;
857e48354ceSNicholas Bellinger 	struct iscsi_param *param;
858e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
859e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
860e48354ceSNicholas Bellinger 
861e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
862e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
863e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
864e48354ceSNicholas Bellinger 
865e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
866e48354ceSNicholas Bellinger 	if (!param)
867e48354ceSNicholas Bellinger 		return -1;
868e48354ceSNicholas Bellinger 
869e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
870e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
871e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
872e48354ceSNicholas Bellinger 			login->req_buf,
873e48354ceSNicholas Bellinger 			payload_length,
8749977bb18SNicholas Bellinger 			conn);
875e48354ceSNicholas Bellinger 	if (ret < 0)
876e48354ceSNicholas Bellinger 		return -1;
877e48354ceSNicholas Bellinger 
878e48354ceSNicholas Bellinger 	if (ret > 0) {
879e48354ceSNicholas Bellinger 		if (login->auth_complete) {
880e48354ceSNicholas Bellinger 			pr_err("Initiator has already been"
881e48354ceSNicholas Bellinger 				" successfully authenticated, but is still"
882e48354ceSNicholas Bellinger 				" sending %s keys.\n", param->value);
883e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
884e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_INIT_ERR);
885e48354ceSNicholas Bellinger 			return -1;
886e48354ceSNicholas Bellinger 		}
887e48354ceSNicholas Bellinger 
888e48354ceSNicholas Bellinger 		goto do_auth;
88991f0abfdSNicholas Bellinger 	} else if (!payload_length) {
89091f0abfdSNicholas Bellinger 		pr_err("Initiator sent zero length security payload,"
89191f0abfdSNicholas Bellinger 		       " login failed\n");
89291f0abfdSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
89391f0abfdSNicholas Bellinger 				    ISCSI_LOGIN_STATUS_AUTH_FAILED);
89491f0abfdSNicholas Bellinger 		return -1;
895e48354ceSNicholas Bellinger 	}
896e48354ceSNicholas Bellinger 
897e48354ceSNicholas Bellinger 	if (login->first_request)
898e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
899e48354ceSNicholas Bellinger 			return -1;
900e48354ceSNicholas Bellinger 
901e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
902e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
903e48354ceSNicholas Bellinger 			SENDER_TARGET,
904e48354ceSNicholas Bellinger 			login->rsp_buf,
905e48354ceSNicholas Bellinger 			&login->rsp_length,
906138d351eSNicholas Bellinger 			conn->param_list,
907138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
908e48354ceSNicholas Bellinger 	if (ret < 0)
909e48354ceSNicholas Bellinger 		return -1;
910e48354ceSNicholas Bellinger 
911e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
912a75fcb09SDmitry Bogdanov 		bool auth_required = iscsi_conn_auth_required(conn);
913a75fcb09SDmitry Bogdanov 
914a75fcb09SDmitry Bogdanov 		if (auth_required) {
915a75fcb09SDmitry Bogdanov 			if (!strncmp(param->value, NONE, 4)) {
916e48354ceSNicholas Bellinger 				pr_err("Initiator sent AuthMethod=None but"
917e48354ceSNicholas Bellinger 				       " Target is enforcing iSCSI Authentication,"
918e48354ceSNicholas Bellinger 				       " login failed.\n");
919a75fcb09SDmitry Bogdanov 				iscsit_tx_login_rsp(conn,
920a75fcb09SDmitry Bogdanov 						ISCSI_STATUS_CLS_INITIATOR_ERR,
921e48354ceSNicholas Bellinger 						ISCSI_LOGIN_STATUS_AUTH_FAILED);
922e48354ceSNicholas Bellinger 				return -1;
923e48354ceSNicholas Bellinger 			}
924e48354ceSNicholas Bellinger 
925a75fcb09SDmitry Bogdanov 			if (!login->auth_complete)
926e48354ceSNicholas Bellinger 				return 0;
927e48354ceSNicholas Bellinger 
928a75fcb09SDmitry Bogdanov 			if (strncmp(param->value, NONE, 4) &&
929a75fcb09SDmitry Bogdanov 			    !login->auth_complete)
930e48354ceSNicholas Bellinger 				return 0;
931a75fcb09SDmitry Bogdanov 		}
932e48354ceSNicholas Bellinger 
933e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
934e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
935e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
936e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
937e48354ceSNicholas Bellinger 			login->current_stage = 1;
938e48354ceSNicholas Bellinger 		}
939e48354ceSNicholas Bellinger 	}
940e48354ceSNicholas Bellinger 
941e48354ceSNicholas Bellinger 	return 0;
942e48354ceSNicholas Bellinger do_auth:
943e48354ceSNicholas Bellinger 	return iscsi_target_do_authentication(conn, login);
944e48354ceSNicholas Bellinger }
945e48354ceSNicholas Bellinger 
946a75fcb09SDmitry Bogdanov static bool iscsi_conn_authenticated(struct iscsit_conn *conn,
947a75fcb09SDmitry Bogdanov 				     struct iscsi_login *login)
948a75fcb09SDmitry Bogdanov {
949a75fcb09SDmitry Bogdanov 	if (!iscsi_conn_auth_required(conn))
950a75fcb09SDmitry Bogdanov 		return true;
951a75fcb09SDmitry Bogdanov 
952a75fcb09SDmitry Bogdanov 	if (login->auth_complete)
953a75fcb09SDmitry Bogdanov 		return true;
954a75fcb09SDmitry Bogdanov 
955a75fcb09SDmitry Bogdanov 	return false;
956a75fcb09SDmitry Bogdanov }
957a75fcb09SDmitry Bogdanov 
958be36d683SMax Gurtovoy static int iscsi_target_handle_csg_one(struct iscsit_conn *conn, struct iscsi_login *login)
959e48354ceSNicholas Bellinger {
960e48354ceSNicholas Bellinger 	int ret;
961e48354ceSNicholas Bellinger 	u32 payload_length;
962e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
963e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
964e48354ceSNicholas Bellinger 
965e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
966e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
967e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
968e48354ceSNicholas Bellinger 
969e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
970e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
971e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
972e48354ceSNicholas Bellinger 			login->req_buf,
973e48354ceSNicholas Bellinger 			payload_length,
9749977bb18SNicholas Bellinger 			conn);
9751c5c12c6SRoland Dreier 	if (ret < 0) {
9761c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
9771c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
978e48354ceSNicholas Bellinger 		return -1;
9791c5c12c6SRoland Dreier 	}
980e48354ceSNicholas Bellinger 
981e48354ceSNicholas Bellinger 	if (login->first_request)
982e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
983e48354ceSNicholas Bellinger 			return -1;
984e48354ceSNicholas Bellinger 
985e48354ceSNicholas Bellinger 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
986e48354ceSNicholas Bellinger 		return -1;
987e48354ceSNicholas Bellinger 
988e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
989e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
990e48354ceSNicholas Bellinger 			SENDER_TARGET,
991e48354ceSNicholas Bellinger 			login->rsp_buf,
992e48354ceSNicholas Bellinger 			&login->rsp_length,
993138d351eSNicholas Bellinger 			conn->param_list,
994138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
9951c5c12c6SRoland Dreier 	if (ret < 0) {
9961c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
9971c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
998e48354ceSNicholas Bellinger 		return -1;
9991c5c12c6SRoland Dreier 	}
1000e48354ceSNicholas Bellinger 
1001a75fcb09SDmitry Bogdanov 	if (!iscsi_conn_authenticated(conn, login)) {
1002e48354ceSNicholas Bellinger 		pr_err("Initiator is requesting CSG: 1, has not been"
1003e48354ceSNicholas Bellinger 		       " successfully authenticated, and the Target is"
1004e48354ceSNicholas Bellinger 		       " enforcing iSCSI Authentication, login failed.\n");
1005e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1006e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
1007e48354ceSNicholas Bellinger 		return -1;
1008e48354ceSNicholas Bellinger 	}
1009e48354ceSNicholas Bellinger 
1010e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list))
1011e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
1012e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
1013e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
1014e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
1015e48354ceSNicholas Bellinger 
1016e48354ceSNicholas Bellinger 	return 0;
1017e48354ceSNicholas Bellinger }
1018e48354ceSNicholas Bellinger 
1019be36d683SMax Gurtovoy static int iscsi_target_do_login(struct iscsit_conn *conn, struct iscsi_login *login)
1020e48354ceSNicholas Bellinger {
1021e48354ceSNicholas Bellinger 	int pdu_count = 0;
1022e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
1023e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
1024e48354ceSNicholas Bellinger 
1025e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1026e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
1027e48354ceSNicholas Bellinger 
1028e48354ceSNicholas Bellinger 	while (1) {
1029e48354ceSNicholas Bellinger 		if (++pdu_count > MAX_LOGIN_PDUS) {
1030e48354ceSNicholas Bellinger 			pr_err("MAX_LOGIN_PDUS count reached.\n");
1031e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1032e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
1033e48354ceSNicholas Bellinger 			return -1;
1034e48354ceSNicholas Bellinger 		}
1035e48354ceSNicholas Bellinger 
10365d358065SAndy Grover 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
1037e48354ceSNicholas Bellinger 		case 0:
10385d358065SAndy Grover 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
1039e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
1040e48354ceSNicholas Bellinger 				return -1;
1041e48354ceSNicholas Bellinger 			break;
1042e48354ceSNicholas Bellinger 		case 1:
1043e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
1044e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_one(conn, login) < 0)
1045e48354ceSNicholas Bellinger 				return -1;
1046e48354ceSNicholas Bellinger 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
104725cdda95SNicholas Bellinger 				/*
104825cdda95SNicholas Bellinger 				 * Check to make sure the TCP connection has not
104925cdda95SNicholas Bellinger 				 * dropped asynchronously while session reinstatement
105025cdda95SNicholas Bellinger 				 * was occuring in this kthread context, before
105125cdda95SNicholas Bellinger 				 * transitioning to full feature phase operation.
105225cdda95SNicholas Bellinger 				 */
105325cdda95SNicholas Bellinger 				if (iscsi_target_sk_check_close(conn))
105425cdda95SNicholas Bellinger 					return -1;
105525cdda95SNicholas Bellinger 
1056e48354ceSNicholas Bellinger 				login->tsih = conn->sess->tsih;
1057baa4d64bSNicholas Bellinger 				login->login_complete = 1;
1058d381a801SNicholas Bellinger 				iscsi_target_restore_sock_callbacks(conn);
1059e48354ceSNicholas Bellinger 				if (iscsi_target_do_tx_login_io(conn,
1060e48354ceSNicholas Bellinger 						login) < 0)
1061e48354ceSNicholas Bellinger 					return -1;
1062d381a801SNicholas Bellinger 				return 1;
1063e48354ceSNicholas Bellinger 			}
1064e48354ceSNicholas Bellinger 			break;
1065e48354ceSNicholas Bellinger 		default:
1066e48354ceSNicholas Bellinger 			pr_err("Illegal CSG: %d received from"
1067e48354ceSNicholas Bellinger 				" Initiator, protocol error.\n",
10685d358065SAndy Grover 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
1069e48354ceSNicholas Bellinger 			break;
1070e48354ceSNicholas Bellinger 		}
1071e48354ceSNicholas Bellinger 
1072ea3a179aSNicholas Bellinger 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
1073e48354ceSNicholas Bellinger 			return -1;
1074e48354ceSNicholas Bellinger 
1075e48354ceSNicholas Bellinger 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1076e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1077e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1078e48354ceSNicholas Bellinger 		}
1079d381a801SNicholas Bellinger 		break;
1080e48354ceSNicholas Bellinger 	}
1081e48354ceSNicholas Bellinger 
1082e48354ceSNicholas Bellinger 	return 0;
1083e48354ceSNicholas Bellinger }
1084e48354ceSNicholas Bellinger 
1085e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower(
1086e48354ceSNicholas Bellinger 	char *param_buf)
1087e48354ceSNicholas Bellinger {
1088e48354ceSNicholas Bellinger 	char *c;
1089e48354ceSNicholas Bellinger 	u32 iqn_size = strlen(param_buf), i;
1090e48354ceSNicholas Bellinger 
1091e48354ceSNicholas Bellinger 	for (i = 0; i < iqn_size; i++) {
10928359cf43SJörn Engel 		c = &param_buf[i];
1093e48354ceSNicholas Bellinger 		if (!isupper(*c))
1094e48354ceSNicholas Bellinger 			continue;
1095e48354ceSNicholas Bellinger 
1096e48354ceSNicholas Bellinger 		*c = tolower(*c);
1097e48354ceSNicholas Bellinger 	}
1098e48354ceSNicholas Bellinger }
1099e48354ceSNicholas Bellinger 
1100e48354ceSNicholas Bellinger /*
1101e48354ceSNicholas Bellinger  * Processes the first Login Request..
1102e48354ceSNicholas Bellinger  */
1103baa4d64bSNicholas Bellinger int iscsi_target_locate_portal(
1104e48354ceSNicholas Bellinger 	struct iscsi_np *np,
1105be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
1106e48354ceSNicholas Bellinger 	struct iscsi_login *login)
1107e48354ceSNicholas Bellinger {
1108e48354ceSNicholas Bellinger 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1109e48354ceSNicholas Bellinger 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
11100873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
1111e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1112d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = NULL;
1113e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
1114988e3a85SNicholas Bellinger 	struct se_node_acl *se_nacl;
1115988e3a85SNicholas Bellinger 	u32 payload_length, queue_depth = 0;
1116988e3a85SNicholas Bellinger 	int sessiontype = 0, ret = 0, tag_num, tag_size;
1117e48354ceSNicholas Bellinger 
1118d381a801SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1119d381a801SNicholas Bellinger 	iscsi_target_set_sock_callbacks(conn);
1120d381a801SNicholas Bellinger 
1121d381a801SNicholas Bellinger 	login->np = np;
1122d381a801SNicholas Bellinger 
1123e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1124e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
1125e48354ceSNicholas Bellinger 
11266235bef6SYang Yingliang 	tmpbuf = kmemdup_nul(login->req_buf, payload_length, GFP_KERNEL);
1127e48354ceSNicholas Bellinger 	if (!tmpbuf) {
1128e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for tmpbuf.\n");
1129e48354ceSNicholas Bellinger 		return -1;
1130e48354ceSNicholas Bellinger 	}
1131e48354ceSNicholas Bellinger 
1132e48354ceSNicholas Bellinger 	start = tmpbuf;
1133e48354ceSNicholas Bellinger 	end = (start + payload_length);
1134e48354ceSNicholas Bellinger 
1135e48354ceSNicholas Bellinger 	/*
1136e48354ceSNicholas Bellinger 	 * Locate the initial keys expected from the Initiator node in
1137e48354ceSNicholas Bellinger 	 * the first login request in order to progress with the login phase.
1138e48354ceSNicholas Bellinger 	 */
1139e48354ceSNicholas Bellinger 	while (start < end) {
1140e48354ceSNicholas Bellinger 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1141e48354ceSNicholas Bellinger 			ret = -1;
1142e48354ceSNicholas Bellinger 			goto out;
1143e48354ceSNicholas Bellinger 		}
1144e48354ceSNicholas Bellinger 
1145e48354ceSNicholas Bellinger 		if (!strncmp(key, "InitiatorName", 13))
1146e48354ceSNicholas Bellinger 			i_buf = value;
1147e48354ceSNicholas Bellinger 		else if (!strncmp(key, "SessionType", 11))
1148e48354ceSNicholas Bellinger 			s_buf = value;
1149e48354ceSNicholas Bellinger 		else if (!strncmp(key, "TargetName", 10))
1150e48354ceSNicholas Bellinger 			t_buf = value;
1151e48354ceSNicholas Bellinger 
1152e48354ceSNicholas Bellinger 		start += strlen(key) + strlen(value) + 2;
1153e48354ceSNicholas Bellinger 	}
1154e48354ceSNicholas Bellinger 	/*
1155e48354ceSNicholas Bellinger 	 * See 5.3.  Login Phase.
1156e48354ceSNicholas Bellinger 	 */
1157e48354ceSNicholas Bellinger 	if (!i_buf) {
1158e48354ceSNicholas Bellinger 		pr_err("InitiatorName key not received"
1159e48354ceSNicholas Bellinger 			" in first login request.\n");
1160e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1161e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1162e48354ceSNicholas Bellinger 		ret = -1;
1163e48354ceSNicholas Bellinger 		goto out;
1164e48354ceSNicholas Bellinger 	}
1165e48354ceSNicholas Bellinger 	/*
1166e48354ceSNicholas Bellinger 	 * Convert the incoming InitiatorName to lowercase following
1167e48354ceSNicholas Bellinger 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1168e48354ceSNicholas Bellinger 	 * are NOT case sensitive.
1169e48354ceSNicholas Bellinger 	 */
1170e48354ceSNicholas Bellinger 	iscsi_initiatorname_tolower(i_buf);
1171e48354ceSNicholas Bellinger 
1172e48354ceSNicholas Bellinger 	if (!s_buf) {
1173e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1174e48354ceSNicholas Bellinger 			goto get_target;
1175e48354ceSNicholas Bellinger 
1176e48354ceSNicholas Bellinger 		pr_err("SessionType key not received"
1177e48354ceSNicholas Bellinger 			" in first login request.\n");
1178e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1179e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1180e48354ceSNicholas Bellinger 		ret = -1;
1181e48354ceSNicholas Bellinger 		goto out;
1182e48354ceSNicholas Bellinger 	}
1183e48354ceSNicholas Bellinger 
1184e48354ceSNicholas Bellinger 	/*
1185e48354ceSNicholas Bellinger 	 * Use default portal group for discovery sessions.
1186e48354ceSNicholas Bellinger 	 */
1187e48354ceSNicholas Bellinger 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1188e48354ceSNicholas Bellinger 	if (!sessiontype) {
1189e48354ceSNicholas Bellinger 		conn->tpg = iscsit_global->discovery_tpg;
1190e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1191e48354ceSNicholas Bellinger 			goto get_target;
1192e48354ceSNicholas Bellinger 
1193e48354ceSNicholas Bellinger 		sess->sess_ops->SessionType = 1;
1194e48354ceSNicholas Bellinger 		/*
1195e48354ceSNicholas Bellinger 		 * Setup crc32c modules from libcrypto
1196e48354ceSNicholas Bellinger 		 */
1197e48354ceSNicholas Bellinger 		if (iscsi_login_setup_crypto(conn) < 0) {
1198e48354ceSNicholas Bellinger 			pr_err("iscsi_login_setup_crypto() failed\n");
1199e48354ceSNicholas Bellinger 			ret = -1;
1200e48354ceSNicholas Bellinger 			goto out;
1201e48354ceSNicholas Bellinger 		}
1202e48354ceSNicholas Bellinger 		/*
1203e48354ceSNicholas Bellinger 		 * Serialize access across the discovery struct iscsi_portal_group to
1204e48354ceSNicholas Bellinger 		 * process login attempt.
1205e48354ceSNicholas Bellinger 		 */
1206e48354ceSNicholas Bellinger 		if (iscsit_access_np(np, conn->tpg) < 0) {
1207e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1208e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1209e48354ceSNicholas Bellinger 			ret = -1;
1210e48354ceSNicholas Bellinger 			goto out;
1211e48354ceSNicholas Bellinger 		}
1212e48354ceSNicholas Bellinger 		ret = 0;
1213988e3a85SNicholas Bellinger 		goto alloc_tags;
1214e48354ceSNicholas Bellinger 	}
1215e48354ceSNicholas Bellinger 
1216e48354ceSNicholas Bellinger get_target:
1217e48354ceSNicholas Bellinger 	if (!t_buf) {
1218e48354ceSNicholas Bellinger 		pr_err("TargetName key not received"
1219e48354ceSNicholas Bellinger 			" in first login request while"
1220e48354ceSNicholas Bellinger 			" SessionType=Normal.\n");
1221e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1222e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1223e48354ceSNicholas Bellinger 		ret = -1;
1224e48354ceSNicholas Bellinger 		goto out;
1225e48354ceSNicholas Bellinger 	}
1226e48354ceSNicholas Bellinger 
1227e48354ceSNicholas Bellinger 	/*
1228e48354ceSNicholas Bellinger 	 * Locate Target IQN from Storage Node.
1229e48354ceSNicholas Bellinger 	 */
1230e48354ceSNicholas Bellinger 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1231e48354ceSNicholas Bellinger 	if (!tiqn) {
1232e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target IQN: %s in"
1233e48354ceSNicholas Bellinger 			" Storage Node\n", t_buf);
1234e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1235e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1236e48354ceSNicholas Bellinger 		ret = -1;
1237e48354ceSNicholas Bellinger 		goto out;
1238e48354ceSNicholas Bellinger 	}
1239e48354ceSNicholas Bellinger 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1240e48354ceSNicholas Bellinger 
1241e48354ceSNicholas Bellinger 	/*
1242e48354ceSNicholas Bellinger 	 * Locate Target Portal Group from Storage Node.
1243e48354ceSNicholas Bellinger 	 */
1244d381a801SNicholas Bellinger 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1245e48354ceSNicholas Bellinger 	if (!conn->tpg) {
1246e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target Portal Group"
1247e48354ceSNicholas Bellinger 				" on %s\n", tiqn->tiqn);
1248e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1249e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1250e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1251e48354ceSNicholas Bellinger 		ret = -1;
1252e48354ceSNicholas Bellinger 		goto out;
1253e48354ceSNicholas Bellinger 	}
1254d381a801SNicholas Bellinger 	conn->tpg_np = tpg_np;
1255e48354ceSNicholas Bellinger 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1256e48354ceSNicholas Bellinger 	/*
1257e48354ceSNicholas Bellinger 	 * Setup crc32c modules from libcrypto
1258e48354ceSNicholas Bellinger 	 */
1259e48354ceSNicholas Bellinger 	if (iscsi_login_setup_crypto(conn) < 0) {
1260e48354ceSNicholas Bellinger 		pr_err("iscsi_login_setup_crypto() failed\n");
1261d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1262d381a801SNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1263d381a801SNicholas Bellinger 		conn->tpg = NULL;
1264e48354ceSNicholas Bellinger 		ret = -1;
1265e48354ceSNicholas Bellinger 		goto out;
1266e48354ceSNicholas Bellinger 	}
1267e48354ceSNicholas Bellinger 	/*
1268e48354ceSNicholas Bellinger 	 * Serialize access across the struct iscsi_portal_group to
1269e48354ceSNicholas Bellinger 	 * process login attempt.
1270e48354ceSNicholas Bellinger 	 */
1271e48354ceSNicholas Bellinger 	if (iscsit_access_np(np, conn->tpg) < 0) {
1272d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1273e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1274e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1275e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1276e48354ceSNicholas Bellinger 		conn->tpg = NULL;
1277d381a801SNicholas Bellinger 		ret = -1;
1278e48354ceSNicholas Bellinger 		goto out;
1279e48354ceSNicholas Bellinger 	}
1280e48354ceSNicholas Bellinger 
1281e48354ceSNicholas Bellinger 	/*
1282e48354ceSNicholas Bellinger 	 * conn->sess->node_acl will be set when the referenced
12830873fe44SMax Gurtovoy 	 * struct iscsit_session is located from received ISID+TSIH in
1284e48354ceSNicholas Bellinger 	 * iscsi_login_non_zero_tsih_s2().
1285e48354ceSNicholas Bellinger 	 */
1286e48354ceSNicholas Bellinger 	if (!login->leading_connection) {
1287e48354ceSNicholas Bellinger 		ret = 0;
1288e48354ceSNicholas Bellinger 		goto out;
1289e48354ceSNicholas Bellinger 	}
1290e48354ceSNicholas Bellinger 
1291e48354ceSNicholas Bellinger 	/*
1292e48354ceSNicholas Bellinger 	 * This value is required in iscsi_login_zero_tsih_s2()
1293e48354ceSNicholas Bellinger 	 */
1294e48354ceSNicholas Bellinger 	sess->sess_ops->SessionType = 0;
1295e48354ceSNicholas Bellinger 
1296e48354ceSNicholas Bellinger 	/*
1297e48354ceSNicholas Bellinger 	 * Locate incoming Initiator IQN reference from Storage Node.
1298e48354ceSNicholas Bellinger 	 */
1299e48354ceSNicholas Bellinger 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1300e48354ceSNicholas Bellinger 			&conn->tpg->tpg_se_tpg, i_buf);
1301e48354ceSNicholas Bellinger 	if (!sess->se_sess->se_node_acl) {
1302e48354ceSNicholas Bellinger 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1303e48354ceSNicholas Bellinger 			" access iSCSI target portal group: %hu.\n",
1304e48354ceSNicholas Bellinger 				i_buf, conn->tpg->tpgt);
1305e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1306e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1307e48354ceSNicholas Bellinger 		ret = -1;
1308e48354ceSNicholas Bellinger 		goto out;
1309e48354ceSNicholas Bellinger 	}
1310988e3a85SNicholas Bellinger 	se_nacl = sess->se_sess->se_node_acl;
1311988e3a85SNicholas Bellinger 	queue_depth = se_nacl->queue_depth;
1312988e3a85SNicholas Bellinger 	/*
1313988e3a85SNicholas Bellinger 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1314988e3a85SNicholas Bellinger 	 * depth for non immediate commands, plus extra tags for immediate
1315988e3a85SNicholas Bellinger 	 * commands.
1316988e3a85SNicholas Bellinger 	 *
1317988e3a85SNicholas Bellinger 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1318988e3a85SNicholas Bellinger 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1319988e3a85SNicholas Bellinger 	 */
1320988e3a85SNicholas Bellinger alloc_tags:
1321988e3a85SNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
13224a4caa29SNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
132366cd9d4eSMax Gurtovoy 	tag_size = sizeof(struct iscsit_cmd) + conn->conn_transport->priv_size;
1324e48354ceSNicholas Bellinger 
1325988e3a85SNicholas Bellinger 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1326988e3a85SNicholas Bellinger 	if (ret < 0) {
1327988e3a85SNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1328988e3a85SNicholas Bellinger 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1329988e3a85SNicholas Bellinger 		ret = -1;
1330988e3a85SNicholas Bellinger 	}
1331e48354ceSNicholas Bellinger out:
1332e48354ceSNicholas Bellinger 	kfree(tmpbuf);
1333e48354ceSNicholas Bellinger 	return ret;
1334e48354ceSNicholas Bellinger }
1335e48354ceSNicholas Bellinger 
1336e48354ceSNicholas Bellinger int iscsi_target_start_negotiation(
1337e48354ceSNicholas Bellinger 	struct iscsi_login *login,
1338be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
1339e48354ceSNicholas Bellinger {
1340baa4d64bSNicholas Bellinger 	int ret;
1341e48354ceSNicholas Bellinger 
1342d381a801SNicholas Bellinger 	if (conn->sock) {
1343d381a801SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
1344e48354ceSNicholas Bellinger 
1345d381a801SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
1346d381a801SNicholas Bellinger 		set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
134725cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1348d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
1349d381a801SNicholas Bellinger 	}
135025cdda95SNicholas Bellinger 	/*
135125cdda95SNicholas Bellinger 	 * If iscsi_target_do_login returns zero to signal more PDU
135225cdda95SNicholas Bellinger 	 * exchanges are required to complete the login, go ahead and
135325cdda95SNicholas Bellinger 	 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
135425cdda95SNicholas Bellinger 	 * is still active.
135525cdda95SNicholas Bellinger 	 *
135625cdda95SNicholas Bellinger 	 * Otherwise if TCP connection dropped asynchronously, go ahead
135725cdda95SNicholas Bellinger 	 * and perform connection cleanup now.
135825cdda95SNicholas Bellinger 	 */
13598f0dfb3dSNicholas Bellinger 	ret = iscsi_target_do_login(conn, login);
136025cdda95SNicholas Bellinger 	if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
136125cdda95SNicholas Bellinger 		ret = -1;
136225cdda95SNicholas Bellinger 
13638f0dfb3dSNicholas Bellinger 	if (ret < 0) {
1364d381a801SNicholas Bellinger 		cancel_delayed_work_sync(&conn->login_work);
1365d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
1366d381a801SNicholas Bellinger 		iscsi_remove_failed_auth_entry(conn);
1367d381a801SNicholas Bellinger 	}
1368d381a801SNicholas Bellinger 	if (ret != 0)
1369baa4d64bSNicholas Bellinger 		iscsi_target_nego_release(conn);
1370d381a801SNicholas Bellinger 
1371e48354ceSNicholas Bellinger 	return ret;
1372e48354ceSNicholas Bellinger }
1373e48354ceSNicholas Bellinger 
1374be36d683SMax Gurtovoy void iscsi_target_nego_release(struct iscsit_conn *conn)
1375e48354ceSNicholas Bellinger {
1376baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1377baa4d64bSNicholas Bellinger 
1378baa4d64bSNicholas Bellinger 	if (!login)
1379baa4d64bSNicholas Bellinger 		return;
1380baa4d64bSNicholas Bellinger 
1381e48354ceSNicholas Bellinger 	kfree(login->req_buf);
1382e48354ceSNicholas Bellinger 	kfree(login->rsp_buf);
1383e48354ceSNicholas Bellinger 	kfree(login);
1384baa4d64bSNicholas Bellinger 
1385baa4d64bSNicholas Bellinger 	conn->conn_login = NULL;
1386e48354ceSNicholas Bellinger }
1387