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