1e48354ceSNicholas Bellinger /*******************************************************************************
2e48354ceSNicholas Bellinger  * This file contains main functions related to iSCSI Parameter negotiation.
3e48354ceSNicholas Bellinger  *
44c76251eSNicholas Bellinger  * (c) Copyright 2007-2013 Datera, Inc.
5e48354ceSNicholas Bellinger  *
6e48354ceSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7e48354ceSNicholas Bellinger  *
8e48354ceSNicholas Bellinger  * This program is free software; you can redistribute it and/or modify
9e48354ceSNicholas Bellinger  * it under the terms of the GNU General Public License as published by
10e48354ceSNicholas Bellinger  * the Free Software Foundation; either version 2 of the License, or
11e48354ceSNicholas Bellinger  * (at your option) any later version.
12e48354ceSNicholas Bellinger  *
13e48354ceSNicholas Bellinger  * This program is distributed in the hope that it will be useful,
14e48354ceSNicholas Bellinger  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15e48354ceSNicholas Bellinger  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16e48354ceSNicholas Bellinger  * GNU General Public License for more details.
17e48354ceSNicholas Bellinger  ******************************************************************************/
18e48354ceSNicholas Bellinger 
19e48354ceSNicholas Bellinger #include <linux/ctype.h>
20e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
21e48354ceSNicholas Bellinger #include <target/target_core_base.h>
22c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
23baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
24e48354ceSNicholas Bellinger 
25e48354ceSNicholas Bellinger #include "iscsi_target_core.h"
26e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
27e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
28e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
29e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
30e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
31e48354ceSNicholas Bellinger #include "iscsi_target.h"
32e48354ceSNicholas Bellinger #include "iscsi_target_auth.h"
33e48354ceSNicholas Bellinger 
34e48354ceSNicholas Bellinger #define MAX_LOGIN_PDUS  7
35e48354ceSNicholas Bellinger #define TEXT_LEN	4096
36e48354ceSNicholas Bellinger 
37e48354ceSNicholas Bellinger void convert_null_to_semi(char *buf, int len)
38e48354ceSNicholas Bellinger {
39e48354ceSNicholas Bellinger 	int i;
40e48354ceSNicholas Bellinger 
41e48354ceSNicholas Bellinger 	for (i = 0; i < len; i++)
42e48354ceSNicholas Bellinger 		if (buf[i] == '\0')
43e48354ceSNicholas Bellinger 			buf[i] = ';';
44e48354ceSNicholas Bellinger }
45e48354ceSNicholas Bellinger 
46fceb5bc7SChristoph Hellwig static int strlen_semi(char *buf)
47e48354ceSNicholas Bellinger {
48e48354ceSNicholas Bellinger 	int i = 0;
49e48354ceSNicholas Bellinger 
50e48354ceSNicholas Bellinger 	while (buf[i] != '\0') {
51e48354ceSNicholas Bellinger 		if (buf[i] == ';')
52e48354ceSNicholas Bellinger 			return i;
53e48354ceSNicholas Bellinger 		i++;
54e48354ceSNicholas Bellinger 	}
55e48354ceSNicholas Bellinger 
56e48354ceSNicholas Bellinger 	return -1;
57e48354ceSNicholas Bellinger }
58e48354ceSNicholas Bellinger 
59e48354ceSNicholas Bellinger int extract_param(
60e48354ceSNicholas Bellinger 	const char *in_buf,
61e48354ceSNicholas Bellinger 	const char *pattern,
62e48354ceSNicholas Bellinger 	unsigned int max_length,
63e48354ceSNicholas Bellinger 	char *out_buf,
64e48354ceSNicholas Bellinger 	unsigned char *type)
65e48354ceSNicholas Bellinger {
66e48354ceSNicholas Bellinger 	char *ptr;
67e48354ceSNicholas Bellinger 	int len;
68e48354ceSNicholas Bellinger 
69e48354ceSNicholas Bellinger 	if (!in_buf || !pattern || !out_buf || !type)
70e48354ceSNicholas Bellinger 		return -1;
71e48354ceSNicholas Bellinger 
72e48354ceSNicholas Bellinger 	ptr = strstr(in_buf, pattern);
73e48354ceSNicholas Bellinger 	if (!ptr)
74e48354ceSNicholas Bellinger 		return -1;
75e48354ceSNicholas Bellinger 
76e48354ceSNicholas Bellinger 	ptr = strstr(ptr, "=");
77e48354ceSNicholas Bellinger 	if (!ptr)
78e48354ceSNicholas Bellinger 		return -1;
79e48354ceSNicholas Bellinger 
80e48354ceSNicholas Bellinger 	ptr += 1;
81e48354ceSNicholas Bellinger 	if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
82e48354ceSNicholas Bellinger 		ptr += 2; /* skip 0x */
83e48354ceSNicholas Bellinger 		*type = HEX;
84e48354ceSNicholas Bellinger 	} else
85e48354ceSNicholas Bellinger 		*type = DECIMAL;
86e48354ceSNicholas Bellinger 
87e48354ceSNicholas Bellinger 	len = strlen_semi(ptr);
88e48354ceSNicholas Bellinger 	if (len < 0)
89e48354ceSNicholas Bellinger 		return -1;
90e48354ceSNicholas Bellinger 
91369653e4SEric Seppanen 	if (len >= max_length) {
925e58b029SMasanari Iida 		pr_err("Length of input: %d exceeds max_length:"
93e48354ceSNicholas Bellinger 			" %d\n", len, max_length);
94e48354ceSNicholas Bellinger 		return -1;
95e48354ceSNicholas Bellinger 	}
96e48354ceSNicholas Bellinger 	memcpy(out_buf, ptr, len);
97e48354ceSNicholas Bellinger 	out_buf[len] = '\0';
98e48354ceSNicholas Bellinger 
99e48354ceSNicholas Bellinger 	return 0;
100e48354ceSNicholas Bellinger }
101e48354ceSNicholas Bellinger 
102e48354ceSNicholas Bellinger static u32 iscsi_handle_authentication(
103e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
104e48354ceSNicholas Bellinger 	char *in_buf,
105e48354ceSNicholas Bellinger 	char *out_buf,
106e48354ceSNicholas Bellinger 	int in_length,
107e48354ceSNicholas Bellinger 	int *out_length,
108e48354ceSNicholas Bellinger 	unsigned char *authtype)
109e48354ceSNicholas Bellinger {
110e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
111e48354ceSNicholas Bellinger 	struct iscsi_node_auth *auth;
112e48354ceSNicholas Bellinger 	struct iscsi_node_acl *iscsi_nacl;
113c3e51442SNicholas Bellinger 	struct iscsi_portal_group *iscsi_tpg;
114e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
115e48354ceSNicholas Bellinger 
116e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType) {
117e48354ceSNicholas Bellinger 		/*
118e48354ceSNicholas Bellinger 		 * For SessionType=Normal
119e48354ceSNicholas Bellinger 		 */
120e48354ceSNicholas Bellinger 		se_nacl = conn->sess->se_sess->se_node_acl;
121e48354ceSNicholas Bellinger 		if (!se_nacl) {
122e48354ceSNicholas Bellinger 			pr_err("Unable to locate struct se_node_acl for"
123e48354ceSNicholas Bellinger 					" CHAP auth\n");
124e48354ceSNicholas Bellinger 			return -1;
125e48354ceSNicholas Bellinger 		}
126e48354ceSNicholas Bellinger 		iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
127e48354ceSNicholas Bellinger 				se_node_acl);
128e48354ceSNicholas Bellinger 		if (!iscsi_nacl) {
129e48354ceSNicholas Bellinger 			pr_err("Unable to locate struct iscsi_node_acl for"
130e48354ceSNicholas Bellinger 					" CHAP auth\n");
131e48354ceSNicholas Bellinger 			return -1;
132e48354ceSNicholas Bellinger 		}
133e48354ceSNicholas Bellinger 
134c3e51442SNicholas Bellinger 		if (se_nacl->dynamic_node_acl) {
135c3e51442SNicholas Bellinger 			iscsi_tpg = container_of(se_nacl->se_tpg,
136c3e51442SNicholas Bellinger 					struct iscsi_portal_group, tpg_se_tpg);
137c3e51442SNicholas Bellinger 
138c3e51442SNicholas Bellinger 			auth = &iscsi_tpg->tpg_demo_auth;
139c3e51442SNicholas Bellinger 		} else {
140c3e51442SNicholas Bellinger 			iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
141c3e51442SNicholas Bellinger 						  se_node_acl);
142c3e51442SNicholas Bellinger 
143b7eec2cdSAndy Grover 			auth = &iscsi_nacl->node_auth;
144c3e51442SNicholas Bellinger 		}
145e48354ceSNicholas Bellinger 	} else {
146e48354ceSNicholas Bellinger 		/*
147e48354ceSNicholas Bellinger 		 * For SessionType=Discovery
148e48354ceSNicholas Bellinger 		 */
149e48354ceSNicholas Bellinger 		auth = &iscsit_global->discovery_acl.node_auth;
150e48354ceSNicholas Bellinger 	}
151e48354ceSNicholas Bellinger 
152e48354ceSNicholas Bellinger 	if (strstr("CHAP", authtype))
153e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, "CHAP");
154e48354ceSNicholas Bellinger 	else
155e48354ceSNicholas Bellinger 		strcpy(conn->sess->auth_type, NONE);
156e48354ceSNicholas Bellinger 
157e48354ceSNicholas Bellinger 	if (strstr("None", authtype))
158e48354ceSNicholas Bellinger 		return 1;
159e48354ceSNicholas Bellinger #ifdef CANSRP
160e48354ceSNicholas Bellinger 	else if (strstr("SRP", authtype))
161e48354ceSNicholas Bellinger 		return srp_main_loop(conn, auth, in_buf, out_buf,
162e48354ceSNicholas Bellinger 				&in_length, out_length);
163e48354ceSNicholas Bellinger #endif
164e48354ceSNicholas Bellinger 	else if (strstr("CHAP", authtype))
165e48354ceSNicholas Bellinger 		return chap_main_loop(conn, auth, in_buf, out_buf,
166e48354ceSNicholas Bellinger 				&in_length, out_length);
167e48354ceSNicholas Bellinger 	else if (strstr("SPKM1", authtype))
168e48354ceSNicholas Bellinger 		return 2;
169e48354ceSNicholas Bellinger 	else if (strstr("SPKM2", authtype))
170e48354ceSNicholas Bellinger 		return 2;
171e48354ceSNicholas Bellinger 	else if (strstr("KRB5", authtype))
172e48354ceSNicholas Bellinger 		return 2;
173e48354ceSNicholas Bellinger 	else
174e48354ceSNicholas Bellinger 		return 2;
175e48354ceSNicholas Bellinger }
176e48354ceSNicholas Bellinger 
177e48354ceSNicholas Bellinger static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
178e48354ceSNicholas Bellinger {
179e48354ceSNicholas Bellinger 	kfree(conn->auth_protocol);
180e48354ceSNicholas Bellinger }
181e48354ceSNicholas Bellinger 
182baa4d64bSNicholas Bellinger int iscsi_target_check_login_request(
183e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
184e48354ceSNicholas Bellinger 	struct iscsi_login *login)
185e48354ceSNicholas Bellinger {
18628168905SJörn Engel 	int req_csg, req_nsg;
187e48354ceSNicholas Bellinger 	u32 payload_length;
188e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
189e48354ceSNicholas Bellinger 
190e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
191e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
192e48354ceSNicholas Bellinger 
193e48354ceSNicholas Bellinger 	switch (login_req->opcode & ISCSI_OPCODE_MASK) {
194e48354ceSNicholas Bellinger 	case ISCSI_OP_LOGIN:
195e48354ceSNicholas Bellinger 		break;
196e48354ceSNicholas Bellinger 	default:
197e48354ceSNicholas Bellinger 		pr_err("Received unknown opcode 0x%02x.\n",
198e48354ceSNicholas Bellinger 				login_req->opcode & ISCSI_OPCODE_MASK);
199e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
200e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
201e48354ceSNicholas Bellinger 		return -1;
202e48354ceSNicholas Bellinger 	}
203e48354ceSNicholas Bellinger 
204e48354ceSNicholas Bellinger 	if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
205e48354ceSNicholas Bellinger 	    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
206e48354ceSNicholas Bellinger 		pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
207e48354ceSNicholas Bellinger 			" and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
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 
2135d358065SAndy Grover 	req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
2145d358065SAndy Grover 	req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
215e48354ceSNicholas Bellinger 
216e48354ceSNicholas Bellinger 	if (req_csg != login->current_stage) {
217e48354ceSNicholas Bellinger 		pr_err("Initiator unexpectedly changed login stage"
218e48354ceSNicholas Bellinger 			" from %d to %d, login failed.\n", login->current_stage,
219e48354ceSNicholas Bellinger 			req_csg);
220e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
221e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
222e48354ceSNicholas Bellinger 		return -1;
223e48354ceSNicholas Bellinger 	}
224e48354ceSNicholas Bellinger 
225e48354ceSNicholas Bellinger 	if ((req_nsg == 2) || (req_csg >= 2) ||
226e48354ceSNicholas Bellinger 	   ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
227e48354ceSNicholas Bellinger 	    (req_nsg <= req_csg))) {
228e48354ceSNicholas Bellinger 		pr_err("Illegal login_req->flags Combination, CSG: %d,"
229e48354ceSNicholas Bellinger 			" NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
230e48354ceSNicholas Bellinger 			req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
231e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
232e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
233e48354ceSNicholas Bellinger 		return -1;
234e48354ceSNicholas Bellinger 	}
235e48354ceSNicholas Bellinger 
236e48354ceSNicholas Bellinger 	if ((login_req->max_version != login->version_max) ||
237e48354ceSNicholas Bellinger 	    (login_req->min_version != login->version_min)) {
238e48354ceSNicholas Bellinger 		pr_err("Login request changed Version Max/Nin"
239e48354ceSNicholas Bellinger 			" unexpectedly to 0x%02x/0x%02x, protocol error\n",
240e48354ceSNicholas Bellinger 			login_req->max_version, login_req->min_version);
241e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
242e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
243e48354ceSNicholas Bellinger 		return -1;
244e48354ceSNicholas Bellinger 	}
245e48354ceSNicholas Bellinger 
246e48354ceSNicholas Bellinger 	if (memcmp(login_req->isid, login->isid, 6) != 0) {
247e48354ceSNicholas Bellinger 		pr_err("Login request changed ISID unexpectedly,"
248e48354ceSNicholas Bellinger 				" protocol error.\n");
249e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
250e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
251e48354ceSNicholas Bellinger 		return -1;
252e48354ceSNicholas Bellinger 	}
253e48354ceSNicholas Bellinger 
254e48354ceSNicholas Bellinger 	if (login_req->itt != login->init_task_tag) {
255e48354ceSNicholas Bellinger 		pr_err("Login request changed ITT unexpectedly to"
256e48354ceSNicholas Bellinger 			" 0x%08x, protocol error.\n", login_req->itt);
257e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
258e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_INIT_ERR);
259e48354ceSNicholas Bellinger 		return -1;
260e48354ceSNicholas Bellinger 	}
261e48354ceSNicholas Bellinger 
262e48354ceSNicholas Bellinger 	if (payload_length > MAX_KEY_VALUE_PAIRS) {
263e48354ceSNicholas Bellinger 		pr_err("Login request payload exceeds default"
264e48354ceSNicholas Bellinger 			" MaxRecvDataSegmentLength: %u, protocol error.\n",
265e48354ceSNicholas Bellinger 				MAX_KEY_VALUE_PAIRS);
266e48354ceSNicholas Bellinger 		return -1;
267e48354ceSNicholas Bellinger 	}
268e48354ceSNicholas Bellinger 
269e48354ceSNicholas Bellinger 	return 0;
270e48354ceSNicholas Bellinger }
271e48354ceSNicholas Bellinger 
272e48354ceSNicholas Bellinger static int iscsi_target_check_first_request(
273e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
274e48354ceSNicholas Bellinger 	struct iscsi_login *login)
275e48354ceSNicholas Bellinger {
276e48354ceSNicholas Bellinger 	struct iscsi_param *param = NULL;
277e48354ceSNicholas Bellinger 	struct se_node_acl *se_nacl;
278e48354ceSNicholas Bellinger 
279e48354ceSNicholas Bellinger 	login->first_request = 0;
280e48354ceSNicholas Bellinger 
281e48354ceSNicholas Bellinger 	list_for_each_entry(param, &conn->param_list->param_list, p_list) {
282e48354ceSNicholas Bellinger 		if (!strncmp(param->name, SESSIONTYPE, 11)) {
283e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
284e48354ceSNicholas Bellinger 				pr_err("SessionType key not received"
285e48354ceSNicholas Bellinger 					" in first login request.\n");
286e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
287e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
288e48354ceSNicholas Bellinger 				return -1;
289e48354ceSNicholas Bellinger 			}
290e48354ceSNicholas Bellinger 			if (!strncmp(param->value, DISCOVERY, 9))
291e48354ceSNicholas Bellinger 				return 0;
292e48354ceSNicholas Bellinger 		}
293e48354ceSNicholas Bellinger 
294e48354ceSNicholas Bellinger 		if (!strncmp(param->name, INITIATORNAME, 13)) {
295e48354ceSNicholas Bellinger 			if (!IS_PSTATE_ACCEPTOR(param)) {
296e48354ceSNicholas Bellinger 				if (!login->leading_connection)
297e48354ceSNicholas Bellinger 					continue;
298e48354ceSNicholas Bellinger 
299e48354ceSNicholas Bellinger 				pr_err("InitiatorName key not received"
300e48354ceSNicholas Bellinger 					" in first login request.\n");
301e48354ceSNicholas Bellinger 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
302e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
303e48354ceSNicholas Bellinger 				return -1;
304e48354ceSNicholas Bellinger 			}
305e48354ceSNicholas Bellinger 
306e48354ceSNicholas Bellinger 			/*
307e48354ceSNicholas Bellinger 			 * For non-leading connections, double check that the
308e48354ceSNicholas Bellinger 			 * received InitiatorName matches the existing session's
309e48354ceSNicholas Bellinger 			 * struct iscsi_node_acl.
310e48354ceSNicholas Bellinger 			 */
311e48354ceSNicholas Bellinger 			if (!login->leading_connection) {
312e48354ceSNicholas Bellinger 				se_nacl = conn->sess->se_sess->se_node_acl;
313e48354ceSNicholas Bellinger 				if (!se_nacl) {
314e48354ceSNicholas Bellinger 					pr_err("Unable to locate"
315e48354ceSNicholas Bellinger 						" struct se_node_acl\n");
316e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
317e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
318e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
319e48354ceSNicholas Bellinger 					return -1;
320e48354ceSNicholas Bellinger 				}
321e48354ceSNicholas Bellinger 
322e48354ceSNicholas Bellinger 				if (strcmp(param->value,
323e48354ceSNicholas Bellinger 						se_nacl->initiatorname)) {
324e48354ceSNicholas Bellinger 					pr_err("Incorrect"
325e48354ceSNicholas Bellinger 						" InitiatorName: %s for this"
326e48354ceSNicholas Bellinger 						" iSCSI Initiator Node.\n",
327e48354ceSNicholas Bellinger 						param->value);
328e48354ceSNicholas Bellinger 					iscsit_tx_login_rsp(conn,
329e48354ceSNicholas Bellinger 							ISCSI_STATUS_CLS_INITIATOR_ERR,
330e48354ceSNicholas Bellinger 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
331e48354ceSNicholas Bellinger 					return -1;
332e48354ceSNicholas Bellinger 				}
333e48354ceSNicholas Bellinger 			}
334e48354ceSNicholas Bellinger 		}
335e48354ceSNicholas Bellinger 	}
336e48354ceSNicholas Bellinger 
337e48354ceSNicholas Bellinger 	return 0;
338e48354ceSNicholas Bellinger }
339e48354ceSNicholas Bellinger 
340e48354ceSNicholas Bellinger static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
341e48354ceSNicholas Bellinger {
342e48354ceSNicholas Bellinger 	u32 padding = 0;
343e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
344e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
345e48354ceSNicholas Bellinger 
346e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
347e48354ceSNicholas Bellinger 
348e48354ceSNicholas Bellinger 	login_rsp->opcode		= ISCSI_OP_LOGIN_RSP;
349e48354ceSNicholas Bellinger 	hton24(login_rsp->dlength, login->rsp_length);
350e48354ceSNicholas Bellinger 	memcpy(login_rsp->isid, login->isid, 6);
351e48354ceSNicholas Bellinger 	login_rsp->tsih			= cpu_to_be16(login->tsih);
35266c7db68SChristoph Hellwig 	login_rsp->itt			= login->init_task_tag;
353e48354ceSNicholas Bellinger 	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
354e48354ceSNicholas Bellinger 	login_rsp->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
355e48354ceSNicholas Bellinger 	login_rsp->max_cmdsn		= cpu_to_be32(conn->sess->max_cmd_sn);
356e48354ceSNicholas Bellinger 
357e48354ceSNicholas Bellinger 	pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
358e48354ceSNicholas Bellinger 		" ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
35966c7db68SChristoph Hellwig 		" %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
360e48354ceSNicholas Bellinger 		ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
361e48354ceSNicholas Bellinger 		ntohl(login_rsp->statsn), login->rsp_length);
362e48354ceSNicholas Bellinger 
363e48354ceSNicholas Bellinger 	padding = ((-login->rsp_length) & 3);
364e48354ceSNicholas Bellinger 
365baa4d64bSNicholas Bellinger 	if (conn->conn_transport->iscsit_put_login_tx(conn, login,
366e48354ceSNicholas Bellinger 					login->rsp_length + padding) < 0)
367e48354ceSNicholas Bellinger 		return -1;
368e48354ceSNicholas Bellinger 
369e48354ceSNicholas Bellinger 	login->rsp_length		= 0;
370e48354ceSNicholas Bellinger 	mutex_lock(&sess->cmdsn_mutex);
37150e5c87dSChristoph Hellwig 	login_rsp->exp_cmdsn		= cpu_to_be32(sess->exp_cmd_sn);
37250e5c87dSChristoph Hellwig 	login_rsp->max_cmdsn		= cpu_to_be32(sess->max_cmd_sn);
373e48354ceSNicholas Bellinger 	mutex_unlock(&sess->cmdsn_mutex);
374e48354ceSNicholas Bellinger 
375e48354ceSNicholas Bellinger 	return 0;
376e48354ceSNicholas Bellinger }
377e48354ceSNicholas Bellinger 
378676d2369SDavid S. Miller static void iscsi_target_sk_data_ready(struct sock *sk)
379d381a801SNicholas Bellinger {
380d381a801SNicholas Bellinger 	struct iscsi_conn *conn = sk->sk_user_data;
381d381a801SNicholas Bellinger 	bool rc;
382d381a801SNicholas Bellinger 
383d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
384d381a801SNicholas Bellinger 
385d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
386d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
387d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
388d381a801SNicholas Bellinger 		return;
389d381a801SNicholas Bellinger 	}
390d381a801SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
391d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
392d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
393d381a801SNicholas Bellinger 		return;
394d381a801SNicholas Bellinger 	}
395d381a801SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
396d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
397d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
398d381a801SNicholas Bellinger 		return;
399d381a801SNicholas Bellinger 	}
400d381a801SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
401d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
402d381a801SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
403d381a801SNicholas Bellinger 		return;
404d381a801SNicholas Bellinger 	}
405d381a801SNicholas Bellinger 
406d381a801SNicholas Bellinger 	rc = schedule_delayed_work(&conn->login_work, 0);
407d381a801SNicholas Bellinger 	if (rc == false) {
408d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
409d381a801SNicholas Bellinger 			 " got false\n");
410d381a801SNicholas Bellinger 	}
411d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
412d381a801SNicholas Bellinger }
413d381a801SNicholas Bellinger 
414bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *);
415bb048357SNicholas Bellinger 
416d381a801SNicholas Bellinger static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
417d381a801SNicholas Bellinger {
418d381a801SNicholas Bellinger 	struct sock *sk;
419d381a801SNicholas Bellinger 
420d381a801SNicholas Bellinger 	if (!conn->sock)
421d381a801SNicholas Bellinger 		return;
422d381a801SNicholas Bellinger 
423d381a801SNicholas Bellinger 	sk = conn->sock->sk;
424d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
425d381a801SNicholas Bellinger 
426d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
427d381a801SNicholas Bellinger 	sk->sk_user_data = conn;
428d381a801SNicholas Bellinger 	conn->orig_data_ready = sk->sk_data_ready;
429bb048357SNicholas Bellinger 	conn->orig_state_change = sk->sk_state_change;
430d381a801SNicholas Bellinger 	sk->sk_data_ready = iscsi_target_sk_data_ready;
431bb048357SNicholas Bellinger 	sk->sk_state_change = iscsi_target_sk_state_change;
432d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
433bb048357SNicholas Bellinger 
434bb048357SNicholas Bellinger 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
435bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
436d381a801SNicholas Bellinger }
437d381a801SNicholas Bellinger 
438d381a801SNicholas Bellinger static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
439d381a801SNicholas Bellinger {
440d381a801SNicholas Bellinger 	struct sock *sk;
441d381a801SNicholas Bellinger 
442d381a801SNicholas Bellinger 	if (!conn->sock)
443d381a801SNicholas Bellinger 		return;
444d381a801SNicholas Bellinger 
445d381a801SNicholas Bellinger 	sk = conn->sock->sk;
446d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
447d381a801SNicholas Bellinger 
448d381a801SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
449d381a801SNicholas Bellinger 	if (!sk->sk_user_data) {
450d381a801SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
451d381a801SNicholas Bellinger 		return;
452d381a801SNicholas Bellinger 	}
453d381a801SNicholas Bellinger 	sk->sk_user_data = NULL;
454d381a801SNicholas Bellinger 	sk->sk_data_ready = conn->orig_data_ready;
455bb048357SNicholas Bellinger 	sk->sk_state_change = conn->orig_state_change;
456d381a801SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
457bb048357SNicholas Bellinger 
458bb048357SNicholas Bellinger 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
459bb048357SNicholas Bellinger 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
460d381a801SNicholas Bellinger }
461d381a801SNicholas Bellinger 
462d381a801SNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
463d381a801SNicholas Bellinger 
464d381a801SNicholas Bellinger static bool iscsi_target_sk_state_check(struct sock *sk)
465d381a801SNicholas Bellinger {
466d381a801SNicholas Bellinger 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
467d381a801SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
468d381a801SNicholas Bellinger 			"returning FALSE\n");
469d381a801SNicholas Bellinger 		return false;
470d381a801SNicholas Bellinger 	}
471d381a801SNicholas Bellinger 	return true;
472d381a801SNicholas Bellinger }
473d381a801SNicholas Bellinger 
474d381a801SNicholas Bellinger static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
475d381a801SNicholas Bellinger {
476d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
477d381a801SNicholas Bellinger 	bool zero_tsih = login->zero_tsih;
478d381a801SNicholas Bellinger 
479d381a801SNicholas Bellinger 	iscsi_remove_failed_auth_entry(conn);
480d381a801SNicholas Bellinger 	iscsi_target_nego_release(conn);
481d381a801SNicholas Bellinger 	iscsi_target_login_sess_out(conn, np, zero_tsih, true);
482d381a801SNicholas Bellinger }
483d381a801SNicholas Bellinger 
484d381a801SNicholas Bellinger static void iscsi_target_login_timeout(unsigned long data)
485d381a801SNicholas Bellinger {
486d381a801SNicholas Bellinger 	struct iscsi_conn *conn = (struct iscsi_conn *)data;
487d381a801SNicholas Bellinger 
488d381a801SNicholas Bellinger 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
489d381a801SNicholas Bellinger 
490d381a801SNicholas Bellinger 	if (conn->login_kworker) {
491d381a801SNicholas Bellinger 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
492d381a801SNicholas Bellinger 			 conn->login_kworker->comm, conn->login_kworker->pid);
493d381a801SNicholas Bellinger 		send_sig(SIGINT, conn->login_kworker, 1);
494d381a801SNicholas Bellinger 	}
495d381a801SNicholas Bellinger }
496d381a801SNicholas Bellinger 
497d381a801SNicholas Bellinger static void iscsi_target_do_login_rx(struct work_struct *work)
498d381a801SNicholas Bellinger {
499d381a801SNicholas Bellinger 	struct iscsi_conn *conn = container_of(work,
500d381a801SNicholas Bellinger 				struct iscsi_conn, login_work.work);
501d381a801SNicholas Bellinger 	struct iscsi_login *login = conn->login;
502d381a801SNicholas Bellinger 	struct iscsi_np *np = login->np;
503d381a801SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
504d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
505d381a801SNicholas Bellinger 	struct timer_list login_timer;
506d381a801SNicholas Bellinger 	int rc, zero_tsih = login->zero_tsih;
507d381a801SNicholas Bellinger 	bool state;
508d381a801SNicholas Bellinger 
509d381a801SNicholas Bellinger 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
510d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
511d381a801SNicholas Bellinger 
512d381a801SNicholas Bellinger 	spin_lock(&tpg->tpg_state_lock);
513d381a801SNicholas Bellinger 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
514d381a801SNicholas Bellinger 	spin_unlock(&tpg->tpg_state_lock);
515d381a801SNicholas Bellinger 
516d381a801SNicholas Bellinger 	if (state == false) {
517d381a801SNicholas Bellinger 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
518d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
519d381a801SNicholas Bellinger 		iscsi_target_login_drop(conn, login);
520d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
521d381a801SNicholas Bellinger 		return;
522d381a801SNicholas Bellinger 	}
523d381a801SNicholas Bellinger 
524d381a801SNicholas Bellinger 	if (conn->sock) {
525d381a801SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
526d381a801SNicholas Bellinger 
527d381a801SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
528d381a801SNicholas Bellinger 		state = iscsi_target_sk_state_check(sk);
529d381a801SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
530d381a801SNicholas Bellinger 
531d381a801SNicholas Bellinger 		if (state == false) {
532d381a801SNicholas Bellinger 			pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
533d381a801SNicholas Bellinger 			iscsi_target_restore_sock_callbacks(conn);
534d381a801SNicholas Bellinger 			iscsi_target_login_drop(conn, login);
535d381a801SNicholas Bellinger 			iscsit_deaccess_np(np, tpg, tpg_np);
536d381a801SNicholas Bellinger 			return;
537d381a801SNicholas Bellinger 		}
538d381a801SNicholas Bellinger 	}
539d381a801SNicholas Bellinger 
540d381a801SNicholas Bellinger 	conn->login_kworker = current;
541d381a801SNicholas Bellinger 	allow_signal(SIGINT);
542d381a801SNicholas Bellinger 
543d381a801SNicholas Bellinger 	init_timer(&login_timer);
544d381a801SNicholas Bellinger 	login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
545d381a801SNicholas Bellinger 	login_timer.data = (unsigned long)conn;
546d381a801SNicholas Bellinger 	login_timer.function = iscsi_target_login_timeout;
547d381a801SNicholas Bellinger 	add_timer(&login_timer);
548d381a801SNicholas Bellinger 	pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
549d381a801SNicholas Bellinger 
550d381a801SNicholas Bellinger 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
551d381a801SNicholas Bellinger 	del_timer_sync(&login_timer);
552d381a801SNicholas Bellinger 	flush_signals(current);
553d381a801SNicholas Bellinger 	conn->login_kworker = NULL;
554d381a801SNicholas Bellinger 
555d381a801SNicholas Bellinger 	if (rc < 0) {
556d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
557d381a801SNicholas Bellinger 		iscsi_target_login_drop(conn, login);
558d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
559d381a801SNicholas Bellinger 		return;
560d381a801SNicholas Bellinger 	}
561d381a801SNicholas Bellinger 
562d381a801SNicholas Bellinger 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
563d381a801SNicholas Bellinger 			conn, current->comm, current->pid);
564d381a801SNicholas Bellinger 
565d381a801SNicholas Bellinger 	rc = iscsi_target_do_login(conn, login);
566d381a801SNicholas Bellinger 	if (rc < 0) {
567d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
568d381a801SNicholas Bellinger 		iscsi_target_login_drop(conn, login);
569d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
570d381a801SNicholas Bellinger 	} else if (!rc) {
571d381a801SNicholas Bellinger 		if (conn->sock) {
572d381a801SNicholas Bellinger 			struct sock *sk = conn->sock->sk;
573d381a801SNicholas Bellinger 
574d381a801SNicholas Bellinger 			write_lock_bh(&sk->sk_callback_lock);
575d381a801SNicholas Bellinger 			clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
576d381a801SNicholas Bellinger 			write_unlock_bh(&sk->sk_callback_lock);
577d381a801SNicholas Bellinger 		}
578d381a801SNicholas Bellinger 	} else if (rc == 1) {
579d381a801SNicholas Bellinger 		iscsi_target_nego_release(conn);
580d381a801SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
581d381a801SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
582d381a801SNicholas Bellinger 	}
583d381a801SNicholas Bellinger }
584d381a801SNicholas Bellinger 
585bb048357SNicholas Bellinger static void iscsi_target_do_cleanup(struct work_struct *work)
586bb048357SNicholas Bellinger {
587bb048357SNicholas Bellinger 	struct iscsi_conn *conn = container_of(work,
588bb048357SNicholas Bellinger 				struct iscsi_conn, login_cleanup_work.work);
589bb048357SNicholas Bellinger 	struct sock *sk = conn->sock->sk;
590bb048357SNicholas Bellinger 	struct iscsi_login *login = conn->login;
591bb048357SNicholas Bellinger 	struct iscsi_np *np = login->np;
592bb048357SNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
593bb048357SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
594bb048357SNicholas Bellinger 
595bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_do_cleanup\n");
596bb048357SNicholas Bellinger 
597bb048357SNicholas Bellinger 	cancel_delayed_work_sync(&conn->login_work);
598bb048357SNicholas Bellinger 	conn->orig_state_change(sk);
599bb048357SNicholas Bellinger 
600bb048357SNicholas Bellinger 	iscsi_target_restore_sock_callbacks(conn);
601bb048357SNicholas Bellinger 	iscsi_target_login_drop(conn, login);
602bb048357SNicholas Bellinger 	iscsit_deaccess_np(np, tpg, tpg_np);
603bb048357SNicholas Bellinger 
604bb048357SNicholas Bellinger 	pr_debug("iscsi_target_do_cleanup done()\n");
605bb048357SNicholas Bellinger }
606bb048357SNicholas Bellinger 
607bb048357SNicholas Bellinger static void iscsi_target_sk_state_change(struct sock *sk)
608bb048357SNicholas Bellinger {
609bb048357SNicholas Bellinger 	struct iscsi_conn *conn;
610bb048357SNicholas Bellinger 	void (*orig_state_change)(struct sock *);
611bb048357SNicholas Bellinger 	bool state;
612bb048357SNicholas Bellinger 
613bb048357SNicholas Bellinger 	pr_debug("Entering iscsi_target_sk_state_change\n");
614bb048357SNicholas Bellinger 
615bb048357SNicholas Bellinger 	write_lock_bh(&sk->sk_callback_lock);
616bb048357SNicholas Bellinger 	conn = sk->sk_user_data;
617bb048357SNicholas Bellinger 	if (!conn) {
618bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
619bb048357SNicholas Bellinger 		return;
620bb048357SNicholas Bellinger 	}
621bb048357SNicholas Bellinger 	orig_state_change = conn->orig_state_change;
622bb048357SNicholas Bellinger 
623bb048357SNicholas Bellinger 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
624bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
625bb048357SNicholas Bellinger 			 conn);
626bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
627bb048357SNicholas Bellinger 		orig_state_change(sk);
628bb048357SNicholas Bellinger 		return;
629bb048357SNicholas Bellinger 	}
630bb048357SNicholas Bellinger 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
631bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
632bb048357SNicholas Bellinger 			 " conn: %p\n", conn);
633bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
634bb048357SNicholas Bellinger 		orig_state_change(sk);
635bb048357SNicholas Bellinger 		return;
636bb048357SNicholas Bellinger 	}
637bb048357SNicholas Bellinger 	if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
638bb048357SNicholas Bellinger 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
639bb048357SNicholas Bellinger 			 conn);
640bb048357SNicholas Bellinger 		write_unlock_bh(&sk->sk_callback_lock);
641bb048357SNicholas Bellinger 		orig_state_change(sk);
642bb048357SNicholas Bellinger 		return;
643bb048357SNicholas Bellinger 	}
644bb048357SNicholas Bellinger 
645bb048357SNicholas Bellinger 	state = iscsi_target_sk_state_check(sk);
646bb048357SNicholas Bellinger 	write_unlock_bh(&sk->sk_callback_lock);
647bb048357SNicholas Bellinger 
648bb048357SNicholas Bellinger 	pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
649bb048357SNicholas Bellinger 
650bb048357SNicholas Bellinger 	if (!state) {
651bb048357SNicholas Bellinger 		pr_debug("iscsi_target_sk_state_change got failed state\n");
652bb048357SNicholas Bellinger 		schedule_delayed_work(&conn->login_cleanup_work, 0);
653bb048357SNicholas Bellinger 		return;
654bb048357SNicholas Bellinger 	}
655bb048357SNicholas Bellinger 	orig_state_change(sk);
656bb048357SNicholas Bellinger }
657bb048357SNicholas Bellinger 
658e48354ceSNicholas Bellinger /*
659e48354ceSNicholas Bellinger  *	NOTE: We check for existing sessions or connections AFTER the initiator
660e48354ceSNicholas Bellinger  *	has been successfully authenticated in order to protect against faked
661e48354ceSNicholas Bellinger  *	ISID/TSIH combinations.
662e48354ceSNicholas Bellinger  */
663e48354ceSNicholas Bellinger static int iscsi_target_check_for_existing_instances(
664e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
665e48354ceSNicholas Bellinger 	struct iscsi_login *login)
666e48354ceSNicholas Bellinger {
667e48354ceSNicholas Bellinger 	if (login->checked_for_existing)
668e48354ceSNicholas Bellinger 		return 0;
669e48354ceSNicholas Bellinger 
670e48354ceSNicholas Bellinger 	login->checked_for_existing = 1;
671e48354ceSNicholas Bellinger 
672e48354ceSNicholas Bellinger 	if (!login->tsih)
673e48354ceSNicholas Bellinger 		return iscsi_check_for_session_reinstatement(conn);
674e48354ceSNicholas Bellinger 	else
675e48354ceSNicholas Bellinger 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
676e48354ceSNicholas Bellinger 				login->initial_exp_statsn);
677e48354ceSNicholas Bellinger }
678e48354ceSNicholas Bellinger 
679e48354ceSNicholas Bellinger static int iscsi_target_do_authentication(
680e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
681e48354ceSNicholas Bellinger 	struct iscsi_login *login)
682e48354ceSNicholas Bellinger {
683e48354ceSNicholas Bellinger 	int authret;
684e48354ceSNicholas Bellinger 	u32 payload_length;
685e48354ceSNicholas Bellinger 	struct iscsi_param *param;
686e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
687e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
688e48354ceSNicholas Bellinger 
689e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
690e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
691e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
692e48354ceSNicholas Bellinger 
693e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
694e48354ceSNicholas Bellinger 	if (!param)
695e48354ceSNicholas Bellinger 		return -1;
696e48354ceSNicholas Bellinger 
697e48354ceSNicholas Bellinger 	authret = iscsi_handle_authentication(
698e48354ceSNicholas Bellinger 			conn,
699e48354ceSNicholas Bellinger 			login->req_buf,
700e48354ceSNicholas Bellinger 			login->rsp_buf,
701e48354ceSNicholas Bellinger 			payload_length,
702e48354ceSNicholas Bellinger 			&login->rsp_length,
703e48354ceSNicholas Bellinger 			param->value);
704e48354ceSNicholas Bellinger 	switch (authret) {
705e48354ceSNicholas Bellinger 	case 0:
706e48354ceSNicholas Bellinger 		pr_debug("Received OK response"
707e48354ceSNicholas Bellinger 		" from LIO Authentication, continuing.\n");
708e48354ceSNicholas Bellinger 		break;
709e48354ceSNicholas Bellinger 	case 1:
710e48354ceSNicholas Bellinger 		pr_debug("iSCSI security negotiation"
711bfb9035cSJoe Perches 			" completed successfully.\n");
712e48354ceSNicholas Bellinger 		login->auth_complete = 1;
713e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
714e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
715e48354ceSNicholas Bellinger 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
716e48354ceSNicholas Bellinger 					     ISCSI_FLAG_LOGIN_TRANSIT);
717e48354ceSNicholas Bellinger 			login->current_stage = 1;
718e48354ceSNicholas Bellinger 		}
719e48354ceSNicholas Bellinger 		return iscsi_target_check_for_existing_instances(
720e48354ceSNicholas Bellinger 				conn, login);
721e48354ceSNicholas Bellinger 	case 2:
722e48354ceSNicholas Bellinger 		pr_err("Security negotiation"
723e48354ceSNicholas Bellinger 			" failed.\n");
724e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
725e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
726e48354ceSNicholas Bellinger 		return -1;
727e48354ceSNicholas Bellinger 	default:
728e48354ceSNicholas Bellinger 		pr_err("Received unknown error %d from LIO"
729e48354ceSNicholas Bellinger 				" Authentication\n", authret);
730e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
731e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
732e48354ceSNicholas Bellinger 		return -1;
733e48354ceSNicholas Bellinger 	}
734e48354ceSNicholas Bellinger 
735e48354ceSNicholas Bellinger 	return 0;
736e48354ceSNicholas Bellinger }
737e48354ceSNicholas Bellinger 
738e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_zero(
739e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
740e48354ceSNicholas Bellinger 	struct iscsi_login *login)
741e48354ceSNicholas Bellinger {
742e48354ceSNicholas Bellinger 	int ret;
743e48354ceSNicholas Bellinger 	u32 payload_length;
744e48354ceSNicholas Bellinger 	struct iscsi_param *param;
745e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
746e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
747e48354ceSNicholas Bellinger 
748e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
749e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
750e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
751e48354ceSNicholas Bellinger 
752e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
753e48354ceSNicholas Bellinger 	if (!param)
754e48354ceSNicholas Bellinger 		return -1;
755e48354ceSNicholas Bellinger 
756e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
757e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
758e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
759e48354ceSNicholas Bellinger 			login->req_buf,
760e48354ceSNicholas Bellinger 			payload_length,
7619977bb18SNicholas Bellinger 			conn);
762e48354ceSNicholas Bellinger 	if (ret < 0)
763e48354ceSNicholas Bellinger 		return -1;
764e48354ceSNicholas Bellinger 
765e48354ceSNicholas Bellinger 	if (ret > 0) {
766e48354ceSNicholas Bellinger 		if (login->auth_complete) {
767e48354ceSNicholas Bellinger 			pr_err("Initiator has already been"
768e48354ceSNicholas Bellinger 				" successfully authenticated, but is still"
769e48354ceSNicholas Bellinger 				" sending %s keys.\n", param->value);
770e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
771e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_INIT_ERR);
772e48354ceSNicholas Bellinger 			return -1;
773e48354ceSNicholas Bellinger 		}
774e48354ceSNicholas Bellinger 
775e48354ceSNicholas Bellinger 		goto do_auth;
77691f0abfdSNicholas Bellinger 	} else if (!payload_length) {
77791f0abfdSNicholas Bellinger 		pr_err("Initiator sent zero length security payload,"
77891f0abfdSNicholas Bellinger 		       " login failed\n");
77991f0abfdSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
78091f0abfdSNicholas Bellinger 				    ISCSI_LOGIN_STATUS_AUTH_FAILED);
78191f0abfdSNicholas Bellinger 		return -1;
782e48354ceSNicholas Bellinger 	}
783e48354ceSNicholas Bellinger 
784e48354ceSNicholas Bellinger 	if (login->first_request)
785e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
786e48354ceSNicholas Bellinger 			return -1;
787e48354ceSNicholas Bellinger 
788e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
789e48354ceSNicholas Bellinger 			PHASE_SECURITY|PHASE_DECLARATIVE,
790e48354ceSNicholas Bellinger 			SENDER_TARGET,
791e48354ceSNicholas Bellinger 			login->rsp_buf,
792e48354ceSNicholas Bellinger 			&login->rsp_length,
793e48354ceSNicholas Bellinger 			conn->param_list);
794e48354ceSNicholas Bellinger 	if (ret < 0)
795e48354ceSNicholas Bellinger 		return -1;
796e48354ceSNicholas Bellinger 
797e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
79860bfcf8eSAndy Grover 		if (conn->tpg->tpg_attrib.authentication &&
799e48354ceSNicholas Bellinger 		    !strncmp(param->value, NONE, 4)) {
800e48354ceSNicholas Bellinger 			pr_err("Initiator sent AuthMethod=None but"
801e48354ceSNicholas Bellinger 				" Target is enforcing iSCSI Authentication,"
802e48354ceSNicholas Bellinger 					" login 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 		}
807e48354ceSNicholas Bellinger 
80860bfcf8eSAndy Grover 		if (conn->tpg->tpg_attrib.authentication &&
809e48354ceSNicholas Bellinger 		    !login->auth_complete)
810e48354ceSNicholas Bellinger 			return 0;
811e48354ceSNicholas Bellinger 
812e48354ceSNicholas Bellinger 		if (strncmp(param->value, NONE, 4) && !login->auth_complete)
813e48354ceSNicholas Bellinger 			return 0;
814e48354ceSNicholas Bellinger 
815e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
816e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
817e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
818e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
819e48354ceSNicholas Bellinger 			login->current_stage = 1;
820e48354ceSNicholas Bellinger 		}
821e48354ceSNicholas Bellinger 	}
822e48354ceSNicholas Bellinger 
823e48354ceSNicholas Bellinger 	return 0;
824e48354ceSNicholas Bellinger do_auth:
825e48354ceSNicholas Bellinger 	return iscsi_target_do_authentication(conn, login);
826e48354ceSNicholas Bellinger }
827e48354ceSNicholas Bellinger 
828e48354ceSNicholas Bellinger static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
829e48354ceSNicholas Bellinger {
830e48354ceSNicholas Bellinger 	int ret;
831e48354ceSNicholas Bellinger 	u32 payload_length;
832e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
833e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
834e48354ceSNicholas Bellinger 
835e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
836e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
837e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
838e48354ceSNicholas Bellinger 
839e48354ceSNicholas Bellinger 	ret = iscsi_decode_text_input(
840e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
841e48354ceSNicholas Bellinger 			SENDER_INITIATOR|SENDER_RECEIVER,
842e48354ceSNicholas Bellinger 			login->req_buf,
843e48354ceSNicholas Bellinger 			payload_length,
8449977bb18SNicholas Bellinger 			conn);
8451c5c12c6SRoland Dreier 	if (ret < 0) {
8461c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
8471c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
848e48354ceSNicholas Bellinger 		return -1;
8491c5c12c6SRoland Dreier 	}
850e48354ceSNicholas Bellinger 
851e48354ceSNicholas Bellinger 	if (login->first_request)
852e48354ceSNicholas Bellinger 		if (iscsi_target_check_first_request(conn, login) < 0)
853e48354ceSNicholas Bellinger 			return -1;
854e48354ceSNicholas Bellinger 
855e48354ceSNicholas Bellinger 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
856e48354ceSNicholas Bellinger 		return -1;
857e48354ceSNicholas Bellinger 
858e48354ceSNicholas Bellinger 	ret = iscsi_encode_text_output(
859e48354ceSNicholas Bellinger 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
860e48354ceSNicholas Bellinger 			SENDER_TARGET,
861e48354ceSNicholas Bellinger 			login->rsp_buf,
862e48354ceSNicholas Bellinger 			&login->rsp_length,
863e48354ceSNicholas Bellinger 			conn->param_list);
8641c5c12c6SRoland Dreier 	if (ret < 0) {
8651c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
8661c5c12c6SRoland Dreier 				ISCSI_LOGIN_STATUS_INIT_ERR);
867e48354ceSNicholas Bellinger 		return -1;
8681c5c12c6SRoland Dreier 	}
869e48354ceSNicholas Bellinger 
870e48354ceSNicholas Bellinger 	if (!login->auth_complete &&
87160bfcf8eSAndy Grover 	     conn->tpg->tpg_attrib.authentication) {
872e48354ceSNicholas Bellinger 		pr_err("Initiator is requesting CSG: 1, has not been"
873e48354ceSNicholas Bellinger 			 " successfully authenticated, and the Target is"
874e48354ceSNicholas Bellinger 			" enforcing iSCSI Authentication, login failed.\n");
875e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
876e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
877e48354ceSNicholas Bellinger 		return -1;
878e48354ceSNicholas Bellinger 	}
879e48354ceSNicholas Bellinger 
880e48354ceSNicholas Bellinger 	if (!iscsi_check_negotiated_keys(conn->param_list))
881e48354ceSNicholas Bellinger 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
882e48354ceSNicholas Bellinger 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
883e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
884e48354ceSNicholas Bellinger 					    ISCSI_FLAG_LOGIN_TRANSIT;
885e48354ceSNicholas Bellinger 
886e48354ceSNicholas Bellinger 	return 0;
887e48354ceSNicholas Bellinger }
888e48354ceSNicholas Bellinger 
889e48354ceSNicholas Bellinger static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
890e48354ceSNicholas Bellinger {
891e48354ceSNicholas Bellinger 	int pdu_count = 0;
892e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
893e48354ceSNicholas Bellinger 	struct iscsi_login_rsp *login_rsp;
894e48354ceSNicholas Bellinger 
895e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
896e48354ceSNicholas Bellinger 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
897e48354ceSNicholas Bellinger 
898e48354ceSNicholas Bellinger 	while (1) {
899e48354ceSNicholas Bellinger 		if (++pdu_count > MAX_LOGIN_PDUS) {
900e48354ceSNicholas Bellinger 			pr_err("MAX_LOGIN_PDUS count reached.\n");
901e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
902e48354ceSNicholas Bellinger 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
903e48354ceSNicholas Bellinger 			return -1;
904e48354ceSNicholas Bellinger 		}
905e48354ceSNicholas Bellinger 
9065d358065SAndy Grover 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
907e48354ceSNicholas Bellinger 		case 0:
9085d358065SAndy Grover 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
909e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
910e48354ceSNicholas Bellinger 				return -1;
911e48354ceSNicholas Bellinger 			break;
912e48354ceSNicholas Bellinger 		case 1:
913e48354ceSNicholas Bellinger 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
914e48354ceSNicholas Bellinger 			if (iscsi_target_handle_csg_one(conn, login) < 0)
915e48354ceSNicholas Bellinger 				return -1;
916e48354ceSNicholas Bellinger 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
917e48354ceSNicholas Bellinger 				login->tsih = conn->sess->tsih;
918baa4d64bSNicholas Bellinger 				login->login_complete = 1;
919d381a801SNicholas Bellinger 				iscsi_target_restore_sock_callbacks(conn);
920e48354ceSNicholas Bellinger 				if (iscsi_target_do_tx_login_io(conn,
921e48354ceSNicholas Bellinger 						login) < 0)
922e48354ceSNicholas Bellinger 					return -1;
923d381a801SNicholas Bellinger 				return 1;
924e48354ceSNicholas Bellinger 			}
925e48354ceSNicholas Bellinger 			break;
926e48354ceSNicholas Bellinger 		default:
927e48354ceSNicholas Bellinger 			pr_err("Illegal CSG: %d received from"
928e48354ceSNicholas Bellinger 				" Initiator, protocol error.\n",
9295d358065SAndy Grover 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
930e48354ceSNicholas Bellinger 			break;
931e48354ceSNicholas Bellinger 		}
932e48354ceSNicholas Bellinger 
933ea3a179aSNicholas Bellinger 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
934e48354ceSNicholas Bellinger 			return -1;
935e48354ceSNicholas Bellinger 
936e48354ceSNicholas Bellinger 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
937e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
938e48354ceSNicholas Bellinger 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
939e48354ceSNicholas Bellinger 		}
940d381a801SNicholas Bellinger 		break;
941e48354ceSNicholas Bellinger 	}
942e48354ceSNicholas Bellinger 
943bb048357SNicholas Bellinger 	if (conn->sock) {
944bb048357SNicholas Bellinger 		struct sock *sk = conn->sock->sk;
945bb048357SNicholas Bellinger 		bool state;
946bb048357SNicholas Bellinger 
947bb048357SNicholas Bellinger 		read_lock_bh(&sk->sk_callback_lock);
948bb048357SNicholas Bellinger 		state = iscsi_target_sk_state_check(sk);
949bb048357SNicholas Bellinger 		read_unlock_bh(&sk->sk_callback_lock);
950bb048357SNicholas Bellinger 
951bb048357SNicholas Bellinger 		if (!state) {
952bb048357SNicholas Bellinger 			pr_debug("iscsi_target_do_login() failed state for"
953bb048357SNicholas Bellinger 				 " conn: %p\n", conn);
954bb048357SNicholas Bellinger 			return -1;
955bb048357SNicholas Bellinger 		}
956bb048357SNicholas Bellinger 	}
957bb048357SNicholas Bellinger 
958e48354ceSNicholas Bellinger 	return 0;
959e48354ceSNicholas Bellinger }
960e48354ceSNicholas Bellinger 
961e48354ceSNicholas Bellinger static void iscsi_initiatorname_tolower(
962e48354ceSNicholas Bellinger 	char *param_buf)
963e48354ceSNicholas Bellinger {
964e48354ceSNicholas Bellinger 	char *c;
965e48354ceSNicholas Bellinger 	u32 iqn_size = strlen(param_buf), i;
966e48354ceSNicholas Bellinger 
967e48354ceSNicholas Bellinger 	for (i = 0; i < iqn_size; i++) {
9688359cf43SJörn Engel 		c = &param_buf[i];
969e48354ceSNicholas Bellinger 		if (!isupper(*c))
970e48354ceSNicholas Bellinger 			continue;
971e48354ceSNicholas Bellinger 
972e48354ceSNicholas Bellinger 		*c = tolower(*c);
973e48354ceSNicholas Bellinger 	}
974e48354ceSNicholas Bellinger }
975e48354ceSNicholas Bellinger 
976e48354ceSNicholas Bellinger /*
977e48354ceSNicholas Bellinger  * Processes the first Login Request..
978e48354ceSNicholas Bellinger  */
979baa4d64bSNicholas Bellinger int iscsi_target_locate_portal(
980e48354ceSNicholas Bellinger 	struct iscsi_np *np,
981e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
982e48354ceSNicholas Bellinger 	struct iscsi_login *login)
983e48354ceSNicholas Bellinger {
984e48354ceSNicholas Bellinger 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
985e48354ceSNicholas Bellinger 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
986e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
987e48354ceSNicholas Bellinger 	struct iscsi_tiqn *tiqn;
988d381a801SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np = NULL;
989e48354ceSNicholas Bellinger 	struct iscsi_login_req *login_req;
990988e3a85SNicholas Bellinger 	struct se_node_acl *se_nacl;
991988e3a85SNicholas Bellinger 	u32 payload_length, queue_depth = 0;
992988e3a85SNicholas Bellinger 	int sessiontype = 0, ret = 0, tag_num, tag_size;
993e48354ceSNicholas Bellinger 
994d381a801SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
995bb048357SNicholas Bellinger 	INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
996d381a801SNicholas Bellinger 	iscsi_target_set_sock_callbacks(conn);
997d381a801SNicholas Bellinger 
998d381a801SNicholas Bellinger 	login->np = np;
999d381a801SNicholas Bellinger 
1000e48354ceSNicholas Bellinger 	login_req = (struct iscsi_login_req *) login->req;
1001e48354ceSNicholas Bellinger 	payload_length = ntoh24(login_req->dlength);
1002e48354ceSNicholas Bellinger 
1003e48354ceSNicholas Bellinger 	tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1004e48354ceSNicholas Bellinger 	if (!tmpbuf) {
1005e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for tmpbuf.\n");
1006e48354ceSNicholas Bellinger 		return -1;
1007e48354ceSNicholas Bellinger 	}
1008e48354ceSNicholas Bellinger 
1009e48354ceSNicholas Bellinger 	memcpy(tmpbuf, login->req_buf, payload_length);
1010e48354ceSNicholas Bellinger 	tmpbuf[payload_length] = '\0';
1011e48354ceSNicholas Bellinger 	start = tmpbuf;
1012e48354ceSNicholas Bellinger 	end = (start + payload_length);
1013e48354ceSNicholas Bellinger 
1014e48354ceSNicholas Bellinger 	/*
1015e48354ceSNicholas Bellinger 	 * Locate the initial keys expected from the Initiator node in
1016e48354ceSNicholas Bellinger 	 * the first login request in order to progress with the login phase.
1017e48354ceSNicholas Bellinger 	 */
1018e48354ceSNicholas Bellinger 	while (start < end) {
1019e48354ceSNicholas Bellinger 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1020e48354ceSNicholas Bellinger 			ret = -1;
1021e48354ceSNicholas Bellinger 			goto out;
1022e48354ceSNicholas Bellinger 		}
1023e48354ceSNicholas Bellinger 
1024e48354ceSNicholas Bellinger 		if (!strncmp(key, "InitiatorName", 13))
1025e48354ceSNicholas Bellinger 			i_buf = value;
1026e48354ceSNicholas Bellinger 		else if (!strncmp(key, "SessionType", 11))
1027e48354ceSNicholas Bellinger 			s_buf = value;
1028e48354ceSNicholas Bellinger 		else if (!strncmp(key, "TargetName", 10))
1029e48354ceSNicholas Bellinger 			t_buf = value;
1030e48354ceSNicholas Bellinger 
1031e48354ceSNicholas Bellinger 		start += strlen(key) + strlen(value) + 2;
1032e48354ceSNicholas Bellinger 	}
1033e48354ceSNicholas Bellinger 	/*
1034e48354ceSNicholas Bellinger 	 * See 5.3.  Login Phase.
1035e48354ceSNicholas Bellinger 	 */
1036e48354ceSNicholas Bellinger 	if (!i_buf) {
1037e48354ceSNicholas Bellinger 		pr_err("InitiatorName key not received"
1038e48354ceSNicholas Bellinger 			" in first login request.\n");
1039e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1040e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1041e48354ceSNicholas Bellinger 		ret = -1;
1042e48354ceSNicholas Bellinger 		goto out;
1043e48354ceSNicholas Bellinger 	}
1044e48354ceSNicholas Bellinger 	/*
1045e48354ceSNicholas Bellinger 	 * Convert the incoming InitiatorName to lowercase following
1046e48354ceSNicholas Bellinger 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1047e48354ceSNicholas Bellinger 	 * are NOT case sensitive.
1048e48354ceSNicholas Bellinger 	 */
1049e48354ceSNicholas Bellinger 	iscsi_initiatorname_tolower(i_buf);
1050e48354ceSNicholas Bellinger 
1051e48354ceSNicholas Bellinger 	if (!s_buf) {
1052e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1053e48354ceSNicholas Bellinger 			goto get_target;
1054e48354ceSNicholas Bellinger 
1055e48354ceSNicholas Bellinger 		pr_err("SessionType key not received"
1056e48354ceSNicholas Bellinger 			" in first login request.\n");
1057e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1058e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1059e48354ceSNicholas Bellinger 		ret = -1;
1060e48354ceSNicholas Bellinger 		goto out;
1061e48354ceSNicholas Bellinger 	}
1062e48354ceSNicholas Bellinger 
1063e48354ceSNicholas Bellinger 	/*
1064e48354ceSNicholas Bellinger 	 * Use default portal group for discovery sessions.
1065e48354ceSNicholas Bellinger 	 */
1066e48354ceSNicholas Bellinger 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1067e48354ceSNicholas Bellinger 	if (!sessiontype) {
1068e48354ceSNicholas Bellinger 		conn->tpg = iscsit_global->discovery_tpg;
1069e48354ceSNicholas Bellinger 		if (!login->leading_connection)
1070e48354ceSNicholas Bellinger 			goto get_target;
1071e48354ceSNicholas Bellinger 
1072e48354ceSNicholas Bellinger 		sess->sess_ops->SessionType = 1;
1073e48354ceSNicholas Bellinger 		/*
1074e48354ceSNicholas Bellinger 		 * Setup crc32c modules from libcrypto
1075e48354ceSNicholas Bellinger 		 */
1076e48354ceSNicholas Bellinger 		if (iscsi_login_setup_crypto(conn) < 0) {
1077e48354ceSNicholas Bellinger 			pr_err("iscsi_login_setup_crypto() failed\n");
1078e48354ceSNicholas Bellinger 			ret = -1;
1079e48354ceSNicholas Bellinger 			goto out;
1080e48354ceSNicholas Bellinger 		}
1081e48354ceSNicholas Bellinger 		/*
1082e48354ceSNicholas Bellinger 		 * Serialize access across the discovery struct iscsi_portal_group to
1083e48354ceSNicholas Bellinger 		 * process login attempt.
1084e48354ceSNicholas Bellinger 		 */
1085e48354ceSNicholas Bellinger 		if (iscsit_access_np(np, conn->tpg) < 0) {
1086e48354ceSNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1087e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1088e48354ceSNicholas Bellinger 			ret = -1;
1089e48354ceSNicholas Bellinger 			goto out;
1090e48354ceSNicholas Bellinger 		}
1091e48354ceSNicholas Bellinger 		ret = 0;
1092988e3a85SNicholas Bellinger 		goto alloc_tags;
1093e48354ceSNicholas Bellinger 	}
1094e48354ceSNicholas Bellinger 
1095e48354ceSNicholas Bellinger get_target:
1096e48354ceSNicholas Bellinger 	if (!t_buf) {
1097e48354ceSNicholas Bellinger 		pr_err("TargetName key not received"
1098e48354ceSNicholas Bellinger 			" in first login request while"
1099e48354ceSNicholas Bellinger 			" SessionType=Normal.\n");
1100e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1101e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1102e48354ceSNicholas Bellinger 		ret = -1;
1103e48354ceSNicholas Bellinger 		goto out;
1104e48354ceSNicholas Bellinger 	}
1105e48354ceSNicholas Bellinger 
1106e48354ceSNicholas Bellinger 	/*
1107e48354ceSNicholas Bellinger 	 * Locate Target IQN from Storage Node.
1108e48354ceSNicholas Bellinger 	 */
1109e48354ceSNicholas Bellinger 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1110e48354ceSNicholas Bellinger 	if (!tiqn) {
1111e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target IQN: %s in"
1112e48354ceSNicholas Bellinger 			" Storage Node\n", t_buf);
1113e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1114e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1115e48354ceSNicholas Bellinger 		ret = -1;
1116e48354ceSNicholas Bellinger 		goto out;
1117e48354ceSNicholas Bellinger 	}
1118e48354ceSNicholas Bellinger 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1119e48354ceSNicholas Bellinger 
1120e48354ceSNicholas Bellinger 	/*
1121e48354ceSNicholas Bellinger 	 * Locate Target Portal Group from Storage Node.
1122e48354ceSNicholas Bellinger 	 */
1123d381a801SNicholas Bellinger 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1124e48354ceSNicholas Bellinger 	if (!conn->tpg) {
1125e48354ceSNicholas Bellinger 		pr_err("Unable to locate Target Portal Group"
1126e48354ceSNicholas Bellinger 				" on %s\n", tiqn->tiqn);
1127e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1128e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1129e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1130e48354ceSNicholas Bellinger 		ret = -1;
1131e48354ceSNicholas Bellinger 		goto out;
1132e48354ceSNicholas Bellinger 	}
1133d381a801SNicholas Bellinger 	conn->tpg_np = tpg_np;
1134e48354ceSNicholas Bellinger 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1135e48354ceSNicholas Bellinger 	/*
1136e48354ceSNicholas Bellinger 	 * Setup crc32c modules from libcrypto
1137e48354ceSNicholas Bellinger 	 */
1138e48354ceSNicholas Bellinger 	if (iscsi_login_setup_crypto(conn) < 0) {
1139e48354ceSNicholas Bellinger 		pr_err("iscsi_login_setup_crypto() failed\n");
1140d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1141d381a801SNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1142d381a801SNicholas Bellinger 		conn->tpg = NULL;
1143e48354ceSNicholas Bellinger 		ret = -1;
1144e48354ceSNicholas Bellinger 		goto out;
1145e48354ceSNicholas Bellinger 	}
1146e48354ceSNicholas Bellinger 	/*
1147e48354ceSNicholas Bellinger 	 * Serialize access across the struct iscsi_portal_group to
1148e48354ceSNicholas Bellinger 	 * process login attempt.
1149e48354ceSNicholas Bellinger 	 */
1150e48354ceSNicholas Bellinger 	if (iscsit_access_np(np, conn->tpg) < 0) {
1151d381a801SNicholas Bellinger 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1152e48354ceSNicholas Bellinger 		iscsit_put_tiqn_for_login(tiqn);
1153e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1154e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1155e48354ceSNicholas Bellinger 		conn->tpg = NULL;
1156d381a801SNicholas Bellinger 		ret = -1;
1157e48354ceSNicholas Bellinger 		goto out;
1158e48354ceSNicholas Bellinger 	}
1159e48354ceSNicholas Bellinger 
1160e48354ceSNicholas Bellinger 	/*
1161e48354ceSNicholas Bellinger 	 * conn->sess->node_acl will be set when the referenced
1162e48354ceSNicholas Bellinger 	 * struct iscsi_session is located from received ISID+TSIH in
1163e48354ceSNicholas Bellinger 	 * iscsi_login_non_zero_tsih_s2().
1164e48354ceSNicholas Bellinger 	 */
1165e48354ceSNicholas Bellinger 	if (!login->leading_connection) {
1166e48354ceSNicholas Bellinger 		ret = 0;
1167e48354ceSNicholas Bellinger 		goto out;
1168e48354ceSNicholas Bellinger 	}
1169e48354ceSNicholas Bellinger 
1170e48354ceSNicholas Bellinger 	/*
1171e48354ceSNicholas Bellinger 	 * This value is required in iscsi_login_zero_tsih_s2()
1172e48354ceSNicholas Bellinger 	 */
1173e48354ceSNicholas Bellinger 	sess->sess_ops->SessionType = 0;
1174e48354ceSNicholas Bellinger 
1175e48354ceSNicholas Bellinger 	/*
1176e48354ceSNicholas Bellinger 	 * Locate incoming Initiator IQN reference from Storage Node.
1177e48354ceSNicholas Bellinger 	 */
1178e48354ceSNicholas Bellinger 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1179e48354ceSNicholas Bellinger 			&conn->tpg->tpg_se_tpg, i_buf);
1180e48354ceSNicholas Bellinger 	if (!sess->se_sess->se_node_acl) {
1181e48354ceSNicholas Bellinger 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1182e48354ceSNicholas Bellinger 			" access iSCSI target portal group: %hu.\n",
1183e48354ceSNicholas Bellinger 				i_buf, conn->tpg->tpgt);
1184e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1185e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1186e48354ceSNicholas Bellinger 		ret = -1;
1187e48354ceSNicholas Bellinger 		goto out;
1188e48354ceSNicholas Bellinger 	}
1189988e3a85SNicholas Bellinger 	se_nacl = sess->se_sess->se_node_acl;
1190988e3a85SNicholas Bellinger 	queue_depth = se_nacl->queue_depth;
1191988e3a85SNicholas Bellinger 	/*
1192988e3a85SNicholas Bellinger 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1193988e3a85SNicholas Bellinger 	 * depth for non immediate commands, plus extra tags for immediate
1194988e3a85SNicholas Bellinger 	 * commands.
1195988e3a85SNicholas Bellinger 	 *
1196988e3a85SNicholas Bellinger 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1197988e3a85SNicholas Bellinger 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1198988e3a85SNicholas Bellinger 	 */
1199988e3a85SNicholas Bellinger alloc_tags:
1200988e3a85SNicholas Bellinger 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
12014a4caa29SNicholas Bellinger 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1202988e3a85SNicholas Bellinger 	tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1203e48354ceSNicholas Bellinger 
1204988e3a85SNicholas Bellinger 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1205988e3a85SNicholas Bellinger 	if (ret < 0) {
1206988e3a85SNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1207988e3a85SNicholas Bellinger 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1208988e3a85SNicholas Bellinger 		ret = -1;
1209988e3a85SNicholas Bellinger 	}
1210e48354ceSNicholas Bellinger out:
1211e48354ceSNicholas Bellinger 	kfree(tmpbuf);
1212e48354ceSNicholas Bellinger 	return ret;
1213e48354ceSNicholas Bellinger }
1214e48354ceSNicholas Bellinger 
1215e48354ceSNicholas Bellinger int iscsi_target_start_negotiation(
1216e48354ceSNicholas Bellinger 	struct iscsi_login *login,
1217e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
1218e48354ceSNicholas Bellinger {
1219baa4d64bSNicholas Bellinger 	int ret;
1220e48354ceSNicholas Bellinger 
1221e48354ceSNicholas Bellinger 	ret = iscsi_target_do_login(conn, login);
1222d381a801SNicholas Bellinger 	if (!ret) {
1223d381a801SNicholas Bellinger 		if (conn->sock) {
1224d381a801SNicholas Bellinger 			struct sock *sk = conn->sock->sk;
1225e48354ceSNicholas Bellinger 
1226d381a801SNicholas Bellinger 			write_lock_bh(&sk->sk_callback_lock);
1227d381a801SNicholas Bellinger 			set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1228d381a801SNicholas Bellinger 			write_unlock_bh(&sk->sk_callback_lock);
1229d381a801SNicholas Bellinger 		}
1230d381a801SNicholas Bellinger 	} else if (ret < 0) {
1231d381a801SNicholas Bellinger 		cancel_delayed_work_sync(&conn->login_work);
1232bb048357SNicholas Bellinger 		cancel_delayed_work_sync(&conn->login_cleanup_work);
1233d381a801SNicholas Bellinger 		iscsi_target_restore_sock_callbacks(conn);
1234d381a801SNicholas Bellinger 		iscsi_remove_failed_auth_entry(conn);
1235d381a801SNicholas Bellinger 	}
1236d381a801SNicholas Bellinger 	if (ret != 0)
1237baa4d64bSNicholas Bellinger 		iscsi_target_nego_release(conn);
1238d381a801SNicholas Bellinger 
1239e48354ceSNicholas Bellinger 	return ret;
1240e48354ceSNicholas Bellinger }
1241e48354ceSNicholas Bellinger 
1242baa4d64bSNicholas Bellinger void iscsi_target_nego_release(struct iscsi_conn *conn)
1243e48354ceSNicholas Bellinger {
1244baa4d64bSNicholas Bellinger 	struct iscsi_login *login = conn->conn_login;
1245baa4d64bSNicholas Bellinger 
1246baa4d64bSNicholas Bellinger 	if (!login)
1247baa4d64bSNicholas Bellinger 		return;
1248baa4d64bSNicholas Bellinger 
1249e48354ceSNicholas Bellinger 	kfree(login->req_buf);
1250e48354ceSNicholas Bellinger 	kfree(login->rsp_buf);
1251e48354ceSNicholas Bellinger 	kfree(login);
1252baa4d64bSNicholas Bellinger 
1253baa4d64bSNicholas Bellinger 	conn->conn_login = NULL;
1254e48354ceSNicholas Bellinger }
1255