xref: /linux/drivers/target/iscsi/iscsi_target_nego.c (revision 680e6ffa15103ab610c0fc1241d2f98c801b13e2)
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 		if (iscsi_target_sk_data_ready == conn->orig_data_ready)
436 			return;
437 		conn->orig_data_ready(sk);
438 		return;
439 	}
440 
441 	rc = schedule_delayed_work(&conn->login_work, 0);
442 	if (!rc) {
443 		pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
444 			 " got false\n");
445 	}
446 	write_unlock_bh(&sk->sk_callback_lock);
447 }
448 
449 static void iscsi_target_sk_state_change(struct sock *);
450 
451 static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
452 {
453 	struct sock *sk;
454 
455 	if (!conn->sock)
456 		return;
457 
458 	sk = conn->sock->sk;
459 	pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
460 
461 	write_lock_bh(&sk->sk_callback_lock);
462 	sk->sk_user_data = conn;
463 	conn->orig_data_ready = sk->sk_data_ready;
464 	conn->orig_state_change = sk->sk_state_change;
465 	sk->sk_data_ready = iscsi_target_sk_data_ready;
466 	sk->sk_state_change = iscsi_target_sk_state_change;
467 	write_unlock_bh(&sk->sk_callback_lock);
468 
469 	sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
470 	sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
471 }
472 
473 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
474 {
475 	struct sock *sk;
476 
477 	if (!conn->sock)
478 		return;
479 
480 	sk = conn->sock->sk;
481 	pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
482 
483 	write_lock_bh(&sk->sk_callback_lock);
484 	if (!sk->sk_user_data) {
485 		write_unlock_bh(&sk->sk_callback_lock);
486 		return;
487 	}
488 	sk->sk_user_data = NULL;
489 	sk->sk_data_ready = conn->orig_data_ready;
490 	sk->sk_state_change = conn->orig_state_change;
491 	write_unlock_bh(&sk->sk_callback_lock);
492 
493 	sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
494 	sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
495 }
496 
497 static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
498 
499 static bool __iscsi_target_sk_check_close(struct sock *sk)
500 {
501 	if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
502 		pr_debug("__iscsi_target_sk_check_close: TCP_CLOSE_WAIT|TCP_CLOSE,"
503 			"returning FALSE\n");
504 		return true;
505 	}
506 	return false;
507 }
508 
509 static bool iscsi_target_sk_check_close(struct iscsi_conn *conn)
510 {
511 	bool state = false;
512 
513 	if (conn->sock) {
514 		struct sock *sk = conn->sock->sk;
515 
516 		read_lock_bh(&sk->sk_callback_lock);
517 		state = (__iscsi_target_sk_check_close(sk) ||
518 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
519 		read_unlock_bh(&sk->sk_callback_lock);
520 	}
521 	return state;
522 }
523 
524 static bool iscsi_target_sk_check_flag(struct iscsi_conn *conn, unsigned int flag)
525 {
526 	bool state = false;
527 
528 	if (conn->sock) {
529 		struct sock *sk = conn->sock->sk;
530 
531 		read_lock_bh(&sk->sk_callback_lock);
532 		state = test_bit(flag, &conn->login_flags);
533 		read_unlock_bh(&sk->sk_callback_lock);
534 	}
535 	return state;
536 }
537 
538 static bool iscsi_target_sk_check_and_clear(struct iscsi_conn *conn, unsigned int flag)
539 {
540 	bool state = false;
541 
542 	if (conn->sock) {
543 		struct sock *sk = conn->sock->sk;
544 
545 		write_lock_bh(&sk->sk_callback_lock);
546 		state = (__iscsi_target_sk_check_close(sk) ||
547 			 test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags));
548 		if (!state)
549 			clear_bit(flag, &conn->login_flags);
550 		write_unlock_bh(&sk->sk_callback_lock);
551 	}
552 	return state;
553 }
554 
555 static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
556 {
557 	struct iscsi_np *np = login->np;
558 	bool zero_tsih = login->zero_tsih;
559 
560 	iscsi_remove_failed_auth_entry(conn);
561 	iscsi_target_nego_release(conn);
562 	iscsi_target_login_sess_out(conn, np, zero_tsih, true);
563 }
564 
565 struct conn_timeout {
566 	struct timer_list timer;
567 	struct iscsi_conn *conn;
568 };
569 
570 static void iscsi_target_login_timeout(struct timer_list *t)
571 {
572 	struct conn_timeout *timeout = from_timer(timeout, t, timer);
573 	struct iscsi_conn *conn = timeout->conn;
574 
575 	pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
576 
577 	if (conn->login_kworker) {
578 		pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
579 			 conn->login_kworker->comm, conn->login_kworker->pid);
580 		send_sig(SIGINT, conn->login_kworker, 1);
581 	}
582 }
583 
584 static void iscsi_target_do_login_rx(struct work_struct *work)
585 {
586 	struct iscsi_conn *conn = container_of(work,
587 				struct iscsi_conn, login_work.work);
588 	struct iscsi_login *login = conn->login;
589 	struct iscsi_np *np = login->np;
590 	struct iscsi_portal_group *tpg = conn->tpg;
591 	struct iscsi_tpg_np *tpg_np = conn->tpg_np;
592 	struct conn_timeout timeout;
593 	int rc, zero_tsih = login->zero_tsih;
594 	bool state;
595 
596 	pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
597 			conn, current->comm, current->pid);
598 	/*
599 	 * If iscsi_target_do_login_rx() has been invoked by ->sk_data_ready()
600 	 * before initial PDU processing in iscsi_target_start_negotiation()
601 	 * has completed, go ahead and retry until it's cleared.
602 	 *
603 	 * Otherwise if the TCP connection drops while this is occuring,
604 	 * iscsi_target_start_negotiation() will detect the failure, call
605 	 * cancel_delayed_work_sync(&conn->login_work), and cleanup the
606 	 * remaining iscsi connection resources from iscsi_np process context.
607 	 */
608 	if (iscsi_target_sk_check_flag(conn, LOGIN_FLAGS_INITIAL_PDU)) {
609 		schedule_delayed_work(&conn->login_work, msecs_to_jiffies(10));
610 		return;
611 	}
612 
613 	spin_lock(&tpg->tpg_state_lock);
614 	state = (tpg->tpg_state == TPG_STATE_ACTIVE);
615 	spin_unlock(&tpg->tpg_state_lock);
616 
617 	if (!state) {
618 		pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
619 		goto err;
620 	}
621 
622 	if (iscsi_target_sk_check_close(conn)) {
623 		pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
624 		goto err;
625 	}
626 
627 	conn->login_kworker = current;
628 	allow_signal(SIGINT);
629 
630 	timeout.conn = conn;
631 	timer_setup_on_stack(&timeout.timer, iscsi_target_login_timeout, 0);
632 	mod_timer(&timeout.timer, jiffies + TA_LOGIN_TIMEOUT * HZ);
633 	pr_debug("Starting login timer for %s/%d\n", current->comm, current->pid);
634 
635 	rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
636 	del_timer_sync(&timeout.timer);
637 	destroy_timer_on_stack(&timeout.timer);
638 	flush_signals(current);
639 	conn->login_kworker = NULL;
640 
641 	if (rc < 0)
642 		goto err;
643 
644 	pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
645 			conn, current->comm, current->pid);
646 
647 	rc = iscsi_target_do_login(conn, login);
648 	if (rc < 0) {
649 		goto err;
650 	} else if (!rc) {
651 		if (iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_READ_ACTIVE))
652 			goto err;
653 	} else if (rc == 1) {
654 		iscsi_target_nego_release(conn);
655 		iscsi_post_login_handler(np, conn, zero_tsih);
656 		iscsit_deaccess_np(np, tpg, tpg_np);
657 	}
658 	return;
659 
660 err:
661 	iscsi_target_restore_sock_callbacks(conn);
662 	iscsi_target_login_drop(conn, login);
663 	iscsit_deaccess_np(np, tpg, tpg_np);
664 }
665 
666 static void iscsi_target_sk_state_change(struct sock *sk)
667 {
668 	struct iscsi_conn *conn;
669 	void (*orig_state_change)(struct sock *);
670 	bool state;
671 
672 	pr_debug("Entering iscsi_target_sk_state_change\n");
673 
674 	write_lock_bh(&sk->sk_callback_lock);
675 	conn = sk->sk_user_data;
676 	if (!conn) {
677 		write_unlock_bh(&sk->sk_callback_lock);
678 		return;
679 	}
680 	orig_state_change = conn->orig_state_change;
681 
682 	if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
683 		pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
684 			 conn);
685 		write_unlock_bh(&sk->sk_callback_lock);
686 		orig_state_change(sk);
687 		return;
688 	}
689 	state = __iscsi_target_sk_check_close(sk);
690 	pr_debug("__iscsi_target_sk_close_change: state: %d\n", state);
691 
692 	if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
693 		pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
694 			 " conn: %p\n", conn);
695 		if (state)
696 			set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
697 		write_unlock_bh(&sk->sk_callback_lock);
698 		orig_state_change(sk);
699 		return;
700 	}
701 	if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
702 		pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
703 			 conn);
704 		write_unlock_bh(&sk->sk_callback_lock);
705 		orig_state_change(sk);
706 		return;
707 	}
708 	/*
709 	 * If the TCP connection has dropped, go ahead and set LOGIN_FLAGS_CLOSED,
710 	 * but only queue conn->login_work -> iscsi_target_do_login_rx()
711 	 * processing if LOGIN_FLAGS_INITIAL_PDU has already been cleared.
712 	 *
713 	 * When iscsi_target_do_login_rx() runs, iscsi_target_sk_check_close()
714 	 * will detect the dropped TCP connection from delayed workqueue context.
715 	 *
716 	 * If LOGIN_FLAGS_INITIAL_PDU is still set, which means the initial
717 	 * iscsi_target_start_negotiation() is running, iscsi_target_do_login()
718 	 * via iscsi_target_sk_check_close() or iscsi_target_start_negotiation()
719 	 * via iscsi_target_sk_check_and_clear() is responsible for detecting the
720 	 * dropped TCP connection in iscsi_np process context, and cleaning up
721 	 * the remaining iscsi connection resources.
722 	 */
723 	if (state) {
724 		pr_debug("iscsi_target_sk_state_change got failed state\n");
725 		set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags);
726 		state = test_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
727 		write_unlock_bh(&sk->sk_callback_lock);
728 
729 		orig_state_change(sk);
730 
731 		if (!state)
732 			schedule_delayed_work(&conn->login_work, 0);
733 		return;
734 	}
735 	write_unlock_bh(&sk->sk_callback_lock);
736 
737 	orig_state_change(sk);
738 }
739 
740 /*
741  *	NOTE: We check for existing sessions or connections AFTER the initiator
742  *	has been successfully authenticated in order to protect against faked
743  *	ISID/TSIH combinations.
744  */
745 static int iscsi_target_check_for_existing_instances(
746 	struct iscsi_conn *conn,
747 	struct iscsi_login *login)
748 {
749 	if (login->checked_for_existing)
750 		return 0;
751 
752 	login->checked_for_existing = 1;
753 
754 	if (!login->tsih)
755 		return iscsi_check_for_session_reinstatement(conn);
756 	else
757 		return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
758 				login->initial_exp_statsn);
759 }
760 
761 static int iscsi_target_do_authentication(
762 	struct iscsi_conn *conn,
763 	struct iscsi_login *login)
764 {
765 	int authret;
766 	u32 payload_length;
767 	struct iscsi_param *param;
768 	struct iscsi_login_req *login_req;
769 	struct iscsi_login_rsp *login_rsp;
770 
771 	login_req = (struct iscsi_login_req *) login->req;
772 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
773 	payload_length = ntoh24(login_req->dlength);
774 
775 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
776 	if (!param)
777 		return -1;
778 
779 	authret = iscsi_handle_authentication(
780 			conn,
781 			login->req_buf,
782 			login->rsp_buf,
783 			payload_length,
784 			&login->rsp_length,
785 			param->value);
786 	switch (authret) {
787 	case 0:
788 		pr_debug("Received OK response"
789 		" from LIO Authentication, continuing.\n");
790 		break;
791 	case 1:
792 		pr_debug("iSCSI security negotiation"
793 			" completed successfully.\n");
794 		login->auth_complete = 1;
795 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
796 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
797 			login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
798 					     ISCSI_FLAG_LOGIN_TRANSIT);
799 			login->current_stage = 1;
800 		}
801 		return iscsi_target_check_for_existing_instances(
802 				conn, login);
803 	case 2:
804 		pr_err("Security negotiation"
805 			" failed.\n");
806 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
807 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
808 		return -1;
809 	default:
810 		pr_err("Received unknown error %d from LIO"
811 				" Authentication\n", authret);
812 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
813 				ISCSI_LOGIN_STATUS_TARGET_ERROR);
814 		return -1;
815 	}
816 
817 	return 0;
818 }
819 
820 static int iscsi_target_handle_csg_zero(
821 	struct iscsi_conn *conn,
822 	struct iscsi_login *login)
823 {
824 	int ret;
825 	u32 payload_length;
826 	struct iscsi_param *param;
827 	struct iscsi_login_req *login_req;
828 	struct iscsi_login_rsp *login_rsp;
829 
830 	login_req = (struct iscsi_login_req *) login->req;
831 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
832 	payload_length = ntoh24(login_req->dlength);
833 
834 	param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
835 	if (!param)
836 		return -1;
837 
838 	ret = iscsi_decode_text_input(
839 			PHASE_SECURITY|PHASE_DECLARATIVE,
840 			SENDER_INITIATOR|SENDER_RECEIVER,
841 			login->req_buf,
842 			payload_length,
843 			conn);
844 	if (ret < 0)
845 		return -1;
846 
847 	if (ret > 0) {
848 		if (login->auth_complete) {
849 			pr_err("Initiator has already been"
850 				" successfully authenticated, but is still"
851 				" sending %s keys.\n", param->value);
852 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
853 					ISCSI_LOGIN_STATUS_INIT_ERR);
854 			return -1;
855 		}
856 
857 		goto do_auth;
858 	} else if (!payload_length) {
859 		pr_err("Initiator sent zero length security payload,"
860 		       " login failed\n");
861 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
862 				    ISCSI_LOGIN_STATUS_AUTH_FAILED);
863 		return -1;
864 	}
865 
866 	if (login->first_request)
867 		if (iscsi_target_check_first_request(conn, login) < 0)
868 			return -1;
869 
870 	ret = iscsi_encode_text_output(
871 			PHASE_SECURITY|PHASE_DECLARATIVE,
872 			SENDER_TARGET,
873 			login->rsp_buf,
874 			&login->rsp_length,
875 			conn->param_list,
876 			conn->tpg->tpg_attrib.login_keys_workaround);
877 	if (ret < 0)
878 		return -1;
879 
880 	if (!iscsi_check_negotiated_keys(conn->param_list)) {
881 		if (conn->tpg->tpg_attrib.authentication &&
882 		    !strncmp(param->value, NONE, 4)) {
883 			pr_err("Initiator sent AuthMethod=None but"
884 				" Target is enforcing iSCSI Authentication,"
885 					" login failed.\n");
886 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
887 					ISCSI_LOGIN_STATUS_AUTH_FAILED);
888 			return -1;
889 		}
890 
891 		if (conn->tpg->tpg_attrib.authentication &&
892 		    !login->auth_complete)
893 			return 0;
894 
895 		if (strncmp(param->value, NONE, 4) && !login->auth_complete)
896 			return 0;
897 
898 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
899 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
900 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
901 					    ISCSI_FLAG_LOGIN_TRANSIT;
902 			login->current_stage = 1;
903 		}
904 	}
905 
906 	return 0;
907 do_auth:
908 	return iscsi_target_do_authentication(conn, login);
909 }
910 
911 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
912 {
913 	int ret;
914 	u32 payload_length;
915 	struct iscsi_login_req *login_req;
916 	struct iscsi_login_rsp *login_rsp;
917 
918 	login_req = (struct iscsi_login_req *) login->req;
919 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
920 	payload_length = ntoh24(login_req->dlength);
921 
922 	ret = iscsi_decode_text_input(
923 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
924 			SENDER_INITIATOR|SENDER_RECEIVER,
925 			login->req_buf,
926 			payload_length,
927 			conn);
928 	if (ret < 0) {
929 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
930 				ISCSI_LOGIN_STATUS_INIT_ERR);
931 		return -1;
932 	}
933 
934 	if (login->first_request)
935 		if (iscsi_target_check_first_request(conn, login) < 0)
936 			return -1;
937 
938 	if (iscsi_target_check_for_existing_instances(conn, login) < 0)
939 		return -1;
940 
941 	ret = iscsi_encode_text_output(
942 			PHASE_OPERATIONAL|PHASE_DECLARATIVE,
943 			SENDER_TARGET,
944 			login->rsp_buf,
945 			&login->rsp_length,
946 			conn->param_list,
947 			conn->tpg->tpg_attrib.login_keys_workaround);
948 	if (ret < 0) {
949 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
950 				ISCSI_LOGIN_STATUS_INIT_ERR);
951 		return -1;
952 	}
953 
954 	if (!login->auth_complete &&
955 	     conn->tpg->tpg_attrib.authentication) {
956 		pr_err("Initiator is requesting CSG: 1, has not been"
957 			 " successfully authenticated, and the Target is"
958 			" enforcing iSCSI Authentication, login failed.\n");
959 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
960 				ISCSI_LOGIN_STATUS_AUTH_FAILED);
961 		return -1;
962 	}
963 
964 	if (!iscsi_check_negotiated_keys(conn->param_list))
965 		if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
966 		    (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
967 			login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
968 					    ISCSI_FLAG_LOGIN_TRANSIT;
969 
970 	return 0;
971 }
972 
973 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
974 {
975 	int pdu_count = 0;
976 	struct iscsi_login_req *login_req;
977 	struct iscsi_login_rsp *login_rsp;
978 
979 	login_req = (struct iscsi_login_req *) login->req;
980 	login_rsp = (struct iscsi_login_rsp *) login->rsp;
981 
982 	while (1) {
983 		if (++pdu_count > MAX_LOGIN_PDUS) {
984 			pr_err("MAX_LOGIN_PDUS count reached.\n");
985 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
986 					ISCSI_LOGIN_STATUS_TARGET_ERROR);
987 			return -1;
988 		}
989 
990 		switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
991 		case 0:
992 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
993 			if (iscsi_target_handle_csg_zero(conn, login) < 0)
994 				return -1;
995 			break;
996 		case 1:
997 			login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
998 			if (iscsi_target_handle_csg_one(conn, login) < 0)
999 				return -1;
1000 			if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1001 				/*
1002 				 * Check to make sure the TCP connection has not
1003 				 * dropped asynchronously while session reinstatement
1004 				 * was occuring in this kthread context, before
1005 				 * transitioning to full feature phase operation.
1006 				 */
1007 				if (iscsi_target_sk_check_close(conn))
1008 					return -1;
1009 
1010 				login->tsih = conn->sess->tsih;
1011 				login->login_complete = 1;
1012 				iscsi_target_restore_sock_callbacks(conn);
1013 				if (iscsi_target_do_tx_login_io(conn,
1014 						login) < 0)
1015 					return -1;
1016 				return 1;
1017 			}
1018 			break;
1019 		default:
1020 			pr_err("Illegal CSG: %d received from"
1021 				" Initiator, protocol error.\n",
1022 				ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
1023 			break;
1024 		}
1025 
1026 		if (iscsi_target_do_tx_login_io(conn, login) < 0)
1027 			return -1;
1028 
1029 		if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
1030 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
1031 			login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
1032 		}
1033 		break;
1034 	}
1035 
1036 	return 0;
1037 }
1038 
1039 static void iscsi_initiatorname_tolower(
1040 	char *param_buf)
1041 {
1042 	char *c;
1043 	u32 iqn_size = strlen(param_buf), i;
1044 
1045 	for (i = 0; i < iqn_size; i++) {
1046 		c = &param_buf[i];
1047 		if (!isupper(*c))
1048 			continue;
1049 
1050 		*c = tolower(*c);
1051 	}
1052 }
1053 
1054 /*
1055  * Processes the first Login Request..
1056  */
1057 int iscsi_target_locate_portal(
1058 	struct iscsi_np *np,
1059 	struct iscsi_conn *conn,
1060 	struct iscsi_login *login)
1061 {
1062 	char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
1063 	char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
1064 	struct iscsi_session *sess = conn->sess;
1065 	struct iscsi_tiqn *tiqn;
1066 	struct iscsi_tpg_np *tpg_np = NULL;
1067 	struct iscsi_login_req *login_req;
1068 	struct se_node_acl *se_nacl;
1069 	u32 payload_length, queue_depth = 0;
1070 	int sessiontype = 0, ret = 0, tag_num, tag_size;
1071 
1072 	INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
1073 	iscsi_target_set_sock_callbacks(conn);
1074 
1075 	login->np = np;
1076 
1077 	login_req = (struct iscsi_login_req *) login->req;
1078 	payload_length = ntoh24(login_req->dlength);
1079 
1080 	tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
1081 	if (!tmpbuf) {
1082 		pr_err("Unable to allocate memory for tmpbuf.\n");
1083 		return -1;
1084 	}
1085 
1086 	memcpy(tmpbuf, login->req_buf, payload_length);
1087 	tmpbuf[payload_length] = '\0';
1088 	start = tmpbuf;
1089 	end = (start + payload_length);
1090 
1091 	/*
1092 	 * Locate the initial keys expected from the Initiator node in
1093 	 * the first login request in order to progress with the login phase.
1094 	 */
1095 	while (start < end) {
1096 		if (iscsi_extract_key_value(start, &key, &value) < 0) {
1097 			ret = -1;
1098 			goto out;
1099 		}
1100 
1101 		if (!strncmp(key, "InitiatorName", 13))
1102 			i_buf = value;
1103 		else if (!strncmp(key, "SessionType", 11))
1104 			s_buf = value;
1105 		else if (!strncmp(key, "TargetName", 10))
1106 			t_buf = value;
1107 
1108 		start += strlen(key) + strlen(value) + 2;
1109 	}
1110 	/*
1111 	 * See 5.3.  Login Phase.
1112 	 */
1113 	if (!i_buf) {
1114 		pr_err("InitiatorName key not received"
1115 			" in first login request.\n");
1116 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1117 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1118 		ret = -1;
1119 		goto out;
1120 	}
1121 	/*
1122 	 * Convert the incoming InitiatorName to lowercase following
1123 	 * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1124 	 * are NOT case sensitive.
1125 	 */
1126 	iscsi_initiatorname_tolower(i_buf);
1127 
1128 	if (!s_buf) {
1129 		if (!login->leading_connection)
1130 			goto get_target;
1131 
1132 		pr_err("SessionType key not received"
1133 			" in first login request.\n");
1134 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1135 			ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1136 		ret = -1;
1137 		goto out;
1138 	}
1139 
1140 	/*
1141 	 * Use default portal group for discovery sessions.
1142 	 */
1143 	sessiontype = strncmp(s_buf, DISCOVERY, 9);
1144 	if (!sessiontype) {
1145 		conn->tpg = iscsit_global->discovery_tpg;
1146 		if (!login->leading_connection)
1147 			goto get_target;
1148 
1149 		sess->sess_ops->SessionType = 1;
1150 		/*
1151 		 * Setup crc32c modules from libcrypto
1152 		 */
1153 		if (iscsi_login_setup_crypto(conn) < 0) {
1154 			pr_err("iscsi_login_setup_crypto() failed\n");
1155 			ret = -1;
1156 			goto out;
1157 		}
1158 		/*
1159 		 * Serialize access across the discovery struct iscsi_portal_group to
1160 		 * process login attempt.
1161 		 */
1162 		if (iscsit_access_np(np, conn->tpg) < 0) {
1163 			iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1164 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1165 			ret = -1;
1166 			goto out;
1167 		}
1168 		ret = 0;
1169 		goto alloc_tags;
1170 	}
1171 
1172 get_target:
1173 	if (!t_buf) {
1174 		pr_err("TargetName key not received"
1175 			" in first login request while"
1176 			" SessionType=Normal.\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 	 * Locate Target IQN from Storage Node.
1185 	 */
1186 	tiqn = iscsit_get_tiqn_for_login(t_buf);
1187 	if (!tiqn) {
1188 		pr_err("Unable to locate Target IQN: %s in"
1189 			" Storage Node\n", t_buf);
1190 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1191 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1192 		ret = -1;
1193 		goto out;
1194 	}
1195 	pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1196 
1197 	/*
1198 	 * Locate Target Portal Group from Storage Node.
1199 	 */
1200 	conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1201 	if (!conn->tpg) {
1202 		pr_err("Unable to locate Target Portal Group"
1203 				" on %s\n", tiqn->tiqn);
1204 		iscsit_put_tiqn_for_login(tiqn);
1205 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1206 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1207 		ret = -1;
1208 		goto out;
1209 	}
1210 	conn->tpg_np = tpg_np;
1211 	pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1212 	/*
1213 	 * Setup crc32c modules from libcrypto
1214 	 */
1215 	if (iscsi_login_setup_crypto(conn) < 0) {
1216 		pr_err("iscsi_login_setup_crypto() failed\n");
1217 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1218 		iscsit_put_tiqn_for_login(tiqn);
1219 		conn->tpg = NULL;
1220 		ret = -1;
1221 		goto out;
1222 	}
1223 	/*
1224 	 * Serialize access across the struct iscsi_portal_group to
1225 	 * process login attempt.
1226 	 */
1227 	if (iscsit_access_np(np, conn->tpg) < 0) {
1228 		kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1229 		iscsit_put_tiqn_for_login(tiqn);
1230 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1231 				ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1232 		conn->tpg = NULL;
1233 		ret = -1;
1234 		goto out;
1235 	}
1236 
1237 	/*
1238 	 * conn->sess->node_acl will be set when the referenced
1239 	 * struct iscsi_session is located from received ISID+TSIH in
1240 	 * iscsi_login_non_zero_tsih_s2().
1241 	 */
1242 	if (!login->leading_connection) {
1243 		ret = 0;
1244 		goto out;
1245 	}
1246 
1247 	/*
1248 	 * This value is required in iscsi_login_zero_tsih_s2()
1249 	 */
1250 	sess->sess_ops->SessionType = 0;
1251 
1252 	/*
1253 	 * Locate incoming Initiator IQN reference from Storage Node.
1254 	 */
1255 	sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1256 			&conn->tpg->tpg_se_tpg, i_buf);
1257 	if (!sess->se_sess->se_node_acl) {
1258 		pr_err("iSCSI Initiator Node: %s is not authorized to"
1259 			" access iSCSI target portal group: %hu.\n",
1260 				i_buf, conn->tpg->tpgt);
1261 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1262 				ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1263 		ret = -1;
1264 		goto out;
1265 	}
1266 	se_nacl = sess->se_sess->se_node_acl;
1267 	queue_depth = se_nacl->queue_depth;
1268 	/*
1269 	 * Setup pre-allocated tags based upon allowed per NodeACL CmdSN
1270 	 * depth for non immediate commands, plus extra tags for immediate
1271 	 * commands.
1272 	 *
1273 	 * Also enforce a ISCSIT_MIN_TAGS to prevent unnecessary contention
1274 	 * in per-cpu-ida tag allocation logic + small queue_depth.
1275 	 */
1276 alloc_tags:
1277 	tag_num = max_t(u32, ISCSIT_MIN_TAGS, queue_depth);
1278 	tag_num = (tag_num * 2) + ISCSIT_EXTRA_TAGS;
1279 	tag_size = sizeof(struct iscsi_cmd) + conn->conn_transport->priv_size;
1280 
1281 	ret = transport_alloc_session_tags(sess->se_sess, tag_num, tag_size);
1282 	if (ret < 0) {
1283 		iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1284 				    ISCSI_LOGIN_STATUS_NO_RESOURCES);
1285 		ret = -1;
1286 	}
1287 out:
1288 	kfree(tmpbuf);
1289 	return ret;
1290 }
1291 
1292 int iscsi_target_start_negotiation(
1293 	struct iscsi_login *login,
1294 	struct iscsi_conn *conn)
1295 {
1296 	int ret;
1297 
1298 	if (conn->sock) {
1299 		struct sock *sk = conn->sock->sk;
1300 
1301 		write_lock_bh(&sk->sk_callback_lock);
1302 		set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1303 		set_bit(LOGIN_FLAGS_INITIAL_PDU, &conn->login_flags);
1304 		write_unlock_bh(&sk->sk_callback_lock);
1305 	}
1306 	/*
1307 	 * If iscsi_target_do_login returns zero to signal more PDU
1308 	 * exchanges are required to complete the login, go ahead and
1309 	 * clear LOGIN_FLAGS_INITIAL_PDU but only if the TCP connection
1310 	 * is still active.
1311 	 *
1312 	 * Otherwise if TCP connection dropped asynchronously, go ahead
1313 	 * and perform connection cleanup now.
1314 	 */
1315 	ret = iscsi_target_do_login(conn, login);
1316 	if (!ret && iscsi_target_sk_check_and_clear(conn, LOGIN_FLAGS_INITIAL_PDU))
1317 		ret = -1;
1318 
1319 	if (ret < 0) {
1320 		cancel_delayed_work_sync(&conn->login_work);
1321 		iscsi_target_restore_sock_callbacks(conn);
1322 		iscsi_remove_failed_auth_entry(conn);
1323 	}
1324 	if (ret != 0)
1325 		iscsi_target_nego_release(conn);
1326 
1327 	return ret;
1328 }
1329 
1330 void iscsi_target_nego_release(struct iscsi_conn *conn)
1331 {
1332 	struct iscsi_login *login = conn->conn_login;
1333 
1334 	if (!login)
1335 		return;
1336 
1337 	kfree(login->req_buf);
1338 	kfree(login->rsp_buf);
1339 	kfree(login);
1340 
1341 	conn->conn_login = NULL;
1342 }
1343