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