1// SPDX-License-Identifier: GPL-2.0-or-later
2/*******************************************************************************
3 * This file contains the login functions used by the iSCSI Target driver.
4 *
5 * (c) Copyright 2007-2013 Datera, Inc.
6 *
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 *
9 ******************************************************************************/
10
11#include <crypto/hash.h>
12#include <linux/module.h>
13#include <linux/string.h>
14#include <linux/kthread.h>
15#include <linux/sched/signal.h>
16#include <linux/idr.h>
17#include <linux/tcp.h>        /* TCP_NODELAY */
18#include <net/ip.h>
19#include <net/ipv6.h>         /* ipv6_addr_v4mapped() */
20#include <scsi/iscsi_proto.h>
21#include <target/target_core_base.h>
22#include <target/target_core_fabric.h>
23
24#include <target/iscsi/iscsi_target_core.h>
25#include <target/iscsi/iscsi_target_stat.h>
26#include "iscsi_target_device.h"
27#include "iscsi_target_nego.h"
28#include "iscsi_target_erl0.h"
29#include "iscsi_target_erl2.h"
30#include "iscsi_target_login.h"
31#include "iscsi_target_tpg.h"
32#include "iscsi_target_util.h"
33#include "iscsi_target.h"
34#include "iscsi_target_parameters.h"
35
36#include <target/iscsi/iscsi_transport.h>
37
38static struct iscsi_login *iscsi_login_init_conn(struct iscsit_conn *conn)
39{
40	struct iscsi_login *login;
41
42	login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
43	if (!login) {
44		pr_err("Unable to allocate memory for struct iscsi_login.\n");
45		return NULL;
46	}
47	conn->login = login;
48	login->conn = conn;
49	login->first_request = 1;
50
51	login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
52	if (!login->req_buf) {
53		pr_err("Unable to allocate memory for response buffer.\n");
54		goto out_login;
55	}
56
57	login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
58	if (!login->rsp_buf) {
59		pr_err("Unable to allocate memory for request buffer.\n");
60		goto out_req_buf;
61	}
62
63	conn->conn_login = login;
64
65	return login;
66
67out_req_buf:
68	kfree(login->req_buf);
69out_login:
70	kfree(login);
71	return NULL;
72}
73
74/*
75 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
76 * per struct iscsit_conn libcrypto contexts for crc32c and crc32-intel
77 */
78int iscsi_login_setup_crypto(struct iscsit_conn *conn)
79{
80	struct crypto_ahash *tfm;
81
82	/*
83	 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
84	 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
85	 * to software 1x8 byte slicing from crc32c.ko
86	 */
87	tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
88	if (IS_ERR(tfm)) {
89		pr_err("crypto_alloc_ahash() failed\n");
90		return -ENOMEM;
91	}
92
93	conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
94	if (!conn->conn_rx_hash) {
95		pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
96		crypto_free_ahash(tfm);
97		return -ENOMEM;
98	}
99	ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
100
101	conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
102	if (!conn->conn_tx_hash) {
103		pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
104		ahash_request_free(conn->conn_rx_hash);
105		conn->conn_rx_hash = NULL;
106		crypto_free_ahash(tfm);
107		return -ENOMEM;
108	}
109	ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
110
111	return 0;
112}
113
114static int iscsi_login_check_initiator_version(
115	struct iscsit_conn *conn,
116	u8 version_max,
117	u8 version_min)
118{
119	if ((version_max != 0x00) || (version_min != 0x00)) {
120		pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
121			" version Min/Max 0x%02x/0x%02x, rejecting login.\n",
122			version_min, version_max);
123		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
124				ISCSI_LOGIN_STATUS_NO_VERSION);
125		return -1;
126	}
127
128	return 0;
129}
130
131int iscsi_check_for_session_reinstatement(struct iscsit_conn *conn)
132{
133	int sessiontype;
134	struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
135	struct iscsi_portal_group *tpg = conn->tpg;
136	struct iscsit_session *sess = NULL, *sess_p = NULL;
137	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
138	struct se_session *se_sess, *se_sess_tmp;
139
140	initiatorname_param = iscsi_find_param_from_key(
141			INITIATORNAME, conn->param_list);
142	sessiontype_param = iscsi_find_param_from_key(
143			SESSIONTYPE, conn->param_list);
144	if (!initiatorname_param || !sessiontype_param) {
145		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
146			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
147		return -1;
148	}
149
150	sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
151
152	spin_lock_bh(&se_tpg->session_lock);
153	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
154			sess_list) {
155
156		sess_p = se_sess->fabric_sess_ptr;
157		spin_lock(&sess_p->conn_lock);
158		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
159		    atomic_read(&sess_p->session_logout) ||
160		    atomic_read(&sess_p->session_close) ||
161		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
162			spin_unlock(&sess_p->conn_lock);
163			continue;
164		}
165		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
166		   (!strcmp(sess_p->sess_ops->InitiatorName,
167			    initiatorname_param->value) &&
168		   (sess_p->sess_ops->SessionType == sessiontype))) {
169			atomic_set(&sess_p->session_reinstatement, 1);
170			atomic_set(&sess_p->session_fall_back_to_erl0, 1);
171			atomic_set(&sess_p->session_close, 1);
172			spin_unlock(&sess_p->conn_lock);
173			iscsit_inc_session_usage_count(sess_p);
174			iscsit_stop_time2retain_timer(sess_p);
175			sess = sess_p;
176			break;
177		}
178		spin_unlock(&sess_p->conn_lock);
179	}
180	spin_unlock_bh(&se_tpg->session_lock);
181	/*
182	 * If the Time2Retain handler has expired, the session is already gone.
183	 */
184	if (!sess)
185		return 0;
186
187	pr_debug("%s iSCSI Session SID %u is still active for %s,"
188		" performing session reinstatement.\n", (sessiontype) ?
189		"Discovery" : "Normal", sess->sid,
190		sess->sess_ops->InitiatorName);
191
192	spin_lock_bh(&sess->conn_lock);
193	if (sess->session_state == TARG_SESS_STATE_FAILED) {
194		spin_unlock_bh(&sess->conn_lock);
195		iscsit_dec_session_usage_count(sess);
196		return 0;
197	}
198	spin_unlock_bh(&sess->conn_lock);
199
200	iscsit_stop_session(sess, 1, 1);
201	iscsit_dec_session_usage_count(sess);
202
203	return 0;
204}
205
206static int iscsi_login_set_conn_values(
207	struct iscsit_session *sess,
208	struct iscsit_conn *conn,
209	__be16 cid)
210{
211	int ret;
212	conn->sess		= sess;
213	conn->cid		= be16_to_cpu(cid);
214	/*
215	 * Generate a random Status sequence number (statsn) for the new
216	 * iSCSI connection.
217	 */
218	ret = get_random_bytes_wait(&conn->stat_sn, sizeof(u32));
219	if (unlikely(ret))
220		return ret;
221
222	mutex_lock(&auth_id_lock);
223	conn->auth_id		= iscsit_global->auth_id++;
224	mutex_unlock(&auth_id_lock);
225	return 0;
226}
227
228__printf(2, 3) int iscsi_change_param_sprintf(
229	struct iscsit_conn *conn,
230	const char *fmt, ...)
231{
232	va_list args;
233	unsigned char buf[64];
234
235	memset(buf, 0, sizeof buf);
236
237	va_start(args, fmt);
238	vsnprintf(buf, sizeof buf, fmt, args);
239	va_end(args);
240
241	if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
242		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
243				ISCSI_LOGIN_STATUS_NO_RESOURCES);
244		return -1;
245	}
246
247	return 0;
248}
249EXPORT_SYMBOL(iscsi_change_param_sprintf);
250
251/*
252 *	This is the leading connection of a new session,
253 *	or session reinstatement.
254 */
255static int iscsi_login_zero_tsih_s1(
256	struct iscsit_conn *conn,
257	unsigned char *buf)
258{
259	struct iscsit_session *sess = NULL;
260	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
261	int ret;
262
263	sess = kzalloc(sizeof(struct iscsit_session), GFP_KERNEL);
264	if (!sess) {
265		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
266				ISCSI_LOGIN_STATUS_NO_RESOURCES);
267		pr_err("Could not allocate memory for session\n");
268		return -ENOMEM;
269	}
270
271	if (iscsi_login_set_conn_values(sess, conn, pdu->cid))
272		goto free_sess;
273
274	sess->init_task_tag	= pdu->itt;
275	memcpy(&sess->isid, pdu->isid, 6);
276	sess->exp_cmd_sn	= be32_to_cpu(pdu->cmdsn);
277	INIT_LIST_HEAD(&sess->sess_conn_list);
278	INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
279	INIT_LIST_HEAD(&sess->cr_active_list);
280	INIT_LIST_HEAD(&sess->cr_inactive_list);
281	init_completion(&sess->async_msg_comp);
282	init_completion(&sess->reinstatement_comp);
283	init_completion(&sess->session_wait_comp);
284	init_completion(&sess->session_waiting_on_uc_comp);
285	mutex_init(&sess->cmdsn_mutex);
286	spin_lock_init(&sess->conn_lock);
287	spin_lock_init(&sess->cr_a_lock);
288	spin_lock_init(&sess->cr_i_lock);
289	spin_lock_init(&sess->session_usage_lock);
290	spin_lock_init(&sess->ttt_lock);
291
292	timer_setup(&sess->time2retain_timer,
293		    iscsit_handle_time2retain_timeout, 0);
294
295	ret = ida_alloc(&sess_ida, GFP_KERNEL);
296	if (ret < 0) {
297		pr_err("Session ID allocation failed %d\n", ret);
298		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
299				ISCSI_LOGIN_STATUS_NO_RESOURCES);
300		goto free_sess;
301	}
302
303	sess->session_index = ret;
304	sess->creation_time = get_jiffies_64();
305	/*
306	 * The FFP CmdSN window values will be allocated from the TPG's
307	 * Initiator Node's ACL once the login has been successfully completed.
308	 */
309	atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
310
311	sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
312	if (!sess->sess_ops) {
313		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
314				ISCSI_LOGIN_STATUS_NO_RESOURCES);
315		pr_err("Unable to allocate memory for"
316				" struct iscsi_sess_ops.\n");
317		goto free_id;
318	}
319
320	sess->se_sess = transport_alloc_session(TARGET_PROT_NORMAL);
321	if (IS_ERR(sess->se_sess)) {
322		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
323				ISCSI_LOGIN_STATUS_NO_RESOURCES);
324		goto free_ops;
325	}
326
327	return 0;
328
329free_ops:
330	kfree(sess->sess_ops);
331free_id:
332	ida_free(&sess_ida, sess->session_index);
333free_sess:
334	kfree(sess);
335	conn->sess = NULL;
336	return -ENOMEM;
337}
338
339static int iscsi_login_zero_tsih_s2(
340	struct iscsit_conn *conn)
341{
342	struct iscsi_node_attrib *na;
343	struct iscsit_session *sess = conn->sess;
344	struct iscsi_param *param;
345	bool iser = false;
346
347	sess->tpg = conn->tpg;
348
349	/*
350	 * Assign a new TPG Session Handle.  Note this is protected with
351	 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
352	 */
353	sess->tsih = ++sess->tpg->ntsih;
354	if (!sess->tsih)
355		sess->tsih = ++sess->tpg->ntsih;
356
357	/*
358	 * Create the default params from user defined values..
359	 */
360	if (iscsi_copy_param_list(&conn->param_list,
361				conn->tpg->param_list, 1) < 0) {
362		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
363				ISCSI_LOGIN_STATUS_NO_RESOURCES);
364		return -1;
365	}
366
367	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
368		iser = true;
369
370	iscsi_set_keys_to_negotiate(conn->param_list, iser);
371
372	if (sess->sess_ops->SessionType)
373		return iscsi_set_keys_irrelevant_for_discovery(
374				conn->param_list);
375
376	na = iscsit_tpg_get_node_attrib(sess);
377
378	/*
379	 * If ACL allows non-authorized access in TPG with CHAP,
380	 * then set None to AuthMethod.
381	 */
382	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
383	if (param && !strstr(param->value, NONE)) {
384		if (!iscsi_conn_auth_required(conn))
385			if (iscsi_change_param_sprintf(conn, "AuthMethod=%s",
386						       NONE))
387				return -1;
388	}
389
390	/*
391	 * Need to send TargetPortalGroupTag back in first login response
392	 * on any iSCSI connection where the Initiator provides TargetName.
393	 * See 5.3.1.  Login Phase Start
394	 *
395	 * In our case, we have already located the struct iscsi_tiqn at this point.
396	 */
397	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
398		return -1;
399
400	/*
401	 * Workaround for Initiators that have broken connection recovery logic.
402	 *
403	 * "We would really like to get rid of this." Linux-iSCSI.org team
404	 */
405	if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
406		return -1;
407
408	/*
409	 * Set RDMAExtensions=Yes by default for iSER enabled network portals
410	 */
411	if (iser) {
412		struct iscsi_param *param;
413		unsigned long mrdsl, off;
414		int rc;
415
416		if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
417			return -1;
418
419		/*
420		 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
421		 * Immediate Data + Unsolicited Data-OUT if necessary..
422		 */
423		param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
424						  conn->param_list);
425		if (!param) {
426			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
427				ISCSI_LOGIN_STATUS_NO_RESOURCES);
428			return -1;
429		}
430		rc = kstrtoul(param->value, 0, &mrdsl);
431		if (rc < 0) {
432			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
433				ISCSI_LOGIN_STATUS_NO_RESOURCES);
434			return -1;
435		}
436		off = mrdsl % PAGE_SIZE;
437		if (!off)
438			goto check_prot;
439
440		if (mrdsl < PAGE_SIZE)
441			mrdsl = PAGE_SIZE;
442		else
443			mrdsl -= off;
444
445		pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
446			" to PAGE_SIZE\n", mrdsl);
447
448		if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
449			return -1;
450		/*
451		 * ISER currently requires that ImmediateData + Unsolicited
452		 * Data be disabled when protection / signature MRs are enabled.
453		 */
454check_prot:
455		if (sess->se_sess->sup_prot_ops &
456		   (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
457		    TARGET_PROT_DOUT_INSERT)) {
458
459			if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
460				return -1;
461
462			if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
463				return -1;
464
465			pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
466				 " T10-PI enabled ISER session\n");
467		}
468	}
469
470	return 0;
471}
472
473static int iscsi_login_non_zero_tsih_s1(
474	struct iscsit_conn *conn,
475	unsigned char *buf)
476{
477	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
478
479	return iscsi_login_set_conn_values(NULL, conn, pdu->cid);
480}
481
482/*
483 *	Add a new connection to an existing session.
484 */
485static int iscsi_login_non_zero_tsih_s2(
486	struct iscsit_conn *conn,
487	unsigned char *buf)
488{
489	struct iscsi_portal_group *tpg = conn->tpg;
490	struct iscsit_session *sess = NULL, *sess_p = NULL;
491	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
492	struct se_session *se_sess, *se_sess_tmp;
493	struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
494	bool iser = false;
495
496	spin_lock_bh(&se_tpg->session_lock);
497	list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
498			sess_list) {
499
500		sess_p = (struct iscsit_session *)se_sess->fabric_sess_ptr;
501		if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
502		    atomic_read(&sess_p->session_logout) ||
503		    atomic_read(&sess_p->session_close) ||
504		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
505			continue;
506		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
507		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
508			iscsit_inc_session_usage_count(sess_p);
509			iscsit_stop_time2retain_timer(sess_p);
510			sess = sess_p;
511			break;
512		}
513	}
514	spin_unlock_bh(&se_tpg->session_lock);
515
516	/*
517	 * If the Time2Retain handler has expired, the session is already gone.
518	 */
519	if (!sess) {
520		pr_err("Initiator attempting to add a connection to"
521			" a non-existent session, rejecting iSCSI Login.\n");
522		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
523				ISCSI_LOGIN_STATUS_NO_SESSION);
524		return -1;
525	}
526
527	/*
528	 * Stop the Time2Retain timer if this is a failed session, we restart
529	 * the timer if the login is not successful.
530	 */
531	spin_lock_bh(&sess->conn_lock);
532	if (sess->session_state == TARG_SESS_STATE_FAILED)
533		atomic_set(&sess->session_continuation, 1);
534	spin_unlock_bh(&sess->conn_lock);
535
536	if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
537	    iscsi_copy_param_list(&conn->param_list,
538			conn->tpg->param_list, 0) < 0) {
539		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
540				ISCSI_LOGIN_STATUS_NO_RESOURCES);
541		return -1;
542	}
543
544	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
545		iser = true;
546
547	iscsi_set_keys_to_negotiate(conn->param_list, iser);
548	/*
549	 * Need to send TargetPortalGroupTag back in first login response
550	 * on any iSCSI connection where the Initiator provides TargetName.
551	 * See 5.3.1.  Login Phase Start
552	 *
553	 * In our case, we have already located the struct iscsi_tiqn at this point.
554	 */
555	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
556		return -1;
557
558	return 0;
559}
560
561int iscsi_login_post_auth_non_zero_tsih(
562	struct iscsit_conn *conn,
563	u16 cid,
564	u32 exp_statsn)
565{
566	struct iscsit_conn *conn_ptr = NULL;
567	struct iscsi_conn_recovery *cr = NULL;
568	struct iscsit_session *sess = conn->sess;
569
570	/*
571	 * By following item 5 in the login table,  if we have found
572	 * an existing ISID and a valid/existing TSIH and an existing
573	 * CID we do connection reinstatement.  Currently we dont not
574	 * support it so we send back an non-zero status class to the
575	 * initiator and release the new connection.
576	 */
577	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
578	if (conn_ptr) {
579		pr_err("Connection exists with CID %hu for %s,"
580			" performing connection reinstatement.\n",
581			conn_ptr->cid, sess->sess_ops->InitiatorName);
582
583		iscsit_connection_reinstatement_rcfr(conn_ptr);
584		iscsit_dec_conn_usage_count(conn_ptr);
585	}
586
587	/*
588	 * Check for any connection recovery entries containing CID.
589	 * We use the original ExpStatSN sent in the first login request
590	 * to acknowledge commands for the failed connection.
591	 *
592	 * Also note that an explict logout may have already been sent,
593	 * but the response may not be sent due to additional connection
594	 * loss.
595	 */
596	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
597		cr = iscsit_get_inactive_connection_recovery_entry(
598				sess, cid);
599		if (cr) {
600			pr_debug("Performing implicit logout"
601				" for connection recovery on CID: %hu\n",
602					conn->cid);
603			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
604		}
605	}
606
607	/*
608	 * Else we follow item 4 from the login table in that we have
609	 * found an existing ISID and a valid/existing TSIH and a new
610	 * CID we go ahead and continue to add a new connection to the
611	 * session.
612	 */
613	pr_debug("Adding CID %hu to existing session for %s.\n",
614			cid, sess->sess_ops->InitiatorName);
615
616	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
617		pr_err("Adding additional connection to this session"
618			" would exceed MaxConnections %d, login failed.\n",
619				sess->sess_ops->MaxConnections);
620		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
621				ISCSI_LOGIN_STATUS_ISID_ERROR);
622		return -1;
623	}
624
625	return 0;
626}
627
628static void iscsi_post_login_start_timers(struct iscsit_conn *conn)
629{
630	struct iscsit_session *sess = conn->sess;
631	/*
632	 * FIXME: Unsolicited NopIN support for ISER
633	 */
634	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
635		return;
636
637	if (!sess->sess_ops->SessionType)
638		iscsit_start_nopin_timer(conn);
639}
640
641int iscsit_start_kthreads(struct iscsit_conn *conn)
642{
643	int ret = 0;
644
645	spin_lock(&iscsit_global->ts_bitmap_lock);
646	conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
647					ISCSIT_BITMAP_BITS, get_order(1));
648	spin_unlock(&iscsit_global->ts_bitmap_lock);
649
650	if (conn->bitmap_id < 0) {
651		pr_err("bitmap_find_free_region() failed for"
652		       " iscsit_start_kthreads()\n");
653		return -ENOMEM;
654	}
655
656	conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
657				      "%s", ISCSI_TX_THREAD_NAME);
658	if (IS_ERR(conn->tx_thread)) {
659		pr_err("Unable to start iscsi_target_tx_thread\n");
660		ret = PTR_ERR(conn->tx_thread);
661		goto out_bitmap;
662	}
663	conn->tx_thread_active = true;
664
665	conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
666				      "%s", ISCSI_RX_THREAD_NAME);
667	if (IS_ERR(conn->rx_thread)) {
668		pr_err("Unable to start iscsi_target_rx_thread\n");
669		ret = PTR_ERR(conn->rx_thread);
670		goto out_tx;
671	}
672	conn->rx_thread_active = true;
673
674	return 0;
675out_tx:
676	send_sig(SIGINT, conn->tx_thread, 1);
677	kthread_stop(conn->tx_thread);
678	conn->tx_thread_active = false;
679out_bitmap:
680	spin_lock(&iscsit_global->ts_bitmap_lock);
681	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
682			      get_order(1));
683	spin_unlock(&iscsit_global->ts_bitmap_lock);
684	return ret;
685}
686
687void iscsi_post_login_handler(
688	struct iscsi_np *np,
689	struct iscsit_conn *conn,
690	u8 zero_tsih)
691{
692	int stop_timer = 0;
693	struct iscsit_session *sess = conn->sess;
694	struct se_session *se_sess = sess->se_sess;
695	struct iscsi_portal_group *tpg = sess->tpg;
696	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
697
698	iscsit_inc_conn_usage_count(conn);
699
700	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
701			ISCSI_LOGIN_STATUS_ACCEPT);
702
703	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
704	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
705
706	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
707	/*
708	 * SCSI Initiator -> SCSI Target Port Mapping
709	 */
710	if (!zero_tsih) {
711		iscsi_set_session_parameters(sess->sess_ops,
712				conn->param_list, 0);
713		iscsi_release_param_list(conn->param_list);
714		conn->param_list = NULL;
715
716		spin_lock_bh(&sess->conn_lock);
717		atomic_set(&sess->session_continuation, 0);
718		if (sess->session_state == TARG_SESS_STATE_FAILED) {
719			pr_debug("Moving to"
720					" TARG_SESS_STATE_LOGGED_IN.\n");
721			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
722			stop_timer = 1;
723		}
724
725		pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
726			" %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
727			&conn->local_sockaddr, tpg->tpgt);
728
729		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
730		atomic_inc(&sess->nconn);
731		pr_debug("Incremented iSCSI Connection count to %d"
732			" from node: %s\n", atomic_read(&sess->nconn),
733			sess->sess_ops->InitiatorName);
734		spin_unlock_bh(&sess->conn_lock);
735
736		iscsi_post_login_start_timers(conn);
737		/*
738		 * Determine CPU mask to ensure connection's RX and TX kthreads
739		 * are scheduled on the same CPU.
740		 */
741		iscsit_thread_get_cpumask(conn);
742		conn->conn_rx_reset_cpumask = 1;
743		conn->conn_tx_reset_cpumask = 1;
744		/*
745		 * Wakeup the sleeping iscsi_target_rx_thread() now that
746		 * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state.
747		 */
748		complete(&conn->rx_login_comp);
749		iscsit_dec_conn_usage_count(conn);
750
751		if (stop_timer) {
752			spin_lock_bh(&se_tpg->session_lock);
753			iscsit_stop_time2retain_timer(sess);
754			spin_unlock_bh(&se_tpg->session_lock);
755		}
756		iscsit_dec_session_usage_count(sess);
757		return;
758	}
759
760	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
761	iscsi_release_param_list(conn->param_list);
762	conn->param_list = NULL;
763
764	iscsit_determine_maxcmdsn(sess);
765
766	spin_lock_bh(&se_tpg->session_lock);
767	__transport_register_session(&sess->tpg->tpg_se_tpg,
768			se_sess->se_node_acl, se_sess, sess);
769	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
770	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
771
772	pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
773		conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
774		tpg->tpgt);
775
776	spin_lock_bh(&sess->conn_lock);
777	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
778	atomic_inc(&sess->nconn);
779	pr_debug("Incremented iSCSI Connection count to %d from node:"
780		" %s\n", atomic_read(&sess->nconn),
781		sess->sess_ops->InitiatorName);
782	spin_unlock_bh(&sess->conn_lock);
783
784	sess->sid = tpg->sid++;
785	if (!sess->sid)
786		sess->sid = tpg->sid++;
787	pr_debug("Established iSCSI session from node: %s\n",
788			sess->sess_ops->InitiatorName);
789
790	tpg->nsessions++;
791	if (tpg->tpg_tiqn)
792		tpg->tpg_tiqn->tiqn_nsessions++;
793
794	pr_debug("Incremented number of active iSCSI sessions to %u on"
795		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
796	spin_unlock_bh(&se_tpg->session_lock);
797
798	iscsi_post_login_start_timers(conn);
799	/*
800	 * Determine CPU mask to ensure connection's RX and TX kthreads
801	 * are scheduled on the same CPU.
802	 */
803	iscsit_thread_get_cpumask(conn);
804	conn->conn_rx_reset_cpumask = 1;
805	conn->conn_tx_reset_cpumask = 1;
806	/*
807	 * Wakeup the sleeping iscsi_target_rx_thread() now that
808	 * iscsit_conn is in TARG_CONN_STATE_LOGGED_IN state.
809	 */
810	complete(&conn->rx_login_comp);
811	iscsit_dec_conn_usage_count(conn);
812}
813
814int iscsit_setup_np(
815	struct iscsi_np *np,
816	struct sockaddr_storage *sockaddr)
817{
818	struct socket *sock = NULL;
819	int backlog = ISCSIT_TCP_BACKLOG, ret, len;
820
821	switch (np->np_network_transport) {
822	case ISCSI_TCP:
823		np->np_ip_proto = IPPROTO_TCP;
824		np->np_sock_type = SOCK_STREAM;
825		break;
826	case ISCSI_SCTP_TCP:
827		np->np_ip_proto = IPPROTO_SCTP;
828		np->np_sock_type = SOCK_STREAM;
829		break;
830	case ISCSI_SCTP_UDP:
831		np->np_ip_proto = IPPROTO_SCTP;
832		np->np_sock_type = SOCK_SEQPACKET;
833		break;
834	default:
835		pr_err("Unsupported network_transport: %d\n",
836				np->np_network_transport);
837		return -EINVAL;
838	}
839
840	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
841			np->np_ip_proto, &sock);
842	if (ret < 0) {
843		pr_err("sock_create() failed.\n");
844		return ret;
845	}
846	np->np_socket = sock;
847	/*
848	 * Setup the np->np_sockaddr from the passed sockaddr setup
849	 * in iscsi_target_configfs.c code..
850	 */
851	memcpy(&np->np_sockaddr, sockaddr,
852			sizeof(struct sockaddr_storage));
853
854	if (sockaddr->ss_family == AF_INET6)
855		len = sizeof(struct sockaddr_in6);
856	else
857		len = sizeof(struct sockaddr_in);
858	/*
859	 * Set SO_REUSEADDR, and disable Nagle Algorithm with TCP_NODELAY.
860	 */
861	if (np->np_network_transport == ISCSI_TCP)
862		tcp_sock_set_nodelay(sock->sk);
863	sock_set_reuseaddr(sock->sk);
864	ip_sock_set_freebind(sock->sk);
865
866	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
867	if (ret < 0) {
868		pr_err("kernel_bind() failed: %d\n", ret);
869		goto fail;
870	}
871
872	ret = kernel_listen(sock, backlog);
873	if (ret != 0) {
874		pr_err("kernel_listen() failed: %d\n", ret);
875		goto fail;
876	}
877
878	return 0;
879fail:
880	np->np_socket = NULL;
881	sock_release(sock);
882	return ret;
883}
884
885int iscsi_target_setup_login_socket(
886	struct iscsi_np *np,
887	struct sockaddr_storage *sockaddr)
888{
889	struct iscsit_transport *t;
890	int rc;
891
892	t = iscsit_get_transport(np->np_network_transport);
893	if (!t)
894		return -EINVAL;
895
896	rc = t->iscsit_setup_np(np, sockaddr);
897	if (rc < 0) {
898		iscsit_put_transport(t);
899		return rc;
900	}
901
902	np->np_transport = t;
903	np->enabled = true;
904	return 0;
905}
906
907int iscsit_accept_np(struct iscsi_np *np, struct iscsit_conn *conn)
908{
909	struct socket *new_sock, *sock = np->np_socket;
910	struct sockaddr_in sock_in;
911	struct sockaddr_in6 sock_in6;
912	int rc;
913
914	rc = kernel_accept(sock, &new_sock, 0);
915	if (rc < 0)
916		return rc;
917
918	conn->sock = new_sock;
919	conn->login_family = np->np_sockaddr.ss_family;
920
921	if (np->np_sockaddr.ss_family == AF_INET6) {
922		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
923
924		rc = conn->sock->ops->getname(conn->sock,
925				(struct sockaddr *)&sock_in6, 1);
926		if (rc >= 0) {
927			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
928				memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
929			} else {
930				/* Pretend to be an ipv4 socket */
931				sock_in.sin_family = AF_INET;
932				sock_in.sin_port = sock_in6.sin6_port;
933				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
934				memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
935			}
936		}
937
938		rc = conn->sock->ops->getname(conn->sock,
939				(struct sockaddr *)&sock_in6, 0);
940		if (rc >= 0) {
941			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
942				memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
943			} else {
944				/* Pretend to be an ipv4 socket */
945				sock_in.sin_family = AF_INET;
946				sock_in.sin_port = sock_in6.sin6_port;
947				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
948				memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
949			}
950		}
951	} else {
952		memset(&sock_in, 0, sizeof(struct sockaddr_in));
953
954		rc = conn->sock->ops->getname(conn->sock,
955				(struct sockaddr *)&sock_in, 1);
956		if (rc >= 0)
957			memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
958
959		rc = conn->sock->ops->getname(conn->sock,
960				(struct sockaddr *)&sock_in, 0);
961		if (rc >= 0)
962			memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
963	}
964
965	return 0;
966}
967
968int iscsit_get_login_rx(struct iscsit_conn *conn, struct iscsi_login *login)
969{
970	struct iscsi_login_req *login_req;
971	u32 padding = 0, payload_length;
972
973	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
974		return -1;
975
976	login_req = (struct iscsi_login_req *)login->req;
977	payload_length	= ntoh24(login_req->dlength);
978	padding = ((-payload_length) & 3);
979
980	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
981		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
982		login_req->flags, login_req->itt, login_req->cmdsn,
983		login_req->exp_statsn, login_req->cid, payload_length);
984	/*
985	 * Setup the initial iscsi_login values from the leading
986	 * login request PDU.
987	 */
988	if (login->first_request) {
989		login_req = (struct iscsi_login_req *)login->req;
990		login->leading_connection = (!login_req->tsih) ? 1 : 0;
991		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
992		login->version_min	= login_req->min_version;
993		login->version_max	= login_req->max_version;
994		memcpy(login->isid, login_req->isid, 6);
995		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
996		login->init_task_tag	= login_req->itt;
997		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
998		login->cid		= be16_to_cpu(login_req->cid);
999		login->tsih		= be16_to_cpu(login_req->tsih);
1000	}
1001
1002	if (iscsi_target_check_login_request(conn, login) < 0)
1003		return -1;
1004
1005	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1006	if (iscsi_login_rx_data(conn, login->req_buf,
1007				payload_length + padding) < 0)
1008		return -1;
1009
1010	return 0;
1011}
1012
1013int iscsit_put_login_tx(struct iscsit_conn *conn, struct iscsi_login *login,
1014			u32 length)
1015{
1016	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1017		return -1;
1018
1019	return 0;
1020}
1021
1022static int
1023iscsit_conn_set_transport(struct iscsit_conn *conn, struct iscsit_transport *t)
1024{
1025	int rc;
1026
1027	if (!t->owner) {
1028		conn->conn_transport = t;
1029		return 0;
1030	}
1031
1032	rc = try_module_get(t->owner);
1033	if (!rc) {
1034		pr_err("try_module_get() failed for %s\n", t->name);
1035		return -EINVAL;
1036	}
1037
1038	conn->conn_transport = t;
1039	return 0;
1040}
1041
1042static struct iscsit_conn *iscsit_alloc_conn(struct iscsi_np *np)
1043{
1044	struct iscsit_conn *conn;
1045
1046	conn = kzalloc(sizeof(struct iscsit_conn), GFP_KERNEL);
1047	if (!conn) {
1048		pr_err("Could not allocate memory for new connection\n");
1049		return NULL;
1050	}
1051	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1052	conn->conn_state = TARG_CONN_STATE_FREE;
1053
1054	init_waitqueue_head(&conn->queues_wq);
1055	INIT_LIST_HEAD(&conn->conn_list);
1056	INIT_LIST_HEAD(&conn->conn_cmd_list);
1057	INIT_LIST_HEAD(&conn->immed_queue_list);
1058	INIT_LIST_HEAD(&conn->response_queue_list);
1059	init_completion(&conn->conn_post_wait_comp);
1060	init_completion(&conn->conn_wait_comp);
1061	init_completion(&conn->conn_wait_rcfr_comp);
1062	init_completion(&conn->conn_waiting_on_uc_comp);
1063	init_completion(&conn->conn_logout_comp);
1064	init_completion(&conn->rx_half_close_comp);
1065	init_completion(&conn->tx_half_close_comp);
1066	init_completion(&conn->rx_login_comp);
1067	spin_lock_init(&conn->cmd_lock);
1068	spin_lock_init(&conn->conn_usage_lock);
1069	spin_lock_init(&conn->immed_queue_lock);
1070	spin_lock_init(&conn->nopin_timer_lock);
1071	spin_lock_init(&conn->response_queue_lock);
1072	spin_lock_init(&conn->state_lock);
1073	spin_lock_init(&conn->login_worker_lock);
1074	spin_lock_init(&conn->login_timer_lock);
1075
1076	timer_setup(&conn->nopin_response_timer,
1077		    iscsit_handle_nopin_response_timeout, 0);
1078	timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0);
1079	timer_setup(&conn->login_timer, iscsit_login_timeout, 0);
1080
1081	if (iscsit_conn_set_transport(conn, np->np_transport) < 0)
1082		goto free_conn;
1083
1084	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
1085	if (!conn->conn_ops) {
1086		pr_err("Unable to allocate memory for struct iscsi_conn_ops.\n");
1087		goto put_transport;
1088	}
1089
1090	if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
1091		pr_err("Unable to allocate conn->conn_cpumask\n");
1092		goto free_conn_ops;
1093	}
1094
1095	if (!zalloc_cpumask_var(&conn->allowed_cpumask, GFP_KERNEL)) {
1096		pr_err("Unable to allocate conn->allowed_cpumask\n");
1097		goto free_conn_cpumask;
1098	}
1099
1100	conn->cmd_cnt = target_alloc_cmd_counter();
1101	if (!conn->cmd_cnt)
1102		goto free_conn_allowed_cpumask;
1103
1104	return conn;
1105
1106free_conn_allowed_cpumask:
1107	free_cpumask_var(conn->allowed_cpumask);
1108free_conn_cpumask:
1109	free_cpumask_var(conn->conn_cpumask);
1110free_conn_ops:
1111	kfree(conn->conn_ops);
1112put_transport:
1113	iscsit_put_transport(conn->conn_transport);
1114free_conn:
1115	kfree(conn);
1116	return NULL;
1117}
1118
1119void iscsit_free_conn(struct iscsit_conn *conn)
1120{
1121	target_free_cmd_counter(conn->cmd_cnt);
1122	free_cpumask_var(conn->allowed_cpumask);
1123	free_cpumask_var(conn->conn_cpumask);
1124	kfree(conn->conn_ops);
1125	iscsit_put_transport(conn->conn_transport);
1126	kfree(conn);
1127}
1128
1129void iscsi_target_login_sess_out(struct iscsit_conn *conn,
1130				 bool zero_tsih, bool new_sess)
1131{
1132	if (!new_sess)
1133		goto old_sess_out;
1134
1135	pr_err("iSCSI Login negotiation failed.\n");
1136	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1137				   ISCSI_LOGIN_STATUS_INIT_ERR);
1138	if (!zero_tsih || !conn->sess)
1139		goto old_sess_out;
1140
1141	transport_free_session(conn->sess->se_sess);
1142	ida_free(&sess_ida, conn->sess->session_index);
1143	kfree(conn->sess->sess_ops);
1144	kfree(conn->sess);
1145	conn->sess = NULL;
1146
1147old_sess_out:
1148	/*
1149	 * If login negotiation fails check if the Time2Retain timer
1150	 * needs to be restarted.
1151	 */
1152	if (!zero_tsih && conn->sess) {
1153		spin_lock_bh(&conn->sess->conn_lock);
1154		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1155			struct se_portal_group *se_tpg =
1156					&conn->tpg->tpg_se_tpg;
1157
1158			atomic_set(&conn->sess->session_continuation, 0);
1159			spin_unlock_bh(&conn->sess->conn_lock);
1160			spin_lock_bh(&se_tpg->session_lock);
1161			iscsit_start_time2retain_handler(conn->sess);
1162			spin_unlock_bh(&se_tpg->session_lock);
1163		} else
1164			spin_unlock_bh(&conn->sess->conn_lock);
1165		iscsit_dec_session_usage_count(conn->sess);
1166	}
1167
1168	ahash_request_free(conn->conn_tx_hash);
1169	if (conn->conn_rx_hash) {
1170		struct crypto_ahash *tfm;
1171
1172		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1173		ahash_request_free(conn->conn_rx_hash);
1174		crypto_free_ahash(tfm);
1175	}
1176
1177	if (conn->param_list) {
1178		iscsi_release_param_list(conn->param_list);
1179		conn->param_list = NULL;
1180	}
1181	iscsi_target_nego_release(conn);
1182
1183	if (conn->sock) {
1184		sock_release(conn->sock);
1185		conn->sock = NULL;
1186	}
1187
1188	if (conn->conn_transport->iscsit_wait_conn)
1189		conn->conn_transport->iscsit_wait_conn(conn);
1190
1191	if (conn->conn_transport->iscsit_free_conn)
1192		conn->conn_transport->iscsit_free_conn(conn);
1193
1194	iscsit_free_conn(conn);
1195}
1196
1197static int __iscsi_target_login_thread(struct iscsi_np *np)
1198{
1199	u8 *buffer, zero_tsih = 0;
1200	int ret = 0, rc;
1201	struct iscsit_conn *conn = NULL;
1202	struct iscsi_login *login;
1203	struct iscsi_portal_group *tpg = NULL;
1204	struct iscsi_login_req *pdu;
1205	struct iscsi_tpg_np *tpg_np;
1206	bool new_sess = false;
1207
1208	flush_signals(current);
1209
1210	spin_lock_bh(&np->np_thread_lock);
1211	if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1212		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1213		spin_unlock_bh(&np->np_thread_lock);
1214		complete(&np->np_restart_comp);
1215		return 1;
1216	} else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1217		spin_unlock_bh(&np->np_thread_lock);
1218		goto exit;
1219	} else {
1220		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1221	}
1222	spin_unlock_bh(&np->np_thread_lock);
1223
1224	conn = iscsit_alloc_conn(np);
1225	if (!conn) {
1226		/* Get another socket */
1227		return 1;
1228	}
1229
1230	rc = np->np_transport->iscsit_accept_np(np, conn);
1231	if (rc == -ENOSYS) {
1232		complete(&np->np_restart_comp);
1233		iscsit_free_conn(conn);
1234		goto exit;
1235	} else if (rc < 0) {
1236		spin_lock_bh(&np->np_thread_lock);
1237		if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1238			np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1239			spin_unlock_bh(&np->np_thread_lock);
1240			complete(&np->np_restart_comp);
1241			iscsit_free_conn(conn);
1242			/* Get another socket */
1243			return 1;
1244		}
1245		spin_unlock_bh(&np->np_thread_lock);
1246		iscsit_free_conn(conn);
1247		return 1;
1248	}
1249	/*
1250	 * Perform the remaining iSCSI connection initialization items..
1251	 */
1252	login = iscsi_login_init_conn(conn);
1253	if (!login) {
1254		goto new_sess_out;
1255	}
1256
1257	iscsit_start_login_timer(conn, current);
1258
1259	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1260	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1261	/*
1262	 * This will process the first login request + payload..
1263	 */
1264	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1265	if (rc == 1)
1266		return 1;
1267	else if (rc < 0)
1268		goto new_sess_out;
1269
1270	buffer = &login->req[0];
1271	pdu = (struct iscsi_login_req *)buffer;
1272	/*
1273	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1274	 * when Status-Class != 0.
1275	*/
1276	conn->login_itt	= pdu->itt;
1277
1278	spin_lock_bh(&np->np_thread_lock);
1279	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1280		spin_unlock_bh(&np->np_thread_lock);
1281		pr_err("iSCSI Network Portal on %pISpc currently not"
1282			" active.\n", &np->np_sockaddr);
1283		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1284				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1285		goto new_sess_out;
1286	}
1287	spin_unlock_bh(&np->np_thread_lock);
1288
1289	conn->network_transport = np->np_network_transport;
1290
1291	pr_debug("Received iSCSI login request from %pISpc on %s Network"
1292		" Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1293		&conn->local_sockaddr);
1294
1295	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1296	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1297
1298	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1299			pdu->min_version) < 0)
1300		goto new_sess_out;
1301
1302	zero_tsih = (pdu->tsih == 0x0000);
1303	if (zero_tsih) {
1304		/*
1305		 * This is the leading connection of a new session.
1306		 * We wait until after authentication to check for
1307		 * session reinstatement.
1308		 */
1309		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1310			goto new_sess_out;
1311	} else {
1312		/*
1313		 * Add a new connection to an existing session.
1314		 * We check for a non-existant session in
1315		 * iscsi_login_non_zero_tsih_s2() below based
1316		 * on ISID/TSIH, but wait until after authentication
1317		 * to check for connection reinstatement, etc.
1318		 */
1319		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1320			goto new_sess_out;
1321	}
1322	/*
1323	 * SessionType: Discovery
1324	 *
1325	 * 	Locates Default Portal
1326	 *
1327	 * SessionType: Normal
1328	 *
1329	 * 	Locates Target Portal from NP -> Target IQN
1330	 */
1331	rc = iscsi_target_locate_portal(np, conn, login);
1332	if (rc < 0) {
1333		tpg = conn->tpg;
1334		goto new_sess_out;
1335	}
1336	login->zero_tsih = zero_tsih;
1337
1338	if (conn->sess)
1339		conn->sess->se_sess->sup_prot_ops =
1340			conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1341
1342	tpg = conn->tpg;
1343	if (!tpg) {
1344		pr_err("Unable to locate struct iscsit_conn->tpg\n");
1345		goto new_sess_out;
1346	}
1347
1348	if (zero_tsih) {
1349		if (iscsi_login_zero_tsih_s2(conn) < 0)
1350			goto new_sess_out;
1351	} else {
1352		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1353			goto old_sess_out;
1354	}
1355
1356	if (conn->conn_transport->iscsit_validate_params) {
1357		ret = conn->conn_transport->iscsit_validate_params(conn);
1358		if (ret < 0) {
1359			if (zero_tsih)
1360				goto new_sess_out;
1361			else
1362				goto old_sess_out;
1363		}
1364	}
1365
1366	ret = iscsi_target_start_negotiation(login, conn);
1367	if (ret < 0)
1368		goto new_sess_out;
1369
1370	if (ret == 1) {
1371		tpg_np = conn->tpg_np;
1372
1373		iscsi_post_login_handler(np, conn, zero_tsih);
1374		iscsit_deaccess_np(np, tpg, tpg_np);
1375	}
1376
1377	tpg = NULL;
1378	tpg_np = NULL;
1379	/* Get another socket */
1380	return 1;
1381
1382new_sess_out:
1383	new_sess = true;
1384old_sess_out:
1385	iscsit_stop_login_timer(conn);
1386	tpg_np = conn->tpg_np;
1387	iscsi_target_login_sess_out(conn, zero_tsih, new_sess);
1388	new_sess = false;
1389
1390	if (tpg) {
1391		iscsit_deaccess_np(np, tpg, tpg_np);
1392		tpg = NULL;
1393		tpg_np = NULL;
1394	}
1395
1396	return 1;
1397
1398exit:
1399	spin_lock_bh(&np->np_thread_lock);
1400	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1401	spin_unlock_bh(&np->np_thread_lock);
1402
1403	return 0;
1404}
1405
1406int iscsi_target_login_thread(void *arg)
1407{
1408	struct iscsi_np *np = arg;
1409	int ret;
1410
1411	allow_signal(SIGINT);
1412
1413	while (1) {
1414		ret = __iscsi_target_login_thread(np);
1415		/*
1416		 * We break and exit here unless another sock_accept() call
1417		 * is expected.
1418		 */
1419		if (ret != 1)
1420			break;
1421	}
1422
1423	while (!kthread_should_stop()) {
1424		msleep(100);
1425	}
1426
1427	return 0;
1428}
1429