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