1e48354ceSNicholas Bellinger /*******************************************************************************
2e48354ceSNicholas Bellinger  * This file contains the login functions used by the iSCSI Target driver.
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/string.h>
20e48354ceSNicholas Bellinger #include <linux/kthread.h>
21e48354ceSNicholas Bellinger #include <linux/crypto.h>
2240401530SAl Viro #include <linux/idr.h>
23e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
24e48354ceSNicholas Bellinger #include <target/target_core_base.h>
25c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
26e48354ceSNicholas Bellinger 
27e48354ceSNicholas Bellinger #include "iscsi_target_core.h"
28e48354ceSNicholas Bellinger #include "iscsi_target_tq.h"
29e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
30e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
31e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
32e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
33e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_stat.h"
35e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
36e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
37e48354ceSNicholas Bellinger #include "iscsi_target.h"
38e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
39e48354ceSNicholas Bellinger 
40baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
41baa4d64bSNicholas Bellinger 
42baa4d64bSNicholas Bellinger static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
43e48354ceSNicholas Bellinger {
44baa4d64bSNicholas Bellinger 	struct iscsi_login *login;
45baa4d64bSNicholas Bellinger 
46baa4d64bSNicholas Bellinger 	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
47baa4d64bSNicholas Bellinger 	if (!login) {
48baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_login.\n");
49baa4d64bSNicholas Bellinger 		return NULL;
50baa4d64bSNicholas Bellinger 	}
51a91eb7d9SNicholas Bellinger 	conn->login = login;
52baa4d64bSNicholas Bellinger 	login->conn = conn;
53baa4d64bSNicholas Bellinger 	login->first_request = 1;
54baa4d64bSNicholas Bellinger 
55baa4d64bSNicholas Bellinger 	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
56baa4d64bSNicholas Bellinger 	if (!login->req_buf) {
57baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for response buffer.\n");
58baa4d64bSNicholas Bellinger 		goto out_login;
59baa4d64bSNicholas Bellinger 	}
60baa4d64bSNicholas Bellinger 
61baa4d64bSNicholas Bellinger 	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
62baa4d64bSNicholas Bellinger 	if (!login->rsp_buf) {
63baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for request buffer.\n");
64baa4d64bSNicholas Bellinger 		goto out_req_buf;
65baa4d64bSNicholas Bellinger 	}
66baa4d64bSNicholas Bellinger 
67baa4d64bSNicholas Bellinger 	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
68baa4d64bSNicholas Bellinger 	if (!conn->conn_ops) {
69baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for"
70baa4d64bSNicholas Bellinger 			" struct iscsi_conn_ops.\n");
71baa4d64bSNicholas Bellinger 		goto out_rsp_buf;
72baa4d64bSNicholas Bellinger 	}
73baa4d64bSNicholas Bellinger 
74d5627acbSRoland Dreier 	init_waitqueue_head(&conn->queues_wq);
75e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_list);
76e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_cmd_list);
77e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->immed_queue_list);
78e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->response_queue_list);
79e48354ceSNicholas Bellinger 	init_completion(&conn->conn_post_wait_comp);
80e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_comp);
81e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_rcfr_comp);
82e48354ceSNicholas Bellinger 	init_completion(&conn->conn_waiting_on_uc_comp);
83e48354ceSNicholas Bellinger 	init_completion(&conn->conn_logout_comp);
84e48354ceSNicholas Bellinger 	init_completion(&conn->rx_half_close_comp);
85e48354ceSNicholas Bellinger 	init_completion(&conn->tx_half_close_comp);
86e48354ceSNicholas Bellinger 	spin_lock_init(&conn->cmd_lock);
87e48354ceSNicholas Bellinger 	spin_lock_init(&conn->conn_usage_lock);
88e48354ceSNicholas Bellinger 	spin_lock_init(&conn->immed_queue_lock);
89e48354ceSNicholas Bellinger 	spin_lock_init(&conn->nopin_timer_lock);
90e48354ceSNicholas Bellinger 	spin_lock_init(&conn->response_queue_lock);
91e48354ceSNicholas Bellinger 	spin_lock_init(&conn->state_lock);
92e48354ceSNicholas Bellinger 
93e48354ceSNicholas Bellinger 	if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
94e48354ceSNicholas Bellinger 		pr_err("Unable to allocate conn->conn_cpumask\n");
95baa4d64bSNicholas Bellinger 		goto out_conn_ops;
96e48354ceSNicholas Bellinger 	}
97baa4d64bSNicholas Bellinger 	conn->conn_login = login;
98e48354ceSNicholas Bellinger 
99baa4d64bSNicholas Bellinger 	return login;
100baa4d64bSNicholas Bellinger 
101baa4d64bSNicholas Bellinger out_conn_ops:
102baa4d64bSNicholas Bellinger 	kfree(conn->conn_ops);
103baa4d64bSNicholas Bellinger out_rsp_buf:
104baa4d64bSNicholas Bellinger 	kfree(login->rsp_buf);
105baa4d64bSNicholas Bellinger out_req_buf:
106baa4d64bSNicholas Bellinger 	kfree(login->req_buf);
107baa4d64bSNicholas Bellinger out_login:
108baa4d64bSNicholas Bellinger 	kfree(login);
109baa4d64bSNicholas Bellinger 	return NULL;
110e48354ceSNicholas Bellinger }
111e48354ceSNicholas Bellinger 
112e48354ceSNicholas Bellinger /*
113e48354ceSNicholas Bellinger  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
114e48354ceSNicholas Bellinger  * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
115e48354ceSNicholas Bellinger  */
116e48354ceSNicholas Bellinger int iscsi_login_setup_crypto(struct iscsi_conn *conn)
117e48354ceSNicholas Bellinger {
118e48354ceSNicholas Bellinger 	/*
119e48354ceSNicholas Bellinger 	 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
120e48354ceSNicholas Bellinger 	 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
121e48354ceSNicholas Bellinger 	 * to software 1x8 byte slicing from crc32c.ko
122e48354ceSNicholas Bellinger 	 */
123e48354ceSNicholas Bellinger 	conn->conn_rx_hash.flags = 0;
124e48354ceSNicholas Bellinger 	conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
125e48354ceSNicholas Bellinger 						CRYPTO_ALG_ASYNC);
126e48354ceSNicholas Bellinger 	if (IS_ERR(conn->conn_rx_hash.tfm)) {
127e48354ceSNicholas Bellinger 		pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
128e48354ceSNicholas Bellinger 		return -ENOMEM;
129e48354ceSNicholas Bellinger 	}
130e48354ceSNicholas Bellinger 
131e48354ceSNicholas Bellinger 	conn->conn_tx_hash.flags = 0;
132e48354ceSNicholas Bellinger 	conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
133e48354ceSNicholas Bellinger 						CRYPTO_ALG_ASYNC);
134e48354ceSNicholas Bellinger 	if (IS_ERR(conn->conn_tx_hash.tfm)) {
135e48354ceSNicholas Bellinger 		pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
136e48354ceSNicholas Bellinger 		crypto_free_hash(conn->conn_rx_hash.tfm);
137e48354ceSNicholas Bellinger 		return -ENOMEM;
138e48354ceSNicholas Bellinger 	}
139e48354ceSNicholas Bellinger 
140e48354ceSNicholas Bellinger 	return 0;
141e48354ceSNicholas Bellinger }
142e48354ceSNicholas Bellinger 
143e48354ceSNicholas Bellinger static int iscsi_login_check_initiator_version(
144e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
145e48354ceSNicholas Bellinger 	u8 version_max,
146e48354ceSNicholas Bellinger 	u8 version_min)
147e48354ceSNicholas Bellinger {
148e48354ceSNicholas Bellinger 	if ((version_max != 0x00) || (version_min != 0x00)) {
149e48354ceSNicholas Bellinger 		pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
150e48354ceSNicholas Bellinger 			" version Min/Max 0x%02x/0x%02x, rejecting login.\n",
151e48354ceSNicholas Bellinger 			version_min, version_max);
152e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
153e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_VERSION);
154e48354ceSNicholas Bellinger 		return -1;
155e48354ceSNicholas Bellinger 	}
156e48354ceSNicholas Bellinger 
157e48354ceSNicholas Bellinger 	return 0;
158e48354ceSNicholas Bellinger }
159e48354ceSNicholas Bellinger 
160e48354ceSNicholas Bellinger int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
161e48354ceSNicholas Bellinger {
162e48354ceSNicholas Bellinger 	int sessiontype;
163e48354ceSNicholas Bellinger 	struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
164e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
165e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
166e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
167e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
168e48354ceSNicholas Bellinger 
169e48354ceSNicholas Bellinger 	initiatorname_param = iscsi_find_param_from_key(
170e48354ceSNicholas Bellinger 			INITIATORNAME, conn->param_list);
171e48354ceSNicholas Bellinger 	sessiontype_param = iscsi_find_param_from_key(
172e48354ceSNicholas Bellinger 			SESSIONTYPE, conn->param_list);
1731c5c12c6SRoland Dreier 	if (!initiatorname_param || !sessiontype_param) {
1741c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1751c5c12c6SRoland Dreier 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
176e48354ceSNicholas Bellinger 		return -1;
1771c5c12c6SRoland Dreier 	}
178e48354ceSNicholas Bellinger 
179e48354ceSNicholas Bellinger 	sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
180e48354ceSNicholas Bellinger 
181e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
182e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
183e48354ceSNicholas Bellinger 			sess_list) {
184e48354ceSNicholas Bellinger 
1858359cf43SJörn Engel 		sess_p = se_sess->fabric_sess_ptr;
186e48354ceSNicholas Bellinger 		spin_lock(&sess_p->conn_lock);
187e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
188e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
189e48354ceSNicholas Bellinger 		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
190e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
191e48354ceSNicholas Bellinger 			continue;
192e48354ceSNicholas Bellinger 		}
1938359cf43SJörn Engel 		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
1948359cf43SJörn Engel 		   (!strcmp(sess_p->sess_ops->InitiatorName,
1958359cf43SJörn Engel 			    initiatorname_param->value) &&
196e48354ceSNicholas Bellinger 		   (sess_p->sess_ops->SessionType == sessiontype))) {
197e48354ceSNicholas Bellinger 			atomic_set(&sess_p->session_reinstatement, 1);
198e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
199e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
200e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
201e48354ceSNicholas Bellinger 			sess = sess_p;
202e48354ceSNicholas Bellinger 			break;
203e48354ceSNicholas Bellinger 		}
204e48354ceSNicholas Bellinger 		spin_unlock(&sess_p->conn_lock);
205e48354ceSNicholas Bellinger 	}
206e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
207e48354ceSNicholas Bellinger 	/*
208e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
209e48354ceSNicholas Bellinger 	 */
210e48354ceSNicholas Bellinger 	if (!sess)
211e48354ceSNicholas Bellinger 		return 0;
212e48354ceSNicholas Bellinger 
213e48354ceSNicholas Bellinger 	pr_debug("%s iSCSI Session SID %u is still active for %s,"
214e48354ceSNicholas Bellinger 		" preforming session reinstatement.\n", (sessiontype) ?
215e48354ceSNicholas Bellinger 		"Discovery" : "Normal", sess->sid,
216e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
217e48354ceSNicholas Bellinger 
218e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
219e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED) {
220e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
221e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
22299367f01SNicholas Bellinger 		target_put_session(sess->se_sess);
22399367f01SNicholas Bellinger 		return 0;
224e48354ceSNicholas Bellinger 	}
225e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
226e48354ceSNicholas Bellinger 
227e48354ceSNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
228e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
229e48354ceSNicholas Bellinger 
23099367f01SNicholas Bellinger 	target_put_session(sess->se_sess);
23199367f01SNicholas Bellinger 	return 0;
232e48354ceSNicholas Bellinger }
233e48354ceSNicholas Bellinger 
234e48354ceSNicholas Bellinger static void iscsi_login_set_conn_values(
235e48354ceSNicholas Bellinger 	struct iscsi_session *sess,
236e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
23750e5c87dSChristoph Hellwig 	__be16 cid)
238e48354ceSNicholas Bellinger {
239e48354ceSNicholas Bellinger 	conn->sess		= sess;
24050e5c87dSChristoph Hellwig 	conn->cid		= be16_to_cpu(cid);
241e48354ceSNicholas Bellinger 	/*
242e48354ceSNicholas Bellinger 	 * Generate a random Status sequence number (statsn) for the new
243e48354ceSNicholas Bellinger 	 * iSCSI connection.
244e48354ceSNicholas Bellinger 	 */
245e48354ceSNicholas Bellinger 	get_random_bytes(&conn->stat_sn, sizeof(u32));
246e48354ceSNicholas Bellinger 
247e48354ceSNicholas Bellinger 	mutex_lock(&auth_id_lock);
248e48354ceSNicholas Bellinger 	conn->auth_id		= iscsit_global->auth_id++;
249e48354ceSNicholas Bellinger 	mutex_unlock(&auth_id_lock);
250e48354ceSNicholas Bellinger }
251e48354ceSNicholas Bellinger 
25279d59d08SRoland Dreier static __printf(2, 3) int iscsi_change_param_sprintf(
25379d59d08SRoland Dreier 	struct iscsi_conn *conn,
25479d59d08SRoland Dreier 	const char *fmt, ...)
25579d59d08SRoland Dreier {
25679d59d08SRoland Dreier 	va_list args;
25779d59d08SRoland Dreier 	unsigned char buf[64];
25879d59d08SRoland Dreier 
25979d59d08SRoland Dreier 	memset(buf, 0, sizeof buf);
26079d59d08SRoland Dreier 
26179d59d08SRoland Dreier 	va_start(args, fmt);
26279d59d08SRoland Dreier 	vsnprintf(buf, sizeof buf, fmt, args);
26379d59d08SRoland Dreier 	va_end(args);
26479d59d08SRoland Dreier 
26579d59d08SRoland Dreier 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
26679d59d08SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
26779d59d08SRoland Dreier 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
26879d59d08SRoland Dreier 		return -1;
26979d59d08SRoland Dreier 	}
27079d59d08SRoland Dreier 
27179d59d08SRoland Dreier 	return 0;
27279d59d08SRoland Dreier }
27379d59d08SRoland Dreier 
274e48354ceSNicholas Bellinger /*
275e48354ceSNicholas Bellinger  *	This is the leading connection of a new session,
276e48354ceSNicholas Bellinger  *	or session reinstatement.
277e48354ceSNicholas Bellinger  */
278e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s1(
279e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
280e48354ceSNicholas Bellinger 	unsigned char *buf)
281e48354ceSNicholas Bellinger {
282e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL;
283e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
284e70beee7SNicholas Bellinger 	enum target_prot_op sup_pro_ops;
28513b5533aSBenjamin Wang 	int ret;
286e48354ceSNicholas Bellinger 
287e48354ceSNicholas Bellinger 	sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
288e48354ceSNicholas Bellinger 	if (!sess) {
289e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
290e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
291e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for session\n");
2920957627aSNicholas Bellinger 		return -ENOMEM;
293e48354ceSNicholas Bellinger 	}
294e48354ceSNicholas Bellinger 
295e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(sess, conn, pdu->cid);
296e48354ceSNicholas Bellinger 	sess->init_task_tag	= pdu->itt;
2978359cf43SJörn Engel 	memcpy(&sess->isid, pdu->isid, 6);
29850e5c87dSChristoph Hellwig 	sess->exp_cmd_sn	= be32_to_cpu(pdu->cmdsn);
299e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_conn_list);
300e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
301e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_active_list);
302e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_inactive_list);
303e48354ceSNicholas Bellinger 	init_completion(&sess->async_msg_comp);
304e48354ceSNicholas Bellinger 	init_completion(&sess->reinstatement_comp);
305e48354ceSNicholas Bellinger 	init_completion(&sess->session_wait_comp);
306e48354ceSNicholas Bellinger 	init_completion(&sess->session_waiting_on_uc_comp);
307e48354ceSNicholas Bellinger 	mutex_init(&sess->cmdsn_mutex);
308e48354ceSNicholas Bellinger 	spin_lock_init(&sess->conn_lock);
309e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_a_lock);
310e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_i_lock);
311e48354ceSNicholas Bellinger 	spin_lock_init(&sess->session_usage_lock);
312e48354ceSNicholas Bellinger 	spin_lock_init(&sess->ttt_lock);
313e48354ceSNicholas Bellinger 
314c9365bd0STejun Heo 	idr_preload(GFP_KERNEL);
315998866b0SRoland Dreier 	spin_lock_bh(&sess_idr_lock);
316c9365bd0STejun Heo 	ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
317c9365bd0STejun Heo 	if (ret >= 0)
318c9365bd0STejun Heo 		sess->session_index = ret;
319998866b0SRoland Dreier 	spin_unlock_bh(&sess_idr_lock);
320c9365bd0STejun Heo 	idr_preload_end();
321e48354ceSNicholas Bellinger 
32213b5533aSBenjamin Wang 	if (ret < 0) {
323c9365bd0STejun Heo 		pr_err("idr_alloc() for sess_idr failed\n");
32413b5533aSBenjamin Wang 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
32513b5533aSBenjamin Wang 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
32613b5533aSBenjamin Wang 		kfree(sess);
32713b5533aSBenjamin Wang 		return -ENOMEM;
32813b5533aSBenjamin Wang 	}
32913b5533aSBenjamin Wang 
330e48354ceSNicholas Bellinger 	sess->creation_time = get_jiffies_64();
331e48354ceSNicholas Bellinger 	/*
332e48354ceSNicholas Bellinger 	 * The FFP CmdSN window values will be allocated from the TPG's
333e48354ceSNicholas Bellinger 	 * Initiator Node's ACL once the login has been successfully completed.
334e48354ceSNicholas Bellinger 	 */
33550e5c87dSChristoph Hellwig 	sess->max_cmd_sn	= be32_to_cpu(pdu->cmdsn);
336e48354ceSNicholas Bellinger 
337e48354ceSNicholas Bellinger 	sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
338e48354ceSNicholas Bellinger 	if (!sess->sess_ops) {
339e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
340e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
341e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
342e48354ceSNicholas Bellinger 				" struct iscsi_sess_ops.\n");
3430957627aSNicholas Bellinger 		kfree(sess);
3440957627aSNicholas Bellinger 		return -ENOMEM;
345e48354ceSNicholas Bellinger 	}
346e70beee7SNicholas Bellinger 	sup_pro_ops = conn->conn_transport->iscsit_get_sup_prot_ops(conn);
347e48354ceSNicholas Bellinger 
348e70beee7SNicholas Bellinger 	sess->se_sess = transport_init_session(sup_pro_ops);
3490957627aSNicholas Bellinger 	if (IS_ERR(sess->se_sess)) {
350e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
351e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
3520957627aSNicholas Bellinger 		kfree(sess);
3530957627aSNicholas Bellinger 		return -ENOMEM;
354e48354ceSNicholas Bellinger 	}
355e48354ceSNicholas Bellinger 
356e48354ceSNicholas Bellinger 	return 0;
357e48354ceSNicholas Bellinger }
358e48354ceSNicholas Bellinger 
359e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s2(
360e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
361e48354ceSNicholas Bellinger {
362e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na;
363e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
36403aa2070SNicholas Bellinger 	bool iser = false;
365e48354ceSNicholas Bellinger 
366e48354ceSNicholas Bellinger 	sess->tpg = conn->tpg;
367e48354ceSNicholas Bellinger 
368e48354ceSNicholas Bellinger 	/*
369e48354ceSNicholas Bellinger 	 * Assign a new TPG Session Handle.  Note this is protected with
370e48354ceSNicholas Bellinger 	 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
371e48354ceSNicholas Bellinger 	 */
37260bfcf8eSAndy Grover 	sess->tsih = ++sess->tpg->ntsih;
373e48354ceSNicholas Bellinger 	if (!sess->tsih)
37460bfcf8eSAndy Grover 		sess->tsih = ++sess->tpg->ntsih;
375e48354ceSNicholas Bellinger 
376e48354ceSNicholas Bellinger 	/*
377e48354ceSNicholas Bellinger 	 * Create the default params from user defined values..
378e48354ceSNicholas Bellinger 	 */
379e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
38060bfcf8eSAndy Grover 				conn->tpg->param_list, 1) < 0) {
381e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
382e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
383e48354ceSNicholas Bellinger 		return -1;
384e48354ceSNicholas Bellinger 	}
385e48354ceSNicholas Bellinger 
38603aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
38703aa2070SNicholas Bellinger 		iser = true;
38803aa2070SNicholas Bellinger 
38903aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
390e48354ceSNicholas Bellinger 
391e48354ceSNicholas Bellinger 	if (sess->sess_ops->SessionType)
392e48354ceSNicholas Bellinger 		return iscsi_set_keys_irrelevant_for_discovery(
393e48354ceSNicholas Bellinger 				conn->param_list);
394e48354ceSNicholas Bellinger 
395e48354ceSNicholas Bellinger 	na = iscsit_tpg_get_node_attrib(sess);
396e48354ceSNicholas Bellinger 
397e48354ceSNicholas Bellinger 	/*
398e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
399e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
400e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
401e48354ceSNicholas Bellinger 	 *
402e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
403e48354ceSNicholas Bellinger 	 */
40479d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
405e48354ceSNicholas Bellinger 		return -1;
406e48354ceSNicholas Bellinger 
407e48354ceSNicholas Bellinger 	/*
408e48354ceSNicholas Bellinger 	 * Workaround for Initiators that have broken connection recovery logic.
409e48354ceSNicholas Bellinger 	 *
410e48354ceSNicholas Bellinger 	 * "We would really like to get rid of this." Linux-iSCSI.org team
411e48354ceSNicholas Bellinger 	 */
41279d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
413e48354ceSNicholas Bellinger 		return -1;
414e48354ceSNicholas Bellinger 
415e48354ceSNicholas Bellinger 	if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
416e48354ceSNicholas Bellinger 		return -1;
41703aa2070SNicholas Bellinger 	/*
41803aa2070SNicholas Bellinger 	 * Set RDMAExtensions=Yes by default for iSER enabled network portals
41903aa2070SNicholas Bellinger 	 */
42003aa2070SNicholas Bellinger 	if (iser) {
42103aa2070SNicholas Bellinger 		struct iscsi_param *param;
42203aa2070SNicholas Bellinger 		unsigned long mrdsl, off;
42303aa2070SNicholas Bellinger 		int rc;
42403aa2070SNicholas Bellinger 
42579d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
42603aa2070SNicholas Bellinger 			return -1;
42779d59d08SRoland Dreier 
42803aa2070SNicholas Bellinger 		/*
42903aa2070SNicholas Bellinger 		 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
43003aa2070SNicholas Bellinger 		 * Immediate Data + Unsolicitied Data-OUT if necessary..
43103aa2070SNicholas Bellinger 		 */
43203aa2070SNicholas Bellinger 		param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
43303aa2070SNicholas Bellinger 						  conn->param_list);
43403aa2070SNicholas Bellinger 		if (!param) {
43503aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
43603aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
43703aa2070SNicholas Bellinger 			return -1;
43803aa2070SNicholas Bellinger 		}
43957103d7fSJingoo Han 		rc = kstrtoul(param->value, 0, &mrdsl);
44003aa2070SNicholas Bellinger 		if (rc < 0) {
44103aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
44203aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
44303aa2070SNicholas Bellinger 			return -1;
44403aa2070SNicholas Bellinger 		}
44503aa2070SNicholas Bellinger 		off = mrdsl % PAGE_SIZE;
44603aa2070SNicholas Bellinger 		if (!off)
44752d0aa79SNicholas Bellinger 			goto check_prot;
44803aa2070SNicholas Bellinger 
44903aa2070SNicholas Bellinger 		if (mrdsl < PAGE_SIZE)
45003aa2070SNicholas Bellinger 			mrdsl = PAGE_SIZE;
45103aa2070SNicholas Bellinger 		else
45203aa2070SNicholas Bellinger 			mrdsl -= off;
45303aa2070SNicholas Bellinger 
45403aa2070SNicholas Bellinger 		pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
45503aa2070SNicholas Bellinger 			" to PAGE_SIZE\n", mrdsl);
45603aa2070SNicholas Bellinger 
45779d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
45803aa2070SNicholas Bellinger 			return -1;
45952d0aa79SNicholas Bellinger 		/*
46052d0aa79SNicholas Bellinger 		 * ISER currently requires that ImmediateData + Unsolicited
46152d0aa79SNicholas Bellinger 		 * Data be disabled when protection / signature MRs are enabled.
46252d0aa79SNicholas Bellinger 		 */
46352d0aa79SNicholas Bellinger check_prot:
46452d0aa79SNicholas Bellinger 		if (sess->se_sess->sup_prot_ops &
46552d0aa79SNicholas Bellinger 		   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
46652d0aa79SNicholas Bellinger 		    TARGET_PROT_DOUT_INSERT)) {
46752d0aa79SNicholas Bellinger 
46879d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
46952d0aa79SNicholas Bellinger 				return -1;
47052d0aa79SNicholas Bellinger 
47179d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
47252d0aa79SNicholas Bellinger 				return -1;
47379d59d08SRoland Dreier 
47452d0aa79SNicholas Bellinger 			pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
47552d0aa79SNicholas Bellinger 				 " T10-PI enabled ISER session\n");
47652d0aa79SNicholas Bellinger 		}
47703aa2070SNicholas Bellinger 	}
478e48354ceSNicholas Bellinger 
479e48354ceSNicholas Bellinger 	return 0;
480e48354ceSNicholas Bellinger }
481e48354ceSNicholas Bellinger 
482e48354ceSNicholas Bellinger /*
483e48354ceSNicholas Bellinger  * Remove PSTATE_NEGOTIATE for the four FIM related keys.
484e48354ceSNicholas Bellinger  * The Initiator node will be able to enable FIM by proposing them itself.
485e48354ceSNicholas Bellinger  */
486e48354ceSNicholas Bellinger int iscsi_login_disable_FIM_keys(
487e48354ceSNicholas Bellinger 	struct iscsi_param_list *param_list,
488e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
489e48354ceSNicholas Bellinger {
490e48354ceSNicholas Bellinger 	struct iscsi_param *param;
491e48354ceSNicholas Bellinger 
492e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("OFMarker", param_list);
493e48354ceSNicholas Bellinger 	if (!param) {
494e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
495e48354ceSNicholas Bellinger 				" OFMarker failed\n");
496e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
497e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
498e48354ceSNicholas Bellinger 		return -1;
499e48354ceSNicholas Bellinger 	}
500e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
501e48354ceSNicholas Bellinger 
502e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("OFMarkInt", param_list);
503e48354ceSNicholas Bellinger 	if (!param) {
504e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
505e48354ceSNicholas Bellinger 				" IFMarker failed\n");
506e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
507e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
508e48354ceSNicholas Bellinger 		return -1;
509e48354ceSNicholas Bellinger 	}
510e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
511e48354ceSNicholas Bellinger 
512e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("IFMarker", param_list);
513e48354ceSNicholas Bellinger 	if (!param) {
514e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
515e48354ceSNicholas Bellinger 				" IFMarker failed\n");
516e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
517e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
518e48354ceSNicholas Bellinger 		return -1;
519e48354ceSNicholas Bellinger 	}
520e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
521e48354ceSNicholas Bellinger 
522e48354ceSNicholas Bellinger 	param = iscsi_find_param_from_key("IFMarkInt", param_list);
523e48354ceSNicholas Bellinger 	if (!param) {
524e48354ceSNicholas Bellinger 		pr_err("iscsi_find_param_from_key() for"
525e48354ceSNicholas Bellinger 				" IFMarker failed\n");
526e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
527e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
528e48354ceSNicholas Bellinger 		return -1;
529e48354ceSNicholas Bellinger 	}
530e48354ceSNicholas Bellinger 	param->state &= ~PSTATE_NEGOTIATE;
531e48354ceSNicholas Bellinger 
532e48354ceSNicholas Bellinger 	return 0;
533e48354ceSNicholas Bellinger }
534e48354ceSNicholas Bellinger 
535e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s1(
536e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
537e48354ceSNicholas Bellinger 	unsigned char *buf)
538e48354ceSNicholas Bellinger {
539e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
540e48354ceSNicholas Bellinger 
541e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(NULL, conn, pdu->cid);
542e48354ceSNicholas Bellinger 	return 0;
543e48354ceSNicholas Bellinger }
544e48354ceSNicholas Bellinger 
545e48354ceSNicholas Bellinger /*
546e48354ceSNicholas Bellinger  *	Add a new connection to an existing session.
547e48354ceSNicholas Bellinger  */
548e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s2(
549e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
550e48354ceSNicholas Bellinger 	unsigned char *buf)
551e48354ceSNicholas Bellinger {
552e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
553e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
554e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
555e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
556e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
55703aa2070SNicholas Bellinger 	bool iser = false;
558e48354ceSNicholas Bellinger 
559e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
560e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
561e48354ceSNicholas Bellinger 			sess_list) {
562e48354ceSNicholas Bellinger 
563e48354ceSNicholas Bellinger 		sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
564e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
565e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
566e48354ceSNicholas Bellinger 		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
567e48354ceSNicholas Bellinger 			continue;
5688359cf43SJörn Engel 		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
56950e5c87dSChristoph Hellwig 		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
570e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
571e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
572e48354ceSNicholas Bellinger 			sess = sess_p;
573e48354ceSNicholas Bellinger 			break;
574e48354ceSNicholas Bellinger 		}
575e48354ceSNicholas Bellinger 	}
576e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
577e48354ceSNicholas Bellinger 
578e48354ceSNicholas Bellinger 	/*
579e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
580e48354ceSNicholas Bellinger 	 */
581e48354ceSNicholas Bellinger 	if (!sess) {
582e48354ceSNicholas Bellinger 		pr_err("Initiator attempting to add a connection to"
583e48354ceSNicholas Bellinger 			" a non-existent session, rejecting iSCSI Login.\n");
584e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
585e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_SESSION);
586e48354ceSNicholas Bellinger 		return -1;
587e48354ceSNicholas Bellinger 	}
588e48354ceSNicholas Bellinger 
589e48354ceSNicholas Bellinger 	/*
590e48354ceSNicholas Bellinger 	 * Stop the Time2Retain timer if this is a failed session, we restart
591e48354ceSNicholas Bellinger 	 * the timer if the login is not successful.
592e48354ceSNicholas Bellinger 	 */
593e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
594e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED)
595e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 1);
596e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
597e48354ceSNicholas Bellinger 
598e48354ceSNicholas Bellinger 	iscsi_login_set_conn_values(sess, conn, pdu->cid);
599e48354ceSNicholas Bellinger 
600e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
60160bfcf8eSAndy Grover 			conn->tpg->param_list, 0) < 0) {
602e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
603e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
604e48354ceSNicholas Bellinger 		return -1;
605e48354ceSNicholas Bellinger 	}
606e48354ceSNicholas Bellinger 
60703aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
60803aa2070SNicholas Bellinger 		iser = true;
60903aa2070SNicholas Bellinger 
61003aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
611e48354ceSNicholas Bellinger 	/*
612e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
613e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
614e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
615e48354ceSNicholas Bellinger 	 *
616e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
617e48354ceSNicholas Bellinger 	 */
61879d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
619e48354ceSNicholas Bellinger 		return -1;
620e48354ceSNicholas Bellinger 
621e48354ceSNicholas Bellinger 	return iscsi_login_disable_FIM_keys(conn->param_list, conn);
622e48354ceSNicholas Bellinger }
623e48354ceSNicholas Bellinger 
624e48354ceSNicholas Bellinger int iscsi_login_post_auth_non_zero_tsih(
625e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
626e48354ceSNicholas Bellinger 	u16 cid,
627e48354ceSNicholas Bellinger 	u32 exp_statsn)
628e48354ceSNicholas Bellinger {
629e48354ceSNicholas Bellinger 	struct iscsi_conn *conn_ptr = NULL;
630e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
631e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
632e48354ceSNicholas Bellinger 
633e48354ceSNicholas Bellinger 	/*
634e48354ceSNicholas Bellinger 	 * By following item 5 in the login table,  if we have found
635e48354ceSNicholas Bellinger 	 * an existing ISID and a valid/existing TSIH and an existing
636e48354ceSNicholas Bellinger 	 * CID we do connection reinstatement.  Currently we dont not
637e48354ceSNicholas Bellinger 	 * support it so we send back an non-zero status class to the
638e48354ceSNicholas Bellinger 	 * initiator and release the new connection.
639e48354ceSNicholas Bellinger 	 */
640e48354ceSNicholas Bellinger 	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
641ee1b1b9cSAndy Grover 	if (conn_ptr) {
642e48354ceSNicholas Bellinger 		pr_err("Connection exists with CID %hu for %s,"
643e48354ceSNicholas Bellinger 			" performing connection reinstatement.\n",
644e48354ceSNicholas Bellinger 			conn_ptr->cid, sess->sess_ops->InitiatorName);
645e48354ceSNicholas Bellinger 
646e48354ceSNicholas Bellinger 		iscsit_connection_reinstatement_rcfr(conn_ptr);
647e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_ptr);
648e48354ceSNicholas Bellinger 	}
649e48354ceSNicholas Bellinger 
650e48354ceSNicholas Bellinger 	/*
651e48354ceSNicholas Bellinger 	 * Check for any connection recovery entires containing CID.
652e48354ceSNicholas Bellinger 	 * We use the original ExpStatSN sent in the first login request
653e48354ceSNicholas Bellinger 	 * to acknowledge commands for the failed connection.
654e48354ceSNicholas Bellinger 	 *
655e48354ceSNicholas Bellinger 	 * Also note that an explict logout may have already been sent,
656e48354ceSNicholas Bellinger 	 * but the response may not be sent due to additional connection
657e48354ceSNicholas Bellinger 	 * loss.
658e48354ceSNicholas Bellinger 	 */
659e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
660e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
661e48354ceSNicholas Bellinger 				sess, cid);
662ee1b1b9cSAndy Grover 		if (cr) {
663e48354ceSNicholas Bellinger 			pr_debug("Performing implicit logout"
664e48354ceSNicholas Bellinger 				" for connection recovery on CID: %hu\n",
665e48354ceSNicholas Bellinger 					conn->cid);
666e48354ceSNicholas Bellinger 			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
667e48354ceSNicholas Bellinger 		}
668e48354ceSNicholas Bellinger 	}
669e48354ceSNicholas Bellinger 
670e48354ceSNicholas Bellinger 	/*
671e48354ceSNicholas Bellinger 	 * Else we follow item 4 from the login table in that we have
672e48354ceSNicholas Bellinger 	 * found an existing ISID and a valid/existing TSIH and a new
673e48354ceSNicholas Bellinger 	 * CID we go ahead and continue to add a new connection to the
674e48354ceSNicholas Bellinger 	 * session.
675e48354ceSNicholas Bellinger 	 */
676e48354ceSNicholas Bellinger 	pr_debug("Adding CID %hu to existing session for %s.\n",
677e48354ceSNicholas Bellinger 			cid, sess->sess_ops->InitiatorName);
678e48354ceSNicholas Bellinger 
679e48354ceSNicholas Bellinger 	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
680e48354ceSNicholas Bellinger 		pr_err("Adding additional connection to this session"
681e48354ceSNicholas Bellinger 			" would exceed MaxConnections %d, login failed.\n",
682e48354ceSNicholas Bellinger 				sess->sess_ops->MaxConnections);
683e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
684e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_ISID_ERROR);
685e48354ceSNicholas Bellinger 		return -1;
686e48354ceSNicholas Bellinger 	}
687e48354ceSNicholas Bellinger 
688e48354ceSNicholas Bellinger 	return 0;
689e48354ceSNicholas Bellinger }
690e48354ceSNicholas Bellinger 
691e48354ceSNicholas Bellinger static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
692e48354ceSNicholas Bellinger {
693e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
69403aa2070SNicholas Bellinger 	/*
69503aa2070SNicholas Bellinger 	 * FIXME: Unsolicitied NopIN support for ISER
69603aa2070SNicholas Bellinger 	 */
69703aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
69803aa2070SNicholas Bellinger 		return;
699e48354ceSNicholas Bellinger 
700e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType)
701e48354ceSNicholas Bellinger 		iscsit_start_nopin_timer(conn);
702e48354ceSNicholas Bellinger }
703e48354ceSNicholas Bellinger 
704a91eb7d9SNicholas Bellinger int iscsi_post_login_handler(
705e48354ceSNicholas Bellinger 	struct iscsi_np *np,
706e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
707e48354ceSNicholas Bellinger 	u8 zero_tsih)
708e48354ceSNicholas Bellinger {
709e48354ceSNicholas Bellinger 	int stop_timer = 0;
710e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
711e48354ceSNicholas Bellinger 	struct se_session *se_sess = sess->se_sess;
71260bfcf8eSAndy Grover 	struct iscsi_portal_group *tpg = sess->tpg;
713e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
714e48354ceSNicholas Bellinger 	struct iscsi_thread_set *ts;
715e48354ceSNicholas Bellinger 
716e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
717e48354ceSNicholas Bellinger 
718e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
719e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_ACCEPT);
720e48354ceSNicholas Bellinger 
721e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
722e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
723e48354ceSNicholas Bellinger 
724e48354ceSNicholas Bellinger 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
725e48354ceSNicholas Bellinger 	iscsit_set_sync_and_steering_values(conn);
726e48354ceSNicholas Bellinger 	/*
727e48354ceSNicholas Bellinger 	 * SCSI Initiator -> SCSI Target Port Mapping
728e48354ceSNicholas Bellinger 	 */
729e48354ceSNicholas Bellinger 	ts = iscsi_get_thread_set();
730e48354ceSNicholas Bellinger 	if (!zero_tsih) {
731e48354ceSNicholas Bellinger 		iscsi_set_session_parameters(sess->sess_ops,
732e48354ceSNicholas Bellinger 				conn->param_list, 0);
733e48354ceSNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
734e48354ceSNicholas Bellinger 		conn->param_list = NULL;
735e48354ceSNicholas Bellinger 
736e48354ceSNicholas Bellinger 		spin_lock_bh(&sess->conn_lock);
737e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 0);
738e48354ceSNicholas Bellinger 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
739e48354ceSNicholas Bellinger 			pr_debug("Moving to"
740e48354ceSNicholas Bellinger 					" TARG_SESS_STATE_LOGGED_IN.\n");
741e48354ceSNicholas Bellinger 			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
742e48354ceSNicholas Bellinger 			stop_timer = 1;
743e48354ceSNicholas Bellinger 		}
744e48354ceSNicholas Bellinger 
745e48354ceSNicholas Bellinger 		pr_debug("iSCSI Login successful on CID: %hu from %s to"
7462f9bc894SNicholas Bellinger 			" %s:%hu,%hu\n", conn->cid, conn->login_ip,
7472f9bc894SNicholas Bellinger 			conn->local_ip, conn->local_port, tpg->tpgt);
748e48354ceSNicholas Bellinger 
749e48354ceSNicholas Bellinger 		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
750e48354ceSNicholas Bellinger 		atomic_inc(&sess->nconn);
751e48354ceSNicholas Bellinger 		pr_debug("Incremented iSCSI Connection count to %hu"
752e48354ceSNicholas Bellinger 			" from node: %s\n", atomic_read(&sess->nconn),
753e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
754e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
755e48354ceSNicholas Bellinger 
756e48354ceSNicholas Bellinger 		iscsi_post_login_start_timers(conn);
757baa4d64bSNicholas Bellinger 
758e48354ceSNicholas Bellinger 		iscsi_activate_thread_set(conn, ts);
759e48354ceSNicholas Bellinger 		/*
760e48354ceSNicholas Bellinger 		 * Determine CPU mask to ensure connection's RX and TX kthreads
761e48354ceSNicholas Bellinger 		 * are scheduled on the same CPU.
762e48354ceSNicholas Bellinger 		 */
763e48354ceSNicholas Bellinger 		iscsit_thread_get_cpumask(conn);
764e48354ceSNicholas Bellinger 		conn->conn_rx_reset_cpumask = 1;
765e48354ceSNicholas Bellinger 		conn->conn_tx_reset_cpumask = 1;
766e48354ceSNicholas Bellinger 
767e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
768e48354ceSNicholas Bellinger 		if (stop_timer) {
769e48354ceSNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
770e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess);
771e48354ceSNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
772e48354ceSNicholas Bellinger 		}
773e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
774e48354ceSNicholas Bellinger 		return 0;
775e48354ceSNicholas Bellinger 	}
776e48354ceSNicholas Bellinger 
777e48354ceSNicholas Bellinger 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
778e48354ceSNicholas Bellinger 	iscsi_release_param_list(conn->param_list);
779e48354ceSNicholas Bellinger 	conn->param_list = NULL;
780e48354ceSNicholas Bellinger 
781e48354ceSNicholas Bellinger 	iscsit_determine_maxcmdsn(sess);
782e48354ceSNicholas Bellinger 
783e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
784e48354ceSNicholas Bellinger 	__transport_register_session(&sess->tpg->tpg_se_tpg,
7858359cf43SJörn Engel 			se_sess->se_node_acl, se_sess, sess);
786e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
787e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
788e48354ceSNicholas Bellinger 
789e48354ceSNicholas Bellinger 	pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
7902f9bc894SNicholas Bellinger 		conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
7912f9bc894SNicholas Bellinger 		tpg->tpgt);
792e48354ceSNicholas Bellinger 
793e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
794e48354ceSNicholas Bellinger 	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
795e48354ceSNicholas Bellinger 	atomic_inc(&sess->nconn);
796e48354ceSNicholas Bellinger 	pr_debug("Incremented iSCSI Connection count to %hu from node:"
797e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
798e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
799e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
800e48354ceSNicholas Bellinger 
801e48354ceSNicholas Bellinger 	sess->sid = tpg->sid++;
802e48354ceSNicholas Bellinger 	if (!sess->sid)
803e48354ceSNicholas Bellinger 		sess->sid = tpg->sid++;
804e48354ceSNicholas Bellinger 	pr_debug("Established iSCSI session from node: %s\n",
805e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
806e48354ceSNicholas Bellinger 
807e48354ceSNicholas Bellinger 	tpg->nsessions++;
808e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
809e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions++;
810e48354ceSNicholas Bellinger 
811e48354ceSNicholas Bellinger 	pr_debug("Incremented number of active iSCSI sessions to %u on"
812e48354ceSNicholas Bellinger 		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
813e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
814e48354ceSNicholas Bellinger 
815e48354ceSNicholas Bellinger 	iscsi_post_login_start_timers(conn);
816e48354ceSNicholas Bellinger 	iscsi_activate_thread_set(conn, ts);
817e48354ceSNicholas Bellinger 	/*
818e48354ceSNicholas Bellinger 	 * Determine CPU mask to ensure connection's RX and TX kthreads
819e48354ceSNicholas Bellinger 	 * are scheduled on the same CPU.
820e48354ceSNicholas Bellinger 	 */
821e48354ceSNicholas Bellinger 	iscsit_thread_get_cpumask(conn);
822e48354ceSNicholas Bellinger 	conn->conn_rx_reset_cpumask = 1;
823e48354ceSNicholas Bellinger 	conn->conn_tx_reset_cpumask = 1;
824e48354ceSNicholas Bellinger 
825e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
826e48354ceSNicholas Bellinger 
827e48354ceSNicholas Bellinger 	return 0;
828e48354ceSNicholas Bellinger }
829e48354ceSNicholas Bellinger 
830e48354ceSNicholas Bellinger static void iscsi_handle_login_thread_timeout(unsigned long data)
831e48354ceSNicholas Bellinger {
832e48354ceSNicholas Bellinger 	struct iscsi_np *np = (struct iscsi_np *) data;
833e48354ceSNicholas Bellinger 
834e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
835e48354ceSNicholas Bellinger 	pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
836e48354ceSNicholas Bellinger 			np->np_ip, np->np_port);
837e48354ceSNicholas Bellinger 
838e48354ceSNicholas Bellinger 	if (np->np_login_timer_flags & ISCSI_TF_STOP) {
839e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
840e48354ceSNicholas Bellinger 		return;
841e48354ceSNicholas Bellinger 	}
842e48354ceSNicholas Bellinger 
843e48354ceSNicholas Bellinger 	if (np->np_thread)
844e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
845e48354ceSNicholas Bellinger 
846e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
847e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
848e48354ceSNicholas Bellinger }
849e48354ceSNicholas Bellinger 
850e48354ceSNicholas Bellinger static void iscsi_start_login_thread_timer(struct iscsi_np *np)
851e48354ceSNicholas Bellinger {
852e48354ceSNicholas Bellinger 	/*
853e48354ceSNicholas Bellinger 	 * This used the TA_LOGIN_TIMEOUT constant because at this
854e48354ceSNicholas Bellinger 	 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
855e48354ceSNicholas Bellinger 	 */
856e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
857e48354ceSNicholas Bellinger 	init_timer(&np->np_login_timer);
858e48354ceSNicholas Bellinger 	np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
859e48354ceSNicholas Bellinger 	np->np_login_timer.data = (unsigned long)np;
860e48354ceSNicholas Bellinger 	np->np_login_timer.function = iscsi_handle_login_thread_timeout;
861e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_STOP;
862e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_RUNNING;
863e48354ceSNicholas Bellinger 	add_timer(&np->np_login_timer);
864e48354ceSNicholas Bellinger 
865e48354ceSNicholas Bellinger 	pr_debug("Added timeout timer to iSCSI login request for"
866e48354ceSNicholas Bellinger 			" %u seconds.\n", TA_LOGIN_TIMEOUT);
867e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
868e48354ceSNicholas Bellinger }
869e48354ceSNicholas Bellinger 
870e48354ceSNicholas Bellinger static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
871e48354ceSNicholas Bellinger {
872e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
873e48354ceSNicholas Bellinger 	if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
874e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
875e48354ceSNicholas Bellinger 		return;
876e48354ceSNicholas Bellinger 	}
877e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_STOP;
878e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
879e48354ceSNicholas Bellinger 
880e48354ceSNicholas Bellinger 	del_timer_sync(&np->np_login_timer);
881e48354ceSNicholas Bellinger 
882e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
883e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
884e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
885e48354ceSNicholas Bellinger }
886e48354ceSNicholas Bellinger 
887baa4d64bSNicholas Bellinger int iscsit_setup_np(
888e48354ceSNicholas Bellinger 	struct iscsi_np *np,
889e48354ceSNicholas Bellinger 	struct __kernel_sockaddr_storage *sockaddr)
890e48354ceSNicholas Bellinger {
891baa4d64bSNicholas Bellinger 	struct socket *sock = NULL;
892837f6452SNicholas Bellinger 	int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
893e48354ceSNicholas Bellinger 
894e48354ceSNicholas Bellinger 	switch (np->np_network_transport) {
895e48354ceSNicholas Bellinger 	case ISCSI_TCP:
896e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_TCP;
897e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
898e48354ceSNicholas Bellinger 		break;
899e48354ceSNicholas Bellinger 	case ISCSI_SCTP_TCP:
900e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
901e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
902e48354ceSNicholas Bellinger 		break;
903e48354ceSNicholas Bellinger 	case ISCSI_SCTP_UDP:
904e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
905e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_SEQPACKET;
906e48354ceSNicholas Bellinger 		break;
907e48354ceSNicholas Bellinger 	default:
908e48354ceSNicholas Bellinger 		pr_err("Unsupported network_transport: %d\n",
909e48354ceSNicholas Bellinger 				np->np_network_transport);
910e48354ceSNicholas Bellinger 		return -EINVAL;
911e48354ceSNicholas Bellinger 	}
912e48354ceSNicholas Bellinger 
913baa4d64bSNicholas Bellinger 	np->np_ip_proto = IPPROTO_TCP;
914baa4d64bSNicholas Bellinger 	np->np_sock_type = SOCK_STREAM;
915baa4d64bSNicholas Bellinger 
916e48354ceSNicholas Bellinger 	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
917e48354ceSNicholas Bellinger 			np->np_ip_proto, &sock);
918e48354ceSNicholas Bellinger 	if (ret < 0) {
919e48354ceSNicholas Bellinger 		pr_err("sock_create() failed.\n");
920e48354ceSNicholas Bellinger 		return ret;
921e48354ceSNicholas Bellinger 	}
922e48354ceSNicholas Bellinger 	np->np_socket = sock;
923e48354ceSNicholas Bellinger 	/*
924e48354ceSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
925e48354ceSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
926e48354ceSNicholas Bellinger 	 */
9278359cf43SJörn Engel 	memcpy(&np->np_sockaddr, sockaddr,
928e48354ceSNicholas Bellinger 			sizeof(struct __kernel_sockaddr_storage));
929e48354ceSNicholas Bellinger 
930e48354ceSNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6)
931e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in6);
932e48354ceSNicholas Bellinger 	else
933e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in);
934e48354ceSNicholas Bellinger 	/*
935e48354ceSNicholas Bellinger 	 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
936e48354ceSNicholas Bellinger 	 */
9378359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
938e48354ceSNicholas Bellinger 	opt = 1;
939e48354ceSNicholas Bellinger 	if (np->np_network_transport == ISCSI_TCP) {
940e48354ceSNicholas Bellinger 		ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
941e48354ceSNicholas Bellinger 				(char *)&opt, sizeof(opt));
942e48354ceSNicholas Bellinger 		if (ret < 0) {
943e48354ceSNicholas Bellinger 			pr_err("kernel_setsockopt() for TCP_NODELAY"
944e48354ceSNicholas Bellinger 				" failed: %d\n", ret);
945e48354ceSNicholas Bellinger 			goto fail;
946e48354ceSNicholas Bellinger 		}
947e48354ceSNicholas Bellinger 	}
948e48354ceSNicholas Bellinger 
9498359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
950e48354ceSNicholas Bellinger 	ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
951e48354ceSNicholas Bellinger 			(char *)&opt, sizeof(opt));
952e48354ceSNicholas Bellinger 	if (ret < 0) {
953e48354ceSNicholas Bellinger 		pr_err("kernel_setsockopt() for SO_REUSEADDR"
954e48354ceSNicholas Bellinger 			" failed\n");
955e48354ceSNicholas Bellinger 		goto fail;
956e48354ceSNicholas Bellinger 	}
957e48354ceSNicholas Bellinger 
9589f9ef6d3SDax Kelson 	ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
9599f9ef6d3SDax Kelson 			(char *)&opt, sizeof(opt));
9609f9ef6d3SDax Kelson 	if (ret < 0) {
9619f9ef6d3SDax Kelson 		pr_err("kernel_setsockopt() for IP_FREEBIND"
9629f9ef6d3SDax Kelson 			" failed\n");
9639f9ef6d3SDax Kelson 		goto fail;
9649f9ef6d3SDax Kelson 	}
9659f9ef6d3SDax Kelson 
966e48354ceSNicholas Bellinger 	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
967e48354ceSNicholas Bellinger 	if (ret < 0) {
968e48354ceSNicholas Bellinger 		pr_err("kernel_bind() failed: %d\n", ret);
969e48354ceSNicholas Bellinger 		goto fail;
970e48354ceSNicholas Bellinger 	}
971e48354ceSNicholas Bellinger 
972e48354ceSNicholas Bellinger 	ret = kernel_listen(sock, backlog);
973e48354ceSNicholas Bellinger 	if (ret != 0) {
974e48354ceSNicholas Bellinger 		pr_err("kernel_listen() failed: %d\n", ret);
975e48354ceSNicholas Bellinger 		goto fail;
976e48354ceSNicholas Bellinger 	}
977e48354ceSNicholas Bellinger 
978e48354ceSNicholas Bellinger 	return 0;
979e48354ceSNicholas Bellinger fail:
980e48354ceSNicholas Bellinger 	np->np_socket = NULL;
981e48354ceSNicholas Bellinger 	sock_release(sock);
982e48354ceSNicholas Bellinger 	return ret;
983e48354ceSNicholas Bellinger }
984e48354ceSNicholas Bellinger 
985baa4d64bSNicholas Bellinger int iscsi_target_setup_login_socket(
986baa4d64bSNicholas Bellinger 	struct iscsi_np *np,
987baa4d64bSNicholas Bellinger 	struct __kernel_sockaddr_storage *sockaddr)
988baa4d64bSNicholas Bellinger {
989baa4d64bSNicholas Bellinger 	struct iscsit_transport *t;
990baa4d64bSNicholas Bellinger 	int rc;
991baa4d64bSNicholas Bellinger 
992baa4d64bSNicholas Bellinger 	t = iscsit_get_transport(np->np_network_transport);
993baa4d64bSNicholas Bellinger 	if (!t)
994baa4d64bSNicholas Bellinger 		return -EINVAL;
995baa4d64bSNicholas Bellinger 
996baa4d64bSNicholas Bellinger 	rc = t->iscsit_setup_np(np, sockaddr);
997baa4d64bSNicholas Bellinger 	if (rc < 0) {
998baa4d64bSNicholas Bellinger 		iscsit_put_transport(t);
999baa4d64bSNicholas Bellinger 		return rc;
1000baa4d64bSNicholas Bellinger 	}
1001baa4d64bSNicholas Bellinger 
1002baa4d64bSNicholas Bellinger 	np->np_transport = t;
100314f4b54fSSagi Grimberg 	np->enabled = true;
1004baa4d64bSNicholas Bellinger 	return 0;
1005baa4d64bSNicholas Bellinger }
1006baa4d64bSNicholas Bellinger 
1007baa4d64bSNicholas Bellinger int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1008baa4d64bSNicholas Bellinger {
1009baa4d64bSNicholas Bellinger 	struct socket *new_sock, *sock = np->np_socket;
1010baa4d64bSNicholas Bellinger 	struct sockaddr_in sock_in;
1011baa4d64bSNicholas Bellinger 	struct sockaddr_in6 sock_in6;
1012baa4d64bSNicholas Bellinger 	int rc, err;
1013baa4d64bSNicholas Bellinger 
1014baa4d64bSNicholas Bellinger 	rc = kernel_accept(sock, &new_sock, 0);
1015baa4d64bSNicholas Bellinger 	if (rc < 0)
1016baa4d64bSNicholas Bellinger 		return rc;
1017baa4d64bSNicholas Bellinger 
1018baa4d64bSNicholas Bellinger 	conn->sock = new_sock;
1019baa4d64bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
1020baa4d64bSNicholas Bellinger 
1021baa4d64bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
1022baa4d64bSNicholas Bellinger 		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1023baa4d64bSNicholas Bellinger 
1024baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1025baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in6, &err, 1);
1026baa4d64bSNicholas Bellinger 		if (!rc) {
1027dfecf611SChris Leech 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1028dfecf611SChris Leech 				snprintf(conn->login_ip, sizeof(conn->login_ip), "[%pI6c]",
1029baa4d64bSNicholas Bellinger 					&sock_in6.sin6_addr.in6_u);
1030dfecf611SChris Leech 			else
1031dfecf611SChris Leech 				snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI4",
1032dfecf611SChris Leech 					&sock_in6.sin6_addr.s6_addr32[3]);
1033baa4d64bSNicholas Bellinger 			conn->login_port = ntohs(sock_in6.sin6_port);
1034baa4d64bSNicholas Bellinger 		}
1035baa4d64bSNicholas Bellinger 
1036baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1037baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in6, &err, 0);
1038baa4d64bSNicholas Bellinger 		if (!rc) {
1039dfecf611SChris Leech 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr))
1040dfecf611SChris Leech 				snprintf(conn->local_ip, sizeof(conn->local_ip), "[%pI6c]",
1041baa4d64bSNicholas Bellinger 					&sock_in6.sin6_addr.in6_u);
1042dfecf611SChris Leech 			else
1043dfecf611SChris Leech 				snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI4",
1044dfecf611SChris Leech 					&sock_in6.sin6_addr.s6_addr32[3]);
1045baa4d64bSNicholas Bellinger 			conn->local_port = ntohs(sock_in6.sin6_port);
1046baa4d64bSNicholas Bellinger 		}
1047baa4d64bSNicholas Bellinger 	} else {
1048baa4d64bSNicholas Bellinger 		memset(&sock_in, 0, sizeof(struct sockaddr_in));
1049baa4d64bSNicholas Bellinger 
1050baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1051baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in, &err, 1);
1052baa4d64bSNicholas Bellinger 		if (!rc) {
1053baa4d64bSNicholas Bellinger 			sprintf(conn->login_ip, "%pI4",
1054baa4d64bSNicholas Bellinger 					&sock_in.sin_addr.s_addr);
1055baa4d64bSNicholas Bellinger 			conn->login_port = ntohs(sock_in.sin_port);
1056baa4d64bSNicholas Bellinger 		}
1057baa4d64bSNicholas Bellinger 
1058baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
1059baa4d64bSNicholas Bellinger 				(struct sockaddr *)&sock_in, &err, 0);
1060baa4d64bSNicholas Bellinger 		if (!rc) {
1061baa4d64bSNicholas Bellinger 			sprintf(conn->local_ip, "%pI4",
1062baa4d64bSNicholas Bellinger 					&sock_in.sin_addr.s_addr);
1063baa4d64bSNicholas Bellinger 			conn->local_port = ntohs(sock_in.sin_port);
1064baa4d64bSNicholas Bellinger 		}
1065baa4d64bSNicholas Bellinger 	}
1066baa4d64bSNicholas Bellinger 
1067baa4d64bSNicholas Bellinger 	return 0;
1068baa4d64bSNicholas Bellinger }
1069baa4d64bSNicholas Bellinger 
1070baa4d64bSNicholas Bellinger int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1071baa4d64bSNicholas Bellinger {
1072baa4d64bSNicholas Bellinger 	struct iscsi_login_req *login_req;
1073baa4d64bSNicholas Bellinger 	u32 padding = 0, payload_length;
1074baa4d64bSNicholas Bellinger 
1075baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1076baa4d64bSNicholas Bellinger 		return -1;
1077baa4d64bSNicholas Bellinger 
1078baa4d64bSNicholas Bellinger 	login_req = (struct iscsi_login_req *)login->req;
1079baa4d64bSNicholas Bellinger 	payload_length	= ntoh24(login_req->dlength);
1080baa4d64bSNicholas Bellinger 	padding = ((-payload_length) & 3);
1081baa4d64bSNicholas Bellinger 
1082baa4d64bSNicholas Bellinger 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1083baa4d64bSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1084baa4d64bSNicholas Bellinger 		login_req->flags, login_req->itt, login_req->cmdsn,
1085baa4d64bSNicholas Bellinger 		login_req->exp_statsn, login_req->cid, payload_length);
1086baa4d64bSNicholas Bellinger 	/*
1087baa4d64bSNicholas Bellinger 	 * Setup the initial iscsi_login values from the leading
1088baa4d64bSNicholas Bellinger 	 * login request PDU.
1089baa4d64bSNicholas Bellinger 	 */
1090baa4d64bSNicholas Bellinger 	if (login->first_request) {
1091baa4d64bSNicholas Bellinger 		login_req = (struct iscsi_login_req *)login->req;
1092baa4d64bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
10933e1c81a9SNicholas Bellinger 		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1094baa4d64bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1095baa4d64bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1096baa4d64bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1097baa4d64bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1098baa4d64bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1099baa4d64bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1100baa4d64bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1101baa4d64bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1102baa4d64bSNicholas Bellinger 	}
1103baa4d64bSNicholas Bellinger 
1104baa4d64bSNicholas Bellinger 	if (iscsi_target_check_login_request(conn, login) < 0)
1105baa4d64bSNicholas Bellinger 		return -1;
1106baa4d64bSNicholas Bellinger 
1107baa4d64bSNicholas Bellinger 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1108baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req_buf,
1109baa4d64bSNicholas Bellinger 				payload_length + padding) < 0)
1110baa4d64bSNicholas Bellinger 		return -1;
1111baa4d64bSNicholas Bellinger 
1112baa4d64bSNicholas Bellinger 	return 0;
1113baa4d64bSNicholas Bellinger }
1114baa4d64bSNicholas Bellinger 
1115baa4d64bSNicholas Bellinger int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1116baa4d64bSNicholas Bellinger 			u32 length)
1117baa4d64bSNicholas Bellinger {
1118baa4d64bSNicholas Bellinger 	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1119baa4d64bSNicholas Bellinger 		return -1;
1120baa4d64bSNicholas Bellinger 
1121baa4d64bSNicholas Bellinger 	return 0;
1122baa4d64bSNicholas Bellinger }
1123baa4d64bSNicholas Bellinger 
1124baa4d64bSNicholas Bellinger static int
1125baa4d64bSNicholas Bellinger iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1126baa4d64bSNicholas Bellinger {
1127baa4d64bSNicholas Bellinger 	int rc;
1128baa4d64bSNicholas Bellinger 
1129baa4d64bSNicholas Bellinger 	if (!t->owner) {
1130baa4d64bSNicholas Bellinger 		conn->conn_transport = t;
1131baa4d64bSNicholas Bellinger 		return 0;
1132baa4d64bSNicholas Bellinger 	}
1133baa4d64bSNicholas Bellinger 
1134baa4d64bSNicholas Bellinger 	rc = try_module_get(t->owner);
1135baa4d64bSNicholas Bellinger 	if (!rc) {
1136baa4d64bSNicholas Bellinger 		pr_err("try_module_get() failed for %s\n", t->name);
1137baa4d64bSNicholas Bellinger 		return -EINVAL;
1138baa4d64bSNicholas Bellinger 	}
1139baa4d64bSNicholas Bellinger 
1140baa4d64bSNicholas Bellinger 	conn->conn_transport = t;
1141baa4d64bSNicholas Bellinger 	return 0;
1142baa4d64bSNicholas Bellinger }
1143baa4d64bSNicholas Bellinger 
1144a91eb7d9SNicholas Bellinger void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1145a91eb7d9SNicholas Bellinger 		struct iscsi_np *np, bool zero_tsih, bool new_sess)
1146a91eb7d9SNicholas Bellinger {
11470bcc297eSChristophe Vu-Brugier 	if (!new_sess)
1148a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1149a91eb7d9SNicholas Bellinger 
1150a91eb7d9SNicholas Bellinger 	pr_err("iSCSI Login negotiation failed.\n");
1151a91eb7d9SNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1152a91eb7d9SNicholas Bellinger 				   ISCSI_LOGIN_STATUS_INIT_ERR);
1153a91eb7d9SNicholas Bellinger 	if (!zero_tsih || !conn->sess)
1154a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1155a91eb7d9SNicholas Bellinger 	if (conn->sess->se_sess)
1156a91eb7d9SNicholas Bellinger 		transport_free_session(conn->sess->se_sess);
1157a91eb7d9SNicholas Bellinger 	if (conn->sess->session_index != 0) {
1158a91eb7d9SNicholas Bellinger 		spin_lock_bh(&sess_idr_lock);
1159a91eb7d9SNicholas Bellinger 		idr_remove(&sess_idr, conn->sess->session_index);
1160a91eb7d9SNicholas Bellinger 		spin_unlock_bh(&sess_idr_lock);
1161a91eb7d9SNicholas Bellinger 	}
1162a91eb7d9SNicholas Bellinger 	kfree(conn->sess->sess_ops);
1163a91eb7d9SNicholas Bellinger 	kfree(conn->sess);
1164a91eb7d9SNicholas Bellinger 
1165a91eb7d9SNicholas Bellinger old_sess_out:
1166a91eb7d9SNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1167a91eb7d9SNicholas Bellinger 	/*
1168a91eb7d9SNicholas Bellinger 	 * If login negotiation fails check if the Time2Retain timer
1169a91eb7d9SNicholas Bellinger 	 * needs to be restarted.
1170a91eb7d9SNicholas Bellinger 	 */
1171a91eb7d9SNicholas Bellinger 	if (!zero_tsih && conn->sess) {
1172a91eb7d9SNicholas Bellinger 		spin_lock_bh(&conn->sess->conn_lock);
1173a91eb7d9SNicholas Bellinger 		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1174a91eb7d9SNicholas Bellinger 			struct se_portal_group *se_tpg =
117560bfcf8eSAndy Grover 					&conn->tpg->tpg_se_tpg;
1176a91eb7d9SNicholas Bellinger 
1177a91eb7d9SNicholas Bellinger 			atomic_set(&conn->sess->session_continuation, 0);
1178a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1179a91eb7d9SNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
1180a91eb7d9SNicholas Bellinger 			iscsit_start_time2retain_handler(conn->sess);
1181a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
1182a91eb7d9SNicholas Bellinger 		} else
1183a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1184a91eb7d9SNicholas Bellinger 		iscsit_dec_session_usage_count(conn->sess);
1185a91eb7d9SNicholas Bellinger 	}
1186a91eb7d9SNicholas Bellinger 
1187a91eb7d9SNicholas Bellinger 	if (!IS_ERR(conn->conn_rx_hash.tfm))
1188a91eb7d9SNicholas Bellinger 		crypto_free_hash(conn->conn_rx_hash.tfm);
1189a91eb7d9SNicholas Bellinger 	if (!IS_ERR(conn->conn_tx_hash.tfm))
1190a91eb7d9SNicholas Bellinger 		crypto_free_hash(conn->conn_tx_hash.tfm);
1191a91eb7d9SNicholas Bellinger 
1192a91eb7d9SNicholas Bellinger 	free_cpumask_var(conn->conn_cpumask);
1193a91eb7d9SNicholas Bellinger 
1194a91eb7d9SNicholas Bellinger 	kfree(conn->conn_ops);
1195a91eb7d9SNicholas Bellinger 
1196a91eb7d9SNicholas Bellinger 	if (conn->param_list) {
1197a91eb7d9SNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
1198a91eb7d9SNicholas Bellinger 		conn->param_list = NULL;
1199a91eb7d9SNicholas Bellinger 	}
1200a91eb7d9SNicholas Bellinger 	iscsi_target_nego_release(conn);
1201a91eb7d9SNicholas Bellinger 
1202a91eb7d9SNicholas Bellinger 	if (conn->sock) {
1203a91eb7d9SNicholas Bellinger 		sock_release(conn->sock);
1204a91eb7d9SNicholas Bellinger 		conn->sock = NULL;
1205a91eb7d9SNicholas Bellinger 	}
1206a91eb7d9SNicholas Bellinger 
1207954f2372SSagi Grimberg 	if (conn->conn_transport->iscsit_wait_conn)
1208954f2372SSagi Grimberg 		conn->conn_transport->iscsit_wait_conn(conn);
1209954f2372SSagi Grimberg 
1210a91eb7d9SNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
1211a91eb7d9SNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
1212a91eb7d9SNicholas Bellinger 
1213a91eb7d9SNicholas Bellinger 	iscsit_put_transport(conn->conn_transport);
1214a91eb7d9SNicholas Bellinger 	kfree(conn);
1215a91eb7d9SNicholas Bellinger }
1216a91eb7d9SNicholas Bellinger 
1217e48354ceSNicholas Bellinger static int __iscsi_target_login_thread(struct iscsi_np *np)
1218e48354ceSNicholas Bellinger {
1219baa4d64bSNicholas Bellinger 	u8 *buffer, zero_tsih = 0;
122081a9c5e7SMikulas Patocka 	int ret = 0, rc;
1221e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = NULL;
1222e48354ceSNicholas Bellinger 	struct iscsi_login *login;
1223e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = NULL;
1224e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu;
1225a91eb7d9SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
1226a91eb7d9SNicholas Bellinger 	bool new_sess = false;
1227e48354ceSNicholas Bellinger 
1228e48354ceSNicholas Bellinger 	flush_signals(current);
1229e48354ceSNicholas Bellinger 
1230e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1231e48354ceSNicholas Bellinger 	if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1232e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1233e48354ceSNicholas Bellinger 		complete(&np->np_restart_comp);
123481a9c5e7SMikulas Patocka 	} else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
123581a9c5e7SMikulas Patocka 		spin_unlock_bh(&np->np_thread_lock);
123681a9c5e7SMikulas Patocka 		goto exit;
1237e48354ceSNicholas Bellinger 	} else {
1238e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1239e48354ceSNicholas Bellinger 	}
1240e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1241e48354ceSNicholas Bellinger 
1242e48354ceSNicholas Bellinger 	conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1243e48354ceSNicholas Bellinger 	if (!conn) {
1244e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for"
1245e48354ceSNicholas Bellinger 			" new connection\n");
1246e48354ceSNicholas Bellinger 		/* Get another socket */
1247e48354ceSNicholas Bellinger 		return 1;
1248e48354ceSNicholas Bellinger 	}
1249e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1250e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_FREE;
1251e48354ceSNicholas Bellinger 
1252baa4d64bSNicholas Bellinger 	if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1253baa4d64bSNicholas Bellinger 		kfree(conn);
1254baa4d64bSNicholas Bellinger 		return 1;
1255baa4d64bSNicholas Bellinger 	}
1256e48354ceSNicholas Bellinger 
1257baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_accept_np(np, conn);
1258baa4d64bSNicholas Bellinger 	if (rc == -ENOSYS) {
1259baa4d64bSNicholas Bellinger 		complete(&np->np_restart_comp);
1260baa4d64bSNicholas Bellinger 		iscsit_put_transport(conn->conn_transport);
1261baa4d64bSNicholas Bellinger 		kfree(conn);
1262baa4d64bSNicholas Bellinger 		conn = NULL;
1263baa4d64bSNicholas Bellinger 		goto exit;
1264baa4d64bSNicholas Bellinger 	} else if (rc < 0) {
1265baa4d64bSNicholas Bellinger 		spin_lock_bh(&np->np_thread_lock);
1266baa4d64bSNicholas Bellinger 		if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1267baa4d64bSNicholas Bellinger 			spin_unlock_bh(&np->np_thread_lock);
1268baa4d64bSNicholas Bellinger 			complete(&np->np_restart_comp);
1269baa4d64bSNicholas Bellinger 			iscsit_put_transport(conn->conn_transport);
1270baa4d64bSNicholas Bellinger 			kfree(conn);
1271baa4d64bSNicholas Bellinger 			conn = NULL;
1272baa4d64bSNicholas Bellinger 			/* Get another socket */
1273baa4d64bSNicholas Bellinger 			return 1;
1274baa4d64bSNicholas Bellinger 		}
1275baa4d64bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1276baa4d64bSNicholas Bellinger 		iscsit_put_transport(conn->conn_transport);
1277baa4d64bSNicholas Bellinger 		kfree(conn);
1278baa4d64bSNicholas Bellinger 		conn = NULL;
1279baa4d64bSNicholas Bellinger 		goto out;
1280e48354ceSNicholas Bellinger 	}
1281e48354ceSNicholas Bellinger 	/*
1282e48354ceSNicholas Bellinger 	 * Perform the remaining iSCSI connection initialization items..
1283e48354ceSNicholas Bellinger 	 */
1284baa4d64bSNicholas Bellinger 	login = iscsi_login_init_conn(conn);
1285baa4d64bSNicholas Bellinger 	if (!login) {
1286e48354ceSNicholas Bellinger 		goto new_sess_out;
1287e48354ceSNicholas Bellinger 	}
1288e48354ceSNicholas Bellinger 
1289baa4d64bSNicholas Bellinger 	iscsi_start_login_thread_timer(np);
1290e48354ceSNicholas Bellinger 
1291baa4d64bSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1292baa4d64bSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1293baa4d64bSNicholas Bellinger 	/*
1294baa4d64bSNicholas Bellinger 	 * This will process the first login request + payload..
1295baa4d64bSNicholas Bellinger 	 */
1296baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1297baa4d64bSNicholas Bellinger 	if (rc == 1)
1298baa4d64bSNicholas Bellinger 		return 1;
1299baa4d64bSNicholas Bellinger 	else if (rc < 0)
1300baa4d64bSNicholas Bellinger 		goto new_sess_out;
1301baa4d64bSNicholas Bellinger 
1302baa4d64bSNicholas Bellinger 	buffer = &login->req[0];
1303e48354ceSNicholas Bellinger 	pdu = (struct iscsi_login_req *)buffer;
1304e48354ceSNicholas Bellinger 	/*
1305e48354ceSNicholas Bellinger 	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1306e48354ceSNicholas Bellinger 	 * when Status-Class != 0.
1307e48354ceSNicholas Bellinger 	*/
1308e48354ceSNicholas Bellinger 	conn->login_itt	= pdu->itt;
1309e48354ceSNicholas Bellinger 
1310e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1311e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1312e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1313e48354ceSNicholas Bellinger 		pr_err("iSCSI Network Portal on %s:%hu currently not"
1314e48354ceSNicholas Bellinger 			" active.\n", np->np_ip, np->np_port);
1315e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1316e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1317e48354ceSNicholas Bellinger 		goto new_sess_out;
1318e48354ceSNicholas Bellinger 	}
1319e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1320e48354ceSNicholas Bellinger 
1321e48354ceSNicholas Bellinger 	conn->network_transport = np->np_network_transport;
1322e48354ceSNicholas Bellinger 
1323e48354ceSNicholas Bellinger 	pr_debug("Received iSCSI login request from %s on %s Network"
1324baa4d64bSNicholas Bellinger 		" Portal %s:%hu\n", conn->login_ip, np->np_transport->name,
13252f9bc894SNicholas Bellinger 		conn->local_ip, conn->local_port);
1326e48354ceSNicholas Bellinger 
1327e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1328e48354ceSNicholas Bellinger 	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1329e48354ceSNicholas Bellinger 
1330e48354ceSNicholas Bellinger 	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1331e48354ceSNicholas Bellinger 			pdu->min_version) < 0)
1332e48354ceSNicholas Bellinger 		goto new_sess_out;
1333e48354ceSNicholas Bellinger 
1334e48354ceSNicholas Bellinger 	zero_tsih = (pdu->tsih == 0x0000);
1335ee1b1b9cSAndy Grover 	if (zero_tsih) {
1336e48354ceSNicholas Bellinger 		/*
1337e48354ceSNicholas Bellinger 		 * This is the leading connection of a new session.
1338e48354ceSNicholas Bellinger 		 * We wait until after authentication to check for
1339e48354ceSNicholas Bellinger 		 * session reinstatement.
1340e48354ceSNicholas Bellinger 		 */
1341e48354ceSNicholas Bellinger 		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1342e48354ceSNicholas Bellinger 			goto new_sess_out;
1343e48354ceSNicholas Bellinger 	} else {
1344e48354ceSNicholas Bellinger 		/*
1345e48354ceSNicholas Bellinger 		 * Add a new connection to an existing session.
1346e48354ceSNicholas Bellinger 		 * We check for a non-existant session in
1347e48354ceSNicholas Bellinger 		 * iscsi_login_non_zero_tsih_s2() below based
1348e48354ceSNicholas Bellinger 		 * on ISID/TSIH, but wait until after authentication
1349e48354ceSNicholas Bellinger 		 * to check for connection reinstatement, etc.
1350e48354ceSNicholas Bellinger 		 */
1351e48354ceSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1352e48354ceSNicholas Bellinger 			goto new_sess_out;
1353e48354ceSNicholas Bellinger 	}
1354e48354ceSNicholas Bellinger 	/*
1355baa4d64bSNicholas Bellinger 	 * SessionType: Discovery
1356baa4d64bSNicholas Bellinger 	 *
1357baa4d64bSNicholas Bellinger 	 * 	Locates Default Portal
1358baa4d64bSNicholas Bellinger 	 *
1359baa4d64bSNicholas Bellinger 	 * SessionType: Normal
1360baa4d64bSNicholas Bellinger 	 *
1361baa4d64bSNicholas Bellinger 	 * 	Locates Target Portal from NP -> Target IQN
1362e48354ceSNicholas Bellinger 	 */
1363baa4d64bSNicholas Bellinger 	rc = iscsi_target_locate_portal(np, conn, login);
1364baa4d64bSNicholas Bellinger 	if (rc < 0) {
1365e48354ceSNicholas Bellinger 		tpg = conn->tpg;
1366e48354ceSNicholas Bellinger 		goto new_sess_out;
1367e48354ceSNicholas Bellinger 	}
1368a91eb7d9SNicholas Bellinger 	login->zero_tsih = zero_tsih;
1369e48354ceSNicholas Bellinger 
1370e48354ceSNicholas Bellinger 	tpg = conn->tpg;
1371e48354ceSNicholas Bellinger 	if (!tpg) {
1372e48354ceSNicholas Bellinger 		pr_err("Unable to locate struct iscsi_conn->tpg\n");
1373e48354ceSNicholas Bellinger 		goto new_sess_out;
1374e48354ceSNicholas Bellinger 	}
1375e48354ceSNicholas Bellinger 
1376e48354ceSNicholas Bellinger 	if (zero_tsih) {
1377baa4d64bSNicholas Bellinger 		if (iscsi_login_zero_tsih_s2(conn) < 0)
1378e48354ceSNicholas Bellinger 			goto new_sess_out;
1379e48354ceSNicholas Bellinger 	} else {
1380baa4d64bSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1381e48354ceSNicholas Bellinger 			goto old_sess_out;
1382e48354ceSNicholas Bellinger 	}
1383e48354ceSNicholas Bellinger 
1384a91eb7d9SNicholas Bellinger 	ret = iscsi_target_start_negotiation(login, conn);
1385a91eb7d9SNicholas Bellinger 	if (ret < 0)
1386e48354ceSNicholas Bellinger 		goto new_sess_out;
1387e48354ceSNicholas Bellinger 
1388e48354ceSNicholas Bellinger 	if (!conn->sess) {
1389e48354ceSNicholas Bellinger 		pr_err("struct iscsi_conn session pointer is NULL!\n");
1390e48354ceSNicholas Bellinger 		goto new_sess_out;
1391e48354ceSNicholas Bellinger 	}
1392e48354ceSNicholas Bellinger 
1393e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1394e48354ceSNicholas Bellinger 
1395e48354ceSNicholas Bellinger 	if (signal_pending(current))
1396e48354ceSNicholas Bellinger 		goto new_sess_out;
1397e48354ceSNicholas Bellinger 
1398a91eb7d9SNicholas Bellinger 	if (ret == 1) {
1399a91eb7d9SNicholas Bellinger 		tpg_np = conn->tpg_np;
1400e48354ceSNicholas Bellinger 
1401a91eb7d9SNicholas Bellinger 		ret = iscsi_post_login_handler(np, conn, zero_tsih);
1402e48354ceSNicholas Bellinger 		if (ret < 0)
1403e48354ceSNicholas Bellinger 			goto new_sess_out;
1404e48354ceSNicholas Bellinger 
1405a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1406a91eb7d9SNicholas Bellinger 	}
1407a91eb7d9SNicholas Bellinger 
1408e48354ceSNicholas Bellinger 	tpg = NULL;
1409a91eb7d9SNicholas Bellinger 	tpg_np = NULL;
1410e48354ceSNicholas Bellinger 	/* Get another socket */
1411e48354ceSNicholas Bellinger 	return 1;
1412e48354ceSNicholas Bellinger 
1413e48354ceSNicholas Bellinger new_sess_out:
1414a91eb7d9SNicholas Bellinger 	new_sess = true;
1415e48354ceSNicholas Bellinger old_sess_out:
1416a91eb7d9SNicholas Bellinger 	tpg_np = conn->tpg_np;
1417a91eb7d9SNicholas Bellinger 	iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1418a91eb7d9SNicholas Bellinger 	new_sess = false;
1419e48354ceSNicholas Bellinger 
1420e48354ceSNicholas Bellinger 	if (tpg) {
1421a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1422e48354ceSNicholas Bellinger 		tpg = NULL;
1423a91eb7d9SNicholas Bellinger 		tpg_np = NULL;
1424e48354ceSNicholas Bellinger 	}
1425e48354ceSNicholas Bellinger 
1426e48354ceSNicholas Bellinger out:
1427e48354ceSNicholas Bellinger 	return 1;
142881a9c5e7SMikulas Patocka 
1429baa4d64bSNicholas Bellinger exit:
1430e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1431e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1432e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1433e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1434baa4d64bSNicholas Bellinger 
1435e48354ceSNicholas Bellinger 	return 0;
1436e48354ceSNicholas Bellinger }
1437e48354ceSNicholas Bellinger 
1438e48354ceSNicholas Bellinger int iscsi_target_login_thread(void *arg)
1439e48354ceSNicholas Bellinger {
14408359cf43SJörn Engel 	struct iscsi_np *np = arg;
1441e48354ceSNicholas Bellinger 	int ret;
1442e48354ceSNicholas Bellinger 
1443e48354ceSNicholas Bellinger 	allow_signal(SIGINT);
1444e48354ceSNicholas Bellinger 
144581a9c5e7SMikulas Patocka 	while (1) {
1446e48354ceSNicholas Bellinger 		ret = __iscsi_target_login_thread(np);
1447e48354ceSNicholas Bellinger 		/*
1448e48354ceSNicholas Bellinger 		 * We break and exit here unless another sock_accept() call
1449e48354ceSNicholas Bellinger 		 * is expected.
1450e48354ceSNicholas Bellinger 		 */
1451e48354ceSNicholas Bellinger 		if (ret != 1)
1452e48354ceSNicholas Bellinger 			break;
1453e48354ceSNicholas Bellinger 	}
1454e48354ceSNicholas Bellinger 
1455e48354ceSNicholas Bellinger 	return 0;
1456e48354ceSNicholas Bellinger }
1457