xref: /linux/drivers/target/iscsi/iscsi_target_login.c (revision 67145c23e70b066443a3c0736e74aa2342a013fd)
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 		    atomic_read(&sess_p->session_close) ||
160 		    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
161 			spin_unlock(&sess_p->conn_lock);
162 			continue;
163 		}
164 		if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
165 		   (!strcmp(sess_p->sess_ops->InitiatorName,
166 			    initiatorname_param->value) &&
167 		   (sess_p->sess_ops->SessionType == sessiontype))) {
168 			atomic_set(&sess_p->session_reinstatement, 1);
169 			atomic_set(&sess_p->session_fall_back_to_erl0, 1);
170 			atomic_set(&sess_p->session_close, 1);
171 			spin_unlock(&sess_p->conn_lock);
172 			iscsit_inc_session_usage_count(sess_p);
173 			iscsit_stop_time2retain_timer(sess_p);
174 			sess = sess_p;
175 			break;
176 		}
177 		spin_unlock(&sess_p->conn_lock);
178 	}
179 	spin_unlock_bh(&se_tpg->session_lock);
180 	/*
181 	 * If the Time2Retain handler has expired, the session is already gone.
182 	 */
183 	if (!sess)
184 		return 0;
185 
186 	pr_debug("%s iSCSI Session SID %u is still active for %s,"
187 		" performing session reinstatement.\n", (sessiontype) ?
188 		"Discovery" : "Normal", sess->sid,
189 		sess->sess_ops->InitiatorName);
190 
191 	spin_lock_bh(&sess->conn_lock);
192 	if (sess->session_state == TARG_SESS_STATE_FAILED) {
193 		spin_unlock_bh(&sess->conn_lock);
194 		iscsit_dec_session_usage_count(sess);
195 		return 0;
196 	}
197 	spin_unlock_bh(&sess->conn_lock);
198 
199 	iscsit_stop_session(sess, 1, 1);
200 	iscsit_dec_session_usage_count(sess);
201 
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 		    atomic_read(&sess_p->session_close) ||
490 		   (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
491 			continue;
492 		if (!memcmp(sess_p->isid, pdu->isid, 6) &&
493 		     (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
494 			iscsit_inc_session_usage_count(sess_p);
495 			iscsit_stop_time2retain_timer(sess_p);
496 			sess = sess_p;
497 			break;
498 		}
499 	}
500 	spin_unlock_bh(&se_tpg->session_lock);
501 
502 	/*
503 	 * If the Time2Retain handler has expired, the session is already gone.
504 	 */
505 	if (!sess) {
506 		pr_err("Initiator attempting to add a connection to"
507 			" a non-existent session, rejecting iSCSI Login.\n");
508 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
509 				ISCSI_LOGIN_STATUS_NO_SESSION);
510 		return -1;
511 	}
512 
513 	/*
514 	 * Stop the Time2Retain timer if this is a failed session, we restart
515 	 * the timer if the login is not successful.
516 	 */
517 	spin_lock_bh(&sess->conn_lock);
518 	if (sess->session_state == TARG_SESS_STATE_FAILED)
519 		atomic_set(&sess->session_continuation, 1);
520 	spin_unlock_bh(&sess->conn_lock);
521 
522 	if (iscsi_login_set_conn_values(sess, conn, pdu->cid) < 0 ||
523 	    iscsi_copy_param_list(&conn->param_list,
524 			conn->tpg->param_list, 0) < 0) {
525 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
526 				ISCSI_LOGIN_STATUS_NO_RESOURCES);
527 		return -1;
528 	}
529 
530 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
531 		iser = true;
532 
533 	iscsi_set_keys_to_negotiate(conn->param_list, iser);
534 	/*
535 	 * Need to send TargetPortalGroupTag back in first login response
536 	 * on any iSCSI connection where the Initiator provides TargetName.
537 	 * See 5.3.1.  Login Phase Start
538 	 *
539 	 * In our case, we have already located the struct iscsi_tiqn at this point.
540 	 */
541 	if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
542 		return -1;
543 
544 	return 0;
545 }
546 
547 int iscsi_login_post_auth_non_zero_tsih(
548 	struct iscsi_conn *conn,
549 	u16 cid,
550 	u32 exp_statsn)
551 {
552 	struct iscsi_conn *conn_ptr = NULL;
553 	struct iscsi_conn_recovery *cr = NULL;
554 	struct iscsi_session *sess = conn->sess;
555 
556 	/*
557 	 * By following item 5 in the login table,  if we have found
558 	 * an existing ISID and a valid/existing TSIH and an existing
559 	 * CID we do connection reinstatement.  Currently we dont not
560 	 * support it so we send back an non-zero status class to the
561 	 * initiator and release the new connection.
562 	 */
563 	conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
564 	if (conn_ptr) {
565 		pr_err("Connection exists with CID %hu for %s,"
566 			" performing connection reinstatement.\n",
567 			conn_ptr->cid, sess->sess_ops->InitiatorName);
568 
569 		iscsit_connection_reinstatement_rcfr(conn_ptr);
570 		iscsit_dec_conn_usage_count(conn_ptr);
571 	}
572 
573 	/*
574 	 * Check for any connection recovery entries containing CID.
575 	 * We use the original ExpStatSN sent in the first login request
576 	 * to acknowledge commands for the failed connection.
577 	 *
578 	 * Also note that an explict logout may have already been sent,
579 	 * but the response may not be sent due to additional connection
580 	 * loss.
581 	 */
582 	if (sess->sess_ops->ErrorRecoveryLevel == 2) {
583 		cr = iscsit_get_inactive_connection_recovery_entry(
584 				sess, cid);
585 		if (cr) {
586 			pr_debug("Performing implicit logout"
587 				" for connection recovery on CID: %hu\n",
588 					conn->cid);
589 			iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
590 		}
591 	}
592 
593 	/*
594 	 * Else we follow item 4 from the login table in that we have
595 	 * found an existing ISID and a valid/existing TSIH and a new
596 	 * CID we go ahead and continue to add a new connection to the
597 	 * session.
598 	 */
599 	pr_debug("Adding CID %hu to existing session for %s.\n",
600 			cid, sess->sess_ops->InitiatorName);
601 
602 	if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
603 		pr_err("Adding additional connection to this session"
604 			" would exceed MaxConnections %d, login failed.\n",
605 				sess->sess_ops->MaxConnections);
606 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
607 				ISCSI_LOGIN_STATUS_ISID_ERROR);
608 		return -1;
609 	}
610 
611 	return 0;
612 }
613 
614 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
615 {
616 	struct iscsi_session *sess = conn->sess;
617 	/*
618 	 * FIXME: Unsolicited NopIN support for ISER
619 	 */
620 	if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
621 		return;
622 
623 	if (!sess->sess_ops->SessionType)
624 		iscsit_start_nopin_timer(conn);
625 }
626 
627 int iscsit_start_kthreads(struct iscsi_conn *conn)
628 {
629 	int ret = 0;
630 
631 	spin_lock(&iscsit_global->ts_bitmap_lock);
632 	conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
633 					ISCSIT_BITMAP_BITS, get_order(1));
634 	spin_unlock(&iscsit_global->ts_bitmap_lock);
635 
636 	if (conn->bitmap_id < 0) {
637 		pr_err("bitmap_find_free_region() failed for"
638 		       " iscsit_start_kthreads()\n");
639 		return -ENOMEM;
640 	}
641 
642 	conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
643 				      "%s", ISCSI_TX_THREAD_NAME);
644 	if (IS_ERR(conn->tx_thread)) {
645 		pr_err("Unable to start iscsi_target_tx_thread\n");
646 		ret = PTR_ERR(conn->tx_thread);
647 		goto out_bitmap;
648 	}
649 	conn->tx_thread_active = true;
650 
651 	conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
652 				      "%s", ISCSI_RX_THREAD_NAME);
653 	if (IS_ERR(conn->rx_thread)) {
654 		pr_err("Unable to start iscsi_target_rx_thread\n");
655 		ret = PTR_ERR(conn->rx_thread);
656 		goto out_tx;
657 	}
658 	conn->rx_thread_active = true;
659 
660 	return 0;
661 out_tx:
662 	send_sig(SIGINT, conn->tx_thread, 1);
663 	kthread_stop(conn->tx_thread);
664 	conn->tx_thread_active = false;
665 out_bitmap:
666 	spin_lock(&iscsit_global->ts_bitmap_lock);
667 	bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
668 			      get_order(1));
669 	spin_unlock(&iscsit_global->ts_bitmap_lock);
670 	return ret;
671 }
672 
673 void iscsi_post_login_handler(
674 	struct iscsi_np *np,
675 	struct iscsi_conn *conn,
676 	u8 zero_tsih)
677 {
678 	int stop_timer = 0;
679 	struct iscsi_session *sess = conn->sess;
680 	struct se_session *se_sess = sess->se_sess;
681 	struct iscsi_portal_group *tpg = sess->tpg;
682 	struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
683 
684 	iscsit_inc_conn_usage_count(conn);
685 
686 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
687 			ISCSI_LOGIN_STATUS_ACCEPT);
688 
689 	pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
690 	conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
691 
692 	iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
693 	/*
694 	 * SCSI Initiator -> SCSI Target Port Mapping
695 	 */
696 	if (!zero_tsih) {
697 		iscsi_set_session_parameters(sess->sess_ops,
698 				conn->param_list, 0);
699 		iscsi_release_param_list(conn->param_list);
700 		conn->param_list = NULL;
701 
702 		spin_lock_bh(&sess->conn_lock);
703 		atomic_set(&sess->session_continuation, 0);
704 		if (sess->session_state == TARG_SESS_STATE_FAILED) {
705 			pr_debug("Moving to"
706 					" TARG_SESS_STATE_LOGGED_IN.\n");
707 			sess->session_state = TARG_SESS_STATE_LOGGED_IN;
708 			stop_timer = 1;
709 		}
710 
711 		pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
712 			" %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
713 			&conn->local_sockaddr, tpg->tpgt);
714 
715 		list_add_tail(&conn->conn_list, &sess->sess_conn_list);
716 		atomic_inc(&sess->nconn);
717 		pr_debug("Incremented iSCSI Connection count to %hu"
718 			" from node: %s\n", atomic_read(&sess->nconn),
719 			sess->sess_ops->InitiatorName);
720 		spin_unlock_bh(&sess->conn_lock);
721 
722 		iscsi_post_login_start_timers(conn);
723 		/*
724 		 * Determine CPU mask to ensure connection's RX and TX kthreads
725 		 * are scheduled on the same CPU.
726 		 */
727 		iscsit_thread_get_cpumask(conn);
728 		conn->conn_rx_reset_cpumask = 1;
729 		conn->conn_tx_reset_cpumask = 1;
730 		/*
731 		 * Wakeup the sleeping iscsi_target_rx_thread() now that
732 		 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
733 		 */
734 		complete(&conn->rx_login_comp);
735 		iscsit_dec_conn_usage_count(conn);
736 
737 		if (stop_timer) {
738 			spin_lock_bh(&se_tpg->session_lock);
739 			iscsit_stop_time2retain_timer(sess);
740 			spin_unlock_bh(&se_tpg->session_lock);
741 		}
742 		iscsit_dec_session_usage_count(sess);
743 		return;
744 	}
745 
746 	iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
747 	iscsi_release_param_list(conn->param_list);
748 	conn->param_list = NULL;
749 
750 	iscsit_determine_maxcmdsn(sess);
751 
752 	spin_lock_bh(&se_tpg->session_lock);
753 	__transport_register_session(&sess->tpg->tpg_se_tpg,
754 			se_sess->se_node_acl, se_sess, sess);
755 	pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
756 	sess->session_state = TARG_SESS_STATE_LOGGED_IN;
757 
758 	pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
759 		conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
760 		tpg->tpgt);
761 
762 	spin_lock_bh(&sess->conn_lock);
763 	list_add_tail(&conn->conn_list, &sess->sess_conn_list);
764 	atomic_inc(&sess->nconn);
765 	pr_debug("Incremented iSCSI Connection count to %hu from node:"
766 		" %s\n", atomic_read(&sess->nconn),
767 		sess->sess_ops->InitiatorName);
768 	spin_unlock_bh(&sess->conn_lock);
769 
770 	sess->sid = tpg->sid++;
771 	if (!sess->sid)
772 		sess->sid = tpg->sid++;
773 	pr_debug("Established iSCSI session from node: %s\n",
774 			sess->sess_ops->InitiatorName);
775 
776 	tpg->nsessions++;
777 	if (tpg->tpg_tiqn)
778 		tpg->tpg_tiqn->tiqn_nsessions++;
779 
780 	pr_debug("Incremented number of active iSCSI sessions to %u on"
781 		" iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
782 	spin_unlock_bh(&se_tpg->session_lock);
783 
784 	iscsi_post_login_start_timers(conn);
785 	/*
786 	 * Determine CPU mask to ensure connection's RX and TX kthreads
787 	 * are scheduled on the same CPU.
788 	 */
789 	iscsit_thread_get_cpumask(conn);
790 	conn->conn_rx_reset_cpumask = 1;
791 	conn->conn_tx_reset_cpumask = 1;
792 	/*
793 	 * Wakeup the sleeping iscsi_target_rx_thread() now that
794 	 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
795 	 */
796 	complete(&conn->rx_login_comp);
797 	iscsit_dec_conn_usage_count(conn);
798 }
799 
800 void iscsi_handle_login_thread_timeout(struct timer_list *t)
801 {
802 	struct iscsi_np *np = from_timer(np, t, np_login_timer);
803 
804 	spin_lock_bh(&np->np_thread_lock);
805 	pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
806 			&np->np_sockaddr);
807 
808 	if (np->np_login_timer_flags & ISCSI_TF_STOP) {
809 		spin_unlock_bh(&np->np_thread_lock);
810 		return;
811 	}
812 
813 	if (np->np_thread)
814 		send_sig(SIGINT, np->np_thread, 1);
815 
816 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
817 	spin_unlock_bh(&np->np_thread_lock);
818 }
819 
820 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
821 {
822 	/*
823 	 * This used the TA_LOGIN_TIMEOUT constant because at this
824 	 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
825 	 */
826 	spin_lock_bh(&np->np_thread_lock);
827 	np->np_login_timer_flags &= ~ISCSI_TF_STOP;
828 	np->np_login_timer_flags |= ISCSI_TF_RUNNING;
829 	mod_timer(&np->np_login_timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
830 
831 	pr_debug("Added timeout timer to iSCSI login request for"
832 			" %u seconds.\n", TA_LOGIN_TIMEOUT);
833 	spin_unlock_bh(&np->np_thread_lock);
834 }
835 
836 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
837 {
838 	spin_lock_bh(&np->np_thread_lock);
839 	if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
840 		spin_unlock_bh(&np->np_thread_lock);
841 		return;
842 	}
843 	np->np_login_timer_flags |= ISCSI_TF_STOP;
844 	spin_unlock_bh(&np->np_thread_lock);
845 
846 	del_timer_sync(&np->np_login_timer);
847 
848 	spin_lock_bh(&np->np_thread_lock);
849 	np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
850 	spin_unlock_bh(&np->np_thread_lock);
851 }
852 
853 int iscsit_setup_np(
854 	struct iscsi_np *np,
855 	struct sockaddr_storage *sockaddr)
856 {
857 	struct socket *sock = NULL;
858 	int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
859 
860 	switch (np->np_network_transport) {
861 	case ISCSI_TCP:
862 		np->np_ip_proto = IPPROTO_TCP;
863 		np->np_sock_type = SOCK_STREAM;
864 		break;
865 	case ISCSI_SCTP_TCP:
866 		np->np_ip_proto = IPPROTO_SCTP;
867 		np->np_sock_type = SOCK_STREAM;
868 		break;
869 	case ISCSI_SCTP_UDP:
870 		np->np_ip_proto = IPPROTO_SCTP;
871 		np->np_sock_type = SOCK_SEQPACKET;
872 		break;
873 	default:
874 		pr_err("Unsupported network_transport: %d\n",
875 				np->np_network_transport);
876 		return -EINVAL;
877 	}
878 
879 	ret = sock_create(sockaddr->ss_family, np->np_sock_type,
880 			np->np_ip_proto, &sock);
881 	if (ret < 0) {
882 		pr_err("sock_create() failed.\n");
883 		return ret;
884 	}
885 	np->np_socket = sock;
886 	/*
887 	 * Setup the np->np_sockaddr from the passed sockaddr setup
888 	 * in iscsi_target_configfs.c code..
889 	 */
890 	memcpy(&np->np_sockaddr, sockaddr,
891 			sizeof(struct sockaddr_storage));
892 
893 	if (sockaddr->ss_family == AF_INET6)
894 		len = sizeof(struct sockaddr_in6);
895 	else
896 		len = sizeof(struct sockaddr_in);
897 	/*
898 	 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
899 	 */
900 	/* FIXME: Someone please explain why this is endian-safe */
901 	opt = 1;
902 	if (np->np_network_transport == ISCSI_TCP) {
903 		ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
904 				(char *)&opt, sizeof(opt));
905 		if (ret < 0) {
906 			pr_err("kernel_setsockopt() for TCP_NODELAY"
907 				" failed: %d\n", ret);
908 			goto fail;
909 		}
910 	}
911 
912 	/* FIXME: Someone please explain why this is endian-safe */
913 	ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
914 			(char *)&opt, sizeof(opt));
915 	if (ret < 0) {
916 		pr_err("kernel_setsockopt() for SO_REUSEADDR"
917 			" failed\n");
918 		goto fail;
919 	}
920 
921 	ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
922 			(char *)&opt, sizeof(opt));
923 	if (ret < 0) {
924 		pr_err("kernel_setsockopt() for IP_FREEBIND"
925 			" failed\n");
926 		goto fail;
927 	}
928 
929 	ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
930 	if (ret < 0) {
931 		pr_err("kernel_bind() failed: %d\n", ret);
932 		goto fail;
933 	}
934 
935 	ret = kernel_listen(sock, backlog);
936 	if (ret != 0) {
937 		pr_err("kernel_listen() failed: %d\n", ret);
938 		goto fail;
939 	}
940 
941 	return 0;
942 fail:
943 	np->np_socket = NULL;
944 	sock_release(sock);
945 	return ret;
946 }
947 
948 int iscsi_target_setup_login_socket(
949 	struct iscsi_np *np,
950 	struct sockaddr_storage *sockaddr)
951 {
952 	struct iscsit_transport *t;
953 	int rc;
954 
955 	t = iscsit_get_transport(np->np_network_transport);
956 	if (!t)
957 		return -EINVAL;
958 
959 	rc = t->iscsit_setup_np(np, sockaddr);
960 	if (rc < 0) {
961 		iscsit_put_transport(t);
962 		return rc;
963 	}
964 
965 	np->np_transport = t;
966 	np->enabled = true;
967 	return 0;
968 }
969 
970 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
971 {
972 	struct socket *new_sock, *sock = np->np_socket;
973 	struct sockaddr_in sock_in;
974 	struct sockaddr_in6 sock_in6;
975 	int rc;
976 
977 	rc = kernel_accept(sock, &new_sock, 0);
978 	if (rc < 0)
979 		return rc;
980 
981 	conn->sock = new_sock;
982 	conn->login_family = np->np_sockaddr.ss_family;
983 
984 	if (np->np_sockaddr.ss_family == AF_INET6) {
985 		memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
986 
987 		rc = conn->sock->ops->getname(conn->sock,
988 				(struct sockaddr *)&sock_in6, 1);
989 		if (rc >= 0) {
990 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
991 				memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
992 			} else {
993 				/* Pretend to be an ipv4 socket */
994 				sock_in.sin_family = AF_INET;
995 				sock_in.sin_port = sock_in6.sin6_port;
996 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
997 				memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
998 			}
999 		}
1000 
1001 		rc = conn->sock->ops->getname(conn->sock,
1002 				(struct sockaddr *)&sock_in6, 0);
1003 		if (rc >= 0) {
1004 			if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1005 				memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
1006 			} else {
1007 				/* Pretend to be an ipv4 socket */
1008 				sock_in.sin_family = AF_INET;
1009 				sock_in.sin_port = sock_in6.sin6_port;
1010 				memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1011 				memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1012 			}
1013 		}
1014 	} else {
1015 		memset(&sock_in, 0, sizeof(struct sockaddr_in));
1016 
1017 		rc = conn->sock->ops->getname(conn->sock,
1018 				(struct sockaddr *)&sock_in, 1);
1019 		if (rc >= 0)
1020 			memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1021 
1022 		rc = conn->sock->ops->getname(conn->sock,
1023 				(struct sockaddr *)&sock_in, 0);
1024 		if (rc >= 0)
1025 			memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1026 	}
1027 
1028 	return 0;
1029 }
1030 
1031 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1032 {
1033 	struct iscsi_login_req *login_req;
1034 	u32 padding = 0, payload_length;
1035 
1036 	if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1037 		return -1;
1038 
1039 	login_req = (struct iscsi_login_req *)login->req;
1040 	payload_length	= ntoh24(login_req->dlength);
1041 	padding = ((-payload_length) & 3);
1042 
1043 	pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1044 		" CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1045 		login_req->flags, login_req->itt, login_req->cmdsn,
1046 		login_req->exp_statsn, login_req->cid, payload_length);
1047 	/*
1048 	 * Setup the initial iscsi_login values from the leading
1049 	 * login request PDU.
1050 	 */
1051 	if (login->first_request) {
1052 		login_req = (struct iscsi_login_req *)login->req;
1053 		login->leading_connection = (!login_req->tsih) ? 1 : 0;
1054 		login->current_stage	= ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1055 		login->version_min	= login_req->min_version;
1056 		login->version_max	= login_req->max_version;
1057 		memcpy(login->isid, login_req->isid, 6);
1058 		login->cmd_sn		= be32_to_cpu(login_req->cmdsn);
1059 		login->init_task_tag	= login_req->itt;
1060 		login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1061 		login->cid		= be16_to_cpu(login_req->cid);
1062 		login->tsih		= be16_to_cpu(login_req->tsih);
1063 	}
1064 
1065 	if (iscsi_target_check_login_request(conn, login) < 0)
1066 		return -1;
1067 
1068 	memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1069 	if (iscsi_login_rx_data(conn, login->req_buf,
1070 				payload_length + padding) < 0)
1071 		return -1;
1072 
1073 	return 0;
1074 }
1075 
1076 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1077 			u32 length)
1078 {
1079 	if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1080 		return -1;
1081 
1082 	return 0;
1083 }
1084 
1085 static int
1086 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1087 {
1088 	int rc;
1089 
1090 	if (!t->owner) {
1091 		conn->conn_transport = t;
1092 		return 0;
1093 	}
1094 
1095 	rc = try_module_get(t->owner);
1096 	if (!rc) {
1097 		pr_err("try_module_get() failed for %s\n", t->name);
1098 		return -EINVAL;
1099 	}
1100 
1101 	conn->conn_transport = t;
1102 	return 0;
1103 }
1104 
1105 static struct iscsi_conn *iscsit_alloc_conn(struct iscsi_np *np)
1106 {
1107 	struct iscsi_conn *conn;
1108 
1109 	conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1110 	if (!conn) {
1111 		pr_err("Could not allocate memory for new connection\n");
1112 		return NULL;
1113 	}
1114 	pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1115 	conn->conn_state = TARG_CONN_STATE_FREE;
1116 
1117 	init_waitqueue_head(&conn->queues_wq);
1118 	INIT_LIST_HEAD(&conn->conn_list);
1119 	INIT_LIST_HEAD(&conn->conn_cmd_list);
1120 	INIT_LIST_HEAD(&conn->immed_queue_list);
1121 	INIT_LIST_HEAD(&conn->response_queue_list);
1122 	init_completion(&conn->conn_post_wait_comp);
1123 	init_completion(&conn->conn_wait_comp);
1124 	init_completion(&conn->conn_wait_rcfr_comp);
1125 	init_completion(&conn->conn_waiting_on_uc_comp);
1126 	init_completion(&conn->conn_logout_comp);
1127 	init_completion(&conn->rx_half_close_comp);
1128 	init_completion(&conn->tx_half_close_comp);
1129 	init_completion(&conn->rx_login_comp);
1130 	spin_lock_init(&conn->cmd_lock);
1131 	spin_lock_init(&conn->conn_usage_lock);
1132 	spin_lock_init(&conn->immed_queue_lock);
1133 	spin_lock_init(&conn->nopin_timer_lock);
1134 	spin_lock_init(&conn->response_queue_lock);
1135 	spin_lock_init(&conn->state_lock);
1136 
1137 	timer_setup(&conn->nopin_response_timer,
1138 		    iscsit_handle_nopin_response_timeout, 0);
1139 	timer_setup(&conn->nopin_timer, iscsit_handle_nopin_timeout, 0);
1140 
1141 	if (iscsit_conn_set_transport(conn, np->np_transport) < 0)
1142 		goto free_conn;
1143 
1144 	conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
1145 	if (!conn->conn_ops) {
1146 		pr_err("Unable to allocate memory for struct iscsi_conn_ops.\n");
1147 		goto put_transport;
1148 	}
1149 
1150 	if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
1151 		pr_err("Unable to allocate conn->conn_cpumask\n");
1152 		goto free_conn_ops;
1153 	}
1154 
1155 	return conn;
1156 
1157 free_conn_ops:
1158 	kfree(conn->conn_ops);
1159 put_transport:
1160 	iscsit_put_transport(conn->conn_transport);
1161 free_conn:
1162 	kfree(conn);
1163 	return NULL;
1164 }
1165 
1166 void iscsit_free_conn(struct iscsi_conn *conn)
1167 {
1168 	free_cpumask_var(conn->conn_cpumask);
1169 	kfree(conn->conn_ops);
1170 	iscsit_put_transport(conn->conn_transport);
1171 	kfree(conn);
1172 }
1173 
1174 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1175 		struct iscsi_np *np, bool zero_tsih, bool new_sess)
1176 {
1177 	if (!new_sess)
1178 		goto old_sess_out;
1179 
1180 	pr_err("iSCSI Login negotiation failed.\n");
1181 	iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1182 				   ISCSI_LOGIN_STATUS_INIT_ERR);
1183 	if (!zero_tsih || !conn->sess)
1184 		goto old_sess_out;
1185 
1186 	transport_free_session(conn->sess->se_sess);
1187 	ida_free(&sess_ida, conn->sess->session_index);
1188 	kfree(conn->sess->sess_ops);
1189 	kfree(conn->sess);
1190 	conn->sess = NULL;
1191 
1192 old_sess_out:
1193 	iscsi_stop_login_thread_timer(np);
1194 	/*
1195 	 * If login negotiation fails check if the Time2Retain timer
1196 	 * needs to be restarted.
1197 	 */
1198 	if (!zero_tsih && conn->sess) {
1199 		spin_lock_bh(&conn->sess->conn_lock);
1200 		if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1201 			struct se_portal_group *se_tpg =
1202 					&conn->tpg->tpg_se_tpg;
1203 
1204 			atomic_set(&conn->sess->session_continuation, 0);
1205 			spin_unlock_bh(&conn->sess->conn_lock);
1206 			spin_lock_bh(&se_tpg->session_lock);
1207 			iscsit_start_time2retain_handler(conn->sess);
1208 			spin_unlock_bh(&se_tpg->session_lock);
1209 		} else
1210 			spin_unlock_bh(&conn->sess->conn_lock);
1211 		iscsit_dec_session_usage_count(conn->sess);
1212 	}
1213 
1214 	ahash_request_free(conn->conn_tx_hash);
1215 	if (conn->conn_rx_hash) {
1216 		struct crypto_ahash *tfm;
1217 
1218 		tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1219 		ahash_request_free(conn->conn_rx_hash);
1220 		crypto_free_ahash(tfm);
1221 	}
1222 
1223 	if (conn->param_list) {
1224 		iscsi_release_param_list(conn->param_list);
1225 		conn->param_list = NULL;
1226 	}
1227 	iscsi_target_nego_release(conn);
1228 
1229 	if (conn->sock) {
1230 		sock_release(conn->sock);
1231 		conn->sock = NULL;
1232 	}
1233 
1234 	if (conn->conn_transport->iscsit_wait_conn)
1235 		conn->conn_transport->iscsit_wait_conn(conn);
1236 
1237 	if (conn->conn_transport->iscsit_free_conn)
1238 		conn->conn_transport->iscsit_free_conn(conn);
1239 
1240 	iscsit_free_conn(conn);
1241 }
1242 
1243 static int __iscsi_target_login_thread(struct iscsi_np *np)
1244 {
1245 	u8 *buffer, zero_tsih = 0;
1246 	int ret = 0, rc;
1247 	struct iscsi_conn *conn = NULL;
1248 	struct iscsi_login *login;
1249 	struct iscsi_portal_group *tpg = NULL;
1250 	struct iscsi_login_req *pdu;
1251 	struct iscsi_tpg_np *tpg_np;
1252 	bool new_sess = false;
1253 
1254 	flush_signals(current);
1255 
1256 	spin_lock_bh(&np->np_thread_lock);
1257 	if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1258 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1259 		spin_unlock_bh(&np->np_thread_lock);
1260 		complete(&np->np_restart_comp);
1261 		return 1;
1262 	} else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1263 		spin_unlock_bh(&np->np_thread_lock);
1264 		goto exit;
1265 	} else {
1266 		np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1267 	}
1268 	spin_unlock_bh(&np->np_thread_lock);
1269 
1270 	conn = iscsit_alloc_conn(np);
1271 	if (!conn) {
1272 		/* Get another socket */
1273 		return 1;
1274 	}
1275 
1276 	rc = np->np_transport->iscsit_accept_np(np, conn);
1277 	if (rc == -ENOSYS) {
1278 		complete(&np->np_restart_comp);
1279 		iscsit_free_conn(conn);
1280 		goto exit;
1281 	} else if (rc < 0) {
1282 		spin_lock_bh(&np->np_thread_lock);
1283 		if (atomic_dec_if_positive(&np->np_reset_count) >= 0) {
1284 			np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1285 			spin_unlock_bh(&np->np_thread_lock);
1286 			complete(&np->np_restart_comp);
1287 			iscsit_free_conn(conn);
1288 			/* Get another socket */
1289 			return 1;
1290 		}
1291 		spin_unlock_bh(&np->np_thread_lock);
1292 		iscsit_free_conn(conn);
1293 		return 1;
1294 	}
1295 	/*
1296 	 * Perform the remaining iSCSI connection initialization items..
1297 	 */
1298 	login = iscsi_login_init_conn(conn);
1299 	if (!login) {
1300 		goto new_sess_out;
1301 	}
1302 
1303 	iscsi_start_login_thread_timer(np);
1304 
1305 	pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1306 	conn->conn_state = TARG_CONN_STATE_XPT_UP;
1307 	/*
1308 	 * This will process the first login request + payload..
1309 	 */
1310 	rc = np->np_transport->iscsit_get_login_rx(conn, login);
1311 	if (rc == 1)
1312 		return 1;
1313 	else if (rc < 0)
1314 		goto new_sess_out;
1315 
1316 	buffer = &login->req[0];
1317 	pdu = (struct iscsi_login_req *)buffer;
1318 	/*
1319 	 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1320 	 * when Status-Class != 0.
1321 	*/
1322 	conn->login_itt	= pdu->itt;
1323 
1324 	spin_lock_bh(&np->np_thread_lock);
1325 	if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1326 		spin_unlock_bh(&np->np_thread_lock);
1327 		pr_err("iSCSI Network Portal on %pISpc currently not"
1328 			" active.\n", &np->np_sockaddr);
1329 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1330 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1331 		goto new_sess_out;
1332 	}
1333 	spin_unlock_bh(&np->np_thread_lock);
1334 
1335 	conn->network_transport = np->np_network_transport;
1336 
1337 	pr_debug("Received iSCSI login request from %pISpc on %s Network"
1338 		" Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1339 		&conn->local_sockaddr);
1340 
1341 	pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1342 	conn->conn_state	= TARG_CONN_STATE_IN_LOGIN;
1343 
1344 	if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1345 			pdu->min_version) < 0)
1346 		goto new_sess_out;
1347 
1348 	zero_tsih = (pdu->tsih == 0x0000);
1349 	if (zero_tsih) {
1350 		/*
1351 		 * This is the leading connection of a new session.
1352 		 * We wait until after authentication to check for
1353 		 * session reinstatement.
1354 		 */
1355 		if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1356 			goto new_sess_out;
1357 	} else {
1358 		/*
1359 		 * Add a new connection to an existing session.
1360 		 * We check for a non-existant session in
1361 		 * iscsi_login_non_zero_tsih_s2() below based
1362 		 * on ISID/TSIH, but wait until after authentication
1363 		 * to check for connection reinstatement, etc.
1364 		 */
1365 		if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1366 			goto new_sess_out;
1367 	}
1368 	/*
1369 	 * SessionType: Discovery
1370 	 *
1371 	 * 	Locates Default Portal
1372 	 *
1373 	 * SessionType: Normal
1374 	 *
1375 	 * 	Locates Target Portal from NP -> Target IQN
1376 	 */
1377 	rc = iscsi_target_locate_portal(np, conn, login);
1378 	if (rc < 0) {
1379 		tpg = conn->tpg;
1380 		goto new_sess_out;
1381 	}
1382 	login->zero_tsih = zero_tsih;
1383 
1384 	if (conn->sess)
1385 		conn->sess->se_sess->sup_prot_ops =
1386 			conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1387 
1388 	tpg = conn->tpg;
1389 	if (!tpg) {
1390 		pr_err("Unable to locate struct iscsi_conn->tpg\n");
1391 		goto new_sess_out;
1392 	}
1393 
1394 	if (zero_tsih) {
1395 		if (iscsi_login_zero_tsih_s2(conn) < 0)
1396 			goto new_sess_out;
1397 	} else {
1398 		if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1399 			goto old_sess_out;
1400 	}
1401 
1402 	if (conn->conn_transport->iscsit_validate_params) {
1403 		ret = conn->conn_transport->iscsit_validate_params(conn);
1404 		if (ret < 0) {
1405 			if (zero_tsih)
1406 				goto new_sess_out;
1407 			else
1408 				goto old_sess_out;
1409 		}
1410 	}
1411 
1412 	ret = iscsi_target_start_negotiation(login, conn);
1413 	if (ret < 0)
1414 		goto new_sess_out;
1415 
1416 	iscsi_stop_login_thread_timer(np);
1417 
1418 	if (ret == 1) {
1419 		tpg_np = conn->tpg_np;
1420 
1421 		iscsi_post_login_handler(np, conn, zero_tsih);
1422 		iscsit_deaccess_np(np, tpg, tpg_np);
1423 	}
1424 
1425 	tpg = NULL;
1426 	tpg_np = NULL;
1427 	/* Get another socket */
1428 	return 1;
1429 
1430 new_sess_out:
1431 	new_sess = true;
1432 old_sess_out:
1433 	tpg_np = conn->tpg_np;
1434 	iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1435 	new_sess = false;
1436 
1437 	if (tpg) {
1438 		iscsit_deaccess_np(np, tpg, tpg_np);
1439 		tpg = NULL;
1440 		tpg_np = NULL;
1441 	}
1442 
1443 	return 1;
1444 
1445 exit:
1446 	iscsi_stop_login_thread_timer(np);
1447 	spin_lock_bh(&np->np_thread_lock);
1448 	np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1449 	spin_unlock_bh(&np->np_thread_lock);
1450 
1451 	return 0;
1452 }
1453 
1454 int iscsi_target_login_thread(void *arg)
1455 {
1456 	struct iscsi_np *np = arg;
1457 	int ret;
1458 
1459 	allow_signal(SIGINT);
1460 
1461 	while (1) {
1462 		ret = __iscsi_target_login_thread(np);
1463 		/*
1464 		 * We break and exit here unless another sock_accept() call
1465 		 * is expected.
1466 		 */
1467 		if (ret != 1)
1468 			break;
1469 	}
1470 
1471 	while (!kthread_should_stop()) {
1472 		msleep(100);
1473 	}
1474 
1475 	return 0;
1476 }
1477