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 
1969110e3cSHerbert Xu #include <crypto/hash.h>
20da5df620SRussell King #include <linux/module.h>
21e48354ceSNicholas Bellinger #include <linux/string.h>
22e48354ceSNicholas Bellinger #include <linux/kthread.h>
233f07c014SIngo Molnar #include <linux/sched/signal.h>
2440401530SAl Viro #include <linux/idr.h>
258dcf07beSBart Van Assche #include <linux/tcp.h>        /* TCP_NODELAY */
268dcf07beSBart Van Assche #include <net/ipv6.h>         /* ipv6_addr_v4mapped() */
27e48354ceSNicholas Bellinger #include <scsi/iscsi_proto.h>
28e48354ceSNicholas Bellinger #include <target/target_core_base.h>
29c4795fb2SChristoph Hellwig #include <target/target_core_fabric.h>
30e48354ceSNicholas Bellinger 
3167f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_core.h>
3267f091f2SSagi Grimberg #include <target/iscsi/iscsi_target_stat.h>
33e48354ceSNicholas Bellinger #include "iscsi_target_device.h"
34e48354ceSNicholas Bellinger #include "iscsi_target_nego.h"
35e48354ceSNicholas Bellinger #include "iscsi_target_erl0.h"
36e48354ceSNicholas Bellinger #include "iscsi_target_erl2.h"
37e48354ceSNicholas Bellinger #include "iscsi_target_login.h"
38e48354ceSNicholas Bellinger #include "iscsi_target_tpg.h"
39e48354ceSNicholas Bellinger #include "iscsi_target_util.h"
40e48354ceSNicholas Bellinger #include "iscsi_target.h"
41e48354ceSNicholas Bellinger #include "iscsi_target_parameters.h"
42e48354ceSNicholas Bellinger 
43baa4d64bSNicholas Bellinger #include <target/iscsi/iscsi_transport.h>
44baa4d64bSNicholas Bellinger 
45baa4d64bSNicholas Bellinger static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
46e48354ceSNicholas Bellinger {
47baa4d64bSNicholas Bellinger 	struct iscsi_login *login;
48baa4d64bSNicholas Bellinger 
49baa4d64bSNicholas Bellinger 	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
50baa4d64bSNicholas Bellinger 	if (!login) {
51baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for struct iscsi_login.\n");
52baa4d64bSNicholas Bellinger 		return NULL;
53baa4d64bSNicholas Bellinger 	}
54a91eb7d9SNicholas Bellinger 	conn->login = login;
55baa4d64bSNicholas Bellinger 	login->conn = conn;
56baa4d64bSNicholas Bellinger 	login->first_request = 1;
57baa4d64bSNicholas Bellinger 
58baa4d64bSNicholas Bellinger 	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
59baa4d64bSNicholas Bellinger 	if (!login->req_buf) {
60baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for response buffer.\n");
61baa4d64bSNicholas Bellinger 		goto out_login;
62baa4d64bSNicholas Bellinger 	}
63baa4d64bSNicholas Bellinger 
64baa4d64bSNicholas Bellinger 	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
65baa4d64bSNicholas Bellinger 	if (!login->rsp_buf) {
66baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for request buffer.\n");
67baa4d64bSNicholas Bellinger 		goto out_req_buf;
68baa4d64bSNicholas Bellinger 	}
69baa4d64bSNicholas Bellinger 
70baa4d64bSNicholas Bellinger 	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
71baa4d64bSNicholas Bellinger 	if (!conn->conn_ops) {
72baa4d64bSNicholas Bellinger 		pr_err("Unable to allocate memory for"
73baa4d64bSNicholas Bellinger 			" struct iscsi_conn_ops.\n");
74baa4d64bSNicholas Bellinger 		goto out_rsp_buf;
75baa4d64bSNicholas Bellinger 	}
76baa4d64bSNicholas Bellinger 
77d5627acbSRoland Dreier 	init_waitqueue_head(&conn->queues_wq);
78e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_list);
79e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->conn_cmd_list);
80e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->immed_queue_list);
81e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&conn->response_queue_list);
82e48354ceSNicholas Bellinger 	init_completion(&conn->conn_post_wait_comp);
83e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_comp);
84e48354ceSNicholas Bellinger 	init_completion(&conn->conn_wait_rcfr_comp);
85e48354ceSNicholas Bellinger 	init_completion(&conn->conn_waiting_on_uc_comp);
86e48354ceSNicholas Bellinger 	init_completion(&conn->conn_logout_comp);
87e48354ceSNicholas Bellinger 	init_completion(&conn->rx_half_close_comp);
88e48354ceSNicholas Bellinger 	init_completion(&conn->tx_half_close_comp);
89e5419865SNicholas Bellinger 	init_completion(&conn->rx_login_comp);
90e48354ceSNicholas Bellinger 	spin_lock_init(&conn->cmd_lock);
91e48354ceSNicholas Bellinger 	spin_lock_init(&conn->conn_usage_lock);
92e48354ceSNicholas Bellinger 	spin_lock_init(&conn->immed_queue_lock);
93e48354ceSNicholas Bellinger 	spin_lock_init(&conn->nopin_timer_lock);
94e48354ceSNicholas Bellinger 	spin_lock_init(&conn->response_queue_lock);
95e48354ceSNicholas Bellinger 	spin_lock_init(&conn->state_lock);
96e48354ceSNicholas Bellinger 
97e48354ceSNicholas Bellinger 	if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
98e48354ceSNicholas Bellinger 		pr_err("Unable to allocate conn->conn_cpumask\n");
99baa4d64bSNicholas Bellinger 		goto out_conn_ops;
100e48354ceSNicholas Bellinger 	}
101baa4d64bSNicholas Bellinger 	conn->conn_login = login;
102e48354ceSNicholas Bellinger 
103baa4d64bSNicholas Bellinger 	return login;
104baa4d64bSNicholas Bellinger 
105baa4d64bSNicholas Bellinger out_conn_ops:
106baa4d64bSNicholas Bellinger 	kfree(conn->conn_ops);
107baa4d64bSNicholas Bellinger out_rsp_buf:
108baa4d64bSNicholas Bellinger 	kfree(login->rsp_buf);
109baa4d64bSNicholas Bellinger out_req_buf:
110baa4d64bSNicholas Bellinger 	kfree(login->req_buf);
111baa4d64bSNicholas Bellinger out_login:
112baa4d64bSNicholas Bellinger 	kfree(login);
113baa4d64bSNicholas Bellinger 	return NULL;
114e48354ceSNicholas Bellinger }
115e48354ceSNicholas Bellinger 
116e48354ceSNicholas Bellinger /*
117e48354ceSNicholas Bellinger  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
118e48354ceSNicholas Bellinger  * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
119e48354ceSNicholas Bellinger  */
120e48354ceSNicholas Bellinger int iscsi_login_setup_crypto(struct iscsi_conn *conn)
121e48354ceSNicholas Bellinger {
12269110e3cSHerbert Xu 	struct crypto_ahash *tfm;
12369110e3cSHerbert Xu 
124e48354ceSNicholas Bellinger 	/*
125e48354ceSNicholas Bellinger 	 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
126e48354ceSNicholas Bellinger 	 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
127e48354ceSNicholas Bellinger 	 * to software 1x8 byte slicing from crc32c.ko
128e48354ceSNicholas Bellinger 	 */
12969110e3cSHerbert Xu 	tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
13069110e3cSHerbert Xu 	if (IS_ERR(tfm)) {
13169110e3cSHerbert Xu 		pr_err("crypto_alloc_ahash() failed\n");
132e48354ceSNicholas Bellinger 		return -ENOMEM;
133e48354ceSNicholas Bellinger 	}
134e48354ceSNicholas Bellinger 
13569110e3cSHerbert Xu 	conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
13669110e3cSHerbert Xu 	if (!conn->conn_rx_hash) {
13769110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
13869110e3cSHerbert Xu 		crypto_free_ahash(tfm);
139e48354ceSNicholas Bellinger 		return -ENOMEM;
140e48354ceSNicholas Bellinger 	}
14169110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
14269110e3cSHerbert Xu 
14369110e3cSHerbert Xu 	conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
14469110e3cSHerbert Xu 	if (!conn->conn_tx_hash) {
14569110e3cSHerbert Xu 		pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
14669110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
14769110e3cSHerbert Xu 		conn->conn_rx_hash = NULL;
14869110e3cSHerbert Xu 		crypto_free_ahash(tfm);
14969110e3cSHerbert Xu 		return -ENOMEM;
15069110e3cSHerbert Xu 	}
15169110e3cSHerbert Xu 	ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
152e48354ceSNicholas Bellinger 
153e48354ceSNicholas Bellinger 	return 0;
154e48354ceSNicholas Bellinger }
155e48354ceSNicholas Bellinger 
156e48354ceSNicholas Bellinger static int iscsi_login_check_initiator_version(
157e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
158e48354ceSNicholas Bellinger 	u8 version_max,
159e48354ceSNicholas Bellinger 	u8 version_min)
160e48354ceSNicholas Bellinger {
161e48354ceSNicholas Bellinger 	if ((version_max != 0x00) || (version_min != 0x00)) {
162e48354ceSNicholas Bellinger 		pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
163e48354ceSNicholas Bellinger 			" version Min/Max 0x%02x/0x%02x, rejecting login.\n",
164e48354ceSNicholas Bellinger 			version_min, version_max);
165e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
166e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_VERSION);
167e48354ceSNicholas Bellinger 		return -1;
168e48354ceSNicholas Bellinger 	}
169e48354ceSNicholas Bellinger 
170e48354ceSNicholas Bellinger 	return 0;
171e48354ceSNicholas Bellinger }
172e48354ceSNicholas Bellinger 
173e48354ceSNicholas Bellinger int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
174e48354ceSNicholas Bellinger {
175e48354ceSNicholas Bellinger 	int sessiontype;
176e48354ceSNicholas Bellinger 	struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
177e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
178e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
179e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
180e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
181e48354ceSNicholas Bellinger 
182e48354ceSNicholas Bellinger 	initiatorname_param = iscsi_find_param_from_key(
183e48354ceSNicholas Bellinger 			INITIATORNAME, conn->param_list);
184e48354ceSNicholas Bellinger 	sessiontype_param = iscsi_find_param_from_key(
185e48354ceSNicholas Bellinger 			SESSIONTYPE, conn->param_list);
1861c5c12c6SRoland Dreier 	if (!initiatorname_param || !sessiontype_param) {
1871c5c12c6SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1881c5c12c6SRoland Dreier 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
189e48354ceSNicholas Bellinger 		return -1;
1901c5c12c6SRoland Dreier 	}
191e48354ceSNicholas Bellinger 
192e48354ceSNicholas Bellinger 	sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
193e48354ceSNicholas Bellinger 
194e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
195e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
196e48354ceSNicholas Bellinger 			sess_list) {
197e48354ceSNicholas Bellinger 
1988359cf43SJörn Engel 		sess_p = se_sess->fabric_sess_ptr;
199e48354ceSNicholas Bellinger 		spin_lock(&sess_p->conn_lock);
200e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
201e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
202e48354ceSNicholas Bellinger 		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
203e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
204e48354ceSNicholas Bellinger 			continue;
205e48354ceSNicholas Bellinger 		}
2068359cf43SJörn Engel 		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
2078359cf43SJörn Engel 		   (!strcmp(sess_p->sess_ops->InitiatorName,
2088359cf43SJörn Engel 			    initiatorname_param->value) &&
209e48354ceSNicholas Bellinger 		   (sess_p->sess_ops->SessionType == sessiontype))) {
210e48354ceSNicholas Bellinger 			atomic_set(&sess_p->session_reinstatement, 1);
211197b806aSNicholas Bellinger 			atomic_set(&sess_p->session_fall_back_to_erl0, 1);
212e48354ceSNicholas Bellinger 			spin_unlock(&sess_p->conn_lock);
213e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
214e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
215e48354ceSNicholas Bellinger 			sess = sess_p;
216e48354ceSNicholas Bellinger 			break;
217e48354ceSNicholas Bellinger 		}
218e48354ceSNicholas Bellinger 		spin_unlock(&sess_p->conn_lock);
219e48354ceSNicholas Bellinger 	}
220e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
221e48354ceSNicholas Bellinger 	/*
222e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
223e48354ceSNicholas Bellinger 	 */
224e48354ceSNicholas Bellinger 	if (!sess)
225e48354ceSNicholas Bellinger 		return 0;
226e48354ceSNicholas Bellinger 
227e48354ceSNicholas Bellinger 	pr_debug("%s iSCSI Session SID %u is still active for %s,"
2280d5efb8aSBart Van Assche 		" performing session reinstatement.\n", (sessiontype) ?
229e48354ceSNicholas Bellinger 		"Discovery" : "Normal", sess->sid,
230e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
231e48354ceSNicholas Bellinger 
232e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
233e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED) {
234e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
235e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
23644f33d0fSChristoph Hellwig 		iscsit_close_session(sess);
23799367f01SNicholas Bellinger 		return 0;
238e48354ceSNicholas Bellinger 	}
239e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
240e48354ceSNicholas Bellinger 
241e48354ceSNicholas Bellinger 	iscsit_stop_session(sess, 1, 1);
242e48354ceSNicholas Bellinger 	iscsit_dec_session_usage_count(sess);
243e48354ceSNicholas Bellinger 
24444f33d0fSChristoph Hellwig 	iscsit_close_session(sess);
24599367f01SNicholas Bellinger 	return 0;
246e48354ceSNicholas Bellinger }
247e48354ceSNicholas Bellinger 
2486787ab81SJason A. Donenfeld static int iscsi_login_set_conn_values(
249e48354ceSNicholas Bellinger 	struct iscsi_session *sess,
250e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
25150e5c87dSChristoph Hellwig 	__be16 cid)
252e48354ceSNicholas Bellinger {
2536787ab81SJason A. Donenfeld 	int ret;
254e48354ceSNicholas Bellinger 	conn->sess		= sess;
25550e5c87dSChristoph Hellwig 	conn->cid		= be16_to_cpu(cid);
256e48354ceSNicholas Bellinger 	/*
257e48354ceSNicholas Bellinger 	 * Generate a random Status sequence number (statsn) for the new
258e48354ceSNicholas Bellinger 	 * iSCSI connection.
259e48354ceSNicholas Bellinger 	 */
2606787ab81SJason A. Donenfeld 	ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
2616787ab81SJason A. Donenfeld 	if (unlikely(ret))
2626787ab81SJason A. Donenfeld 		return ret;
263e48354ceSNicholas Bellinger 
264e48354ceSNicholas Bellinger 	mutex_lock(&auth_id_lock);
265e48354ceSNicholas Bellinger 	conn->auth_id		= iscsit_global->auth_id++;
266e48354ceSNicholas Bellinger 	mutex_unlock(&auth_id_lock);
2676787ab81SJason A. Donenfeld 	return 0;
268e48354ceSNicholas Bellinger }
269e48354ceSNicholas Bellinger 
270d2faaefbSVarun Prakash __printf(2, 3) int iscsi_change_param_sprintf(
27179d59d08SRoland Dreier 	struct iscsi_conn *conn,
27279d59d08SRoland Dreier 	const char *fmt, ...)
27379d59d08SRoland Dreier {
27479d59d08SRoland Dreier 	va_list args;
27579d59d08SRoland Dreier 	unsigned char buf[64];
27679d59d08SRoland Dreier 
27779d59d08SRoland Dreier 	memset(buf, 0, sizeof buf);
27879d59d08SRoland Dreier 
27979d59d08SRoland Dreier 	va_start(args, fmt);
28079d59d08SRoland Dreier 	vsnprintf(buf, sizeof buf, fmt, args);
28179d59d08SRoland Dreier 	va_end(args);
28279d59d08SRoland Dreier 
28379d59d08SRoland Dreier 	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
28479d59d08SRoland Dreier 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
28579d59d08SRoland Dreier 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
28679d59d08SRoland Dreier 		return -1;
28779d59d08SRoland Dreier 	}
28879d59d08SRoland Dreier 
28979d59d08SRoland Dreier 	return 0;
29079d59d08SRoland Dreier }
291d2faaefbSVarun Prakash EXPORT_SYMBOL(iscsi_change_param_sprintf);
29279d59d08SRoland Dreier 
293e48354ceSNicholas Bellinger /*
294e48354ceSNicholas Bellinger  *	This is the leading connection of a new session,
295e48354ceSNicholas Bellinger  *	or session reinstatement.
296e48354ceSNicholas Bellinger  */
297e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s1(
298e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
299e48354ceSNicholas Bellinger 	unsigned char *buf)
300e48354ceSNicholas Bellinger {
301e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL;
302e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
30313b5533aSBenjamin Wang 	int ret;
304e48354ceSNicholas Bellinger 
305e48354ceSNicholas Bellinger 	sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
306e48354ceSNicholas Bellinger 	if (!sess) {
307e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
308e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
309e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for session\n");
3100957627aSNicholas Bellinger 		return -ENOMEM;
311e48354ceSNicholas Bellinger 	}
312e48354ceSNicholas Bellinger 
3136787ab81SJason A. Donenfeld 	ret = iscsi_login_set_conn_values(sess, conn, pdu->cid);
3146787ab81SJason A. Donenfeld 	if (unlikely(ret)) {
3156787ab81SJason A. Donenfeld 		kfree(sess);
3166787ab81SJason A. Donenfeld 		return ret;
3176787ab81SJason A. Donenfeld 	}
318e48354ceSNicholas Bellinger 	sess->init_task_tag	= pdu->itt;
3198359cf43SJörn Engel 	memcpy(&sess->isid, pdu->isid, 6);
32050e5c87dSChristoph Hellwig 	sess->exp_cmd_sn	= be32_to_cpu(pdu->cmdsn);
321e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_conn_list);
322e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
323e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_active_list);
324e48354ceSNicholas Bellinger 	INIT_LIST_HEAD(&sess->cr_inactive_list);
325e48354ceSNicholas Bellinger 	init_completion(&sess->async_msg_comp);
326e48354ceSNicholas Bellinger 	init_completion(&sess->reinstatement_comp);
327e48354ceSNicholas Bellinger 	init_completion(&sess->session_wait_comp);
328e48354ceSNicholas Bellinger 	init_completion(&sess->session_waiting_on_uc_comp);
329e48354ceSNicholas Bellinger 	mutex_init(&sess->cmdsn_mutex);
330e48354ceSNicholas Bellinger 	spin_lock_init(&sess->conn_lock);
331e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_a_lock);
332e48354ceSNicholas Bellinger 	spin_lock_init(&sess->cr_i_lock);
333e48354ceSNicholas Bellinger 	spin_lock_init(&sess->session_usage_lock);
334e48354ceSNicholas Bellinger 	spin_lock_init(&sess->ttt_lock);
335e48354ceSNicholas Bellinger 
336f7c9564aSKees Cook 	timer_setup(&sess->time2retain_timer,
337f7c9564aSKees Cook 		    iscsit_handle_time2retain_timeout, 0);
3388a47aa9dSBart Van Assche 
33931ff0ceeSMatthew Wilcox 	ret = ida_alloc(&sess_ida, GFP_KERNEL);
34013b5533aSBenjamin Wang 	if (ret < 0) {
34131ff0ceeSMatthew Wilcox 		pr_err("Session ID allocation failed %d\n", ret);
34213b5533aSBenjamin Wang 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
34313b5533aSBenjamin Wang 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
34426abc916SMike Christie 		goto free_sess;
34513b5533aSBenjamin Wang 	}
34613b5533aSBenjamin Wang 
34731ff0ceeSMatthew Wilcox 	sess->session_index = ret;
348e48354ceSNicholas Bellinger 	sess->creation_time = get_jiffies_64();
349e48354ceSNicholas Bellinger 	/*
350e48354ceSNicholas Bellinger 	 * The FFP CmdSN window values will be allocated from the TPG's
351e48354ceSNicholas Bellinger 	 * Initiator Node's ACL once the login has been successfully completed.
352e48354ceSNicholas Bellinger 	 */
353109e2381SRoland Dreier 	atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
354e48354ceSNicholas Bellinger 
355e48354ceSNicholas Bellinger 	sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
356e48354ceSNicholas Bellinger 	if (!sess->sess_ops) {
357e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
358e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
359e48354ceSNicholas Bellinger 		pr_err("Unable to allocate memory for"
360e48354ceSNicholas Bellinger 				" struct iscsi_sess_ops.\n");
36131ff0ceeSMatthew Wilcox 		goto free_id;
362e48354ceSNicholas Bellinger 	}
363e48354ceSNicholas Bellinger 
36423a548eeSSagi Grimberg 	sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
3650957627aSNicholas Bellinger 	if (IS_ERR(sess->se_sess)) {
366e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
367e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
36826abc916SMike Christie 		goto free_ops;
369e48354ceSNicholas Bellinger 	}
370e48354ceSNicholas Bellinger 
371e48354ceSNicholas Bellinger 	return 0;
37226abc916SMike Christie 
37326abc916SMike Christie free_ops:
37426abc916SMike Christie 	kfree(sess->sess_ops);
37531ff0ceeSMatthew Wilcox free_id:
37631ff0ceeSMatthew Wilcox 	ida_free(&sess_ida, sess->session_index);
37726abc916SMike Christie free_sess:
37826abc916SMike Christie 	kfree(sess);
37926abc916SMike Christie 	conn->sess = NULL;
38026abc916SMike Christie 	return -ENOMEM;
381e48354ceSNicholas Bellinger }
382e48354ceSNicholas Bellinger 
383e48354ceSNicholas Bellinger static int iscsi_login_zero_tsih_s2(
384e48354ceSNicholas Bellinger 	struct iscsi_conn *conn)
385e48354ceSNicholas Bellinger {
386e48354ceSNicholas Bellinger 	struct iscsi_node_attrib *na;
387e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
38803aa2070SNicholas Bellinger 	bool iser = false;
389e48354ceSNicholas Bellinger 
390e48354ceSNicholas Bellinger 	sess->tpg = conn->tpg;
391e48354ceSNicholas Bellinger 
392e48354ceSNicholas Bellinger 	/*
393e48354ceSNicholas Bellinger 	 * Assign a new TPG Session Handle.  Note this is protected with
394e48354ceSNicholas Bellinger 	 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
395e48354ceSNicholas Bellinger 	 */
39660bfcf8eSAndy Grover 	sess->tsih = ++sess->tpg->ntsih;
397e48354ceSNicholas Bellinger 	if (!sess->tsih)
39860bfcf8eSAndy Grover 		sess->tsih = ++sess->tpg->ntsih;
399e48354ceSNicholas Bellinger 
400e48354ceSNicholas Bellinger 	/*
401e48354ceSNicholas Bellinger 	 * Create the default params from user defined values..
402e48354ceSNicholas Bellinger 	 */
403e48354ceSNicholas Bellinger 	if (iscsi_copy_param_list(&conn->param_list,
40460bfcf8eSAndy Grover 				conn->tpg->param_list, 1) < 0) {
405e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
406e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
407e48354ceSNicholas Bellinger 		return -1;
408e48354ceSNicholas Bellinger 	}
409e48354ceSNicholas Bellinger 
41003aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
41103aa2070SNicholas Bellinger 		iser = true;
41203aa2070SNicholas Bellinger 
41303aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
414e48354ceSNicholas Bellinger 
415e48354ceSNicholas Bellinger 	if (sess->sess_ops->SessionType)
416e48354ceSNicholas Bellinger 		return iscsi_set_keys_irrelevant_for_discovery(
417e48354ceSNicholas Bellinger 				conn->param_list);
418e48354ceSNicholas Bellinger 
419e48354ceSNicholas Bellinger 	na = iscsit_tpg_get_node_attrib(sess);
420e48354ceSNicholas Bellinger 
421e48354ceSNicholas Bellinger 	/*
422e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
423e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
424e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
425e48354ceSNicholas Bellinger 	 *
426e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
427e48354ceSNicholas Bellinger 	 */
42879d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
429e48354ceSNicholas Bellinger 		return -1;
430e48354ceSNicholas Bellinger 
431e48354ceSNicholas Bellinger 	/*
432e48354ceSNicholas Bellinger 	 * Workaround for Initiators that have broken connection recovery logic.
433e48354ceSNicholas Bellinger 	 *
434e48354ceSNicholas Bellinger 	 * "We would really like to get rid of this." Linux-iSCSI.org team
435e48354ceSNicholas Bellinger 	 */
43679d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
437e48354ceSNicholas Bellinger 		return -1;
438e48354ceSNicholas Bellinger 
43903aa2070SNicholas Bellinger 	/*
44003aa2070SNicholas Bellinger 	 * Set RDMAExtensions=Yes by default for iSER enabled network portals
44103aa2070SNicholas Bellinger 	 */
44203aa2070SNicholas Bellinger 	if (iser) {
44303aa2070SNicholas Bellinger 		struct iscsi_param *param;
44403aa2070SNicholas Bellinger 		unsigned long mrdsl, off;
44503aa2070SNicholas Bellinger 		int rc;
44603aa2070SNicholas Bellinger 
44779d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
44803aa2070SNicholas Bellinger 			return -1;
44979d59d08SRoland Dreier 
45003aa2070SNicholas Bellinger 		/*
45103aa2070SNicholas Bellinger 		 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
4523fc6a642SColin Ian King 		 * Immediate Data + Unsolicited Data-OUT if necessary..
45303aa2070SNicholas Bellinger 		 */
45403aa2070SNicholas Bellinger 		param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
45503aa2070SNicholas Bellinger 						  conn->param_list);
45603aa2070SNicholas Bellinger 		if (!param) {
45703aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
45803aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
45903aa2070SNicholas Bellinger 			return -1;
46003aa2070SNicholas Bellinger 		}
46157103d7fSJingoo Han 		rc = kstrtoul(param->value, 0, &mrdsl);
46203aa2070SNicholas Bellinger 		if (rc < 0) {
46303aa2070SNicholas Bellinger 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
46403aa2070SNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
46503aa2070SNicholas Bellinger 			return -1;
46603aa2070SNicholas Bellinger 		}
46703aa2070SNicholas Bellinger 		off = mrdsl % PAGE_SIZE;
46803aa2070SNicholas Bellinger 		if (!off)
46952d0aa79SNicholas Bellinger 			goto check_prot;
47003aa2070SNicholas Bellinger 
47103aa2070SNicholas Bellinger 		if (mrdsl < PAGE_SIZE)
47203aa2070SNicholas Bellinger 			mrdsl = PAGE_SIZE;
47303aa2070SNicholas Bellinger 		else
47403aa2070SNicholas Bellinger 			mrdsl -= off;
47503aa2070SNicholas Bellinger 
47603aa2070SNicholas Bellinger 		pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
47703aa2070SNicholas Bellinger 			" to PAGE_SIZE\n", mrdsl);
47803aa2070SNicholas Bellinger 
47979d59d08SRoland Dreier 		if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
48003aa2070SNicholas Bellinger 			return -1;
48152d0aa79SNicholas Bellinger 		/*
48252d0aa79SNicholas Bellinger 		 * ISER currently requires that ImmediateData + Unsolicited
48352d0aa79SNicholas Bellinger 		 * Data be disabled when protection / signature MRs are enabled.
48452d0aa79SNicholas Bellinger 		 */
48552d0aa79SNicholas Bellinger check_prot:
48652d0aa79SNicholas Bellinger 		if (sess->se_sess->sup_prot_ops &
48752d0aa79SNicholas Bellinger 		   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
48852d0aa79SNicholas Bellinger 		    TARGET_PROT_DOUT_INSERT)) {
48952d0aa79SNicholas Bellinger 
49079d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
49152d0aa79SNicholas Bellinger 				return -1;
49252d0aa79SNicholas Bellinger 
49379d59d08SRoland Dreier 			if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
49452d0aa79SNicholas Bellinger 				return -1;
49579d59d08SRoland Dreier 
49652d0aa79SNicholas Bellinger 			pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
49752d0aa79SNicholas Bellinger 				 " T10-PI enabled ISER session\n");
49852d0aa79SNicholas Bellinger 		}
49903aa2070SNicholas Bellinger 	}
500e48354ceSNicholas Bellinger 
501e48354ceSNicholas Bellinger 	return 0;
502e48354ceSNicholas Bellinger }
503e48354ceSNicholas Bellinger 
504e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s1(
505e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
506e48354ceSNicholas Bellinger 	unsigned char *buf)
507e48354ceSNicholas Bellinger {
508e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
509e48354ceSNicholas Bellinger 
5106787ab81SJason A. Donenfeld 	return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
511e48354ceSNicholas Bellinger }
512e48354ceSNicholas Bellinger 
513e48354ceSNicholas Bellinger /*
514e48354ceSNicholas Bellinger  *	Add a new connection to an existing session.
515e48354ceSNicholas Bellinger  */
516e48354ceSNicholas Bellinger static int iscsi_login_non_zero_tsih_s2(
517e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
518e48354ceSNicholas Bellinger 	unsigned char *buf)
519e48354ceSNicholas Bellinger {
520e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = conn->tpg;
521e48354ceSNicholas Bellinger 	struct iscsi_session *sess = NULL, *sess_p = NULL;
522e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
523e48354ceSNicholas Bellinger 	struct se_session *se_sess, *se_sess_tmp;
524e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
52503aa2070SNicholas Bellinger 	bool iser = false;
526e48354ceSNicholas Bellinger 
527e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
528e48354ceSNicholas Bellinger 	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
529e48354ceSNicholas Bellinger 			sess_list) {
530e48354ceSNicholas Bellinger 
531e48354ceSNicholas Bellinger 		sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
532e48354ceSNicholas Bellinger 		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
533e48354ceSNicholas Bellinger 		    atomic_read(&sess_p->session_logout) ||
534e48354ceSNicholas Bellinger 		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
535e48354ceSNicholas Bellinger 			continue;
5368359cf43SJörn Engel 		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
53750e5c87dSChristoph Hellwig 		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
538e48354ceSNicholas Bellinger 			iscsit_inc_session_usage_count(sess_p);
539e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess_p);
540e48354ceSNicholas Bellinger 			sess = sess_p;
541e48354ceSNicholas Bellinger 			break;
542e48354ceSNicholas Bellinger 		}
543e48354ceSNicholas Bellinger 	}
544e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
545e48354ceSNicholas Bellinger 
546e48354ceSNicholas Bellinger 	/*
547e48354ceSNicholas Bellinger 	 * If the Time2Retain handler has expired, the session is already gone.
548e48354ceSNicholas Bellinger 	 */
549e48354ceSNicholas Bellinger 	if (!sess) {
550e48354ceSNicholas Bellinger 		pr_err("Initiator attempting to add a connection to"
551e48354ceSNicholas Bellinger 			" a non-existent session, rejecting iSCSI Login.\n");
552e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
553e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_SESSION);
554e48354ceSNicholas Bellinger 		return -1;
555e48354ceSNicholas Bellinger 	}
556e48354ceSNicholas Bellinger 
557e48354ceSNicholas Bellinger 	/*
558e48354ceSNicholas Bellinger 	 * Stop the Time2Retain timer if this is a failed session, we restart
559e48354ceSNicholas Bellinger 	 * the timer if the login is not successful.
560e48354ceSNicholas Bellinger 	 */
561e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
562e48354ceSNicholas Bellinger 	if (sess->session_state == TARG_SESS_STATE_FAILED)
563e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 1);
564e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
565e48354ceSNicholas Bellinger 
5666787ab81SJason A. Donenfeld 	if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
5676787ab81SJason A. Donenfeld 	    iscsi_copy_param_list(&conn->param_list,
56860bfcf8eSAndy Grover 			conn->tpg->param_list, 0) < 0) {
569e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
570e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
571e48354ceSNicholas Bellinger 		return -1;
572e48354ceSNicholas Bellinger 	}
573e48354ceSNicholas Bellinger 
57403aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
57503aa2070SNicholas Bellinger 		iser = true;
57603aa2070SNicholas Bellinger 
57703aa2070SNicholas Bellinger 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
578e48354ceSNicholas Bellinger 	/*
579e48354ceSNicholas Bellinger 	 * Need to send TargetPortalGroupTag back in first login response
580e48354ceSNicholas Bellinger 	 * on any iSCSI connection where the Initiator provides TargetName.
581e48354ceSNicholas Bellinger 	 * See 5.3.1.  Login Phase Start
582e48354ceSNicholas Bellinger 	 *
583e48354ceSNicholas Bellinger 	 * In our case, we have already located the struct iscsi_tiqn at this point.
584e48354ceSNicholas Bellinger 	 */
58579d59d08SRoland Dreier 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
586e48354ceSNicholas Bellinger 		return -1;
587e48354ceSNicholas Bellinger 
588c04a6091SChristophe Vu-Brugier 	return 0;
589e48354ceSNicholas Bellinger }
590e48354ceSNicholas Bellinger 
591e48354ceSNicholas Bellinger int iscsi_login_post_auth_non_zero_tsih(
592e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
593e48354ceSNicholas Bellinger 	u16 cid,
594e48354ceSNicholas Bellinger 	u32 exp_statsn)
595e48354ceSNicholas Bellinger {
596e48354ceSNicholas Bellinger 	struct iscsi_conn *conn_ptr = NULL;
597e48354ceSNicholas Bellinger 	struct iscsi_conn_recovery *cr = NULL;
598e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
599e48354ceSNicholas Bellinger 
600e48354ceSNicholas Bellinger 	/*
601e48354ceSNicholas Bellinger 	 * By following item 5 in the login table,  if we have found
602e48354ceSNicholas Bellinger 	 * an existing ISID and a valid/existing TSIH and an existing
603e48354ceSNicholas Bellinger 	 * CID we do connection reinstatement.  Currently we dont not
604e48354ceSNicholas Bellinger 	 * support it so we send back an non-zero status class to the
605e48354ceSNicholas Bellinger 	 * initiator and release the new connection.
606e48354ceSNicholas Bellinger 	 */
607e48354ceSNicholas Bellinger 	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
608ee1b1b9cSAndy Grover 	if (conn_ptr) {
609e48354ceSNicholas Bellinger 		pr_err("Connection exists with CID %hu for %s,"
610e48354ceSNicholas Bellinger 			" performing connection reinstatement.\n",
611e48354ceSNicholas Bellinger 			conn_ptr->cid, sess->sess_ops->InitiatorName);
612e48354ceSNicholas Bellinger 
613e48354ceSNicholas Bellinger 		iscsit_connection_reinstatement_rcfr(conn_ptr);
614e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn_ptr);
615e48354ceSNicholas Bellinger 	}
616e48354ceSNicholas Bellinger 
617e48354ceSNicholas Bellinger 	/*
618e48354ceSNicholas Bellinger 	 * Check for any connection recovery entires containing CID.
619e48354ceSNicholas Bellinger 	 * We use the original ExpStatSN sent in the first login request
620e48354ceSNicholas Bellinger 	 * to acknowledge commands for the failed connection.
621e48354ceSNicholas Bellinger 	 *
622e48354ceSNicholas Bellinger 	 * Also note that an explict logout may have already been sent,
623e48354ceSNicholas Bellinger 	 * but the response may not be sent due to additional connection
624e48354ceSNicholas Bellinger 	 * loss.
625e48354ceSNicholas Bellinger 	 */
626e48354ceSNicholas Bellinger 	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
627e48354ceSNicholas Bellinger 		cr = iscsit_get_inactive_connection_recovery_entry(
628e48354ceSNicholas Bellinger 				sess, cid);
629ee1b1b9cSAndy Grover 		if (cr) {
630e48354ceSNicholas Bellinger 			pr_debug("Performing implicit logout"
631e48354ceSNicholas Bellinger 				" for connection recovery on CID: %hu\n",
632e48354ceSNicholas Bellinger 					conn->cid);
633e48354ceSNicholas Bellinger 			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
634e48354ceSNicholas Bellinger 		}
635e48354ceSNicholas Bellinger 	}
636e48354ceSNicholas Bellinger 
637e48354ceSNicholas Bellinger 	/*
638e48354ceSNicholas Bellinger 	 * Else we follow item 4 from the login table in that we have
639e48354ceSNicholas Bellinger 	 * found an existing ISID and a valid/existing TSIH and a new
640e48354ceSNicholas Bellinger 	 * CID we go ahead and continue to add a new connection to the
641e48354ceSNicholas Bellinger 	 * session.
642e48354ceSNicholas Bellinger 	 */
643e48354ceSNicholas Bellinger 	pr_debug("Adding CID %hu to existing session for %s.\n",
644e48354ceSNicholas Bellinger 			cid, sess->sess_ops->InitiatorName);
645e48354ceSNicholas Bellinger 
646e48354ceSNicholas Bellinger 	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
647e48354ceSNicholas Bellinger 		pr_err("Adding additional connection to this session"
648e48354ceSNicholas Bellinger 			" would exceed MaxConnections %d, login failed.\n",
649e48354ceSNicholas Bellinger 				sess->sess_ops->MaxConnections);
650e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
651e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_ISID_ERROR);
652e48354ceSNicholas Bellinger 		return -1;
653e48354ceSNicholas Bellinger 	}
654e48354ceSNicholas Bellinger 
655e48354ceSNicholas Bellinger 	return 0;
656e48354ceSNicholas Bellinger }
657e48354ceSNicholas Bellinger 
658e48354ceSNicholas Bellinger static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
659e48354ceSNicholas Bellinger {
660e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
66103aa2070SNicholas Bellinger 	/*
6623fc6a642SColin Ian King 	 * FIXME: Unsolicited NopIN support for ISER
66303aa2070SNicholas Bellinger 	 */
66403aa2070SNicholas Bellinger 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
66503aa2070SNicholas Bellinger 		return;
666e48354ceSNicholas Bellinger 
667e48354ceSNicholas Bellinger 	if (!sess->sess_ops->SessionType)
668e48354ceSNicholas Bellinger 		iscsit_start_nopin_timer(conn);
669e48354ceSNicholas Bellinger }
670e48354ceSNicholas Bellinger 
671e5419865SNicholas Bellinger int iscsit_start_kthreads(struct iscsi_conn *conn)
67288dcd2daSNicholas Bellinger {
67388dcd2daSNicholas Bellinger 	int ret = 0;
67488dcd2daSNicholas Bellinger 
67588dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
67688dcd2daSNicholas Bellinger 	conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
67788dcd2daSNicholas Bellinger 					ISCSIT_BITMAP_BITS, get_order(1));
67888dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
67988dcd2daSNicholas Bellinger 
68088dcd2daSNicholas Bellinger 	if (conn->bitmap_id < 0) {
68188dcd2daSNicholas Bellinger 		pr_err("bitmap_find_free_region() failed for"
68288dcd2daSNicholas Bellinger 		       " iscsit_start_kthreads()\n");
68388dcd2daSNicholas Bellinger 		return -ENOMEM;
68488dcd2daSNicholas Bellinger 	}
68588dcd2daSNicholas Bellinger 
68688dcd2daSNicholas Bellinger 	conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
68788dcd2daSNicholas Bellinger 				      "%s", ISCSI_TX_THREAD_NAME);
68888dcd2daSNicholas Bellinger 	if (IS_ERR(conn->tx_thread)) {
68988dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_tx_thread\n");
69088dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->tx_thread);
69188dcd2daSNicholas Bellinger 		goto out_bitmap;
69288dcd2daSNicholas Bellinger 	}
69388dcd2daSNicholas Bellinger 	conn->tx_thread_active = true;
69488dcd2daSNicholas Bellinger 
69588dcd2daSNicholas Bellinger 	conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
69688dcd2daSNicholas Bellinger 				      "%s", ISCSI_RX_THREAD_NAME);
69788dcd2daSNicholas Bellinger 	if (IS_ERR(conn->rx_thread)) {
69888dcd2daSNicholas Bellinger 		pr_err("Unable to start iscsi_target_rx_thread\n");
69988dcd2daSNicholas Bellinger 		ret = PTR_ERR(conn->rx_thread);
70088dcd2daSNicholas Bellinger 		goto out_tx;
70188dcd2daSNicholas Bellinger 	}
70288dcd2daSNicholas Bellinger 	conn->rx_thread_active = true;
70388dcd2daSNicholas Bellinger 
70488dcd2daSNicholas Bellinger 	return 0;
70588dcd2daSNicholas Bellinger out_tx:
706e5419865SNicholas Bellinger 	send_sig(SIGINT, conn->tx_thread, 1);
70788dcd2daSNicholas Bellinger 	kthread_stop(conn->tx_thread);
70888dcd2daSNicholas Bellinger 	conn->tx_thread_active = false;
70988dcd2daSNicholas Bellinger out_bitmap:
71088dcd2daSNicholas Bellinger 	spin_lock(&iscsit_global->ts_bitmap_lock);
71188dcd2daSNicholas Bellinger 	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
71288dcd2daSNicholas Bellinger 			      get_order(1));
71388dcd2daSNicholas Bellinger 	spin_unlock(&iscsit_global->ts_bitmap_lock);
71488dcd2daSNicholas Bellinger 	return ret;
71588dcd2daSNicholas Bellinger }
71688dcd2daSNicholas Bellinger 
717e5419865SNicholas Bellinger void iscsi_post_login_handler(
718e48354ceSNicholas Bellinger 	struct iscsi_np *np,
719e48354ceSNicholas Bellinger 	struct iscsi_conn *conn,
720e48354ceSNicholas Bellinger 	u8 zero_tsih)
721e48354ceSNicholas Bellinger {
722e48354ceSNicholas Bellinger 	int stop_timer = 0;
723e48354ceSNicholas Bellinger 	struct iscsi_session *sess = conn->sess;
724e48354ceSNicholas Bellinger 	struct se_session *se_sess = sess->se_sess;
72560bfcf8eSAndy Grover 	struct iscsi_portal_group *tpg = sess->tpg;
726e48354ceSNicholas Bellinger 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
727e48354ceSNicholas Bellinger 
728e48354ceSNicholas Bellinger 	iscsit_inc_conn_usage_count(conn);
729e48354ceSNicholas Bellinger 
730e48354ceSNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
731e48354ceSNicholas Bellinger 			ISCSI_LOGIN_STATUS_ACCEPT);
732e48354ceSNicholas Bellinger 
733e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
734e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
735e48354ceSNicholas Bellinger 
736e48354ceSNicholas Bellinger 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
737e48354ceSNicholas Bellinger 	/*
738e48354ceSNicholas Bellinger 	 * SCSI Initiator -> SCSI Target Port Mapping
739e48354ceSNicholas Bellinger 	 */
740e48354ceSNicholas Bellinger 	if (!zero_tsih) {
741e48354ceSNicholas Bellinger 		iscsi_set_session_parameters(sess->sess_ops,
742e48354ceSNicholas Bellinger 				conn->param_list, 0);
743e48354ceSNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
744e48354ceSNicholas Bellinger 		conn->param_list = NULL;
745e48354ceSNicholas Bellinger 
746e48354ceSNicholas Bellinger 		spin_lock_bh(&sess->conn_lock);
747e48354ceSNicholas Bellinger 		atomic_set(&sess->session_continuation, 0);
748e48354ceSNicholas Bellinger 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
749e48354ceSNicholas Bellinger 			pr_debug("Moving to"
750e48354ceSNicholas Bellinger 					" TARG_SESS_STATE_LOGGED_IN.\n");
751e48354ceSNicholas Bellinger 			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
752e48354ceSNicholas Bellinger 			stop_timer = 1;
753e48354ceSNicholas Bellinger 		}
754e48354ceSNicholas Bellinger 
755dc58f760SAndy Grover 		pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
756dc58f760SAndy Grover 			" %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
75769d75574SAndy Grover 			&conn->local_sockaddr, tpg->tpgt);
758e48354ceSNicholas Bellinger 
759e48354ceSNicholas Bellinger 		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
760e48354ceSNicholas Bellinger 		atomic_inc(&sess->nconn);
761e48354ceSNicholas Bellinger 		pr_debug("Incremented iSCSI Connection count to %hu"
762e48354ceSNicholas Bellinger 			" from node: %s\n", atomic_read(&sess->nconn),
763e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
764e48354ceSNicholas Bellinger 		spin_unlock_bh(&sess->conn_lock);
765e48354ceSNicholas Bellinger 
76688dcd2daSNicholas Bellinger 		iscsi_post_login_start_timers(conn);
767e48354ceSNicholas Bellinger 		/*
768e48354ceSNicholas Bellinger 		 * Determine CPU mask to ensure connection's RX and TX kthreads
769e48354ceSNicholas Bellinger 		 * are scheduled on the same CPU.
770e48354ceSNicholas Bellinger 		 */
771e48354ceSNicholas Bellinger 		iscsit_thread_get_cpumask(conn);
772e48354ceSNicholas Bellinger 		conn->conn_rx_reset_cpumask = 1;
773e48354ceSNicholas Bellinger 		conn->conn_tx_reset_cpumask = 1;
774e5419865SNicholas Bellinger 		/*
775e5419865SNicholas Bellinger 		 * Wakeup the sleeping iscsi_target_rx_thread() now that
776e5419865SNicholas Bellinger 		 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
777e5419865SNicholas Bellinger 		 */
778e5419865SNicholas Bellinger 		complete(&conn->rx_login_comp);
779e48354ceSNicholas Bellinger 		iscsit_dec_conn_usage_count(conn);
780e5419865SNicholas Bellinger 
781e48354ceSNicholas Bellinger 		if (stop_timer) {
782e48354ceSNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
783e48354ceSNicholas Bellinger 			iscsit_stop_time2retain_timer(sess);
784e48354ceSNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
785e48354ceSNicholas Bellinger 		}
786e48354ceSNicholas Bellinger 		iscsit_dec_session_usage_count(sess);
787e5419865SNicholas Bellinger 		return;
788e48354ceSNicholas Bellinger 	}
789e48354ceSNicholas Bellinger 
790e48354ceSNicholas Bellinger 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
791e48354ceSNicholas Bellinger 	iscsi_release_param_list(conn->param_list);
792e48354ceSNicholas Bellinger 	conn->param_list = NULL;
793e48354ceSNicholas Bellinger 
794e48354ceSNicholas Bellinger 	iscsit_determine_maxcmdsn(sess);
795e48354ceSNicholas Bellinger 
796e48354ceSNicholas Bellinger 	spin_lock_bh(&se_tpg->session_lock);
797e48354ceSNicholas Bellinger 	__transport_register_session(&sess->tpg->tpg_se_tpg,
7988359cf43SJörn Engel 			se_sess->se_node_acl, se_sess, sess);
799e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
800e48354ceSNicholas Bellinger 	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
801e48354ceSNicholas Bellinger 
802dc58f760SAndy Grover 	pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
803dc58f760SAndy Grover 		conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
8042f9bc894SNicholas Bellinger 		tpg->tpgt);
805e48354ceSNicholas Bellinger 
806e48354ceSNicholas Bellinger 	spin_lock_bh(&sess->conn_lock);
807e48354ceSNicholas Bellinger 	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
808e48354ceSNicholas Bellinger 	atomic_inc(&sess->nconn);
809e48354ceSNicholas Bellinger 	pr_debug("Incremented iSCSI Connection count to %hu from node:"
810e48354ceSNicholas Bellinger 		" %s\n", atomic_read(&sess->nconn),
811e48354ceSNicholas Bellinger 		sess->sess_ops->InitiatorName);
812e48354ceSNicholas Bellinger 	spin_unlock_bh(&sess->conn_lock);
813e48354ceSNicholas Bellinger 
814e48354ceSNicholas Bellinger 	sess->sid = tpg->sid++;
815e48354ceSNicholas Bellinger 	if (!sess->sid)
816e48354ceSNicholas Bellinger 		sess->sid = tpg->sid++;
817e48354ceSNicholas Bellinger 	pr_debug("Established iSCSI session from node: %s\n",
818e48354ceSNicholas Bellinger 			sess->sess_ops->InitiatorName);
819e48354ceSNicholas Bellinger 
820e48354ceSNicholas Bellinger 	tpg->nsessions++;
821e48354ceSNicholas Bellinger 	if (tpg->tpg_tiqn)
822e48354ceSNicholas Bellinger 		tpg->tpg_tiqn->tiqn_nsessions++;
823e48354ceSNicholas Bellinger 
824e48354ceSNicholas Bellinger 	pr_debug("Incremented number of active iSCSI sessions to %u on"
825e48354ceSNicholas Bellinger 		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
826e48354ceSNicholas Bellinger 	spin_unlock_bh(&se_tpg->session_lock);
827e48354ceSNicholas Bellinger 
828e48354ceSNicholas Bellinger 	iscsi_post_login_start_timers(conn);
829e48354ceSNicholas Bellinger 	/*
830e48354ceSNicholas Bellinger 	 * Determine CPU mask to ensure connection's RX and TX kthreads
831e48354ceSNicholas Bellinger 	 * are scheduled on the same CPU.
832e48354ceSNicholas Bellinger 	 */
833e48354ceSNicholas Bellinger 	iscsit_thread_get_cpumask(conn);
834e48354ceSNicholas Bellinger 	conn->conn_rx_reset_cpumask = 1;
835e48354ceSNicholas Bellinger 	conn->conn_tx_reset_cpumask = 1;
836e5419865SNicholas Bellinger 	/*
837e5419865SNicholas Bellinger 	 * Wakeup the sleeping iscsi_target_rx_thread() now that
838e5419865SNicholas Bellinger 	 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
839e5419865SNicholas Bellinger 	 */
840e5419865SNicholas Bellinger 	complete(&conn->rx_login_comp);
841e48354ceSNicholas Bellinger 	iscsit_dec_conn_usage_count(conn);
842e48354ceSNicholas Bellinger }
843e48354ceSNicholas Bellinger 
844f7c9564aSKees Cook void iscsi_handle_login_thread_timeout(struct timer_list *t)
845e48354ceSNicholas Bellinger {
846f7c9564aSKees Cook 	struct iscsi_np *np = from_timer(np, t, np_login_timer);
847e48354ceSNicholas Bellinger 
848e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
84969d75574SAndy Grover 	pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
85069d75574SAndy Grover 			&np->np_sockaddr);
851e48354ceSNicholas Bellinger 
852e48354ceSNicholas Bellinger 	if (np->np_login_timer_flags & ISCSI_TF_STOP) {
853e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
854e48354ceSNicholas Bellinger 		return;
855e48354ceSNicholas Bellinger 	}
856e48354ceSNicholas Bellinger 
857e48354ceSNicholas Bellinger 	if (np->np_thread)
858e48354ceSNicholas Bellinger 		send_sig(SIGINT, np->np_thread, 1);
859e48354ceSNicholas Bellinger 
860e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
861e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
862e48354ceSNicholas Bellinger }
863e48354ceSNicholas Bellinger 
864e48354ceSNicholas Bellinger static void iscsi_start_login_thread_timer(struct iscsi_np *np)
865e48354ceSNicholas Bellinger {
866e48354ceSNicholas Bellinger 	/*
867e48354ceSNicholas Bellinger 	 * This used the TA_LOGIN_TIMEOUT constant because at this
868e48354ceSNicholas Bellinger 	 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
869e48354ceSNicholas Bellinger 	 */
870e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
871e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_STOP;
872e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_RUNNING;
8738a47aa9dSBart Van Assche 	mod_timer(&np->np_login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
874e48354ceSNicholas Bellinger 
875e48354ceSNicholas Bellinger 	pr_debug("Added timeout timer to iSCSI login request for"
876e48354ceSNicholas Bellinger 			" %u seconds.\n", TA_LOGIN_TIMEOUT);
877e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
878e48354ceSNicholas Bellinger }
879e48354ceSNicholas Bellinger 
880e48354ceSNicholas Bellinger static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
881e48354ceSNicholas Bellinger {
882e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
883e48354ceSNicholas Bellinger 	if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
884e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
885e48354ceSNicholas Bellinger 		return;
886e48354ceSNicholas Bellinger 	}
887e48354ceSNicholas Bellinger 	np->np_login_timer_flags |= ISCSI_TF_STOP;
888e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
889e48354ceSNicholas Bellinger 
890e48354ceSNicholas Bellinger 	del_timer_sync(&np->np_login_timer);
891e48354ceSNicholas Bellinger 
892e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
893e48354ceSNicholas Bellinger 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
894e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
895e48354ceSNicholas Bellinger }
896e48354ceSNicholas Bellinger 
897baa4d64bSNicholas Bellinger int iscsit_setup_np(
898e48354ceSNicholas Bellinger 	struct iscsi_np *np,
89913a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
900e48354ceSNicholas Bellinger {
901baa4d64bSNicholas Bellinger 	struct socket *sock = NULL;
902837f6452SNicholas Bellinger 	int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
903e48354ceSNicholas Bellinger 
904e48354ceSNicholas Bellinger 	switch (np->np_network_transport) {
905e48354ceSNicholas Bellinger 	case ISCSI_TCP:
906e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_TCP;
907e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
908e48354ceSNicholas Bellinger 		break;
909e48354ceSNicholas Bellinger 	case ISCSI_SCTP_TCP:
910e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
911e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_STREAM;
912e48354ceSNicholas Bellinger 		break;
913e48354ceSNicholas Bellinger 	case ISCSI_SCTP_UDP:
914e48354ceSNicholas Bellinger 		np->np_ip_proto = IPPROTO_SCTP;
915e48354ceSNicholas Bellinger 		np->np_sock_type = SOCK_SEQPACKET;
916e48354ceSNicholas Bellinger 		break;
917e48354ceSNicholas Bellinger 	default:
918e48354ceSNicholas Bellinger 		pr_err("Unsupported network_transport: %d\n",
919e48354ceSNicholas Bellinger 				np->np_network_transport);
920e48354ceSNicholas Bellinger 		return -EINVAL;
921e48354ceSNicholas Bellinger 	}
922e48354ceSNicholas Bellinger 
923baa4d64bSNicholas Bellinger 	np->np_ip_proto = IPPROTO_TCP;
924baa4d64bSNicholas Bellinger 	np->np_sock_type = SOCK_STREAM;
925baa4d64bSNicholas Bellinger 
926e48354ceSNicholas Bellinger 	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
927e48354ceSNicholas Bellinger 			np->np_ip_proto, &sock);
928e48354ceSNicholas Bellinger 	if (ret < 0) {
929e48354ceSNicholas Bellinger 		pr_err("sock_create() failed.\n");
930e48354ceSNicholas Bellinger 		return ret;
931e48354ceSNicholas Bellinger 	}
932e48354ceSNicholas Bellinger 	np->np_socket = sock;
933e48354ceSNicholas Bellinger 	/*
934e48354ceSNicholas Bellinger 	 * Setup the np->np_sockaddr from the passed sockaddr setup
935e48354ceSNicholas Bellinger 	 * in iscsi_target_configfs.c code..
936e48354ceSNicholas Bellinger 	 */
9378359cf43SJörn Engel 	memcpy(&np->np_sockaddr, sockaddr,
93813a3cf08SAndy Grover 			sizeof(struct sockaddr_storage));
939e48354ceSNicholas Bellinger 
940e48354ceSNicholas Bellinger 	if (sockaddr->ss_family == AF_INET6)
941e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in6);
942e48354ceSNicholas Bellinger 	else
943e48354ceSNicholas Bellinger 		len = sizeof(struct sockaddr_in);
944e48354ceSNicholas Bellinger 	/*
945e48354ceSNicholas Bellinger 	 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
946e48354ceSNicholas Bellinger 	 */
9478359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
948e48354ceSNicholas Bellinger 	opt = 1;
949e48354ceSNicholas Bellinger 	if (np->np_network_transport == ISCSI_TCP) {
950e48354ceSNicholas Bellinger 		ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
951e48354ceSNicholas Bellinger 				(char *)&opt, sizeof(opt));
952e48354ceSNicholas Bellinger 		if (ret < 0) {
953e48354ceSNicholas Bellinger 			pr_err("kernel_setsockopt() for TCP_NODELAY"
954e48354ceSNicholas Bellinger 				" failed: %d\n", ret);
955e48354ceSNicholas Bellinger 			goto fail;
956e48354ceSNicholas Bellinger 		}
957e48354ceSNicholas Bellinger 	}
958e48354ceSNicholas Bellinger 
9598359cf43SJörn Engel 	/* FIXME: Someone please explain why this is endian-safe */
960e48354ceSNicholas Bellinger 	ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
961e48354ceSNicholas Bellinger 			(char *)&opt, sizeof(opt));
962e48354ceSNicholas Bellinger 	if (ret < 0) {
963e48354ceSNicholas Bellinger 		pr_err("kernel_setsockopt() for SO_REUSEADDR"
964e48354ceSNicholas Bellinger 			" failed\n");
965e48354ceSNicholas Bellinger 		goto fail;
966e48354ceSNicholas Bellinger 	}
967e48354ceSNicholas Bellinger 
9689f9ef6d3SDax Kelson 	ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
9699f9ef6d3SDax Kelson 			(char *)&opt, sizeof(opt));
9709f9ef6d3SDax Kelson 	if (ret < 0) {
9719f9ef6d3SDax Kelson 		pr_err("kernel_setsockopt() for IP_FREEBIND"
9729f9ef6d3SDax Kelson 			" failed\n");
9739f9ef6d3SDax Kelson 		goto fail;
9749f9ef6d3SDax Kelson 	}
9759f9ef6d3SDax Kelson 
976e48354ceSNicholas Bellinger 	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
977e48354ceSNicholas Bellinger 	if (ret < 0) {
978e48354ceSNicholas Bellinger 		pr_err("kernel_bind() failed: %d\n", ret);
979e48354ceSNicholas Bellinger 		goto fail;
980e48354ceSNicholas Bellinger 	}
981e48354ceSNicholas Bellinger 
982e48354ceSNicholas Bellinger 	ret = kernel_listen(sock, backlog);
983e48354ceSNicholas Bellinger 	if (ret != 0) {
984e48354ceSNicholas Bellinger 		pr_err("kernel_listen() failed: %d\n", ret);
985e48354ceSNicholas Bellinger 		goto fail;
986e48354ceSNicholas Bellinger 	}
987e48354ceSNicholas Bellinger 
988e48354ceSNicholas Bellinger 	return 0;
989e48354ceSNicholas Bellinger fail:
990e48354ceSNicholas Bellinger 	np->np_socket = NULL;
991e48354ceSNicholas Bellinger 	sock_release(sock);
992e48354ceSNicholas Bellinger 	return ret;
993e48354ceSNicholas Bellinger }
994e48354ceSNicholas Bellinger 
995baa4d64bSNicholas Bellinger int iscsi_target_setup_login_socket(
996baa4d64bSNicholas Bellinger 	struct iscsi_np *np,
99713a3cf08SAndy Grover 	struct sockaddr_storage *sockaddr)
998baa4d64bSNicholas Bellinger {
999baa4d64bSNicholas Bellinger 	struct iscsit_transport *t;
1000baa4d64bSNicholas Bellinger 	int rc;
1001baa4d64bSNicholas Bellinger 
1002baa4d64bSNicholas Bellinger 	t = iscsit_get_transport(np->np_network_transport);
1003baa4d64bSNicholas Bellinger 	if (!t)
1004baa4d64bSNicholas Bellinger 		return -EINVAL;
1005baa4d64bSNicholas Bellinger 
1006baa4d64bSNicholas Bellinger 	rc = t->iscsit_setup_np(np, sockaddr);
1007baa4d64bSNicholas Bellinger 	if (rc < 0) {
1008baa4d64bSNicholas Bellinger 		iscsit_put_transport(t);
1009baa4d64bSNicholas Bellinger 		return rc;
1010baa4d64bSNicholas Bellinger 	}
1011baa4d64bSNicholas Bellinger 
1012baa4d64bSNicholas Bellinger 	np->np_transport = t;
101314f4b54fSSagi Grimberg 	np->enabled = true;
1014baa4d64bSNicholas Bellinger 	return 0;
1015baa4d64bSNicholas Bellinger }
1016baa4d64bSNicholas Bellinger 
1017baa4d64bSNicholas Bellinger int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1018baa4d64bSNicholas Bellinger {
1019baa4d64bSNicholas Bellinger 	struct socket *new_sock, *sock = np->np_socket;
1020baa4d64bSNicholas Bellinger 	struct sockaddr_in sock_in;
1021baa4d64bSNicholas Bellinger 	struct sockaddr_in6 sock_in6;
10229b2c45d4SDenys Vlasenko 	int rc;
1023baa4d64bSNicholas Bellinger 
1024baa4d64bSNicholas Bellinger 	rc = kernel_accept(sock, &new_sock, 0);
1025baa4d64bSNicholas Bellinger 	if (rc < 0)
1026baa4d64bSNicholas Bellinger 		return rc;
1027baa4d64bSNicholas Bellinger 
1028baa4d64bSNicholas Bellinger 	conn->sock = new_sock;
1029baa4d64bSNicholas Bellinger 	conn->login_family = np->np_sockaddr.ss_family;
1030baa4d64bSNicholas Bellinger 
1031baa4d64bSNicholas Bellinger 	if (np->np_sockaddr.ss_family == AF_INET6) {
1032baa4d64bSNicholas Bellinger 		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1033baa4d64bSNicholas Bellinger 
1034baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
10359b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in6, 1);
10369b2c45d4SDenys Vlasenko 		if (rc >= 0) {
1037dc58f760SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1038dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
1039dc58f760SAndy Grover 			} else {
1040dc58f760SAndy Grover 				/* Pretend to be an ipv4 socket */
1041dc58f760SAndy Grover 				sock_in.sin_family = AF_INET;
1042dc58f760SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
1043dc58f760SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1044dc58f760SAndy Grover 				memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1045dc58f760SAndy Grover 			}
1046baa4d64bSNicholas Bellinger 		}
1047baa4d64bSNicholas Bellinger 
1048baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
10499b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in6, 0);
10509b2c45d4SDenys Vlasenko 		if (rc >= 0) {
105169d75574SAndy Grover 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
105269d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
105369d75574SAndy Grover 			} else {
105469d75574SAndy Grover 				/* Pretend to be an ipv4 socket */
105569d75574SAndy Grover 				sock_in.sin_family = AF_INET;
105669d75574SAndy Grover 				sock_in.sin_port = sock_in6.sin6_port;
105769d75574SAndy Grover 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
105869d75574SAndy Grover 				memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
105969d75574SAndy Grover 			}
1060baa4d64bSNicholas Bellinger 		}
1061baa4d64bSNicholas Bellinger 	} else {
1062baa4d64bSNicholas Bellinger 		memset(&sock_in, 0, sizeof(struct sockaddr_in));
1063baa4d64bSNicholas Bellinger 
1064baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
10659b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in, 1);
10669b2c45d4SDenys Vlasenko 		if (rc >= 0)
1067dc58f760SAndy Grover 			memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1068baa4d64bSNicholas Bellinger 
1069baa4d64bSNicholas Bellinger 		rc = conn->sock->ops->getname(conn->sock,
10709b2c45d4SDenys Vlasenko 				(struct sockaddr *)&sock_in, 0);
10719b2c45d4SDenys Vlasenko 		if (rc >= 0)
107269d75574SAndy Grover 			memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1073baa4d64bSNicholas Bellinger 	}
1074baa4d64bSNicholas Bellinger 
1075baa4d64bSNicholas Bellinger 	return 0;
1076baa4d64bSNicholas Bellinger }
1077baa4d64bSNicholas Bellinger 
1078baa4d64bSNicholas Bellinger int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1079baa4d64bSNicholas Bellinger {
1080baa4d64bSNicholas Bellinger 	struct iscsi_login_req *login_req;
1081baa4d64bSNicholas Bellinger 	u32 padding = 0, payload_length;
1082baa4d64bSNicholas Bellinger 
1083baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1084baa4d64bSNicholas Bellinger 		return -1;
1085baa4d64bSNicholas Bellinger 
1086baa4d64bSNicholas Bellinger 	login_req = (struct iscsi_login_req *)login->req;
1087baa4d64bSNicholas Bellinger 	payload_length	= ntoh24(login_req->dlength);
1088baa4d64bSNicholas Bellinger 	padding = ((-payload_length) & 3);
1089baa4d64bSNicholas Bellinger 
1090baa4d64bSNicholas Bellinger 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1091baa4d64bSNicholas Bellinger 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1092baa4d64bSNicholas Bellinger 		login_req->flags, login_req->itt, login_req->cmdsn,
1093baa4d64bSNicholas Bellinger 		login_req->exp_statsn, login_req->cid, payload_length);
1094baa4d64bSNicholas Bellinger 	/*
1095baa4d64bSNicholas Bellinger 	 * Setup the initial iscsi_login values from the leading
1096baa4d64bSNicholas Bellinger 	 * login request PDU.
1097baa4d64bSNicholas Bellinger 	 */
1098baa4d64bSNicholas Bellinger 	if (login->first_request) {
1099baa4d64bSNicholas Bellinger 		login_req = (struct iscsi_login_req *)login->req;
1100baa4d64bSNicholas Bellinger 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
11013e1c81a9SNicholas Bellinger 		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1102baa4d64bSNicholas Bellinger 		login->version_min	= login_req->min_version;
1103baa4d64bSNicholas Bellinger 		login->version_max	= login_req->max_version;
1104baa4d64bSNicholas Bellinger 		memcpy(login->isid, login_req->isid, 6);
1105baa4d64bSNicholas Bellinger 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1106baa4d64bSNicholas Bellinger 		login->init_task_tag	= login_req->itt;
1107baa4d64bSNicholas Bellinger 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1108baa4d64bSNicholas Bellinger 		login->cid		= be16_to_cpu(login_req->cid);
1109baa4d64bSNicholas Bellinger 		login->tsih		= be16_to_cpu(login_req->tsih);
1110baa4d64bSNicholas Bellinger 	}
1111baa4d64bSNicholas Bellinger 
1112baa4d64bSNicholas Bellinger 	if (iscsi_target_check_login_request(conn, login) < 0)
1113baa4d64bSNicholas Bellinger 		return -1;
1114baa4d64bSNicholas Bellinger 
1115baa4d64bSNicholas Bellinger 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1116baa4d64bSNicholas Bellinger 	if (iscsi_login_rx_data(conn, login->req_buf,
1117baa4d64bSNicholas Bellinger 				payload_length + padding) < 0)
1118baa4d64bSNicholas Bellinger 		return -1;
1119baa4d64bSNicholas Bellinger 
1120baa4d64bSNicholas Bellinger 	return 0;
1121baa4d64bSNicholas Bellinger }
1122baa4d64bSNicholas Bellinger 
1123baa4d64bSNicholas Bellinger int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1124baa4d64bSNicholas Bellinger 			u32 length)
1125baa4d64bSNicholas Bellinger {
1126baa4d64bSNicholas Bellinger 	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1127baa4d64bSNicholas Bellinger 		return -1;
1128baa4d64bSNicholas Bellinger 
1129baa4d64bSNicholas Bellinger 	return 0;
1130baa4d64bSNicholas Bellinger }
1131baa4d64bSNicholas Bellinger 
1132baa4d64bSNicholas Bellinger static int
1133baa4d64bSNicholas Bellinger iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1134baa4d64bSNicholas Bellinger {
1135baa4d64bSNicholas Bellinger 	int rc;
1136baa4d64bSNicholas Bellinger 
1137baa4d64bSNicholas Bellinger 	if (!t->owner) {
1138baa4d64bSNicholas Bellinger 		conn->conn_transport = t;
1139baa4d64bSNicholas Bellinger 		return 0;
1140baa4d64bSNicholas Bellinger 	}
1141baa4d64bSNicholas Bellinger 
1142baa4d64bSNicholas Bellinger 	rc = try_module_get(t->owner);
1143baa4d64bSNicholas Bellinger 	if (!rc) {
1144baa4d64bSNicholas Bellinger 		pr_err("try_module_get() failed for %s\n", t->name);
1145baa4d64bSNicholas Bellinger 		return -EINVAL;
1146baa4d64bSNicholas Bellinger 	}
1147baa4d64bSNicholas Bellinger 
1148baa4d64bSNicholas Bellinger 	conn->conn_transport = t;
1149baa4d64bSNicholas Bellinger 	return 0;
1150baa4d64bSNicholas Bellinger }
1151baa4d64bSNicholas Bellinger 
1152a91eb7d9SNicholas Bellinger void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1153a91eb7d9SNicholas Bellinger 		struct iscsi_np *np, bool zero_tsih, bool new_sess)
1154a91eb7d9SNicholas Bellinger {
11550bcc297eSChristophe Vu-Brugier 	if (!new_sess)
1156a91eb7d9SNicholas Bellinger 		goto old_sess_out;
1157a91eb7d9SNicholas Bellinger 
1158a91eb7d9SNicholas Bellinger 	pr_err("iSCSI Login negotiation failed.\n");
1159a91eb7d9SNicholas Bellinger 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1160a91eb7d9SNicholas Bellinger 				   ISCSI_LOGIN_STATUS_INIT_ERR);
1161a91eb7d9SNicholas Bellinger 	if (!zero_tsih || !conn->sess)
1162a91eb7d9SNicholas Bellinger 		goto old_sess_out;
116326abc916SMike Christie 
1164a91eb7d9SNicholas Bellinger 	transport_free_session(conn->sess->se_sess);
116531ff0ceeSMatthew Wilcox 	ida_free(&sess_ida, conn->sess->session_index);
1166a91eb7d9SNicholas Bellinger 	kfree(conn->sess->sess_ops);
1167a91eb7d9SNicholas Bellinger 	kfree(conn->sess);
1168a0b3b9b2SSagi Grimberg 	conn->sess = NULL;
1169a91eb7d9SNicholas Bellinger 
1170a91eb7d9SNicholas Bellinger old_sess_out:
1171a91eb7d9SNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1172a91eb7d9SNicholas Bellinger 	/*
1173a91eb7d9SNicholas Bellinger 	 * If login negotiation fails check if the Time2Retain timer
1174a91eb7d9SNicholas Bellinger 	 * needs to be restarted.
1175a91eb7d9SNicholas Bellinger 	 */
1176a91eb7d9SNicholas Bellinger 	if (!zero_tsih && conn->sess) {
1177a91eb7d9SNicholas Bellinger 		spin_lock_bh(&conn->sess->conn_lock);
1178a91eb7d9SNicholas Bellinger 		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1179a91eb7d9SNicholas Bellinger 			struct se_portal_group *se_tpg =
118060bfcf8eSAndy Grover 					&conn->tpg->tpg_se_tpg;
1181a91eb7d9SNicholas Bellinger 
1182a91eb7d9SNicholas Bellinger 			atomic_set(&conn->sess->session_continuation, 0);
1183a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1184a91eb7d9SNicholas Bellinger 			spin_lock_bh(&se_tpg->session_lock);
1185a91eb7d9SNicholas Bellinger 			iscsit_start_time2retain_handler(conn->sess);
1186a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&se_tpg->session_lock);
1187a91eb7d9SNicholas Bellinger 		} else
1188a91eb7d9SNicholas Bellinger 			spin_unlock_bh(&conn->sess->conn_lock);
1189a91eb7d9SNicholas Bellinger 		iscsit_dec_session_usage_count(conn->sess);
1190a91eb7d9SNicholas Bellinger 	}
1191a91eb7d9SNicholas Bellinger 
119269110e3cSHerbert Xu 	ahash_request_free(conn->conn_tx_hash);
119369110e3cSHerbert Xu 	if (conn->conn_rx_hash) {
119469110e3cSHerbert Xu 		struct crypto_ahash *tfm;
119569110e3cSHerbert Xu 
119669110e3cSHerbert Xu 		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
119769110e3cSHerbert Xu 		ahash_request_free(conn->conn_rx_hash);
119869110e3cSHerbert Xu 		crypto_free_ahash(tfm);
119969110e3cSHerbert Xu 	}
1200a91eb7d9SNicholas Bellinger 
1201a91eb7d9SNicholas Bellinger 	free_cpumask_var(conn->conn_cpumask);
1202a91eb7d9SNicholas Bellinger 
1203a91eb7d9SNicholas Bellinger 	kfree(conn->conn_ops);
1204a91eb7d9SNicholas Bellinger 
1205a91eb7d9SNicholas Bellinger 	if (conn->param_list) {
1206a91eb7d9SNicholas Bellinger 		iscsi_release_param_list(conn->param_list);
1207a91eb7d9SNicholas Bellinger 		conn->param_list = NULL;
1208a91eb7d9SNicholas Bellinger 	}
1209a91eb7d9SNicholas Bellinger 	iscsi_target_nego_release(conn);
1210a91eb7d9SNicholas Bellinger 
1211a91eb7d9SNicholas Bellinger 	if (conn->sock) {
1212a91eb7d9SNicholas Bellinger 		sock_release(conn->sock);
1213a91eb7d9SNicholas Bellinger 		conn->sock = NULL;
1214a91eb7d9SNicholas Bellinger 	}
1215a91eb7d9SNicholas Bellinger 
1216954f2372SSagi Grimberg 	if (conn->conn_transport->iscsit_wait_conn)
1217954f2372SSagi Grimberg 		conn->conn_transport->iscsit_wait_conn(conn);
1218954f2372SSagi Grimberg 
1219a91eb7d9SNicholas Bellinger 	if (conn->conn_transport->iscsit_free_conn)
1220a91eb7d9SNicholas Bellinger 		conn->conn_transport->iscsit_free_conn(conn);
1221a91eb7d9SNicholas Bellinger 
1222a91eb7d9SNicholas Bellinger 	iscsit_put_transport(conn->conn_transport);
1223a91eb7d9SNicholas Bellinger 	kfree(conn);
1224a91eb7d9SNicholas Bellinger }
1225a91eb7d9SNicholas Bellinger 
1226e48354ceSNicholas Bellinger static int __iscsi_target_login_thread(struct iscsi_np *np)
1227e48354ceSNicholas Bellinger {
1228baa4d64bSNicholas Bellinger 	u8 *buffer, zero_tsih = 0;
122981a9c5e7SMikulas Patocka 	int ret = 0, rc;
1230e48354ceSNicholas Bellinger 	struct iscsi_conn *conn = NULL;
1231e48354ceSNicholas Bellinger 	struct iscsi_login *login;
1232e48354ceSNicholas Bellinger 	struct iscsi_portal_group *tpg = NULL;
1233e48354ceSNicholas Bellinger 	struct iscsi_login_req *pdu;
1234a91eb7d9SNicholas Bellinger 	struct iscsi_tpg_np *tpg_np;
1235a91eb7d9SNicholas Bellinger 	bool new_sess = false;
1236e48354ceSNicholas Bellinger 
1237e48354ceSNicholas Bellinger 	flush_signals(current);
1238e48354ceSNicholas Bellinger 
1239e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1240978d13d6SNicholas Bellinger 	if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1241e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1242978d13d6SNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1243e48354ceSNicholas Bellinger 		complete(&np->np_restart_comp);
1244978d13d6SNicholas Bellinger 		return 1;
124581a9c5e7SMikulas Patocka 	} else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
124681a9c5e7SMikulas Patocka 		spin_unlock_bh(&np->np_thread_lock);
124781a9c5e7SMikulas Patocka 		goto exit;
1248e48354ceSNicholas Bellinger 	} else {
1249e48354ceSNicholas Bellinger 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1250e48354ceSNicholas Bellinger 	}
1251e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1252e48354ceSNicholas Bellinger 
1253e48354ceSNicholas Bellinger 	conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1254e48354ceSNicholas Bellinger 	if (!conn) {
1255e48354ceSNicholas Bellinger 		pr_err("Could not allocate memory for"
1256e48354ceSNicholas Bellinger 			" new connection\n");
1257e48354ceSNicholas Bellinger 		/* Get another socket */
1258e48354ceSNicholas Bellinger 		return 1;
1259e48354ceSNicholas Bellinger 	}
1260e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1261e48354ceSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_FREE;
1262e48354ceSNicholas Bellinger 
1263f7c9564aSKees Cook 	timer_setup(&conn->nopin_response_timer,
1264f7c9564aSKees Cook 		    iscsit_handle_nopin_response_timeout, 0);
1265f7c9564aSKees Cook 	timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0);
12668a47aa9dSBart Van Assche 
1267baa4d64bSNicholas Bellinger 	if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1268baa4d64bSNicholas Bellinger 		kfree(conn);
1269baa4d64bSNicholas Bellinger 		return 1;
1270baa4d64bSNicholas Bellinger 	}
1271e48354ceSNicholas Bellinger 
1272baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_accept_np(np, conn);
1273baa4d64bSNicholas Bellinger 	if (rc == -ENOSYS) {
1274baa4d64bSNicholas Bellinger 		complete(&np->np_restart_comp);
1275baa4d64bSNicholas Bellinger 		iscsit_put_transport(conn->conn_transport);
1276baa4d64bSNicholas Bellinger 		kfree(conn);
1277baa4d64bSNicholas Bellinger 		conn = NULL;
1278baa4d64bSNicholas Bellinger 		goto exit;
1279baa4d64bSNicholas Bellinger 	} else if (rc < 0) {
1280baa4d64bSNicholas Bellinger 		spin_lock_bh(&np->np_thread_lock);
1281978d13d6SNicholas Bellinger 		if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1282978d13d6SNicholas Bellinger 			np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1283baa4d64bSNicholas Bellinger 			spin_unlock_bh(&np->np_thread_lock);
1284baa4d64bSNicholas Bellinger 			complete(&np->np_restart_comp);
1285baa4d64bSNicholas Bellinger 			iscsit_put_transport(conn->conn_transport);
1286baa4d64bSNicholas Bellinger 			kfree(conn);
1287baa4d64bSNicholas Bellinger 			conn = NULL;
1288baa4d64bSNicholas Bellinger 			/* Get another socket */
1289baa4d64bSNicholas Bellinger 			return 1;
1290baa4d64bSNicholas Bellinger 		}
1291baa4d64bSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
1292baa4d64bSNicholas Bellinger 		iscsit_put_transport(conn->conn_transport);
1293baa4d64bSNicholas Bellinger 		kfree(conn);
1294baa4d64bSNicholas Bellinger 		conn = NULL;
1295baa4d64bSNicholas Bellinger 		goto out;
1296e48354ceSNicholas Bellinger 	}
1297e48354ceSNicholas Bellinger 	/*
1298e48354ceSNicholas Bellinger 	 * Perform the remaining iSCSI connection initialization items..
1299e48354ceSNicholas Bellinger 	 */
1300baa4d64bSNicholas Bellinger 	login = iscsi_login_init_conn(conn);
1301baa4d64bSNicholas Bellinger 	if (!login) {
1302e48354ceSNicholas Bellinger 		goto new_sess_out;
1303e48354ceSNicholas Bellinger 	}
1304e48354ceSNicholas Bellinger 
1305baa4d64bSNicholas Bellinger 	iscsi_start_login_thread_timer(np);
1306e48354ceSNicholas Bellinger 
1307baa4d64bSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1308baa4d64bSNicholas Bellinger 	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1309baa4d64bSNicholas Bellinger 	/*
1310baa4d64bSNicholas Bellinger 	 * This will process the first login request + payload..
1311baa4d64bSNicholas Bellinger 	 */
1312baa4d64bSNicholas Bellinger 	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1313baa4d64bSNicholas Bellinger 	if (rc == 1)
1314baa4d64bSNicholas Bellinger 		return 1;
1315baa4d64bSNicholas Bellinger 	else if (rc < 0)
1316baa4d64bSNicholas Bellinger 		goto new_sess_out;
1317baa4d64bSNicholas Bellinger 
1318baa4d64bSNicholas Bellinger 	buffer = &login->req[0];
1319e48354ceSNicholas Bellinger 	pdu = (struct iscsi_login_req *)buffer;
1320e48354ceSNicholas Bellinger 	/*
1321e48354ceSNicholas Bellinger 	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1322e48354ceSNicholas Bellinger 	 * when Status-Class != 0.
1323e48354ceSNicholas Bellinger 	*/
1324e48354ceSNicholas Bellinger 	conn->login_itt	= pdu->itt;
1325e48354ceSNicholas Bellinger 
1326e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1327e48354ceSNicholas Bellinger 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1328e48354ceSNicholas Bellinger 		spin_unlock_bh(&np->np_thread_lock);
132969d75574SAndy Grover 		pr_err("iSCSI Network Portal on %pISpc currently not"
133069d75574SAndy Grover 			" active.\n", &np->np_sockaddr);
1331e48354ceSNicholas Bellinger 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1332e48354ceSNicholas Bellinger 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1333e48354ceSNicholas Bellinger 		goto new_sess_out;
1334e48354ceSNicholas Bellinger 	}
1335e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1336e48354ceSNicholas Bellinger 
1337e48354ceSNicholas Bellinger 	conn->network_transport = np->np_network_transport;
1338e48354ceSNicholas Bellinger 
1339dc58f760SAndy Grover 	pr_debug("Received iSCSI login request from %pISpc on %s Network"
1340dc58f760SAndy Grover 		" Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
134169d75574SAndy Grover 		&conn->local_sockaddr);
1342e48354ceSNicholas Bellinger 
1343e48354ceSNicholas Bellinger 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1344e48354ceSNicholas Bellinger 	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1345e48354ceSNicholas Bellinger 
1346e48354ceSNicholas Bellinger 	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1347e48354ceSNicholas Bellinger 			pdu->min_version) < 0)
1348e48354ceSNicholas Bellinger 		goto new_sess_out;
1349e48354ceSNicholas Bellinger 
1350e48354ceSNicholas Bellinger 	zero_tsih = (pdu->tsih == 0x0000);
1351ee1b1b9cSAndy Grover 	if (zero_tsih) {
1352e48354ceSNicholas Bellinger 		/*
1353e48354ceSNicholas Bellinger 		 * This is the leading connection of a new session.
1354e48354ceSNicholas Bellinger 		 * We wait until after authentication to check for
1355e48354ceSNicholas Bellinger 		 * session reinstatement.
1356e48354ceSNicholas Bellinger 		 */
1357e48354ceSNicholas Bellinger 		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1358e48354ceSNicholas Bellinger 			goto new_sess_out;
1359e48354ceSNicholas Bellinger 	} else {
1360e48354ceSNicholas Bellinger 		/*
1361e48354ceSNicholas Bellinger 		 * Add a new connection to an existing session.
1362e48354ceSNicholas Bellinger 		 * We check for a non-existant session in
1363e48354ceSNicholas Bellinger 		 * iscsi_login_non_zero_tsih_s2() below based
1364e48354ceSNicholas Bellinger 		 * on ISID/TSIH, but wait until after authentication
1365e48354ceSNicholas Bellinger 		 * to check for connection reinstatement, etc.
1366e48354ceSNicholas Bellinger 		 */
1367e48354ceSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1368e48354ceSNicholas Bellinger 			goto new_sess_out;
1369e48354ceSNicholas Bellinger 	}
1370e48354ceSNicholas Bellinger 	/*
1371baa4d64bSNicholas Bellinger 	 * SessionType: Discovery
1372baa4d64bSNicholas Bellinger 	 *
1373baa4d64bSNicholas Bellinger 	 * 	Locates Default Portal
1374baa4d64bSNicholas Bellinger 	 *
1375baa4d64bSNicholas Bellinger 	 * SessionType: Normal
1376baa4d64bSNicholas Bellinger 	 *
1377baa4d64bSNicholas Bellinger 	 * 	Locates Target Portal from NP -> Target IQN
1378e48354ceSNicholas Bellinger 	 */
1379baa4d64bSNicholas Bellinger 	rc = iscsi_target_locate_portal(np, conn, login);
1380baa4d64bSNicholas Bellinger 	if (rc < 0) {
1381e48354ceSNicholas Bellinger 		tpg = conn->tpg;
1382e48354ceSNicholas Bellinger 		goto new_sess_out;
1383e48354ceSNicholas Bellinger 	}
1384a91eb7d9SNicholas Bellinger 	login->zero_tsih = zero_tsih;
1385e48354ceSNicholas Bellinger 
13868abc718dSFeng Li 	if (conn->sess)
138723a548eeSSagi Grimberg 		conn->sess->se_sess->sup_prot_ops =
138823a548eeSSagi Grimberg 			conn->conn_transport->iscsit_get_sup_prot_ops(conn);
138923a548eeSSagi Grimberg 
1390e48354ceSNicholas Bellinger 	tpg = conn->tpg;
1391e48354ceSNicholas Bellinger 	if (!tpg) {
1392e48354ceSNicholas Bellinger 		pr_err("Unable to locate struct iscsi_conn->tpg\n");
1393e48354ceSNicholas Bellinger 		goto new_sess_out;
1394e48354ceSNicholas Bellinger 	}
1395e48354ceSNicholas Bellinger 
1396e48354ceSNicholas Bellinger 	if (zero_tsih) {
1397baa4d64bSNicholas Bellinger 		if (iscsi_login_zero_tsih_s2(conn) < 0)
1398e48354ceSNicholas Bellinger 			goto new_sess_out;
1399e48354ceSNicholas Bellinger 	} else {
1400baa4d64bSNicholas Bellinger 		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1401e48354ceSNicholas Bellinger 			goto old_sess_out;
1402e48354ceSNicholas Bellinger 	}
1403e48354ceSNicholas Bellinger 
140442bc3e57SVarun Prakash 	if (conn->conn_transport->iscsit_validate_params) {
140542bc3e57SVarun Prakash 		ret = conn->conn_transport->iscsit_validate_params(conn);
140642bc3e57SVarun Prakash 		if (ret < 0) {
140742bc3e57SVarun Prakash 			if (zero_tsih)
140842bc3e57SVarun Prakash 				goto new_sess_out;
140942bc3e57SVarun Prakash 			else
141042bc3e57SVarun Prakash 				goto old_sess_out;
141142bc3e57SVarun Prakash 		}
141242bc3e57SVarun Prakash 	}
141342bc3e57SVarun Prakash 
1414a91eb7d9SNicholas Bellinger 	ret = iscsi_target_start_negotiation(login, conn);
1415a91eb7d9SNicholas Bellinger 	if (ret < 0)
1416e48354ceSNicholas Bellinger 		goto new_sess_out;
1417e48354ceSNicholas Bellinger 
1418e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1419e48354ceSNicholas Bellinger 
1420a91eb7d9SNicholas Bellinger 	if (ret == 1) {
1421a91eb7d9SNicholas Bellinger 		tpg_np = conn->tpg_np;
1422e48354ceSNicholas Bellinger 
1423e5419865SNicholas Bellinger 		iscsi_post_login_handler(np, conn, zero_tsih);
1424a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1425a91eb7d9SNicholas Bellinger 	}
1426a91eb7d9SNicholas Bellinger 
1427e48354ceSNicholas Bellinger 	tpg = NULL;
1428a91eb7d9SNicholas Bellinger 	tpg_np = NULL;
1429e48354ceSNicholas Bellinger 	/* Get another socket */
1430e48354ceSNicholas Bellinger 	return 1;
1431e48354ceSNicholas Bellinger 
1432e48354ceSNicholas Bellinger new_sess_out:
1433a91eb7d9SNicholas Bellinger 	new_sess = true;
1434e48354ceSNicholas Bellinger old_sess_out:
1435a91eb7d9SNicholas Bellinger 	tpg_np = conn->tpg_np;
1436a91eb7d9SNicholas Bellinger 	iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1437a91eb7d9SNicholas Bellinger 	new_sess = false;
1438e48354ceSNicholas Bellinger 
1439e48354ceSNicholas Bellinger 	if (tpg) {
1440a91eb7d9SNicholas Bellinger 		iscsit_deaccess_np(np, tpg, tpg_np);
1441e48354ceSNicholas Bellinger 		tpg = NULL;
1442a91eb7d9SNicholas Bellinger 		tpg_np = NULL;
1443e48354ceSNicholas Bellinger 	}
1444e48354ceSNicholas Bellinger 
1445e48354ceSNicholas Bellinger out:
1446e48354ceSNicholas Bellinger 	return 1;
144781a9c5e7SMikulas Patocka 
1448baa4d64bSNicholas Bellinger exit:
1449e48354ceSNicholas Bellinger 	iscsi_stop_login_thread_timer(np);
1450e48354ceSNicholas Bellinger 	spin_lock_bh(&np->np_thread_lock);
1451e48354ceSNicholas Bellinger 	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1452e48354ceSNicholas Bellinger 	spin_unlock_bh(&np->np_thread_lock);
1453baa4d64bSNicholas Bellinger 
1454e48354ceSNicholas Bellinger 	return 0;
1455e48354ceSNicholas Bellinger }
1456e48354ceSNicholas Bellinger 
1457e48354ceSNicholas Bellinger int iscsi_target_login_thread(void *arg)
1458e48354ceSNicholas Bellinger {
14598359cf43SJörn Engel 	struct iscsi_np *np = arg;
1460e48354ceSNicholas Bellinger 	int ret;
1461e48354ceSNicholas Bellinger 
1462e48354ceSNicholas Bellinger 	allow_signal(SIGINT);
1463e48354ceSNicholas Bellinger 
146481a9c5e7SMikulas Patocka 	while (1) {
1465e48354ceSNicholas Bellinger 		ret = __iscsi_target_login_thread(np);
1466e48354ceSNicholas Bellinger 		/*
1467e48354ceSNicholas Bellinger 		 * We break and exit here unless another sock_accept() call
1468e48354ceSNicholas Bellinger 		 * is expected.
1469e48354ceSNicholas Bellinger 		 */
1470e48354ceSNicholas Bellinger 		if (ret != 1)
1471e48354ceSNicholas Bellinger 			break;
1472e48354ceSNicholas Bellinger 	}
1473e48354ceSNicholas Bellinger 
14745e0cf5e6SJiang Yi 	while (!kthread_should_stop()) {
14755e0cf5e6SJiang Yi 		msleep(100);
14765e0cf5e6SJiang Yi 	}
14775e0cf5e6SJiang Yi 
1478e48354ceSNicholas Bellinger 	return 0;
1479e48354ceSNicholas Bellinger }
1480