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>
1640e0b090SPeilin Ye #include <trace/events/sock.h>
17e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
18e48354ceSNicholas Bellinger #include <target/target_core_base.h>
19c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
20baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
21e48354ceSNicholas Bellinger 
2267f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
23e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
24e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
25e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
26e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
27e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
28e48354ceSNicholas Bellinger #include "iscsi_target.h"
29e48354ceSNicholas Bellinger #include "iscsi_target_auth.h"
30e48354ceSNicholas Bellinger 
31e48354ceSNicholas Bellinger #define MAX_LOGIN_PDUS  7
32e48354ceSNicholas Bellinger 
convert_null_to_semi(char * buf,int len)33e48354ceSNicholas Bellinger void convert_null_to_semi(char *buf, int len)
34e48354ceSNicholas Bellinger {
35e48354ceSNicholas Bellinger 	int i;
36e48354ceSNicholas Bellinger 
37e48354ceSNicholas Bellinger 	for (i = 0; i < len; i++)
38e48354ceSNicholas Bellinger 		if (buf[i] == '\0')
39e48354ceSNicholas Bellinger 			buf[i] = ';';
40e48354ceSNicholas Bellinger }
41e48354ceSNicholas Bellinger 
strlen_semi(char * buf)42fceb5bc7SChristoph Hellwig static int strlen_semi(char *buf)
43e48354ceSNicholas Bellinger {
44e48354ceSNicholas Bellinger 	int i = 0;
45e48354ceSNicholas Bellinger 
46e48354ceSNicholas Bellinger 	while (buf[i] != '\0') {
47e48354ceSNicholas Bellinger 		if (buf[i] == ';')
48e48354ceSNicholas Bellinger 			return i;
49e48354ceSNicholas Bellinger 		i++;
50e48354ceSNicholas Bellinger 	}
51e48354ceSNicholas Bellinger 
52e48354ceSNicholas Bellinger 	return -1;
53e48354ceSNicholas Bellinger }
54e48354ceSNicholas Bellinger 
extract_param(const char * in_buf,const char * pattern,unsigned int max_length,char * out_buf,unsigned char * type)55e48354ceSNicholas Bellinger int extract_param(
56e48354ceSNicholas Bellinger 	const char *in_buf,
57e48354ceSNicholas Bellinger 	const char *pattern,
58e48354ceSNicholas Bellinger 	unsigned int max_length,
59e48354ceSNicholas Bellinger 	char *out_buf,
60e48354ceSNicholas Bellinger 	unsigned char *type)
61e48354ceSNicholas Bellinger {
62e48354ceSNicholas Bellinger 	char *ptr;
63e48354ceSNicholas Bellinger 	int len;
64e48354ceSNicholas Bellinger 
65e48354ceSNicholas Bellinger 	if (!in_buf || !pattern || !out_buf || !type)
66292cef5eSDmitry Bogdanov 		return -EINVAL;
67e48354ceSNicholas Bellinger 
68e48354ceSNicholas Bellinger 	ptr = strstr(in_buf, pattern);
69e48354ceSNicholas Bellinger 	if (!ptr)
70292cef5eSDmitry Bogdanov 		return -ENOENT;
71e48354ceSNicholas Bellinger 
72e48354ceSNicholas Bellinger 	ptr = strstr(ptr, "=");
73e48354ceSNicholas Bellinger 	if (!ptr)
74292cef5eSDmitry Bogdanov 		return -EINVAL;
75e48354ceSNicholas Bellinger 
76e48354ceSNicholas Bellinger 	ptr += 1;
77e48354ceSNicholas Bellinger 	if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
78e48354ceSNicholas Bellinger 		ptr += 2; /* skip 0x */
79e48354ceSNicholas Bellinger 		*type = HEX;
801e573388SDmitry Bogdanov 	} else if (*ptr == '0' && (*(ptr+1) == 'b' || *(ptr+1) == 'B')) {
811e573388SDmitry Bogdanov 		ptr += 2; /* skip 0b */
821e573388SDmitry Bogdanov 		*type = BASE64;
83e48354ceSNicholas Bellinger 	} else
84e48354ceSNicholas Bellinger 		*type = DECIMAL;
85e48354ceSNicholas Bellinger 
86e48354ceSNicholas Bellinger 	len = strlen_semi(ptr);
87e48354ceSNicholas Bellinger 	if (len < 0)
88292cef5eSDmitry Bogdanov 		return -EINVAL;
89e48354ceSNicholas Bellinger 
90369653e4SEric Seppanen 	if (len >= max_length) {
915e58b029SMasanari Iida 		pr_err("Length of input: %d exceeds max_length:"
92e48354ceSNicholas Bellinger 			" %d\n", len, max_length);
93292cef5eSDmitry Bogdanov 		return -EINVAL;
94e48354ceSNicholas Bellinger 	}
95e48354ceSNicholas Bellinger 	memcpy(out_buf, ptr, len);
96e48354ceSNicholas Bellinger 	out_buf[len] = '\0';
97e48354ceSNicholas Bellinger 
98e48354ceSNicholas Bellinger 	return 0;
99e48354ceSNicholas Bellinger }
100e48354ceSNicholas Bellinger 
iscsi_get_node_auth(struct iscsit_conn * conn)101a75fcb09SDmitry Bogdanov static struct iscsi_node_auth *iscsi_get_node_auth(struct iscsit_conn *conn)
102a75fcb09SDmitry Bogdanov {
103a75fcb09SDmitry Bogdanov 	struct iscsi_portal_group *tpg;
104a75fcb09SDmitry Bogdanov 	struct iscsi_node_acl *nacl;
105a75fcb09SDmitry Bogdanov 	struct se_node_acl *se_nacl;
106a75fcb09SDmitry Bogdanov 
107a75fcb09SDmitry Bogdanov 	if (conn->sess->sess_ops->SessionType)
108a75fcb09SDmitry Bogdanov 		return &iscsit_global->discovery_acl.node_auth;
109a75fcb09SDmitry Bogdanov 
110a75fcb09SDmitry Bogdanov 	se_nacl = conn->sess->se_sess->se_node_acl;
111a75fcb09SDmitry Bogdanov 	if (!se_nacl) {
112a75fcb09SDmitry Bogdanov 		pr_err("Unable to locate struct se_node_acl for CHAP auth\n");
113a75fcb09SDmitry Bogdanov 		return NULL;
114a75fcb09SDmitry Bogdanov 	}
115a75fcb09SDmitry Bogdanov 
116a75fcb09SDmitry Bogdanov 	if (se_nacl->dynamic_node_acl) {
117a75fcb09SDmitry Bogdanov 		tpg = to_iscsi_tpg(se_nacl->se_tpg);
118a75fcb09SDmitry Bogdanov 		return &tpg->tpg_demo_auth;
119a75fcb09SDmitry Bogdanov 	}
120a75fcb09SDmitry Bogdanov 
121a75fcb09SDmitry Bogdanov 	nacl = to_iscsi_nacl(se_nacl);
122a75fcb09SDmitry Bogdanov 
123a75fcb09SDmitry Bogdanov 	return &nacl->node_auth;
124a75fcb09SDmitry Bogdanov }
125a75fcb09SDmitry Bogdanov 
iscsi_handle_authentication(struct iscsit_conn * conn,char * in_buf,char * out_buf,int in_length,int * out_length,unsigned char * authtype)126e48354ceSNicholas Bellinger static u32 iscsi_handle_authentication(
127be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
128e48354ceSNicholas Bellinger 	char *in_buf,
129e48354ceSNicholas Bellinger 	char *out_buf,
130e48354ceSNicholas Bellinger 	int in_length,
131e48354ceSNicholas Bellinger 	int *out_length,
132e48354ceSNicholas Bellinger 	unsigned char *authtype)
133e48354ceSNicholas Bellinger {
134e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth;
135e48354ceSNicholas Bellinger 
136a75fcb09SDmitry Bogdanov 	auth = iscsi_get_node_auth(conn);
137a75fcb09SDmitry Bogdanov 	if (!auth)
138e48354ceSNicholas Bellinger 		return -1;
139e48354ceSNicholas Bellinger 
140e48354ceSNicholas Bellinger 	if (strstr("CHAP", authtype))
141e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, "CHAP");
142e48354ceSNicholas Bellinger 	else
143e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, NONE);
144e48354ceSNicholas Bellinger 
145e48354ceSNicholas Bellinger 	if (strstr("None", authtype))
146e48354ceSNicholas Bellinger 		return 1;
147e48354ceSNicholas Bellinger 	else if (strstr("CHAP", authtype))
148e48354ceSNicholas Bellinger 		return chap_main_loop(conn, auth, in_buf, out_buf,
149e48354ceSNicholas Bellinger 				&in_length, out_length);
1508a914f32SHariprasad Kelam 	/* SRP, SPKM1, SPKM2 and KRB5 are unsupported */
151e48354ceSNicholas Bellinger 	return 2;
152e48354ceSNicholas Bellinger }
153e48354ceSNicholas Bellinger 
iscsi_remove_failed_auth_entry(struct iscsit_conn * conn)154be36d683SMax Gurtovoy static void iscsi_remove_failed_auth_entry(struct iscsit_conn *conn)
155e48354ceSNicholas Bellinger {
156e48354ceSNicholas Bellinger 	kfree(conn->auth_protocol);
157e48354ceSNicholas Bellinger }
158e48354ceSNicholas Bellinger 
iscsi_target_check_login_request(struct iscsit_conn * conn,struct iscsi_login * login)159baa4d64bSNicholas Bellinger int iscsi_target_check_login_request(
160be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
161e48354ceSNicholas Bellinger 	struct iscsi_login *login)
162e48354ceSNicholas Bellinger {
16328168905SJörn Engel 	int req_csg, req_nsg;
164e48354ceSNicholas Bellinger 	u32 payload_length;
165e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
166e48354ceSNicholas Bellinger 
167e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
168e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
169e48354ceSNicholas Bellinger 
170e48354ceSNicholas Bellinger 	switch (login_req->opcode & ISCSI_OPCODE_MASK) {
171e48354ceSNicholas Bellinger 	case ISCSI_OP_LOGIN:
172e48354ceSNicholas Bellinger 		break;
173e48354ceSNicholas Bellinger 	default:
174e48354ceSNicholas Bellinger 		pr_err("Received unknown opcode 0x%02x.\n",
175e48354ceSNicholas Bellinger 				login_req->opcode & ISCSI_OPCODE_MASK);
176e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
177e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
178e48354ceSNicholas Bellinger 		return -1;
179e48354ceSNicholas Bellinger 	}
180e48354ceSNicholas Bellinger 
181e48354ceSNicholas Bellinger 	if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
182e48354ceSNicholas Bellinger 	    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
183e48354ceSNicholas Bellinger 		pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
184e48354ceSNicholas Bellinger 			" and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
185e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
186e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
187e48354ceSNicholas Bellinger 		return -1;
188e48354ceSNicholas Bellinger 	}
189e48354ceSNicholas Bellinger 
1905d358065SAndy Grover 	req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1915d358065SAndy Grover 	req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
192e48354ceSNicholas Bellinger 
193e48354ceSNicholas Bellinger 	if (req_csg != login->current_stage) {
194e48354ceSNicholas Bellinger 		pr_err("Initiator unexpectedly changed login stage"
195e48354ceSNicholas Bellinger 			" from %d to %d, login failed.\n", login->current_stage,
196e48354ceSNicholas Bellinger 			req_csg);
197e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
198e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
199e48354ceSNicholas Bellinger 		return -1;
200e48354ceSNicholas Bellinger 	}
201e48354ceSNicholas Bellinger 
202e48354ceSNicholas Bellinger 	if ((req_nsg == 2) || (req_csg >= 2) ||
203e48354ceSNicholas Bellinger 	   ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
204e48354ceSNicholas Bellinger 	    (req_nsg <= req_csg))) {
205e48354ceSNicholas Bellinger 		pr_err("Illegal login_req->flags Combination, CSG: %d,"
206e48354ceSNicholas Bellinger 			" NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
207e48354ceSNicholas Bellinger 			req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
208e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
209e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
210e48354ceSNicholas Bellinger 		return -1;
211e48354ceSNicholas Bellinger 	}
212e48354ceSNicholas Bellinger 
213e48354ceSNicholas Bellinger 	if ((login_req->max_version != login->version_max) ||
214e48354ceSNicholas Bellinger 	    (login_req->min_version != login->version_min)) {
215e48354ceSNicholas Bellinger 		pr_err("Login request changed Version Max/Nin"
216e48354ceSNicholas Bellinger 			" unexpectedly to 0x%02x/0x%02x, protocol error\n",
217e48354ceSNicholas Bellinger 			login_req->max_version, login_req->min_version);
218e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
219e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
220e48354ceSNicholas Bellinger 		return -1;
221e48354ceSNicholas Bellinger 	}
222e48354ceSNicholas Bellinger 
223e48354ceSNicholas Bellinger 	if (memcmp(login_req->isid, login->isid, 6) != 0) {
224e48354ceSNicholas Bellinger 		pr_err("Login request changed ISID unexpectedly,"
225e48354ceSNicholas Bellinger 				" protocol error.\n");
226e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
227e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
228e48354ceSNicholas Bellinger 		return -1;
229e48354ceSNicholas Bellinger 	}
230e48354ceSNicholas Bellinger 
231e48354ceSNicholas Bellinger 	if (login_req->itt != login->init_task_tag) {
232e48354ceSNicholas Bellinger 		pr_err("Login request changed ITT unexpectedly to"
233e48354ceSNicholas Bellinger 			" 0x%08x, protocol error.\n", login_req->itt);
234e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
235e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
236e48354ceSNicholas Bellinger 		return -1;
237e48354ceSNicholas Bellinger 	}
238e48354ceSNicholas Bellinger 
239e48354ceSNicholas Bellinger 	if (payload_length > MAX_KEY_VALUE_PAIRS) {
240e48354ceSNicholas Bellinger 		pr_err("Login request payload exceeds default"
241e48354ceSNicholas Bellinger 			" MaxRecvDataSegmentLength: %u, protocol error.\n",
242e48354ceSNicholas Bellinger 				MAX_KEY_VALUE_PAIRS);
243e48354ceSNicholas Bellinger 		return -1;
244e48354ceSNicholas Bellinger 	}
245e48354ceSNicholas Bellinger 
246e48354ceSNicholas Bellinger 	return 0;
247e48354ceSNicholas Bellinger }
248d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_target_check_login_request);
249e48354ceSNicholas Bellinger 
iscsi_target_check_first_request(struct iscsit_conn * conn,struct iscsi_login * login)250e48354ceSNicholas Bellinger static int iscsi_target_check_first_request(
251be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
252e48354ceSNicholas Bellinger 	struct iscsi_login *login)
253e48354ceSNicholas Bellinger {
254e48354ceSNicholas Bellinger 	struct iscsi_param *param = NULL;
255e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
256e48354ceSNicholas Bellinger 
257e48354ceSNicholas Bellinger 	login->first_request = 0;
258e48354ceSNicholas Bellinger 
259e48354ceSNicholas Bellinger 	list_for_each_entry(param, &conn->param_list->param_list, p_list) {
260e48354ceSNicholas Bellinger 		if (!strncmp(param->name, SESSIONTYPE, 11)) {
261e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
262e48354ceSNicholas Bellinger 				pr_err("SessionType key not received"
263e48354ceSNicholas Bellinger 					" in first login request.\n");
264e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
265e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
266e48354ceSNicholas Bellinger 				return -1;
267e48354ceSNicholas Bellinger 			}
268e48354ceSNicholas Bellinger 			if (!strncmp(param->value, DISCOVERY, 9))
269e48354ceSNicholas Bellinger 				return 0;
270e48354ceSNicholas Bellinger 		}
271e48354ceSNicholas Bellinger 
272e48354ceSNicholas Bellinger 		if (!strncmp(param->name, INITIATORNAME, 13)) {
273e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
274e48354ceSNicholas Bellinger 				if (!login->leading_connection)
275e48354ceSNicholas Bellinger 					continue;
276e48354ceSNicholas Bellinger 
277e48354ceSNicholas Bellinger 				pr_err("InitiatorName key not received"
278e48354ceSNicholas Bellinger 					" in first login request.\n");
279e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
280e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
281e48354ceSNicholas Bellinger 				return -1;
282e48354ceSNicholas Bellinger 			}
283e48354ceSNicholas Bellinger 
284e48354ceSNicholas Bellinger 			/*
285e48354ceSNicholas Bellinger 			 * For non-leading connections, double check that the
286e48354ceSNicholas Bellinger 			 * received InitiatorName matches the existing session's
287e48354ceSNicholas Bellinger 			 * struct iscsi_node_acl.
288e48354ceSNicholas Bellinger 			 */
289e48354ceSNicholas Bellinger 			if (!login->leading_connection) {
290e48354ceSNicholas Bellinger 				se_nacl = conn->sess->se_sess->se_node_acl;
291e48354ceSNicholas Bellinger 				if (!se_nacl) {
292e48354ceSNicholas Bellinger 					pr_err("Unable to locate"
293e48354ceSNicholas Bellinger 						" struct se_node_acl\n");
294e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
295e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
296e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
297e48354ceSNicholas Bellinger 					return -1;
298e48354ceSNicholas Bellinger 				}
299e48354ceSNicholas Bellinger 
300e48354ceSNicholas Bellinger 				if (strcmp(param->value,
301e48354ceSNicholas Bellinger 						se_nacl->initiatorname)) {
302e48354ceSNicholas Bellinger 					pr_err("Incorrect"
303e48354ceSNicholas Bellinger 						" InitiatorName: %s for this"
304e48354ceSNicholas Bellinger 						" iSCSI Initiator Node.\n",
305e48354ceSNicholas Bellinger 						param->value);
306e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
307e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
308e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
309e48354ceSNicholas Bellinger 					return -1;
310e48354ceSNicholas Bellinger 				}
311e48354ceSNicholas Bellinger 			}
312e48354ceSNicholas Bellinger 		}
313e48354ceSNicholas Bellinger 	}
314e48354ceSNicholas Bellinger 
315e48354ceSNicholas Bellinger 	return 0;
316e48354ceSNicholas Bellinger }
317e48354ceSNicholas Bellinger 
iscsi_target_do_tx_login_io(struct iscsit_conn * conn,struct iscsi_login * login)318be36d683SMax Gurtovoy static int iscsi_target_do_tx_login_io(struct iscsit_conn *conn, struct iscsi_login *login)
319e48354ceSNicholas Bellinger {
320e48354ceSNicholas Bellinger 	u32 padding = 0;
321e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
322e48354ceSNicholas Bellinger 
323e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
324e48354ceSNicholas Bellinger 
325e48354ceSNicholas Bellinger 	login_rsp->opcode		= ISCSI_OP_LOGIN_RSP;
326e48354ceSNicholas Bellinger 	hton24(login_rsp->dlength, login->rsp_length);
327e48354ceSNicholas Bellinger 	memcpy(login_rsp->isid, login->isid, 6);
328e48354ceSNicholas Bellinger 	login_rsp->tsih			= cpu_to_be16(login->tsih);
32966c7db68SChristoph Hellwig 	login_rsp->itt			= login->init_task_tag;
330e48354ceSNicholas Bellinger 	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
331e48354ceSNicholas Bellinger 	login_rsp->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
332109e2381SRoland Dreier 	login_rsp->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
333e48354ceSNicholas Bellinger 
334e48354ceSNicholas Bellinger 	pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
335e48354ceSNicholas Bellinger 		" ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
33666c7db68SChristoph Hellwig 		" %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
337e48354ceSNicholas Bellinger 		ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
338e48354ceSNicholas Bellinger 		ntohl(login_rsp->statsn), login->rsp_length);
339e48354ceSNicholas Bellinger 
340e48354ceSNicholas Bellinger 	padding = ((-login->rsp_length) & 3);
341e5419865SNicholas Bellinger 	/*
342e5419865SNicholas Bellinger 	 * Before sending the last login response containing the transition
343e5419865SNicholas Bellinger 	 * bit for full-feature-phase, go ahead and start up TX/RX threads
344e5419865SNicholas Bellinger 	 * now to avoid potential resource allocation failures after the
345e5419865SNicholas Bellinger 	 * final login response has been sent.
346e5419865SNicholas Bellinger 	 */
347e5419865SNicholas Bellinger 	if (login->login_complete) {
348e5419865SNicholas Bellinger 		int rc = iscsit_start_kthreads(conn);
349e5419865SNicholas Bellinger 		if (rc) {
350e5419865SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
351e5419865SNicholas Bellinger 					    ISCSI_LOGIN_STATUS_NO_RESOURCES);
352e5419865SNicholas Bellinger 			return -1;
353e5419865SNicholas Bellinger 		}
354e5419865SNicholas Bellinger 	}
355e48354ceSNicholas Bellinger 
356baa4d64bSNicholas Bellinger 	if (conn->conn_transport->iscsit_put_login_tx(conn, login,
357e48354ceSNicholas Bellinger 					login->rsp_length + padding) < 0)
358e5419865SNicholas Bellinger 		goto err;
359e48354ceSNicholas Bellinger 
360e48354ceSNicholas Bellinger 	login->rsp_length		= 0;
361e48354ceSNicholas Bellinger 
362e48354ceSNicholas Bellinger 	return 0;
363e5419865SNicholas Bellinger 
364e5419865SNicholas Bellinger err:
365e5419865SNicholas Bellinger 	if (login->login_complete) {
366e5419865SNicholas Bellinger 		if (conn->rx_thread && conn->rx_thread_active) {
367e5419865SNicholas Bellinger 			send_sig(SIGINT, conn->rx_thread, 1);
368ca82c2bdSNicholas Bellinger 			complete(&conn->rx_login_comp);
369e5419865SNicholas Bellinger 			kthread_stop(conn->rx_thread);
370e5419865SNicholas Bellinger 		}
371e5419865SNicholas Bellinger 		if (conn->tx_thread && conn->tx_thread_active) {
372e5419865SNicholas Bellinger 			send_sig(SIGINT, conn->tx_thread, 1);
373e5419865SNicholas Bellinger 			kthread_stop(conn->tx_thread);
374e5419865SNicholas Bellinger 		}
375e5419865SNicholas Bellinger 		spin_lock(&iscsit_global->ts_bitmap_lock);
376e5419865SNicholas Bellinger 		bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
377e5419865SNicholas Bellinger 				      get_order(1));
378e5419865SNicholas Bellinger 		spin_unlock(&iscsit_global->ts_bitmap_lock);
379e5419865SNicholas Bellinger 	}
380e5419865SNicholas Bellinger 	return -1;
381e48354ceSNicholas Bellinger }
382e48354ceSNicholas Bellinger 
iscsi_target_sk_data_ready(struct sock * sk)383676d2369SDavid S. Miller static void iscsi_target_sk_data_ready(struct sock *sk)
384d381a801SNicholas Bellinger {
385be36d683SMax Gurtovoy 	struct iscsit_conn *conn = sk->sk_user_data;
386d381a801SNicholas Bellinger 	bool rc;
387d381a801SNicholas Bellinger 
38840e0b090SPeilin Ye 	trace_sk_data_ready(sk);
389d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
390d381a801SNicholas Bellinger 
391d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
392d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
393d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
394d381a801SNicholas Bellinger 		return;
395d381a801SNicholas Bellinger 	}
396d381a801SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
397d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
398d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
399d381a801SNicholas Bellinger 		return;
400d381a801SNicholas Bellinger 	}
401d381a801SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
402d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
403d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
404d381a801SNicholas Bellinger 		return;
405d381a801SNicholas Bellinger 	}
406d381a801SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
407d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
408d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
4091c130ae0SFlorian Westphal 		if (iscsi_target_sk_data_ready == conn->orig_data_ready)
4101c130ae0SFlorian Westphal 			return;
4111c130ae0SFlorian Westphal 		conn->orig_data_ready(sk);
412d381a801SNicholas Bellinger 		return;
413d381a801SNicholas Bellinger 	}
414d381a801SNicholas Bellinger 
415d381a801SNicholas Bellinger 	rc = schedule_delayed_work(&conn->login_work, 0);
4160bcc297eSChristophe Vu-Brugier 	if (!rc) {
417d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
418d381a801SNicholas Bellinger 			 " got false\n");
419d381a801SNicholas Bellinger 	}
420d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
421d381a801SNicholas Bellinger }
422d381a801SNicholas Bellinger 
423bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *);
424bb048357SNicholas Bellinger 
iscsi_target_set_sock_callbacks(struct iscsit_conn * conn)425be36d683SMax Gurtovoy static void iscsi_target_set_sock_callbacks(struct iscsit_conn *conn)
426d381a801SNicholas Bellinger {
427d381a801SNicholas Bellinger 	struct sock *sk;
428d381a801SNicholas Bellinger 
429d381a801SNicholas Bellinger 	if (!conn->sock)
430d381a801SNicholas Bellinger 		return;
431d381a801SNicholas Bellinger 
432d381a801SNicholas Bellinger 	sk = conn->sock->sk;
433d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
434d381a801SNicholas Bellinger 
435d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
436d381a801SNicholas Bellinger 	sk->sk_user_data = conn;
437d381a801SNicholas Bellinger 	conn->orig_data_ready = sk->sk_data_ready;
438bb048357SNicholas Bellinger 	conn->orig_state_change = sk->sk_state_change;
439d381a801SNicholas Bellinger 	sk->sk_data_ready = iscsi_target_sk_data_ready;
440bb048357SNicholas Bellinger 	sk->sk_state_change = iscsi_target_sk_state_change;
441d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
442bb048357SNicholas Bellinger 
443bb048357SNicholas Bellinger 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
444bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
445d381a801SNicholas Bellinger }
446d381a801SNicholas Bellinger 
iscsi_target_restore_sock_callbacks(struct iscsit_conn * conn)447be36d683SMax Gurtovoy static void iscsi_target_restore_sock_callbacks(struct iscsit_conn *conn)
448d381a801SNicholas Bellinger {
449d381a801SNicholas Bellinger 	struct sock *sk;
450d381a801SNicholas Bellinger 
451d381a801SNicholas Bellinger 	if (!conn->sock)
452d381a801SNicholas Bellinger 		return;
453d381a801SNicholas Bellinger 
454d381a801SNicholas Bellinger 	sk = conn->sock->sk;
455d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
456d381a801SNicholas Bellinger 
457d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
458d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
459d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
460d381a801SNicholas Bellinger 		return;
461d381a801SNicholas Bellinger 	}
462d381a801SNicholas Bellinger 	sk->sk_user_data = NULL;
463d381a801SNicholas Bellinger 	sk->sk_data_ready = conn->orig_data_ready;
464bb048357SNicholas Bellinger 	sk->sk_state_change = conn->orig_state_change;
465d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
466bb048357SNicholas Bellinger 
467bb048357SNicholas Bellinger 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
468bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
469d381a801SNicholas Bellinger }
470d381a801SNicholas Bellinger 
471be36d683SMax Gurtovoy static int iscsi_target_do_login(struct iscsit_conn *, struct iscsi_login *);
472d381a801SNicholas Bellinger 
__iscsi_target_sk_check_close(struct sock * sk)47325cdda95SNicholas Bellinger static bool __iscsi_target_sk_check_close(struct sock *sk)
474d381a801SNicholas Bellinger {
475d381a801SNicholas Bellinger 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
47625cdda95SNicholas Bellinger 		pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
477df2de6f2SHou Pu 			"returning TRUE\n");
47825cdda95SNicholas Bellinger 		return true;
47925cdda95SNicholas Bellinger 	}
480d381a801SNicholas Bellinger 	return false;
481d381a801SNicholas Bellinger }
48225cdda95SNicholas Bellinger 
iscsi_target_sk_check_close(struct iscsit_conn * conn)483be36d683SMax Gurtovoy static bool iscsi_target_sk_check_close(struct iscsit_conn *conn)
48425cdda95SNicholas Bellinger {
48525cdda95SNicholas Bellinger 	bool state = false;
48625cdda95SNicholas Bellinger 
48725cdda95SNicholas Bellinger 	if (conn->sock) {
48825cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
48925cdda95SNicholas Bellinger 
49025cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
49125cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
49225cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
49325cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
49425cdda95SNicholas Bellinger 	}
49525cdda95SNicholas Bellinger 	return state;
49625cdda95SNicholas Bellinger }
49725cdda95SNicholas Bellinger 
iscsi_target_sk_check_flag(struct iscsit_conn * conn,unsigned int flag)498be36d683SMax Gurtovoy static bool iscsi_target_sk_check_flag(struct iscsit_conn *conn, unsigned int flag)
49925cdda95SNicholas Bellinger {
50025cdda95SNicholas Bellinger 	bool state = false;
50125cdda95SNicholas Bellinger 
50225cdda95SNicholas Bellinger 	if (conn->sock) {
50325cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
50425cdda95SNicholas Bellinger 
50525cdda95SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
50625cdda95SNicholas Bellinger 		state = test_bit(flag, &conn->login_flags);
50725cdda95SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
50825cdda95SNicholas Bellinger 	}
50925cdda95SNicholas Bellinger 	return state;
51025cdda95SNicholas Bellinger }
51125cdda95SNicholas Bellinger 
iscsi_target_sk_check_and_clear(struct iscsit_conn * conn,unsigned int flag)512be36d683SMax Gurtovoy static bool iscsi_target_sk_check_and_clear(struct iscsit_conn *conn, unsigned int flag)
51325cdda95SNicholas Bellinger {
51425cdda95SNicholas Bellinger 	bool state = false;
51525cdda95SNicholas Bellinger 
51625cdda95SNicholas Bellinger 	if (conn->sock) {
51725cdda95SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
51825cdda95SNicholas Bellinger 
51925cdda95SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
52025cdda95SNicholas Bellinger 		state = (__iscsi_target_sk_check_close(sk) ||
52125cdda95SNicholas Bellinger 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
52225cdda95SNicholas Bellinger 		if (!state)
52325cdda95SNicholas Bellinger 			clear_bit(flag, &conn->login_flags);
52425cdda95SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
52525cdda95SNicholas Bellinger 	}
52625cdda95SNicholas Bellinger 	return state;
527d381a801SNicholas Bellinger }
528d381a801SNicholas Bellinger 
iscsi_target_login_drop(struct iscsit_conn * conn,struct iscsi_login * login)529be36d683SMax Gurtovoy static void iscsi_target_login_drop(struct iscsit_conn *conn, struct iscsi_login *login)
530d381a801SNicholas Bellinger {
531d381a801SNicholas Bellinger 	bool zero_tsih = login->zero_tsih;
532d381a801SNicholas Bellinger 
533d381a801SNicholas Bellinger 	iscsi_remove_failed_auth_entry(conn);
534d381a801SNicholas Bellinger 	iscsi_target_nego_release(conn);
535ed43ffeaSHou Pu 	iscsi_target_login_sess_out(conn, zero_tsih, true);
536d381a801SNicholas Bellinger }
537d381a801SNicholas Bellinger 
iscsi_target_do_login_rx(struct work_struct * work)538d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work)
539d381a801SNicholas Bellinger {
540be36d683SMax Gurtovoy 	struct iscsit_conn *conn = container_of(work,
541be36d683SMax Gurtovoy 				struct iscsit_conn, login_work.work);
542d381a801SNicholas Bellinger 	struct iscsi_login *login = conn->login;
543d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
544d381a801SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
545d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
546d381a801SNicholas Bellinger 	int rc, zero_tsih = login->zero_tsih;
547d381a801SNicholas Bellinger 	bool state;
548d381a801SNicholas Bellinger 
549d381a801SNicholas Bellinger 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
550d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
55113247018SMaurizio Lombardi 
55213247018SMaurizio Lombardi 	spin_lock(&conn->login_worker_lock);
55313247018SMaurizio Lombardi 	set_bit(LOGIN_FLAGS_WORKER_RUNNING, &conn->login_flags);
55413247018SMaurizio Lombardi 	spin_unlock(&conn->login_worker_lock);
55525cdda95SNicholas Bellinger 	/*
55625cdda95SNicholas Bellinger 	 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
55725cdda95SNicholas Bellinger 	 * before initial PDU processing in iscsi_target_start_negotiation()
55825cdda95SNicholas Bellinger 	 * has completed, go ahead and retry until it's cleared.
55925cdda95SNicholas Bellinger 	 *
56025cdda95SNicholas Bellinger 	 * Otherwise if the TCP connection drops while this is occuring,
56125cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() will detect the failure, call
56225cdda95SNicholas Bellinger 	 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
56325cdda95SNicholas Bellinger 	 * remaining iscsi connection resources from iscsi_np process context.
56425cdda95SNicholas Bellinger 	 */
56525cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
56625cdda95SNicholas Bellinger 		schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
56725cdda95SNicholas Bellinger 		return;
56825cdda95SNicholas Bellinger 	}
569d381a801SNicholas Bellinger 
570d381a801SNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
571d381a801SNicholas Bellinger 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
572d381a801SNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
573d381a801SNicholas Bellinger 
5740bcc297eSChristophe Vu-Brugier 	if (!state) {
575d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
57625cdda95SNicholas Bellinger 		goto err;
577d381a801SNicholas Bellinger 	}
578d381a801SNicholas Bellinger 
57925cdda95SNicholas Bellinger 	if (iscsi_target_sk_check_close(conn)) {
580d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
58125cdda95SNicholas Bellinger 		goto err;
582d381a801SNicholas Bellinger 	}
583d381a801SNicholas Bellinger 
584d381a801SNicholas Bellinger 	allow_signal(SIGINT);
58513247018SMaurizio Lombardi 	rc = iscsit_set_login_timer_kworker(conn, current);
58613247018SMaurizio Lombardi 	if (rc < 0) {
58713247018SMaurizio Lombardi 		/* The login timer has already expired */
58813247018SMaurizio Lombardi 		pr_debug("iscsi_target_do_login_rx, login failed\n");
58913247018SMaurizio Lombardi 		goto err;
59013247018SMaurizio Lombardi 	}
591d381a801SNicholas Bellinger 
592d381a801SNicholas Bellinger 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
593d381a801SNicholas Bellinger 	flush_signals(current);
594d381a801SNicholas Bellinger 
59525cdda95SNicholas Bellinger 	if (rc < 0)
59625cdda95SNicholas Bellinger 		goto err;
597d381a801SNicholas Bellinger 
598d381a801SNicholas Bellinger 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
599d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
600d381a801SNicholas Bellinger 
6014e108d4fSHou Pu 	/*
6024e108d4fSHou Pu 	 * LOGIN_FLAGS_READ_ACTIVE is cleared so that sk_data_ready
6034e108d4fSHou Pu 	 * could be triggered again after this.
6044e108d4fSHou Pu 	 *
6054e108d4fSHou Pu 	 * LOGIN_FLAGS_WRITE_ACTIVE is cleared after we successfully
6064e108d4fSHou Pu 	 * process a login PDU, so that sk_state_chage can do login
6074e108d4fSHou Pu 	 * cleanup as needed if the socket is closed. If a delayed work is
6084e108d4fSHou Pu 	 * ongoing (LOGIN_FLAGS_WRITE_ACTIVE or LOGIN_FLAGS_READ_ACTIVE),
6094e108d4fSHou Pu 	 * sk_state_change will leave the cleanup to the delayed work or
6104e108d4fSHou Pu 	 * it will schedule a delayed work to do cleanup.
6114e108d4fSHou Pu 	 */
6124e108d4fSHou Pu 	if (conn->sock) {
6134e108d4fSHou Pu 		struct sock *sk = conn->sock->sk;
6144e108d4fSHou Pu 
6154e108d4fSHou Pu 		write_lock_bh(&sk->sk_callback_lock);
6164e108d4fSHou Pu 		if (!test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags)) {
6174e108d4fSHou Pu 			clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
6184e108d4fSHou Pu 			set_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags);
6194e108d4fSHou Pu 		}
6204e108d4fSHou Pu 		write_unlock_bh(&sk->sk_callback_lock);
6214e108d4fSHou Pu 	}
6224e108d4fSHou Pu 
623d381a801SNicholas Bellinger 	rc = iscsi_target_do_login(conn, login);
624d381a801SNicholas Bellinger 	if (rc < 0) {
62525cdda95SNicholas Bellinger 		goto err;
626d381a801SNicholas Bellinger 	} else if (!rc) {
6274e108d4fSHou Pu 		if (iscsi_target_sk_check_and_clear(conn,
6284e108d4fSHou Pu 						    LOGIN_FLAGS_WRITE_ACTIVE))
62925cdda95SNicholas Bellinger 			goto err;
63013247018SMaurizio Lombardi 
63113247018SMaurizio Lombardi 		/*
63213247018SMaurizio Lombardi 		 * Set the login timer thread pointer to NULL to prevent the
63313247018SMaurizio Lombardi 		 * login process from getting stuck if the initiator
63413247018SMaurizio Lombardi 		 * stops sending data.
63513247018SMaurizio Lombardi 		 */
63613247018SMaurizio Lombardi 		rc = iscsit_set_login_timer_kworker(conn, NULL);
63713247018SMaurizio Lombardi 		if (rc < 0)
63813247018SMaurizio Lombardi 			goto err;
639d381a801SNicholas Bellinger 	} else if (rc == 1) {
64013247018SMaurizio Lombardi 		iscsit_stop_login_timer(conn);
6414e108d4fSHou Pu 		cancel_delayed_work(&conn->login_work);
642d381a801SNicholas Bellinger 		iscsi_target_nego_release(conn);
643d381a801SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
644d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
645d381a801SNicholas Bellinger 	}
64625cdda95SNicholas Bellinger 	return;
64725cdda95SNicholas Bellinger 
64825cdda95SNicholas Bellinger err:
64925cdda95SNicholas Bellinger 	iscsi_target_restore_sock_callbacks(conn);
65013247018SMaurizio Lombardi 	iscsit_stop_login_timer(conn);
6514e108d4fSHou Pu 	cancel_delayed_work(&conn->login_work);
65225cdda95SNicholas Bellinger 	iscsi_target_login_drop(conn, login);
65325cdda95SNicholas Bellinger 	iscsit_deaccess_np(np, tpg, tpg_np);
654d381a801SNicholas Bellinger }
655d381a801SNicholas Bellinger 
iscsi_target_sk_state_change(struct sock * sk)656bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk)
657bb048357SNicholas Bellinger {
658be36d683SMax Gurtovoy 	struct iscsit_conn *conn;
659bb048357SNicholas Bellinger 	void (*orig_state_change)(struct sock *);
660bb048357SNicholas Bellinger 	bool state;
661bb048357SNicholas Bellinger 
662bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_state_change\n");
663bb048357SNicholas Bellinger 
664bb048357SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
665bb048357SNicholas Bellinger 	conn = sk->sk_user_data;
666bb048357SNicholas Bellinger 	if (!conn) {
667bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
668bb048357SNicholas Bellinger 		return;
669bb048357SNicholas Bellinger 	}
670bb048357SNicholas Bellinger 	orig_state_change = conn->orig_state_change;
671bb048357SNicholas Bellinger 
672bb048357SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
673bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
674bb048357SNicholas Bellinger 			 conn);
675bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
676bb048357SNicholas Bellinger 		orig_state_change(sk);
677bb048357SNicholas Bellinger 		return;
678bb048357SNicholas Bellinger 	}
67925cdda95SNicholas Bellinger 	state = __iscsi_target_sk_check_close(sk);
68025cdda95SNicholas Bellinger 	pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
68125cdda95SNicholas Bellinger 
6824e108d4fSHou Pu 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags) ||
6834e108d4fSHou Pu 	    test_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags)) {
6844e108d4fSHou Pu 		pr_debug("Got LOGIN_FLAGS_{READ|WRITE}_ACTIVE=1"
6854e108d4fSHou Pu 			 " sk_state_change conn: %p\n", conn);
68625cdda95SNicholas Bellinger 		if (state)
68725cdda95SNicholas Bellinger 			set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
688bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
689bb048357SNicholas Bellinger 		orig_state_change(sk);
690bb048357SNicholas Bellinger 		return;
691bb048357SNicholas Bellinger 	}
69225cdda95SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
693bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
694bb048357SNicholas Bellinger 			 conn);
695bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
696bb048357SNicholas Bellinger 		orig_state_change(sk);
697bb048357SNicholas Bellinger 		return;
698bb048357SNicholas Bellinger 	}
69925cdda95SNicholas Bellinger 	/*
70025cdda95SNicholas Bellinger 	 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
70125cdda95SNicholas Bellinger 	 * but only queue conn->login_work -> iscsi_target_do_login_rx()
70225cdda95SNicholas Bellinger 	 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
70325cdda95SNicholas Bellinger 	 *
70425cdda95SNicholas Bellinger 	 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
70525cdda95SNicholas Bellinger 	 * will detect the dropped TCP connection from delayed workqueue context.
70625cdda95SNicholas Bellinger 	 *
70725cdda95SNicholas Bellinger 	 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
70825cdda95SNicholas Bellinger 	 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
70925cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
71025cdda95SNicholas Bellinger 	 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
71125cdda95SNicholas Bellinger 	 * dropped TCP connection in iscsi_np process context, and cleaning up
71225cdda95SNicholas Bellinger 	 * the remaining iscsi connection resources.
71325cdda95SNicholas Bellinger 	 */
71425cdda95SNicholas Bellinger 	if (state) {
71525cdda95SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_change got failed state\n");
71625cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
71725cdda95SNicholas Bellinger 		state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
718bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
719bb048357SNicholas Bellinger 
72025cdda95SNicholas Bellinger 		orig_state_change(sk);
721bb048357SNicholas Bellinger 
72225cdda95SNicholas Bellinger 		if (!state)
72325cdda95SNicholas Bellinger 			schedule_delayed_work(&conn->login_work, 0);
724bb048357SNicholas Bellinger 		return;
725bb048357SNicholas Bellinger 	}
72625cdda95SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
72725cdda95SNicholas Bellinger 
728bb048357SNicholas Bellinger 	orig_state_change(sk);
729bb048357SNicholas Bellinger }
730bb048357SNicholas Bellinger 
731e48354ceSNicholas Bellinger /*
732e48354ceSNicholas Bellinger  *	NOTE: We check for existing sessions or connections AFTER the initiator
733e48354ceSNicholas Bellinger  *	has been successfully authenticated in order to protect against faked
734e48354ceSNicholas Bellinger  *	ISID/TSIH combinations.
735e48354ceSNicholas Bellinger  */
iscsi_target_check_for_existing_instances(struct iscsit_conn * conn,struct iscsi_login * login)736e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances(
737be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
738e48354ceSNicholas Bellinger 	struct iscsi_login *login)
739e48354ceSNicholas Bellinger {
740e48354ceSNicholas Bellinger 	if (login->checked_for_existing)
741e48354ceSNicholas Bellinger 		return 0;
742e48354ceSNicholas Bellinger 
743e48354ceSNicholas Bellinger 	login->checked_for_existing = 1;
744e48354ceSNicholas Bellinger 
745e48354ceSNicholas Bellinger 	if (!login->tsih)
746e48354ceSNicholas Bellinger 		return iscsi_check_for_session_reinstatement(conn);
747e48354ceSNicholas Bellinger 	else
748e48354ceSNicholas Bellinger 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
749e48354ceSNicholas Bellinger 				login->initial_exp_statsn);
750e48354ceSNicholas Bellinger }
751e48354ceSNicholas Bellinger 
iscsi_target_do_authentication(struct iscsit_conn * conn,struct iscsi_login * login)752e48354ceSNicholas Bellinger static int iscsi_target_do_authentication(
753be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
754e48354ceSNicholas Bellinger 	struct iscsi_login *login)
755e48354ceSNicholas Bellinger {
756e48354ceSNicholas Bellinger 	int authret;
757e48354ceSNicholas Bellinger 	u32 payload_length;
758e48354ceSNicholas Bellinger 	struct iscsi_param *param;
759e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
760e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
761e48354ceSNicholas Bellinger 
762e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
763e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
764e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
765e48354ceSNicholas Bellinger 
766e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
767e48354ceSNicholas Bellinger 	if (!param)
768e48354ceSNicholas Bellinger 		return -1;
769e48354ceSNicholas Bellinger 
770e48354ceSNicholas Bellinger 	authret = iscsi_handle_authentication(
771e48354ceSNicholas Bellinger 			conn,
772e48354ceSNicholas Bellinger 			login->req_buf,
773e48354ceSNicholas Bellinger 			login->rsp_buf,
774e48354ceSNicholas Bellinger 			payload_length,
775e48354ceSNicholas Bellinger 			&login->rsp_length,
776e48354ceSNicholas Bellinger 			param->value);
777e48354ceSNicholas Bellinger 	switch (authret) {
778e48354ceSNicholas Bellinger 	case 0:
779e48354ceSNicholas Bellinger 		pr_debug("Received OK response"
780e48354ceSNicholas Bellinger 		" from LIO Authentication, continuing.\n");
781e48354ceSNicholas Bellinger 		break;
782e48354ceSNicholas Bellinger 	case 1:
783e48354ceSNicholas Bellinger 		pr_debug("iSCSI security negotiation"
784bfb9035cSJoe Perches 			" completed successfully.\n");
785e48354ceSNicholas Bellinger 		login->auth_complete = 1;
786e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
787e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
788e48354ceSNicholas Bellinger 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
789e48354ceSNicholas Bellinger 					     ISCSI_FLAG_LOGIN_TRANSIT);
790e48354ceSNicholas Bellinger 			login->current_stage = 1;
791e48354ceSNicholas Bellinger 		}
792e48354ceSNicholas Bellinger 		return iscsi_target_check_for_existing_instances(
793e48354ceSNicholas Bellinger 				conn, login);
794e48354ceSNicholas Bellinger 	case 2:
795e48354ceSNicholas Bellinger 		pr_err("Security negotiation"
796e48354ceSNicholas Bellinger 			" failed.\n");
797e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
798e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
799e48354ceSNicholas Bellinger 		return -1;
800e48354ceSNicholas Bellinger 	default:
801e48354ceSNicholas Bellinger 		pr_err("Received unknown error %d from LIO"
802e48354ceSNicholas Bellinger 				" Authentication\n", authret);
803e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
804e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
805e48354ceSNicholas Bellinger 		return -1;
806e48354ceSNicholas Bellinger 	}
807e48354ceSNicholas Bellinger 
808e48354ceSNicholas Bellinger 	return 0;
809e48354ceSNicholas Bellinger }
810e48354ceSNicholas Bellinger 
iscsi_conn_auth_required(struct iscsit_conn * conn)811e52b904bSDmitry Bogdanov bool iscsi_conn_auth_required(struct iscsit_conn *conn)
812a75fcb09SDmitry Bogdanov {
813a6e0d179SDmitry Bogdanov 	struct iscsi_node_acl *nacl;
814a75fcb09SDmitry Bogdanov 	struct se_node_acl *se_nacl;
815a75fcb09SDmitry Bogdanov 
816a75fcb09SDmitry Bogdanov 	if (conn->sess->sess_ops->SessionType) {
817a75fcb09SDmitry Bogdanov 		/*
818a75fcb09SDmitry Bogdanov 		 * For SessionType=Discovery
819a75fcb09SDmitry Bogdanov 		 */
820a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
821a75fcb09SDmitry Bogdanov 	}
822a75fcb09SDmitry Bogdanov 	/*
823a75fcb09SDmitry Bogdanov 	 * For SessionType=Normal
824a75fcb09SDmitry Bogdanov 	 */
825a75fcb09SDmitry Bogdanov 	se_nacl = conn->sess->se_sess->se_node_acl;
826a75fcb09SDmitry Bogdanov 	if (!se_nacl) {
82735bf020bSYang Li 		pr_debug("Unknown ACL is trying to connect\n");
828a75fcb09SDmitry Bogdanov 		return true;
829a75fcb09SDmitry Bogdanov 	}
830a75fcb09SDmitry Bogdanov 
831a75fcb09SDmitry Bogdanov 	if (se_nacl->dynamic_node_acl) {
832a75fcb09SDmitry Bogdanov 		pr_debug("Dynamic ACL %s is trying to connect\n",
833a75fcb09SDmitry Bogdanov 			 se_nacl->initiatorname);
834a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
835a75fcb09SDmitry Bogdanov 	}
836a75fcb09SDmitry Bogdanov 
837a75fcb09SDmitry Bogdanov 	pr_debug("Known ACL %s is trying to connect\n",
838a75fcb09SDmitry Bogdanov 		 se_nacl->initiatorname);
839a6e0d179SDmitry Bogdanov 
840a6e0d179SDmitry Bogdanov 	nacl = to_iscsi_nacl(se_nacl);
841a6e0d179SDmitry Bogdanov 	if (nacl->node_attrib.authentication == NA_AUTHENTICATION_INHERITED)
842a75fcb09SDmitry Bogdanov 		return conn->tpg->tpg_attrib.authentication;
843a6e0d179SDmitry Bogdanov 
844a6e0d179SDmitry Bogdanov 	return nacl->node_attrib.authentication;
845a75fcb09SDmitry Bogdanov }
846a75fcb09SDmitry Bogdanov 
iscsi_target_handle_csg_zero(struct iscsit_conn * conn,struct iscsi_login * login)847e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero(
848be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
849e48354ceSNicholas Bellinger 	struct iscsi_login *login)
850e48354ceSNicholas Bellinger {
851e48354ceSNicholas Bellinger 	int ret;
852e48354ceSNicholas Bellinger 	u32 payload_length;
853e48354ceSNicholas Bellinger 	struct iscsi_param *param;
854e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
855e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
856e48354ceSNicholas Bellinger 
857e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
858e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
859e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
860e48354ceSNicholas Bellinger 
861e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
862e48354ceSNicholas Bellinger 	if (!param)
863e48354ceSNicholas Bellinger 		return -1;
864e48354ceSNicholas Bellinger 
865e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
866e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
867e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
868e48354ceSNicholas Bellinger 			login->req_buf,
869e48354ceSNicholas Bellinger 			payload_length,
8709977bb18SNicholas Bellinger 			conn);
871e48354ceSNicholas Bellinger 	if (ret < 0)
872e48354ceSNicholas Bellinger 		return -1;
873e48354ceSNicholas Bellinger 
874e48354ceSNicholas Bellinger 	if (ret > 0) {
875e48354ceSNicholas Bellinger 		if (login->auth_complete) {
876e48354ceSNicholas Bellinger 			pr_err("Initiator has already been"
877e48354ceSNicholas Bellinger 				" successfully authenticated, but is still"
878e48354ceSNicholas Bellinger 				" sending %s keys.\n", param->value);
879e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
880e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_INIT_ERR);
881e48354ceSNicholas Bellinger 			return -1;
882e48354ceSNicholas Bellinger 		}
883e48354ceSNicholas Bellinger 
884e48354ceSNicholas Bellinger 		goto do_auth;
88591f0abfdSNicholas Bellinger 	} else if (!payload_length) {
88691f0abfdSNicholas Bellinger 		pr_err("Initiator sent zero length security payload,"
88791f0abfdSNicholas Bellinger 		       " login failed\n");
88891f0abfdSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
88991f0abfdSNicholas Bellinger 				    ISCSI_LOGIN_STATUS_AUTH_FAILED);
89091f0abfdSNicholas Bellinger 		return -1;
891e48354ceSNicholas Bellinger 	}
892e48354ceSNicholas Bellinger 
893e48354ceSNicholas Bellinger 	if (login->first_request)
894e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
895e48354ceSNicholas Bellinger 			return -1;
896e48354ceSNicholas Bellinger 
897e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
898e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
899e48354ceSNicholas Bellinger 			SENDER_TARGET,
900e48354ceSNicholas Bellinger 			login->rsp_buf,
901e48354ceSNicholas Bellinger 			&login->rsp_length,
902138d351eSNicholas Bellinger 			conn->param_list,
903138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
904e48354ceSNicholas Bellinger 	if (ret < 0)
905e48354ceSNicholas Bellinger 		return -1;
906e48354ceSNicholas Bellinger 
907e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
908a75fcb09SDmitry Bogdanov 		bool auth_required = iscsi_conn_auth_required(conn);
909a75fcb09SDmitry Bogdanov 
910a75fcb09SDmitry Bogdanov 		if (auth_required) {
911a75fcb09SDmitry Bogdanov 			if (!strncmp(param->value, NONE, 4)) {
912e48354ceSNicholas Bellinger 				pr_err("Initiator sent AuthMethod=None but"
913e48354ceSNicholas Bellinger 				       " Target is enforcing iSCSI Authentication,"
914e48354ceSNicholas Bellinger 				       " login failed.\n");
915a75fcb09SDmitry Bogdanov 				iscsit_tx_login_rsp(conn,
916a75fcb09SDmitry Bogdanov 						ISCSI_STATUS_CLS_INITIATOR_ERR,
917e48354ceSNicholas Bellinger 						ISCSI_LOGIN_STATUS_AUTH_FAILED);
918e48354ceSNicholas Bellinger 				return -1;
919e48354ceSNicholas Bellinger 			}
920e48354ceSNicholas Bellinger 
921a75fcb09SDmitry Bogdanov 			if (!login->auth_complete)
922e48354ceSNicholas Bellinger 				return 0;
923e48354ceSNicholas Bellinger 
924a75fcb09SDmitry Bogdanov 			if (strncmp(param->value, NONE, 4) &&
925a75fcb09SDmitry Bogdanov 			    !login->auth_complete)
926e48354ceSNicholas Bellinger 				return 0;
927a75fcb09SDmitry Bogdanov 		}
928e48354ceSNicholas Bellinger 
929e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
930e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
931e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
932e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
933e48354ceSNicholas Bellinger 			login->current_stage = 1;
934e48354ceSNicholas Bellinger 		}
935e48354ceSNicholas Bellinger 	}
936e48354ceSNicholas Bellinger 
937e48354ceSNicholas Bellinger 	return 0;
938e48354ceSNicholas Bellinger do_auth:
939e48354ceSNicholas Bellinger 	return iscsi_target_do_authentication(conn, login);
940e48354ceSNicholas Bellinger }
941e48354ceSNicholas Bellinger 
iscsi_conn_authenticated(struct iscsit_conn * conn,struct iscsi_login * login)942a75fcb09SDmitry Bogdanov static bool iscsi_conn_authenticated(struct iscsit_conn *conn,
943a75fcb09SDmitry Bogdanov 				     struct iscsi_login *login)
944a75fcb09SDmitry Bogdanov {
945a75fcb09SDmitry Bogdanov 	if (!iscsi_conn_auth_required(conn))
946a75fcb09SDmitry Bogdanov 		return true;
947a75fcb09SDmitry Bogdanov 
948a75fcb09SDmitry Bogdanov 	if (login->auth_complete)
949a75fcb09SDmitry Bogdanov 		return true;
950a75fcb09SDmitry Bogdanov 
951a75fcb09SDmitry Bogdanov 	return false;
952a75fcb09SDmitry Bogdanov }
953a75fcb09SDmitry Bogdanov 
iscsi_target_handle_csg_one(struct iscsit_conn * conn,struct iscsi_login * login)954be36d683SMax Gurtovoy static int iscsi_target_handle_csg_one(struct iscsit_conn *conn, struct iscsi_login *login)
955e48354ceSNicholas Bellinger {
956e48354ceSNicholas Bellinger 	int ret;
957e48354ceSNicholas Bellinger 	u32 payload_length;
958e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
959e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
960e48354ceSNicholas Bellinger 
961e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
962e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
963e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
964e48354ceSNicholas Bellinger 
965e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
966e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
967e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
968e48354ceSNicholas Bellinger 			login->req_buf,
969e48354ceSNicholas Bellinger 			payload_length,
9709977bb18SNicholas Bellinger 			conn);
9711c5c12c6SRoland Dreier 	if (ret < 0) {
9721c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
9731c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
974e48354ceSNicholas Bellinger 		return -1;
9751c5c12c6SRoland Dreier 	}
976e48354ceSNicholas Bellinger 
977e48354ceSNicholas Bellinger 	if (login->first_request)
978e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
979e48354ceSNicholas Bellinger 			return -1;
980e48354ceSNicholas Bellinger 
981e48354ceSNicholas Bellinger 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
982e48354ceSNicholas Bellinger 		return -1;
983e48354ceSNicholas Bellinger 
984e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
985e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
986e48354ceSNicholas Bellinger 			SENDER_TARGET,
987e48354ceSNicholas Bellinger 			login->rsp_buf,
988e48354ceSNicholas Bellinger 			&login->rsp_length,
989138d351eSNicholas Bellinger 			conn->param_list,
990138d351eSNicholas Bellinger 			conn->tpg->tpg_attrib.login_keys_workaround);
9911c5c12c6SRoland Dreier 	if (ret < 0) {
9921c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
9931c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
994e48354ceSNicholas Bellinger 		return -1;
9951c5c12c6SRoland Dreier 	}
996e48354ceSNicholas Bellinger 
997a75fcb09SDmitry Bogdanov 	if (!iscsi_conn_authenticated(conn, login)) {
998e48354ceSNicholas Bellinger 		pr_err("Initiator is requesting CSG: 1, has not been"
999e48354ceSNicholas Bellinger 		       " successfully authenticated, and the Target is"
1000e48354ceSNicholas Bellinger 		       " enforcing iSCSI Authentication, login failed.\n");
1001e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1002e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
1003e48354ceSNicholas Bellinger 		return -1;
1004e48354ceSNicholas Bellinger 	}
1005e48354ceSNicholas Bellinger 
1006e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list))
1007e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
1008e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
1009e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
1010e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
1011e48354ceSNicholas Bellinger 
1012e48354ceSNicholas Bellinger 	return 0;
1013e48354ceSNicholas Bellinger }
1014e48354ceSNicholas Bellinger 
1015fec1b2faSMaurizio Lombardi /*
1016fec1b2faSMaurizio Lombardi  * RETURN VALUE:
1017fec1b2faSMaurizio Lombardi  *
1018fec1b2faSMaurizio Lombardi  *  1 = Login successful
1019fec1b2faSMaurizio Lombardi  * -1 = Login failed
1020fec1b2faSMaurizio Lombardi  *  0 = More PDU exchanges required
1021fec1b2faSMaurizio Lombardi  */
iscsi_target_do_login(struct iscsit_conn * conn,struct iscsi_login * login)1022be36d683SMax Gurtovoy static int iscsi_target_do_login(struct iscsit_conn *conn, struct iscsi_login *login)
1023e48354ceSNicholas Bellinger {
1024e48354ceSNicholas Bellinger 	int pdu_count = 0;
1025e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
1026e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
1027e48354ceSNicholas Bellinger 
1028e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1029e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
1030e48354ceSNicholas Bellinger 
1031e48354ceSNicholas Bellinger 	while (1) {
1032e48354ceSNicholas Bellinger 		if (++pdu_count > MAX_LOGIN_PDUS) {
1033e48354ceSNicholas Bellinger 			pr_err("MAX_LOGIN_PDUS count reached.\n");
1034e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1035e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
1036e48354ceSNicholas Bellinger 			return -1;
1037e48354ceSNicholas Bellinger 		}
1038e48354ceSNicholas Bellinger 
10395d358065SAndy Grover 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
1040e48354ceSNicholas Bellinger 		case 0:
10415d358065SAndy Grover 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
1042e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
1043e48354ceSNicholas Bellinger 				return -1;
1044e48354ceSNicholas Bellinger 			break;
1045e48354ceSNicholas Bellinger 		case 1:
1046e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
1047e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_one(conn, login) < 0)
1048e48354ceSNicholas Bellinger 				return -1;
1049e48354ceSNicholas Bellinger 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
105025cdda95SNicholas Bellinger 				/*
105125cdda95SNicholas Bellinger 				 * Check to make sure the TCP connection has not
105225cdda95SNicholas Bellinger 				 * dropped asynchronously while session reinstatement
105325cdda95SNicholas Bellinger 				 * was occuring in this kthread context, before
105425cdda95SNicholas Bellinger 				 * transitioning to full feature phase operation.
105525cdda95SNicholas Bellinger 				 */
105625cdda95SNicholas Bellinger 				if (iscsi_target_sk_check_close(conn))
105725cdda95SNicholas Bellinger 					return -1;
105825cdda95SNicholas Bellinger 
1059e48354ceSNicholas Bellinger 				login->tsih = conn->sess->tsih;
1060baa4d64bSNicholas Bellinger 				login->login_complete = 1;
1061d381a801SNicholas Bellinger 				iscsi_target_restore_sock_callbacks(conn);
1062e48354ceSNicholas Bellinger 				if (iscsi_target_do_tx_login_io(conn,
1063e48354ceSNicholas Bellinger 						login) < 0)
1064e48354ceSNicholas Bellinger 					return -1;
1065d381a801SNicholas Bellinger 				return 1;
1066e48354ceSNicholas Bellinger 			}
1067e48354ceSNicholas Bellinger 			break;
1068e48354ceSNicholas Bellinger 		default:
1069e48354ceSNicholas Bellinger 			pr_err("Illegal CSG: %d received from"
1070e48354ceSNicholas Bellinger 				" Initiator, protocol error.\n",
10715d358065SAndy Grover 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
1072e48354ceSNicholas Bellinger 			break;
1073e48354ceSNicholas Bellinger 		}
1074e48354ceSNicholas Bellinger 
1075ea3a179aSNicholas Bellinger 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
1076e48354ceSNicholas Bellinger 			return -1;
1077e48354ceSNicholas Bellinger 
1078e48354ceSNicholas Bellinger 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1079e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1080e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1081e48354ceSNicholas Bellinger 		}
1082d381a801SNicholas Bellinger 		break;
1083e48354ceSNicholas Bellinger 	}
1084e48354ceSNicholas Bellinger 
1085e48354ceSNicholas Bellinger 	return 0;
1086e48354ceSNicholas Bellinger }
1087e48354ceSNicholas Bellinger 
iscsi_initiatorname_tolower(char * param_buf)1088e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower(
1089e48354ceSNicholas Bellinger 	char *param_buf)
1090e48354ceSNicholas Bellinger {
1091e48354ceSNicholas Bellinger 	char *c;
1092e48354ceSNicholas Bellinger 	u32 iqn_size = strlen(param_buf), i;
1093e48354ceSNicholas Bellinger 
1094e48354ceSNicholas Bellinger 	for (i = 0; i < iqn_size; i++) {
10958359cf43SJörn Engel 		c = &param_buf[i];
1096e48354ceSNicholas Bellinger 		if (!isupper(*c))
1097e48354ceSNicholas Bellinger 			continue;
1098e48354ceSNicholas Bellinger 
1099e48354ceSNicholas Bellinger 		*c = tolower(*c);
1100e48354ceSNicholas Bellinger 	}
1101e48354ceSNicholas Bellinger }
1102e48354ceSNicholas Bellinger 
1103e48354ceSNicholas Bellinger /*
1104e48354ceSNicholas Bellinger  * Processes the first Login Request..
1105e48354ceSNicholas Bellinger  */
iscsi_target_locate_portal(struct iscsi_np * np,struct iscsit_conn * conn,struct iscsi_login * login)1106baa4d64bSNicholas Bellinger int iscsi_target_locate_portal(
1107e48354ceSNicholas Bellinger 	struct iscsi_np *np,
1108be36d683SMax Gurtovoy 	struct iscsit_conn *conn,
1109e48354ceSNicholas Bellinger 	struct iscsi_login *login)
1110e48354ceSNicholas Bellinger {
1111e48354ceSNicholas Bellinger 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1112e48354ceSNicholas Bellinger 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
11130873fe44SMax Gurtovoy 	struct iscsit_session *sess = conn->sess;
1114e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
1115d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = NULL;
1116e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
1117988e3a85SNicholas Bellinger 	struct se_node_acl *se_nacl;
1118988e3a85SNicholas Bellinger 	u32 payload_length, queue_depth = 0;
1119988e3a85SNicholas Bellinger 	int sessiontype = 0, ret = 0, tag_num, tag_size;
1120e48354ceSNicholas Bellinger 
1121d381a801SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1122d381a801SNicholas Bellinger 	iscsi_target_set_sock_callbacks(conn);
1123d381a801SNicholas Bellinger 
1124d381a801SNicholas Bellinger 	login->np = np;
1125*2a737d3bSMaurizio Lombardi 	conn->tpg = NULL;
1126d381a801SNicholas Bellinger 
1127e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1128e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
1129e48354ceSNicholas Bellinger 
11306235bef6SYang Yingliang 	tmpbuf = kmemdup_nul(login->req_buf, payload_length, GFP_KERNEL);
1131e48354ceSNicholas Bellinger 	if (!tmpbuf) {
1132e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for tmpbuf.\n");
1133e48354ceSNicholas Bellinger 		return -1;
1134e48354ceSNicholas Bellinger 	}
1135e48354ceSNicholas Bellinger 
1136e48354ceSNicholas Bellinger 	start = tmpbuf;
1137e48354ceSNicholas Bellinger 	end = (start + payload_length);
1138e48354ceSNicholas Bellinger 
1139e48354ceSNicholas Bellinger 	/*
1140e48354ceSNicholas Bellinger 	 * Locate the initial keys expected from the Initiator node in
1141e48354ceSNicholas Bellinger 	 * the first login request in order to progress with the login phase.
1142e48354ceSNicholas Bellinger 	 */
1143e48354ceSNicholas Bellinger 	while (start < end) {
1144e48354ceSNicholas Bellinger 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1145e48354ceSNicholas Bellinger 			ret = -1;
1146e48354ceSNicholas Bellinger 			goto out;
1147e48354ceSNicholas Bellinger 		}
1148e48354ceSNicholas Bellinger 
1149e48354ceSNicholas Bellinger 		if (!strncmp(key, "InitiatorName", 13))
1150e48354ceSNicholas Bellinger 			i_buf = value;
1151e48354ceSNicholas Bellinger 		else if (!strncmp(key, "SessionType", 11))
1152e48354ceSNicholas Bellinger 			s_buf = value;
1153e48354ceSNicholas Bellinger 		else if (!strncmp(key, "TargetName", 10))
1154e48354ceSNicholas Bellinger 			t_buf = value;
1155e48354ceSNicholas Bellinger 
1156e48354ceSNicholas Bellinger 		start += strlen(key) + strlen(value) + 2;
1157e48354ceSNicholas Bellinger 	}
1158e48354ceSNicholas Bellinger 	/*
1159e48354ceSNicholas Bellinger 	 * See 5.3.  Login Phase.
1160e48354ceSNicholas Bellinger 	 */
1161e48354ceSNicholas Bellinger 	if (!i_buf) {
1162e48354ceSNicholas Bellinger 		pr_err("InitiatorName key not received"
1163e48354ceSNicholas Bellinger 			" in first login request.\n");
1164e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1165e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1166e48354ceSNicholas Bellinger 		ret = -1;
1167e48354ceSNicholas Bellinger 		goto out;
1168e48354ceSNicholas Bellinger 	}
1169e48354ceSNicholas Bellinger 	/*
1170e48354ceSNicholas Bellinger 	 * Convert the incoming InitiatorName to lowercase following
1171e48354ceSNicholas Bellinger 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1172e48354ceSNicholas Bellinger 	 * are NOT case sensitive.
1173e48354ceSNicholas Bellinger 	 */
1174e48354ceSNicholas Bellinger 	iscsi_initiatorname_tolower(i_buf);
1175e48354ceSNicholas Bellinger 
1176e48354ceSNicholas Bellinger 	if (!s_buf) {
1177e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1178e48354ceSNicholas Bellinger 			goto get_target;
1179e48354ceSNicholas Bellinger 
1180e48354ceSNicholas Bellinger 		pr_err("SessionType key not received"
1181e48354ceSNicholas Bellinger 			" in first login request.\n");
1182e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1183e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1184e48354ceSNicholas Bellinger 		ret = -1;
1185e48354ceSNicholas Bellinger 		goto out;
1186e48354ceSNicholas Bellinger 	}
1187e48354ceSNicholas Bellinger 
1188e48354ceSNicholas Bellinger 	/*
1189e48354ceSNicholas Bellinger 	 * Use default portal group for discovery sessions.
1190e48354ceSNicholas Bellinger 	 */
1191e48354ceSNicholas Bellinger 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1192e48354ceSNicholas Bellinger 	if (!sessiontype) {
1193e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1194e48354ceSNicholas Bellinger 			goto get_target;
1195e48354ceSNicholas Bellinger 
1196e48354ceSNicholas Bellinger 		sess->sess_ops->SessionType = 1;
1197e48354ceSNicholas Bellinger 		/*
1198e48354ceSNicholas Bellinger 		 * Setup crc32c modules from libcrypto
1199e48354ceSNicholas Bellinger 		 */
1200e48354ceSNicholas Bellinger 		if (iscsi_login_setup_crypto(conn) < 0) {
1201e48354ceSNicholas Bellinger 			pr_err("iscsi_login_setup_crypto() failed\n");
1202e48354ceSNicholas Bellinger 			ret = -1;
1203e48354ceSNicholas Bellinger 			goto out;
1204e48354ceSNicholas Bellinger 		}
1205e48354ceSNicholas Bellinger 		/*
1206e48354ceSNicholas Bellinger 		 * Serialize access across the discovery struct iscsi_portal_group to
1207e48354ceSNicholas Bellinger 		 * process login attempt.
1208e48354ceSNicholas Bellinger 		 */
1209*2a737d3bSMaurizio Lombardi 		conn->tpg = iscsit_global->discovery_tpg;
1210e48354ceSNicholas Bellinger 		if (iscsit_access_np(np, conn->tpg) < 0) {
1211e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1212e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1213*2a737d3bSMaurizio Lombardi 			conn->tpg = NULL;
1214e48354ceSNicholas Bellinger 			ret = -1;
1215e48354ceSNicholas Bellinger 			goto out;
1216e48354ceSNicholas Bellinger 		}
1217e48354ceSNicholas Bellinger 		ret = 0;
1218988e3a85SNicholas Bellinger 		goto alloc_tags;
1219e48354ceSNicholas Bellinger 	}
1220e48354ceSNicholas Bellinger 
1221e48354ceSNicholas Bellinger get_target:
1222e48354ceSNicholas Bellinger 	if (!t_buf) {
1223e48354ceSNicholas Bellinger 		pr_err("TargetName key not received"
1224e48354ceSNicholas Bellinger 			" in first login request while"
1225e48354ceSNicholas Bellinger 			" SessionType=Normal.\n");
1226e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1227e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1228e48354ceSNicholas Bellinger 		ret = -1;
1229e48354ceSNicholas Bellinger 		goto out;
1230e48354ceSNicholas Bellinger 	}
1231e48354ceSNicholas Bellinger 
1232e48354ceSNicholas Bellinger 	/*
1233e48354ceSNicholas Bellinger 	 * Locate Target IQN from Storage Node.
1234e48354ceSNicholas Bellinger 	 */
1235e48354ceSNicholas Bellinger 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1236e48354ceSNicholas Bellinger 	if (!tiqn) {
1237e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target IQN: %s in"
1238e48354ceSNicholas Bellinger 			" Storage Node\n", t_buf);
1239e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1240e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1241e48354ceSNicholas Bellinger 		ret = -1;
1242e48354ceSNicholas Bellinger 		goto out;
1243e48354ceSNicholas Bellinger 	}
1244e48354ceSNicholas Bellinger 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1245e48354ceSNicholas Bellinger 
1246e48354ceSNicholas Bellinger 	/*
1247e48354ceSNicholas Bellinger 	 * Locate Target Portal Group from Storage Node.
1248e48354ceSNicholas Bellinger 	 */
1249d381a801SNicholas Bellinger 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1250e48354ceSNicholas Bellinger 	if (!conn->tpg) {
1251e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target Portal Group"
1252e48354ceSNicholas Bellinger 				" on %s\n", tiqn->tiqn);
1253e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1254e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1255e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1256e48354ceSNicholas Bellinger 		ret = -1;
1257e48354ceSNicholas Bellinger 		goto out;
1258e48354ceSNicholas Bellinger 	}
1259d381a801SNicholas Bellinger 	conn->tpg_np = tpg_np;
1260e48354ceSNicholas Bellinger 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1261e48354ceSNicholas Bellinger 	/*
1262e48354ceSNicholas Bellinger 	 * Setup crc32c modules from libcrypto
1263e48354ceSNicholas Bellinger 	 */
1264e48354ceSNicholas Bellinger 	if (iscsi_login_setup_crypto(conn) < 0) {
1265e48354ceSNicholas Bellinger 		pr_err("iscsi_login_setup_crypto() failed\n");
1266d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1267d381a801SNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1268d381a801SNicholas Bellinger 		conn->tpg = NULL;
1269e48354ceSNicholas Bellinger 		ret = -1;
1270e48354ceSNicholas Bellinger 		goto out;
1271e48354ceSNicholas Bellinger 	}
1272e48354ceSNicholas Bellinger 	/*
1273e48354ceSNicholas Bellinger 	 * Serialize access across the struct iscsi_portal_group to
1274e48354ceSNicholas Bellinger 	 * process login attempt.
1275e48354ceSNicholas Bellinger 	 */
1276e48354ceSNicholas Bellinger 	if (iscsit_access_np(np, conn->tpg) < 0) {
1277d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1278e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1279e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1280e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1281e48354ceSNicholas Bellinger 		conn->tpg = NULL;
1282d381a801SNicholas Bellinger 		ret = -1;
1283e48354ceSNicholas Bellinger 		goto out;
1284e48354ceSNicholas Bellinger 	}
1285e48354ceSNicholas Bellinger 
1286e48354ceSNicholas Bellinger 	/*
1287e48354ceSNicholas Bellinger 	 * conn->sess->node_acl will be set when the referenced
12880873fe44SMax Gurtovoy 	 * struct iscsit_session is located from received ISID+TSIH in
1289e48354ceSNicholas Bellinger 	 * iscsi_login_non_zero_tsih_s2().
1290e48354ceSNicholas Bellinger 	 */
1291e48354ceSNicholas Bellinger 	if (!login->leading_connection) {
1292e48354ceSNicholas Bellinger 		ret = 0;
1293e48354ceSNicholas Bellinger 		goto out;
1294e48354ceSNicholas Bellinger 	}
1295e48354ceSNicholas Bellinger 
1296e48354ceSNicholas Bellinger 	/*
1297e48354ceSNicholas Bellinger 	 * This value is required in iscsi_login_zero_tsih_s2()
1298e48354ceSNicholas Bellinger 	 */
1299e48354ceSNicholas Bellinger 	sess->sess_ops->SessionType = 0;
1300e48354ceSNicholas Bellinger 
1301e48354ceSNicholas Bellinger 	/*
1302e48354ceSNicholas Bellinger 	 * Locate incoming Initiator IQN reference from Storage Node.
1303e48354ceSNicholas Bellinger 	 */
1304e48354ceSNicholas Bellinger 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1305e48354ceSNicholas Bellinger 			&conn->tpg->tpg_se_tpg, i_buf);
1306e48354ceSNicholas Bellinger 	if (!sess->se_sess->se_node_acl) {
1307e48354ceSNicholas Bellinger 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1308e48354ceSNicholas Bellinger 			" access iSCSI target portal group: %hu.\n",
1309e48354ceSNicholas Bellinger 				i_buf, conn->tpg->tpgt);
1310e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1311e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1312e48354ceSNicholas Bellinger 		ret = -1;
1313e48354ceSNicholas Bellinger 		goto out;
1314e48354ceSNicholas Bellinger 	}
1315988e3a85SNicholas Bellinger 	se_nacl = sess->se_sess->se_node_acl;
1316988e3a85SNicholas Bellinger 	queue_depth = se_nacl->queue_depth;
1317988e3a85SNicholas Bellinger 	/*
1318988e3a85SNicholas Bellinger 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1319988e3a85SNicholas Bellinger 	 * depth for non immediate commands, plus extra tags for immediate
1320988e3a85SNicholas Bellinger 	 * commands.
1321988e3a85SNicholas Bellinger 	 *
1322988e3a85SNicholas Bellinger 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1323988e3a85SNicholas Bellinger 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1324988e3a85SNicholas Bellinger 	 */
1325988e3a85SNicholas Bellinger alloc_tags:
1326988e3a85SNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
13274a4caa29SNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
132866cd9d4eSMax Gurtovoy 	tag_size = sizeof(struct iscsit_cmd) + conn->conn_transport->priv_size;
1329e48354ceSNicholas Bellinger 
1330988e3a85SNicholas Bellinger 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1331988e3a85SNicholas Bellinger 	if (ret < 0) {
1332988e3a85SNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1333988e3a85SNicholas Bellinger 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1334988e3a85SNicholas Bellinger 		ret = -1;
1335988e3a85SNicholas Bellinger 	}
1336e48354ceSNicholas Bellinger out:
1337e48354ceSNicholas Bellinger 	kfree(tmpbuf);
1338e48354ceSNicholas Bellinger 	return ret;
1339e48354ceSNicholas Bellinger }
1340e48354ceSNicholas Bellinger 
iscsi_target_start_negotiation(struct iscsi_login * login,struct iscsit_conn * conn)1341e48354ceSNicholas Bellinger int iscsi_target_start_negotiation(
1342e48354ceSNicholas Bellinger 	struct iscsi_login *login,
1343be36d683SMax Gurtovoy 	struct iscsit_conn *conn)
1344e48354ceSNicholas Bellinger {
1345baa4d64bSNicholas Bellinger 	int ret;
1346e48354ceSNicholas Bellinger 
1347d381a801SNicholas Bellinger 	if (conn->sock) {
1348d381a801SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
1349e48354ceSNicholas Bellinger 
1350d381a801SNicholas Bellinger 		write_lock_bh(&sk->sk_callback_lock);
1351d381a801SNicholas Bellinger 		set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
135225cdda95SNicholas Bellinger 		set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1353d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
1354d381a801SNicholas Bellinger 	}
135525cdda95SNicholas Bellinger 	/*
135625cdda95SNicholas Bellinger 	 * If iscsi_target_do_login returns zero to signal more PDU
135725cdda95SNicholas Bellinger 	 * exchanges are required to complete the login, go ahead and
135825cdda95SNicholas Bellinger 	 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
135925cdda95SNicholas Bellinger 	 * is still active.
136025cdda95SNicholas Bellinger 	 *
136125cdda95SNicholas Bellinger 	 * Otherwise if TCP connection dropped asynchronously, go ahead
136225cdda95SNicholas Bellinger 	 * and perform connection cleanup now.
136325cdda95SNicholas Bellinger 	 */
13648f0dfb3dSNicholas Bellinger 	ret = iscsi_target_do_login(conn, login);
136513247018SMaurizio Lombardi 	if (!ret) {
136613247018SMaurizio Lombardi 		spin_lock(&conn->login_worker_lock);
136713247018SMaurizio Lombardi 
136813247018SMaurizio Lombardi 		if (iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
136925cdda95SNicholas Bellinger 			ret = -1;
137013247018SMaurizio Lombardi 		else if (!test_bit(LOGIN_FLAGS_WORKER_RUNNING, &conn->login_flags)) {
137113247018SMaurizio Lombardi 			if (iscsit_set_login_timer_kworker(conn, NULL) < 0) {
137213247018SMaurizio Lombardi 				/*
137313247018SMaurizio Lombardi 				 * The timeout has expired already.
137413247018SMaurizio Lombardi 				 * Schedule login_work to perform the cleanup.
137513247018SMaurizio Lombardi 				 */
137613247018SMaurizio Lombardi 				schedule_delayed_work(&conn->login_work, 0);
137713247018SMaurizio Lombardi 			}
137813247018SMaurizio Lombardi 		}
137913247018SMaurizio Lombardi 
138013247018SMaurizio Lombardi 		spin_unlock(&conn->login_worker_lock);
138113247018SMaurizio Lombardi 	}
138225cdda95SNicholas Bellinger 
13838f0dfb3dSNicholas Bellinger 	if (ret < 0) {
1384d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
1385d381a801SNicholas Bellinger 		iscsi_remove_failed_auth_entry(conn);
1386d381a801SNicholas Bellinger 	}
1387fec1b2faSMaurizio Lombardi 	if (ret != 0) {
138813247018SMaurizio Lombardi 		iscsit_stop_login_timer(conn);
1389fec1b2faSMaurizio Lombardi 		cancel_delayed_work_sync(&conn->login_work);
1390baa4d64bSNicholas Bellinger 		iscsi_target_nego_release(conn);
1391fec1b2faSMaurizio Lombardi 	}
1392d381a801SNicholas Bellinger 
1393e48354ceSNicholas Bellinger 	return ret;
1394e48354ceSNicholas Bellinger }
1395e48354ceSNicholas Bellinger 
iscsi_target_nego_release(struct iscsit_conn * conn)1396be36d683SMax Gurtovoy void iscsi_target_nego_release(struct iscsit_conn *conn)
1397e48354ceSNicholas Bellinger {
1398baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1399baa4d64bSNicholas Bellinger 
1400baa4d64bSNicholas Bellinger 	if (!login)
1401baa4d64bSNicholas Bellinger 		return;
1402baa4d64bSNicholas Bellinger 
1403e48354ceSNicholas Bellinger 	kfree(login->req_buf);
1404e48354ceSNicholas Bellinger 	kfree(login->rsp_buf);
1405e48354ceSNicholas Bellinger 	kfree(login);
1406baa4d64bSNicholas Bellinger 
1407baa4d64bSNicholas Bellinger 	conn->conn_login = NULL;
1408e48354ceSNicholas Bellinger }
1409