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