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