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