1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3  * This file contains main functions related to iSCSI Parameter negotiation.
4  *
5  * (c) Copyright 2007-2013 Datera, Inc.
6  *
7  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8  *
9  ******************************************************************************/
10 
11 #include <linux/ctype.h>
12 #include <linux/kthread.h>
13 #include <linux/slab.h>
14 #include <linux/sched/signal.h>
15 #include <net/sock.h>
16 #include <scsi/iscsi_proto.h>
17 #include <target/target_core_base.h>
18 #include <target/target_core_fabric.h>
19 #include <target/iscsi/iscsi_transport.h>
20 
21 #include <target/iscsi/iscsi_target_core.h>
22 #include "iscsi_target_parameters.h"
23 #include "iscsi_target_login.h"
24 #include "iscsi_target_nego.h"
25 #include "iscsi_target_tpg.h"
26 #include "iscsi_target_util.h"
27 #include "iscsi_target.h"
28 #include "iscsi_target_auth.h"
29 
30 #define MAX_LOGIN_PDUS  7
31 
32 void convert_null_to_semi(char *buf, int len)
33 {
34 	int i;
35 
36 	for (i = 0; i < len; i++)
37 		if (buf[i] == '\0')
38 			buf[i] = ';';
39 }
40 
41 static int strlen_semi(char *buf)
42 {
43 	int i = 0;
44 
45 	while (buf[i] != '\0') {
46 		if (buf[i] == ';')
47 			return i;
48 		i++;
49 	}
50 
51 	return -1;
52 }
53 
54 int extract_param(
55 	const char *in_buf,
56 	const char *pattern,
57 	unsigned int max_length,
58 	char *out_buf,
59 	unsigned char *type)
60 {
61 	char *ptr;
62 	int len;
63 
64 	if (!in_buf || !pattern || !out_buf || !type)
65 		return -1;
66 
67 	ptr = strstr(in_buf, pattern);
68 	if (!ptr)
69 		return -1;
70 
71 	ptr = strstr(ptr, "=");
72 	if (!ptr)
73 		return -1;
74 
75 	ptr += 1;
76 	if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
77 		ptr += 2; /* skip 0x */
78 		*type = HEX;
79 	} else
80 		*type = DECIMAL;
81 
82 	len = strlen_semi(ptr);
83 	if (len < 0)
84 		return -1;
85 
86 	if (len >= max_length) {
87 		pr_err("Length of input: %d exceeds max_length:"
88 			" %d\n", len, max_length);
89 		return -1;
90 	}
91 	memcpy(out_buf, ptr, len);
92 	out_buf[len] = '\0';
93 
94 	return 0;
95 }
96 
97 static u32 iscsi_handle_authentication(
98 	struct iscsi_conn *conn,
99 	char *in_buf,
100 	char *out_buf,
101 	int in_length,
102 	int *out_length,
103 	unsigned char *authtype)
104 {
105 	struct iscsi_session *sess = conn->sess;
106 	struct iscsi_node_auth *auth;
107 	struct iscsi_node_acl *iscsi_nacl;
108 	struct iscsi_portal_group *iscsi_tpg;
109 	struct se_node_acl *se_nacl;
110 
111 	if (!sess->sess_ops->SessionType) {
112 		/*
113 		 * For SessionType=Normal
114 		 */
115 		se_nacl = conn->sess->se_sess->se_node_acl;
116 		if (!se_nacl) {
117 			pr_err("Unable to locate struct se_node_acl for"
118 					" CHAP auth\n");
119 			return -1;
120 		}
121 		iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
122 				se_node_acl);
123 		if (!iscsi_nacl) {
124 			pr_err("Unable to locate struct iscsi_node_acl for"
125 					" CHAP auth\n");
126 			return -1;
127 		}
128 
129 		if (se_nacl->dynamic_node_acl) {
130 			iscsi_tpg = container_of(se_nacl->se_tpg,
131 					struct iscsi_portal_group, tpg_se_tpg);
132 
133 			auth = &iscsi_tpg->tpg_demo_auth;
134 		} else {
135 			iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
136 						  se_node_acl);
137 
138 			auth = &iscsi_nacl->node_auth;
139 		}
140 	} else {
141 		/*
142 		 * For SessionType=Discovery
143 		 */
144 		auth = &iscsit_global->discovery_acl.node_auth;
145 	}
146 
147 	if (strstr("CHAP", authtype))
148 		strcpy(conn->sess->auth_type, "CHAP");
149 	else
150 		strcpy(conn->sess->auth_type, NONE);
151 
152 	if (strstr("None", authtype))
153 		return 1;
154 	else if (strstr("CHAP", authtype))
155 		return chap_main_loop(conn, auth, in_buf, out_buf,
156 				&in_length, out_length);
157 	/* SRP, SPKM1, SPKM2 and KRB5 are unsupported */
158 	return 2;
159 }
160 
161 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
162 {
163 	kfree(conn->auth_protocol);
164 }
165 
166 int iscsi_target_check_login_request(
167 	struct iscsi_conn *conn,
168 	struct iscsi_login *login)
169 {
170 	int req_csg, req_nsg;
171 	u32 payload_length;
172 	struct iscsi_login_req *login_req;
173 
174 	login_req = (struct iscsi_login_req *) login->req;
175 	payload_length = ntoh24(login_req->dlength);
176 
177 	switch (login_req->opcode & ISCSI_OPCODE_MASK) {
178 	case ISCSI_OP_LOGIN:
179 		break;
180 	default:
181 		pr_err("Received unknown opcode 0x%02x.\n",
182 				login_req->opcode & ISCSI_OPCODE_MASK);
183 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
184 				ISCSI_LOGIN_STATUS_INIT_ERR);
185 		return -1;
186 	}
187 
188 	if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
189 	    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
190 		pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
191 			" and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
192 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
193 				ISCSI_LOGIN_STATUS_INIT_ERR);
194 		return -1;
195 	}
196 
197 	req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
198 	req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
199 
200 	if (req_csg != login->current_stage) {
201 		pr_err("Initiator unexpectedly changed login stage"
202 			" from %d to %d, login failed.\n", login->current_stage,
203 			req_csg);
204 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
205 				ISCSI_LOGIN_STATUS_INIT_ERR);
206 		return -1;
207 	}
208 
209 	if ((req_nsg == 2) || (req_csg >= 2) ||
210 	   ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
211 	    (req_nsg <= req_csg))) {
212 		pr_err("Illegal login_req->flags Combination, CSG: %d,"
213 			" NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
214 			req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
215 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
216 				ISCSI_LOGIN_STATUS_INIT_ERR);
217 		return -1;
218 	}
219 
220 	if ((login_req->max_version != login->version_max) ||
221 	    (login_req->min_version != login->version_min)) {
222 		pr_err("Login request changed Version Max/Nin"
223 			" unexpectedly to 0x%02x/0x%02x, protocol error\n",
224 			login_req->max_version, login_req->min_version);
225 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
226 				ISCSI_LOGIN_STATUS_INIT_ERR);
227 		return -1;
228 	}
229 
230 	if (memcmp(login_req->isid, login->isid, 6) != 0) {
231 		pr_err("Login request changed ISID unexpectedly,"
232 				" protocol error.\n");
233 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
234 				ISCSI_LOGIN_STATUS_INIT_ERR);
235 		return -1;
236 	}
237 
238 	if (login_req->itt != login->init_task_tag) {
239 		pr_err("Login request changed ITT unexpectedly to"
240 			" 0x%08x, protocol error.\n", login_req->itt);
241 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
242 				ISCSI_LOGIN_STATUS_INIT_ERR);
243 		return -1;
244 	}
245 
246 	if (payload_length > MAX_KEY_VALUE_PAIRS) {
247 		pr_err("Login request payload exceeds default"
248 			" MaxRecvDataSegmentLength: %u, protocol error.\n",
249 				MAX_KEY_VALUE_PAIRS);
250 		return -1;
251 	}
252 
253 	return 0;
254 }
255 EXPORT_SYMBOL(iscsi_target_check_login_request);
256 
257 static int iscsi_target_check_first_request(
258 	struct iscsi_conn *conn,
259 	struct iscsi_login *login)
260 {
261 	struct iscsi_param *param = NULL;
262 	struct se_node_acl *se_nacl;
263 
264 	login->first_request = 0;
265 
266 	list_for_each_entry(param, &conn->param_list->param_list, p_list) {
267 		if (!strncmp(param->name, SESSIONTYPE, 11)) {
268 			if (!IS_PSTATE_ACCEPTOR(param)) {
269 				pr_err("SessionType key not received"
270 					" in first login request.\n");
271 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
272 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
273 				return -1;
274 			}
275 			if (!strncmp(param->value, DISCOVERY, 9))
276 				return 0;
277 		}
278 
279 		if (!strncmp(param->name, INITIATORNAME, 13)) {
280 			if (!IS_PSTATE_ACCEPTOR(param)) {
281 				if (!login->leading_connection)
282 					continue;
283 
284 				pr_err("InitiatorName key not received"
285 					" in first login request.\n");
286 				iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
287 					ISCSI_LOGIN_STATUS_MISSING_FIELDS);
288 				return -1;
289 			}
290 
291 			/*
292 			 * For non-leading connections, double check that the
293 			 * received InitiatorName matches the existing session's
294 			 * struct iscsi_node_acl.
295 			 */
296 			if (!login->leading_connection) {
297 				se_nacl = conn->sess->se_sess->se_node_acl;
298 				if (!se_nacl) {
299 					pr_err("Unable to locate"
300 						" struct se_node_acl\n");
301 					iscsit_tx_login_rsp(conn,
302 							ISCSI_STATUS_CLS_INITIATOR_ERR,
303 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
304 					return -1;
305 				}
306 
307 				if (strcmp(param->value,
308 						se_nacl->initiatorname)) {
309 					pr_err("Incorrect"
310 						" InitiatorName: %s for this"
311 						" iSCSI Initiator Node.\n",
312 						param->value);
313 					iscsit_tx_login_rsp(conn,
314 							ISCSI_STATUS_CLS_INITIATOR_ERR,
315 							ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
316 					return -1;
317 				}
318 			}
319 		}
320 	}
321 
322 	return 0;
323 }
324 
325 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
326 {
327 	u32 padding = 0;
328 	struct iscsi_login_rsp *login_rsp;
329 
330 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
331 
332 	login_rsp->opcode		= ISCSI_OP_LOGIN_RSP;
333 	hton24(login_rsp->dlength, login->rsp_length);
334 	memcpy(login_rsp->isid, login->isid, 6);
335 	login_rsp->tsih			= cpu_to_be16(login->tsih);
336 	login_rsp->itt			= login->init_task_tag;
337 	login_rsp->statsn		= cpu_to_be32(conn->stat_sn++);
338 	login_rsp->exp_cmdsn		= cpu_to_be32(conn->sess->exp_cmd_sn);
339 	login_rsp->max_cmdsn		= cpu_to_be32((u32) atomic_read(&conn->sess->max_cmd_sn));
340 
341 	pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
342 		" ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
343 		" %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
344 		ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
345 		ntohl(login_rsp->statsn), login->rsp_length);
346 
347 	padding = ((-login->rsp_length) & 3);
348 	/*
349 	 * Before sending the last login response containing the transition
350 	 * bit for full-feature-phase, go ahead and start up TX/RX threads
351 	 * now to avoid potential resource allocation failures after the
352 	 * final login response has been sent.
353 	 */
354 	if (login->login_complete) {
355 		int rc = iscsit_start_kthreads(conn);
356 		if (rc) {
357 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
358 					    ISCSI_LOGIN_STATUS_NO_RESOURCES);
359 			return -1;
360 		}
361 	}
362 
363 	if (conn->conn_transport->iscsit_put_login_tx(conn, login,
364 					login->rsp_length + padding) < 0)
365 		goto err;
366 
367 	login->rsp_length		= 0;
368 
369 	return 0;
370 
371 err:
372 	if (login->login_complete) {
373 		if (conn->rx_thread && conn->rx_thread_active) {
374 			send_sig(SIGINT, conn->rx_thread, 1);
375 			complete(&conn->rx_login_comp);
376 			kthread_stop(conn->rx_thread);
377 		}
378 		if (conn->tx_thread && conn->tx_thread_active) {
379 			send_sig(SIGINT, conn->tx_thread, 1);
380 			kthread_stop(conn->tx_thread);
381 		}
382 		spin_lock(&iscsit_global->ts_bitmap_lock);
383 		bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
384 				      get_order(1));
385 		spin_unlock(&iscsit_global->ts_bitmap_lock);
386 	}
387 	return -1;
388 }
389 
390 static void iscsi_target_sk_data_ready(struct sock *sk)
391 {
392 	struct iscsi_conn *conn = sk->sk_user_data;
393 	bool rc;
394 
395 	pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
396 
397 	write_lock_bh(&sk->sk_callback_lock);
398 	if (!sk->sk_user_data) {
399 		write_unlock_bh(&sk->sk_callback_lock);
400 		return;
401 	}
402 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
403 		write_unlock_bh(&sk->sk_callback_lock);
404 		pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
405 		return;
406 	}
407 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
408 		write_unlock_bh(&sk->sk_callback_lock);
409 		pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
410 		return;
411 	}
412 	if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
413 		write_unlock_bh(&sk->sk_callback_lock);
414 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
415 		if (iscsi_target_sk_data_ready == conn->orig_data_ready)
416 			return;
417 		conn->orig_data_ready(sk);
418 		return;
419 	}
420 
421 	rc = schedule_delayed_work(&conn->login_work, 0);
422 	if (!rc) {
423 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
424 			 " got false\n");
425 	}
426 	write_unlock_bh(&sk->sk_callback_lock);
427 }
428 
429 static void iscsi_target_sk_state_change(struct sock *);
430 
431 static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
432 {
433 	struct sock *sk;
434 
435 	if (!conn->sock)
436 		return;
437 
438 	sk = conn->sock->sk;
439 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
440 
441 	write_lock_bh(&sk->sk_callback_lock);
442 	sk->sk_user_data = conn;
443 	conn->orig_data_ready = sk->sk_data_ready;
444 	conn->orig_state_change = sk->sk_state_change;
445 	sk->sk_data_ready = iscsi_target_sk_data_ready;
446 	sk->sk_state_change = iscsi_target_sk_state_change;
447 	write_unlock_bh(&sk->sk_callback_lock);
448 
449 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
450 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
451 }
452 
453 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
454 {
455 	struct sock *sk;
456 
457 	if (!conn->sock)
458 		return;
459 
460 	sk = conn->sock->sk;
461 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
462 
463 	write_lock_bh(&sk->sk_callback_lock);
464 	if (!sk->sk_user_data) {
465 		write_unlock_bh(&sk->sk_callback_lock);
466 		return;
467 	}
468 	sk->sk_user_data = NULL;
469 	sk->sk_data_ready = conn->orig_data_ready;
470 	sk->sk_state_change = conn->orig_state_change;
471 	write_unlock_bh(&sk->sk_callback_lock);
472 
473 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
474 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
475 }
476 
477 static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
478 
479 static bool __iscsi_target_sk_check_close(struct sock *sk)
480 {
481 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
482 		pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
483 			"returning TRUE\n");
484 		return true;
485 	}
486 	return false;
487 }
488 
489 static bool iscsi_target_sk_check_close(struct iscsi_conn *conn)
490 {
491 	bool state = false;
492 
493 	if (conn->sock) {
494 		struct sock *sk = conn->sock->sk;
495 
496 		read_lock_bh(&sk->sk_callback_lock);
497 		state = (__iscsi_target_sk_check_close(sk) ||
498 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
499 		read_unlock_bh(&sk->sk_callback_lock);
500 	}
501 	return state;
502 }
503 
504 static bool iscsi_target_sk_check_flag(struct iscsi_conn *conn, unsigned int flag)
505 {
506 	bool state = false;
507 
508 	if (conn->sock) {
509 		struct sock *sk = conn->sock->sk;
510 
511 		read_lock_bh(&sk->sk_callback_lock);
512 		state = test_bit(flag, &conn->login_flags);
513 		read_unlock_bh(&sk->sk_callback_lock);
514 	}
515 	return state;
516 }
517 
518 static bool iscsi_target_sk_check_and_clear(struct iscsi_conn *conn, unsigned int flag)
519 {
520 	bool state = false;
521 
522 	if (conn->sock) {
523 		struct sock *sk = conn->sock->sk;
524 
525 		write_lock_bh(&sk->sk_callback_lock);
526 		state = (__iscsi_target_sk_check_close(sk) ||
527 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
528 		if (!state)
529 			clear_bit(flag, &conn->login_flags);
530 		write_unlock_bh(&sk->sk_callback_lock);
531 	}
532 	return state;
533 }
534 
535 static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
536 {
537 	bool zero_tsih = login->zero_tsih;
538 
539 	iscsi_remove_failed_auth_entry(conn);
540 	iscsi_target_nego_release(conn);
541 	iscsi_target_login_sess_out(conn, zero_tsih, true);
542 }
543 
544 struct conn_timeout {
545 	struct timer_list timer;
546 	struct iscsi_conn *conn;
547 };
548 
549 static void iscsi_target_login_timeout(struct timer_list *t)
550 {
551 	struct conn_timeout *timeout = from_timer(timeout, t, timer);
552 	struct iscsi_conn *conn = timeout->conn;
553 
554 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
555 
556 	if (conn->login_kworker) {
557 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
558 			 conn->login_kworker->comm, conn->login_kworker->pid);
559 		send_sig(SIGINT, conn->login_kworker, 1);
560 	}
561 }
562 
563 static void iscsi_target_do_login_rx(struct work_struct *work)
564 {
565 	struct iscsi_conn *conn = container_of(work,
566 				struct iscsi_conn, login_work.work);
567 	struct iscsi_login *login = conn->login;
568 	struct iscsi_np *np = login->np;
569 	struct iscsi_portal_group *tpg = conn->tpg;
570 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
571 	struct conn_timeout timeout;
572 	int rc, zero_tsih = login->zero_tsih;
573 	bool state;
574 
575 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
576 			conn, current->comm, current->pid);
577 	/*
578 	 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
579 	 * before initial PDU processing in iscsi_target_start_negotiation()
580 	 * has completed, go ahead and retry until it's cleared.
581 	 *
582 	 * Otherwise if the TCP connection drops while this is occuring,
583 	 * iscsi_target_start_negotiation() will detect the failure, call
584 	 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
585 	 * remaining iscsi connection resources from iscsi_np process context.
586 	 */
587 	if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
588 		schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
589 		return;
590 	}
591 
592 	spin_lock(&tpg->tpg_state_lock);
593 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
594 	spin_unlock(&tpg->tpg_state_lock);
595 
596 	if (!state) {
597 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
598 		goto err;
599 	}
600 
601 	if (iscsi_target_sk_check_close(conn)) {
602 		pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
603 		goto err;
604 	}
605 
606 	conn->login_kworker = current;
607 	allow_signal(SIGINT);
608 
609 	timeout.conn = conn;
610 	timer_setup_on_stack(&timeout.timer, iscsi_target_login_timeout, 0);
611 	mod_timer(&timeout.timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
612 	pr_debug("Starting login timer for %s/%d\n", current->comm, current->pid);
613 
614 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
615 	del_timer_sync(&timeout.timer);
616 	destroy_timer_on_stack(&timeout.timer);
617 	flush_signals(current);
618 	conn->login_kworker = NULL;
619 
620 	if (rc < 0)
621 		goto err;
622 
623 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
624 			conn, current->comm, current->pid);
625 
626 	/*
627 	 * LOGIN_FLAGS_READ_ACTIVE is cleared so that sk_data_ready
628 	 * could be triggered again after this.
629 	 *
630 	 * LOGIN_FLAGS_WRITE_ACTIVE is cleared after we successfully
631 	 * process a login PDU, so that sk_state_chage can do login
632 	 * cleanup as needed if the socket is closed. If a delayed work is
633 	 * ongoing (LOGIN_FLAGS_WRITE_ACTIVE or LOGIN_FLAGS_READ_ACTIVE),
634 	 * sk_state_change will leave the cleanup to the delayed work or
635 	 * it will schedule a delayed work to do cleanup.
636 	 */
637 	if (conn->sock) {
638 		struct sock *sk = conn->sock->sk;
639 
640 		write_lock_bh(&sk->sk_callback_lock);
641 		if (!test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags)) {
642 			clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
643 			set_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags);
644 		}
645 		write_unlock_bh(&sk->sk_callback_lock);
646 	}
647 
648 	rc = iscsi_target_do_login(conn, login);
649 	if (rc < 0) {
650 		goto err;
651 	} else if (!rc) {
652 		if (iscsi_target_sk_check_and_clear(conn,
653 						    LOGIN_FLAGS_WRITE_ACTIVE))
654 			goto err;
655 	} else if (rc == 1) {
656 		cancel_delayed_work(&conn->login_work);
657 		iscsi_target_nego_release(conn);
658 		iscsi_post_login_handler(np, conn, zero_tsih);
659 		iscsit_deaccess_np(np, tpg, tpg_np);
660 	}
661 	return;
662 
663 err:
664 	iscsi_target_restore_sock_callbacks(conn);
665 	cancel_delayed_work(&conn->login_work);
666 	iscsi_target_login_drop(conn, login);
667 	iscsit_deaccess_np(np, tpg, tpg_np);
668 }
669 
670 static void iscsi_target_sk_state_change(struct sock *sk)
671 {
672 	struct iscsi_conn *conn;
673 	void (*orig_state_change)(struct sock *);
674 	bool state;
675 
676 	pr_debug("Entering iscsi_target_sk_state_change\n");
677 
678 	write_lock_bh(&sk->sk_callback_lock);
679 	conn = sk->sk_user_data;
680 	if (!conn) {
681 		write_unlock_bh(&sk->sk_callback_lock);
682 		return;
683 	}
684 	orig_state_change = conn->orig_state_change;
685 
686 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
687 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
688 			 conn);
689 		write_unlock_bh(&sk->sk_callback_lock);
690 		orig_state_change(sk);
691 		return;
692 	}
693 	state = __iscsi_target_sk_check_close(sk);
694 	pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
695 
696 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags) ||
697 	    test_bit(LOGIN_FLAGS_WRITE_ACTIVE, &conn->login_flags)) {
698 		pr_debug("Got LOGIN_FLAGS_{READ|WRITE}_ACTIVE=1"
699 			 " sk_state_change conn: %p\n", conn);
700 		if (state)
701 			set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
702 		write_unlock_bh(&sk->sk_callback_lock);
703 		orig_state_change(sk);
704 		return;
705 	}
706 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
707 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
708 			 conn);
709 		write_unlock_bh(&sk->sk_callback_lock);
710 		orig_state_change(sk);
711 		return;
712 	}
713 	/*
714 	 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
715 	 * but only queue conn->login_work -> iscsi_target_do_login_rx()
716 	 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
717 	 *
718 	 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
719 	 * will detect the dropped TCP connection from delayed workqueue context.
720 	 *
721 	 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
722 	 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
723 	 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
724 	 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
725 	 * dropped TCP connection in iscsi_np process context, and cleaning up
726 	 * the remaining iscsi connection resources.
727 	 */
728 	if (state) {
729 		pr_debug("iscsi_target_sk_state_change got failed state\n");
730 		set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
731 		state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
732 		write_unlock_bh(&sk->sk_callback_lock);
733 
734 		orig_state_change(sk);
735 
736 		if (!state)
737 			schedule_delayed_work(&conn->login_work, 0);
738 		return;
739 	}
740 	write_unlock_bh(&sk->sk_callback_lock);
741 
742 	orig_state_change(sk);
743 }
744 
745 /*
746  *	NOTE: We check for existing sessions or connections AFTER the initiator
747  *	has been successfully authenticated in order to protect against faked
748  *	ISID/TSIH combinations.
749  */
750 static int iscsi_target_check_for_existing_instances(
751 	struct iscsi_conn *conn,
752 	struct iscsi_login *login)
753 {
754 	if (login->checked_for_existing)
755 		return 0;
756 
757 	login->checked_for_existing = 1;
758 
759 	if (!login->tsih)
760 		return iscsi_check_for_session_reinstatement(conn);
761 	else
762 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
763 				login->initial_exp_statsn);
764 }
765 
766 static int iscsi_target_do_authentication(
767 	struct iscsi_conn *conn,
768 	struct iscsi_login *login)
769 {
770 	int authret;
771 	u32 payload_length;
772 	struct iscsi_param *param;
773 	struct iscsi_login_req *login_req;
774 	struct iscsi_login_rsp *login_rsp;
775 
776 	login_req = (struct iscsi_login_req *) login->req;
777 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
778 	payload_length = ntoh24(login_req->dlength);
779 
780 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
781 	if (!param)
782 		return -1;
783 
784 	authret = iscsi_handle_authentication(
785 			conn,
786 			login->req_buf,
787 			login->rsp_buf,
788 			payload_length,
789 			&login->rsp_length,
790 			param->value);
791 	switch (authret) {
792 	case 0:
793 		pr_debug("Received OK response"
794 		" from LIO Authentication, continuing.\n");
795 		break;
796 	case 1:
797 		pr_debug("iSCSI security negotiation"
798 			" completed successfully.\n");
799 		login->auth_complete = 1;
800 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
801 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
802 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
803 					     ISCSI_FLAG_LOGIN_TRANSIT);
804 			login->current_stage = 1;
805 		}
806 		return iscsi_target_check_for_existing_instances(
807 				conn, login);
808 	case 2:
809 		pr_err("Security negotiation"
810 			" failed.\n");
811 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
812 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
813 		return -1;
814 	default:
815 		pr_err("Received unknown error %d from LIO"
816 				" Authentication\n", authret);
817 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
818 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
819 		return -1;
820 	}
821 
822 	return 0;
823 }
824 
825 static int iscsi_target_handle_csg_zero(
826 	struct iscsi_conn *conn,
827 	struct iscsi_login *login)
828 {
829 	int ret;
830 	u32 payload_length;
831 	struct iscsi_param *param;
832 	struct iscsi_login_req *login_req;
833 	struct iscsi_login_rsp *login_rsp;
834 
835 	login_req = (struct iscsi_login_req *) login->req;
836 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
837 	payload_length = ntoh24(login_req->dlength);
838 
839 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
840 	if (!param)
841 		return -1;
842 
843 	ret = iscsi_decode_text_input(
844 			PHASE_SECURITY|PHASE_DECLARATIVE,
845 			SENDER_INITIATOR|SENDER_RECEIVER,
846 			login->req_buf,
847 			payload_length,
848 			conn);
849 	if (ret < 0)
850 		return -1;
851 
852 	if (ret > 0) {
853 		if (login->auth_complete) {
854 			pr_err("Initiator has already been"
855 				" successfully authenticated, but is still"
856 				" sending %s keys.\n", param->value);
857 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
858 					ISCSI_LOGIN_STATUS_INIT_ERR);
859 			return -1;
860 		}
861 
862 		goto do_auth;
863 	} else if (!payload_length) {
864 		pr_err("Initiator sent zero length security payload,"
865 		       " login failed\n");
866 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
867 				    ISCSI_LOGIN_STATUS_AUTH_FAILED);
868 		return -1;
869 	}
870 
871 	if (login->first_request)
872 		if (iscsi_target_check_first_request(conn, login) < 0)
873 			return -1;
874 
875 	ret = iscsi_encode_text_output(
876 			PHASE_SECURITY|PHASE_DECLARATIVE,
877 			SENDER_TARGET,
878 			login->rsp_buf,
879 			&login->rsp_length,
880 			conn->param_list,
881 			conn->tpg->tpg_attrib.login_keys_workaround);
882 	if (ret < 0)
883 		return -1;
884 
885 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
886 		if (conn->tpg->tpg_attrib.authentication &&
887 		    !strncmp(param->value, NONE, 4)) {
888 			pr_err("Initiator sent AuthMethod=None but"
889 				" Target is enforcing iSCSI Authentication,"
890 					" login failed.\n");
891 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
892 					ISCSI_LOGIN_STATUS_AUTH_FAILED);
893 			return -1;
894 		}
895 
896 		if (conn->tpg->tpg_attrib.authentication &&
897 		    !login->auth_complete)
898 			return 0;
899 
900 		if (strncmp(param->value, NONE, 4) && !login->auth_complete)
901 			return 0;
902 
903 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
904 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
905 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
906 					    ISCSI_FLAG_LOGIN_TRANSIT;
907 			login->current_stage = 1;
908 		}
909 	}
910 
911 	return 0;
912 do_auth:
913 	return iscsi_target_do_authentication(conn, login);
914 }
915 
916 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
917 {
918 	int ret;
919 	u32 payload_length;
920 	struct iscsi_login_req *login_req;
921 	struct iscsi_login_rsp *login_rsp;
922 
923 	login_req = (struct iscsi_login_req *) login->req;
924 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
925 	payload_length = ntoh24(login_req->dlength);
926 
927 	ret = iscsi_decode_text_input(
928 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
929 			SENDER_INITIATOR|SENDER_RECEIVER,
930 			login->req_buf,
931 			payload_length,
932 			conn);
933 	if (ret < 0) {
934 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
935 				ISCSI_LOGIN_STATUS_INIT_ERR);
936 		return -1;
937 	}
938 
939 	if (login->first_request)
940 		if (iscsi_target_check_first_request(conn, login) < 0)
941 			return -1;
942 
943 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
944 		return -1;
945 
946 	ret = iscsi_encode_text_output(
947 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
948 			SENDER_TARGET,
949 			login->rsp_buf,
950 			&login->rsp_length,
951 			conn->param_list,
952 			conn->tpg->tpg_attrib.login_keys_workaround);
953 	if (ret < 0) {
954 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
955 				ISCSI_LOGIN_STATUS_INIT_ERR);
956 		return -1;
957 	}
958 
959 	if (!login->auth_complete &&
960 	     conn->tpg->tpg_attrib.authentication) {
961 		pr_err("Initiator is requesting CSG: 1, has not been"
962 			 " successfully authenticated, and the Target is"
963 			" enforcing iSCSI Authentication, login failed.\n");
964 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
965 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
966 		return -1;
967 	}
968 
969 	if (!iscsi_check_negotiated_keys(conn->param_list))
970 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
971 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
972 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
973 					    ISCSI_FLAG_LOGIN_TRANSIT;
974 
975 	return 0;
976 }
977 
978 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
979 {
980 	int pdu_count = 0;
981 	struct iscsi_login_req *login_req;
982 	struct iscsi_login_rsp *login_rsp;
983 
984 	login_req = (struct iscsi_login_req *) login->req;
985 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
986 
987 	while (1) {
988 		if (++pdu_count > MAX_LOGIN_PDUS) {
989 			pr_err("MAX_LOGIN_PDUS count reached.\n");
990 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
991 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
992 			return -1;
993 		}
994 
995 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
996 		case 0:
997 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
998 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
999 				return -1;
1000 			break;
1001 		case 1:
1002 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
1003 			if (iscsi_target_handle_csg_one(conn, login) < 0)
1004 				return -1;
1005 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1006 				/*
1007 				 * Check to make sure the TCP connection has not
1008 				 * dropped asynchronously while session reinstatement
1009 				 * was occuring in this kthread context, before
1010 				 * transitioning to full feature phase operation.
1011 				 */
1012 				if (iscsi_target_sk_check_close(conn))
1013 					return -1;
1014 
1015 				login->tsih = conn->sess->tsih;
1016 				login->login_complete = 1;
1017 				iscsi_target_restore_sock_callbacks(conn);
1018 				if (iscsi_target_do_tx_login_io(conn,
1019 						login) < 0)
1020 					return -1;
1021 				return 1;
1022 			}
1023 			break;
1024 		default:
1025 			pr_err("Illegal CSG: %d received from"
1026 				" Initiator, protocol error.\n",
1027 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
1028 			break;
1029 		}
1030 
1031 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
1032 			return -1;
1033 
1034 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1035 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1036 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1037 		}
1038 		break;
1039 	}
1040 
1041 	return 0;
1042 }
1043 
1044 static void iscsi_initiatorname_tolower(
1045 	char *param_buf)
1046 {
1047 	char *c;
1048 	u32 iqn_size = strlen(param_buf), i;
1049 
1050 	for (i = 0; i < iqn_size; i++) {
1051 		c = &param_buf[i];
1052 		if (!isupper(*c))
1053 			continue;
1054 
1055 		*c = tolower(*c);
1056 	}
1057 }
1058 
1059 /*
1060  * Processes the first Login Request..
1061  */
1062 int iscsi_target_locate_portal(
1063 	struct iscsi_np *np,
1064 	struct iscsi_conn *conn,
1065 	struct iscsi_login *login)
1066 {
1067 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1068 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1069 	struct iscsi_session *sess = conn->sess;
1070 	struct iscsi_tiqn *tiqn;
1071 	struct iscsi_tpg_np *tpg_np = NULL;
1072 	struct iscsi_login_req *login_req;
1073 	struct se_node_acl *se_nacl;
1074 	u32 payload_length, queue_depth = 0;
1075 	int sessiontype = 0, ret = 0, tag_num, tag_size;
1076 
1077 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1078 	iscsi_target_set_sock_callbacks(conn);
1079 
1080 	login->np = np;
1081 
1082 	login_req = (struct iscsi_login_req *) login->req;
1083 	payload_length = ntoh24(login_req->dlength);
1084 
1085 	tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1086 	if (!tmpbuf) {
1087 		pr_err("Unable to allocate memory for tmpbuf.\n");
1088 		return -1;
1089 	}
1090 
1091 	memcpy(tmpbuf, login->req_buf, payload_length);
1092 	tmpbuf[payload_length] = '\0';
1093 	start = tmpbuf;
1094 	end = (start + payload_length);
1095 
1096 	/*
1097 	 * Locate the initial keys expected from the Initiator node in
1098 	 * the first login request in order to progress with the login phase.
1099 	 */
1100 	while (start < end) {
1101 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1102 			ret = -1;
1103 			goto out;
1104 		}
1105 
1106 		if (!strncmp(key, "InitiatorName", 13))
1107 			i_buf = value;
1108 		else if (!strncmp(key, "SessionType", 11))
1109 			s_buf = value;
1110 		else if (!strncmp(key, "TargetName", 10))
1111 			t_buf = value;
1112 
1113 		start += strlen(key) + strlen(value) + 2;
1114 	}
1115 	/*
1116 	 * See 5.3.  Login Phase.
1117 	 */
1118 	if (!i_buf) {
1119 		pr_err("InitiatorName key not received"
1120 			" in first login request.\n");
1121 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1122 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1123 		ret = -1;
1124 		goto out;
1125 	}
1126 	/*
1127 	 * Convert the incoming InitiatorName to lowercase following
1128 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1129 	 * are NOT case sensitive.
1130 	 */
1131 	iscsi_initiatorname_tolower(i_buf);
1132 
1133 	if (!s_buf) {
1134 		if (!login->leading_connection)
1135 			goto get_target;
1136 
1137 		pr_err("SessionType key not received"
1138 			" in first login request.\n");
1139 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1140 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1141 		ret = -1;
1142 		goto out;
1143 	}
1144 
1145 	/*
1146 	 * Use default portal group for discovery sessions.
1147 	 */
1148 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1149 	if (!sessiontype) {
1150 		conn->tpg = iscsit_global->discovery_tpg;
1151 		if (!login->leading_connection)
1152 			goto get_target;
1153 
1154 		sess->sess_ops->SessionType = 1;
1155 		/*
1156 		 * Setup crc32c modules from libcrypto
1157 		 */
1158 		if (iscsi_login_setup_crypto(conn) < 0) {
1159 			pr_err("iscsi_login_setup_crypto() failed\n");
1160 			ret = -1;
1161 			goto out;
1162 		}
1163 		/*
1164 		 * Serialize access across the discovery struct iscsi_portal_group to
1165 		 * process login attempt.
1166 		 */
1167 		if (iscsit_access_np(np, conn->tpg) < 0) {
1168 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1169 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1170 			ret = -1;
1171 			goto out;
1172 		}
1173 		ret = 0;
1174 		goto alloc_tags;
1175 	}
1176 
1177 get_target:
1178 	if (!t_buf) {
1179 		pr_err("TargetName key not received"
1180 			" in first login request while"
1181 			" SessionType=Normal.\n");
1182 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1183 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1184 		ret = -1;
1185 		goto out;
1186 	}
1187 
1188 	/*
1189 	 * Locate Target IQN from Storage Node.
1190 	 */
1191 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1192 	if (!tiqn) {
1193 		pr_err("Unable to locate Target IQN: %s in"
1194 			" Storage Node\n", t_buf);
1195 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1196 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1197 		ret = -1;
1198 		goto out;
1199 	}
1200 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1201 
1202 	/*
1203 	 * Locate Target Portal Group from Storage Node.
1204 	 */
1205 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1206 	if (!conn->tpg) {
1207 		pr_err("Unable to locate Target Portal Group"
1208 				" on %s\n", tiqn->tiqn);
1209 		iscsit_put_tiqn_for_login(tiqn);
1210 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1211 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1212 		ret = -1;
1213 		goto out;
1214 	}
1215 	conn->tpg_np = tpg_np;
1216 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1217 	/*
1218 	 * Setup crc32c modules from libcrypto
1219 	 */
1220 	if (iscsi_login_setup_crypto(conn) < 0) {
1221 		pr_err("iscsi_login_setup_crypto() failed\n");
1222 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1223 		iscsit_put_tiqn_for_login(tiqn);
1224 		conn->tpg = NULL;
1225 		ret = -1;
1226 		goto out;
1227 	}
1228 	/*
1229 	 * Serialize access across the struct iscsi_portal_group to
1230 	 * process login attempt.
1231 	 */
1232 	if (iscsit_access_np(np, conn->tpg) < 0) {
1233 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1234 		iscsit_put_tiqn_for_login(tiqn);
1235 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1236 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1237 		conn->tpg = NULL;
1238 		ret = -1;
1239 		goto out;
1240 	}
1241 
1242 	/*
1243 	 * conn->sess->node_acl will be set when the referenced
1244 	 * struct iscsi_session is located from received ISID+TSIH in
1245 	 * iscsi_login_non_zero_tsih_s2().
1246 	 */
1247 	if (!login->leading_connection) {
1248 		ret = 0;
1249 		goto out;
1250 	}
1251 
1252 	/*
1253 	 * This value is required in iscsi_login_zero_tsih_s2()
1254 	 */
1255 	sess->sess_ops->SessionType = 0;
1256 
1257 	/*
1258 	 * Locate incoming Initiator IQN reference from Storage Node.
1259 	 */
1260 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1261 			&conn->tpg->tpg_se_tpg, i_buf);
1262 	if (!sess->se_sess->se_node_acl) {
1263 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1264 			" access iSCSI target portal group: %hu.\n",
1265 				i_buf, conn->tpg->tpgt);
1266 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1267 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1268 		ret = -1;
1269 		goto out;
1270 	}
1271 	se_nacl = sess->se_sess->se_node_acl;
1272 	queue_depth = se_nacl->queue_depth;
1273 	/*
1274 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1275 	 * depth for non immediate commands, plus extra tags for immediate
1276 	 * commands.
1277 	 *
1278 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1279 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1280 	 */
1281 alloc_tags:
1282 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
1283 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1284 	tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1285 
1286 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1287 	if (ret < 0) {
1288 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1289 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1290 		ret = -1;
1291 	}
1292 out:
1293 	kfree(tmpbuf);
1294 	return ret;
1295 }
1296 
1297 int iscsi_target_start_negotiation(
1298 	struct iscsi_login *login,
1299 	struct iscsi_conn *conn)
1300 {
1301 	int ret;
1302 
1303 	if (conn->sock) {
1304 		struct sock *sk = conn->sock->sk;
1305 
1306 		write_lock_bh(&sk->sk_callback_lock);
1307 		set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1308 		set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1309 		write_unlock_bh(&sk->sk_callback_lock);
1310 	}
1311 	/*
1312 	 * If iscsi_target_do_login returns zero to signal more PDU
1313 	 * exchanges are required to complete the login, go ahead and
1314 	 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
1315 	 * is still active.
1316 	 *
1317 	 * Otherwise if TCP connection dropped asynchronously, go ahead
1318 	 * and perform connection cleanup now.
1319 	 */
1320 	ret = iscsi_target_do_login(conn, login);
1321 	if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
1322 		ret = -1;
1323 
1324 	if (ret < 0) {
1325 		cancel_delayed_work_sync(&conn->login_work);
1326 		iscsi_target_restore_sock_callbacks(conn);
1327 		iscsi_remove_failed_auth_entry(conn);
1328 	}
1329 	if (ret != 0)
1330 		iscsi_target_nego_release(conn);
1331 
1332 	return ret;
1333 }
1334 
1335 void iscsi_target_nego_release(struct iscsi_conn *conn)
1336 {
1337 	struct iscsi_login *login = conn->conn_login;
1338 
1339 	if (!login)
1340 		return;
1341 
1342 	kfree(login->req_buf);
1343 	kfree(login->rsp_buf);
1344 	kfree(login);
1345 
1346 	conn->conn_login = NULL;
1347 }
1348