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