Input: sur40 - skip all blobs that are not touches
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / target / iscsi / iscsi_target_login.c
1 /*******************************************************************************
2 * This file contains the login functions used by the iSCSI Target driver.
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 <crypto/hash.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/sched/signal.h>
24 #include <linux/idr.h>
25 #include <linux/tcp.h> /* TCP_NODELAY */
26 #include <net/ipv6.h> /* ipv6_addr_v4mapped() */
27 #include <scsi/iscsi_proto.h>
28 #include <target/target_core_base.h>
29 #include <target/target_core_fabric.h>
30
31 #include <target/iscsi/iscsi_target_core.h>
32 #include <target/iscsi/iscsi_target_stat.h>
33 #include "iscsi_target_device.h"
34 #include "iscsi_target_nego.h"
35 #include "iscsi_target_erl0.h"
36 #include "iscsi_target_erl2.h"
37 #include "iscsi_target_login.h"
38 #include "iscsi_target_tpg.h"
39 #include "iscsi_target_util.h"
40 #include "iscsi_target.h"
41 #include "iscsi_target_parameters.h"
42
43 #include <target/iscsi/iscsi_transport.h>
44
45 static struct iscsi_login *iscsi_login_init_conn(struct iscsi_conn *conn)
46 {
47 struct iscsi_login *login;
48
49 login = kzalloc(sizeof(struct iscsi_login), GFP_KERNEL);
50 if (!login) {
51 pr_err("Unable to allocate memory for struct iscsi_login.\n");
52 return NULL;
53 }
54 conn->login = login;
55 login->conn = conn;
56 login->first_request = 1;
57
58 login->req_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
59 if (!login->req_buf) {
60 pr_err("Unable to allocate memory for response buffer.\n");
61 goto out_login;
62 }
63
64 login->rsp_buf = kzalloc(MAX_KEY_VALUE_PAIRS, GFP_KERNEL);
65 if (!login->rsp_buf) {
66 pr_err("Unable to allocate memory for request buffer.\n");
67 goto out_req_buf;
68 }
69
70 conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
71 if (!conn->conn_ops) {
72 pr_err("Unable to allocate memory for"
73 " struct iscsi_conn_ops.\n");
74 goto out_rsp_buf;
75 }
76
77 init_waitqueue_head(&conn->queues_wq);
78 INIT_LIST_HEAD(&conn->conn_list);
79 INIT_LIST_HEAD(&conn->conn_cmd_list);
80 INIT_LIST_HEAD(&conn->immed_queue_list);
81 INIT_LIST_HEAD(&conn->response_queue_list);
82 init_completion(&conn->conn_post_wait_comp);
83 init_completion(&conn->conn_wait_comp);
84 init_completion(&conn->conn_wait_rcfr_comp);
85 init_completion(&conn->conn_waiting_on_uc_comp);
86 init_completion(&conn->conn_logout_comp);
87 init_completion(&conn->rx_half_close_comp);
88 init_completion(&conn->tx_half_close_comp);
89 init_completion(&conn->rx_login_comp);
90 spin_lock_init(&conn->cmd_lock);
91 spin_lock_init(&conn->conn_usage_lock);
92 spin_lock_init(&conn->immed_queue_lock);
93 spin_lock_init(&conn->nopin_timer_lock);
94 spin_lock_init(&conn->response_queue_lock);
95 spin_lock_init(&conn->state_lock);
96
97 if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
98 pr_err("Unable to allocate conn->conn_cpumask\n");
99 goto out_conn_ops;
100 }
101 conn->conn_login = login;
102
103 return login;
104
105 out_conn_ops:
106 kfree(conn->conn_ops);
107 out_rsp_buf:
108 kfree(login->rsp_buf);
109 out_req_buf:
110 kfree(login->req_buf);
111 out_login:
112 kfree(login);
113 return NULL;
114 }
115
116 /*
117 * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
118 * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
119 */
120 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
121 {
122 struct crypto_ahash *tfm;
123
124 /*
125 * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
126 * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
127 * to software 1x8 byte slicing from crc32c.ko
128 */
129 tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
130 if (IS_ERR(tfm)) {
131 pr_err("crypto_alloc_ahash() failed\n");
132 return -ENOMEM;
133 }
134
135 conn->conn_rx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
136 if (!conn->conn_rx_hash) {
137 pr_err("ahash_request_alloc() failed for conn_rx_hash\n");
138 crypto_free_ahash(tfm);
139 return -ENOMEM;
140 }
141 ahash_request_set_callback(conn->conn_rx_hash, 0, NULL, NULL);
142
143 conn->conn_tx_hash = ahash_request_alloc(tfm, GFP_KERNEL);
144 if (!conn->conn_tx_hash) {
145 pr_err("ahash_request_alloc() failed for conn_tx_hash\n");
146 ahash_request_free(conn->conn_rx_hash);
147 conn->conn_rx_hash = NULL;
148 crypto_free_ahash(tfm);
149 return -ENOMEM;
150 }
151 ahash_request_set_callback(conn->conn_tx_hash, 0, NULL, NULL);
152
153 return 0;
154 }
155
156 static int iscsi_login_check_initiator_version(
157 struct iscsi_conn *conn,
158 u8 version_max,
159 u8 version_min)
160 {
161 if ((version_max != 0x00) || (version_min != 0x00)) {
162 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
163 " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
164 version_min, version_max);
165 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
166 ISCSI_LOGIN_STATUS_NO_VERSION);
167 return -1;
168 }
169
170 return 0;
171 }
172
173 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
174 {
175 int sessiontype;
176 struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
177 struct iscsi_portal_group *tpg = conn->tpg;
178 struct iscsi_session *sess = NULL, *sess_p = NULL;
179 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
180 struct se_session *se_sess, *se_sess_tmp;
181
182 initiatorname_param = iscsi_find_param_from_key(
183 INITIATORNAME, conn->param_list);
184 sessiontype_param = iscsi_find_param_from_key(
185 SESSIONTYPE, conn->param_list);
186 if (!initiatorname_param || !sessiontype_param) {
187 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
188 ISCSI_LOGIN_STATUS_MISSING_FIELDS);
189 return -1;
190 }
191
192 sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
193
194 spin_lock_bh(&se_tpg->session_lock);
195 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
196 sess_list) {
197
198 sess_p = se_sess->fabric_sess_ptr;
199 spin_lock(&sess_p->conn_lock);
200 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
201 atomic_read(&sess_p->session_logout) ||
202 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
203 spin_unlock(&sess_p->conn_lock);
204 continue;
205 }
206 if (!memcmp(sess_p->isid, conn->sess->isid, 6) &&
207 (!strcmp(sess_p->sess_ops->InitiatorName,
208 initiatorname_param->value) &&
209 (sess_p->sess_ops->SessionType == sessiontype))) {
210 atomic_set(&sess_p->session_reinstatement, 1);
211 atomic_set(&sess_p->session_fall_back_to_erl0, 1);
212 spin_unlock(&sess_p->conn_lock);
213 iscsit_inc_session_usage_count(sess_p);
214 iscsit_stop_time2retain_timer(sess_p);
215 sess = sess_p;
216 break;
217 }
218 spin_unlock(&sess_p->conn_lock);
219 }
220 spin_unlock_bh(&se_tpg->session_lock);
221 /*
222 * If the Time2Retain handler has expired, the session is already gone.
223 */
224 if (!sess)
225 return 0;
226
227 pr_debug("%s iSCSI Session SID %u is still active for %s,"
228 " performing session reinstatement.\n", (sessiontype) ?
229 "Discovery" : "Normal", sess->sid,
230 sess->sess_ops->InitiatorName);
231
232 spin_lock_bh(&sess->conn_lock);
233 if (sess->session_state == TARG_SESS_STATE_FAILED) {
234 spin_unlock_bh(&sess->conn_lock);
235 iscsit_dec_session_usage_count(sess);
236 iscsit_close_session(sess);
237 return 0;
238 }
239 spin_unlock_bh(&sess->conn_lock);
240
241 iscsit_stop_session(sess, 1, 1);
242 iscsit_dec_session_usage_count(sess);
243
244 iscsit_close_session(sess);
245 return 0;
246 }
247
248 static void iscsi_login_set_conn_values(
249 struct iscsi_session *sess,
250 struct iscsi_conn *conn,
251 __be16 cid)
252 {
253 conn->sess = sess;
254 conn->cid = be16_to_cpu(cid);
255 /*
256 * Generate a random Status sequence number (statsn) for the new
257 * iSCSI connection.
258 */
259 get_random_bytes(&conn->stat_sn, sizeof(u32));
260
261 mutex_lock(&auth_id_lock);
262 conn->auth_id = iscsit_global->auth_id++;
263 mutex_unlock(&auth_id_lock);
264 }
265
266 __printf(2, 3) int iscsi_change_param_sprintf(
267 struct iscsi_conn *conn,
268 const char *fmt, ...)
269 {
270 va_list args;
271 unsigned char buf[64];
272
273 memset(buf, 0, sizeof buf);
274
275 va_start(args, fmt);
276 vsnprintf(buf, sizeof buf, fmt, args);
277 va_end(args);
278
279 if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
280 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
281 ISCSI_LOGIN_STATUS_NO_RESOURCES);
282 return -1;
283 }
284
285 return 0;
286 }
287 EXPORT_SYMBOL(iscsi_change_param_sprintf);
288
289 /*
290 * This is the leading connection of a new session,
291 * or session reinstatement.
292 */
293 static int iscsi_login_zero_tsih_s1(
294 struct iscsi_conn *conn,
295 unsigned char *buf)
296 {
297 struct iscsi_session *sess = NULL;
298 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
299 int ret;
300
301 sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
302 if (!sess) {
303 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
304 ISCSI_LOGIN_STATUS_NO_RESOURCES);
305 pr_err("Could not allocate memory for session\n");
306 return -ENOMEM;
307 }
308
309 iscsi_login_set_conn_values(sess, conn, pdu->cid);
310 sess->init_task_tag = pdu->itt;
311 memcpy(&sess->isid, pdu->isid, 6);
312 sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn);
313 INIT_LIST_HEAD(&sess->sess_conn_list);
314 INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
315 INIT_LIST_HEAD(&sess->cr_active_list);
316 INIT_LIST_HEAD(&sess->cr_inactive_list);
317 init_completion(&sess->async_msg_comp);
318 init_completion(&sess->reinstatement_comp);
319 init_completion(&sess->session_wait_comp);
320 init_completion(&sess->session_waiting_on_uc_comp);
321 mutex_init(&sess->cmdsn_mutex);
322 spin_lock_init(&sess->conn_lock);
323 spin_lock_init(&sess->cr_a_lock);
324 spin_lock_init(&sess->cr_i_lock);
325 spin_lock_init(&sess->session_usage_lock);
326 spin_lock_init(&sess->ttt_lock);
327
328 idr_preload(GFP_KERNEL);
329 spin_lock_bh(&sess_idr_lock);
330 ret = idr_alloc(&sess_idr, NULL, 0, 0, GFP_NOWAIT);
331 if (ret >= 0)
332 sess->session_index = ret;
333 spin_unlock_bh(&sess_idr_lock);
334 idr_preload_end();
335
336 if (ret < 0) {
337 pr_err("idr_alloc() for sess_idr failed\n");
338 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
339 ISCSI_LOGIN_STATUS_NO_RESOURCES);
340 kfree(sess);
341 return -ENOMEM;
342 }
343
344 sess->creation_time = get_jiffies_64();
345 /*
346 * The FFP CmdSN window values will be allocated from the TPG's
347 * Initiator Node's ACL once the login has been successfully completed.
348 */
349 atomic_set(&sess->max_cmd_sn, be32_to_cpu(pdu->cmdsn));
350
351 sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
352 if (!sess->sess_ops) {
353 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
354 ISCSI_LOGIN_STATUS_NO_RESOURCES);
355 pr_err("Unable to allocate memory for"
356 " struct iscsi_sess_ops.\n");
357 kfree(sess);
358 return -ENOMEM;
359 }
360
361 sess->se_sess = transport_init_session(TARGET_PROT_NORMAL);
362 if (IS_ERR(sess->se_sess)) {
363 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
364 ISCSI_LOGIN_STATUS_NO_RESOURCES);
365 kfree(sess->sess_ops);
366 kfree(sess);
367 return -ENOMEM;
368 }
369
370 return 0;
371 }
372
373 static int iscsi_login_zero_tsih_s2(
374 struct iscsi_conn *conn)
375 {
376 struct iscsi_node_attrib *na;
377 struct iscsi_session *sess = conn->sess;
378 bool iser = false;
379
380 sess->tpg = conn->tpg;
381
382 /*
383 * Assign a new TPG Session Handle. Note this is protected with
384 * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
385 */
386 sess->tsih = ++sess->tpg->ntsih;
387 if (!sess->tsih)
388 sess->tsih = ++sess->tpg->ntsih;
389
390 /*
391 * Create the default params from user defined values..
392 */
393 if (iscsi_copy_param_list(&conn->param_list,
394 conn->tpg->param_list, 1) < 0) {
395 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
396 ISCSI_LOGIN_STATUS_NO_RESOURCES);
397 return -1;
398 }
399
400 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
401 iser = true;
402
403 iscsi_set_keys_to_negotiate(conn->param_list, iser);
404
405 if (sess->sess_ops->SessionType)
406 return iscsi_set_keys_irrelevant_for_discovery(
407 conn->param_list);
408
409 na = iscsit_tpg_get_node_attrib(sess);
410
411 /*
412 * Need to send TargetPortalGroupTag back in first login response
413 * on any iSCSI connection where the Initiator provides TargetName.
414 * See 5.3.1. Login Phase Start
415 *
416 * In our case, we have already located the struct iscsi_tiqn at this point.
417 */
418 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
419 return -1;
420
421 /*
422 * Workaround for Initiators that have broken connection recovery logic.
423 *
424 * "We would really like to get rid of this." Linux-iSCSI.org team
425 */
426 if (iscsi_change_param_sprintf(conn, "ErrorRecoveryLevel=%d", na->default_erl))
427 return -1;
428
429 /*
430 * Set RDMAExtensions=Yes by default for iSER enabled network portals
431 */
432 if (iser) {
433 struct iscsi_param *param;
434 unsigned long mrdsl, off;
435 int rc;
436
437 if (iscsi_change_param_sprintf(conn, "RDMAExtensions=Yes"))
438 return -1;
439
440 /*
441 * Make MaxRecvDataSegmentLength PAGE_SIZE aligned for
442 * Immediate Data + Unsolicited Data-OUT if necessary..
443 */
444 param = iscsi_find_param_from_key("MaxRecvDataSegmentLength",
445 conn->param_list);
446 if (!param) {
447 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
448 ISCSI_LOGIN_STATUS_NO_RESOURCES);
449 return -1;
450 }
451 rc = kstrtoul(param->value, 0, &mrdsl);
452 if (rc < 0) {
453 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
454 ISCSI_LOGIN_STATUS_NO_RESOURCES);
455 return -1;
456 }
457 off = mrdsl % PAGE_SIZE;
458 if (!off)
459 goto check_prot;
460
461 if (mrdsl < PAGE_SIZE)
462 mrdsl = PAGE_SIZE;
463 else
464 mrdsl -= off;
465
466 pr_warn("Aligning ISER MaxRecvDataSegmentLength: %lu down"
467 " to PAGE_SIZE\n", mrdsl);
468
469 if (iscsi_change_param_sprintf(conn, "MaxRecvDataSegmentLength=%lu\n", mrdsl))
470 return -1;
471 /*
472 * ISER currently requires that ImmediateData + Unsolicited
473 * Data be disabled when protection / signature MRs are enabled.
474 */
475 check_prot:
476 if (sess->se_sess->sup_prot_ops &
477 (TARGET_PROT_DOUT_STRIP | TARGET_PROT_DOUT_PASS |
478 TARGET_PROT_DOUT_INSERT)) {
479
480 if (iscsi_change_param_sprintf(conn, "ImmediateData=No"))
481 return -1;
482
483 if (iscsi_change_param_sprintf(conn, "InitialR2T=Yes"))
484 return -1;
485
486 pr_debug("Forcing ImmediateData=No + InitialR2T=Yes for"
487 " T10-PI enabled ISER session\n");
488 }
489 }
490
491 return 0;
492 }
493
494 static int iscsi_login_non_zero_tsih_s1(
495 struct iscsi_conn *conn,
496 unsigned char *buf)
497 {
498 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
499
500 iscsi_login_set_conn_values(NULL, conn, pdu->cid);
501 return 0;
502 }
503
504 /*
505 * Add a new connection to an existing session.
506 */
507 static int iscsi_login_non_zero_tsih_s2(
508 struct iscsi_conn *conn,
509 unsigned char *buf)
510 {
511 struct iscsi_portal_group *tpg = conn->tpg;
512 struct iscsi_session *sess = NULL, *sess_p = NULL;
513 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
514 struct se_session *se_sess, *se_sess_tmp;
515 struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
516 bool iser = false;
517
518 spin_lock_bh(&se_tpg->session_lock);
519 list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
520 sess_list) {
521
522 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
523 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
524 atomic_read(&sess_p->session_logout) ||
525 (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
526 continue;
527 if (!memcmp(sess_p->isid, pdu->isid, 6) &&
528 (sess_p->tsih == be16_to_cpu(pdu->tsih))) {
529 iscsit_inc_session_usage_count(sess_p);
530 iscsit_stop_time2retain_timer(sess_p);
531 sess = sess_p;
532 break;
533 }
534 }
535 spin_unlock_bh(&se_tpg->session_lock);
536
537 /*
538 * If the Time2Retain handler has expired, the session is already gone.
539 */
540 if (!sess) {
541 pr_err("Initiator attempting to add a connection to"
542 " a non-existent session, rejecting iSCSI Login.\n");
543 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
544 ISCSI_LOGIN_STATUS_NO_SESSION);
545 return -1;
546 }
547
548 /*
549 * Stop the Time2Retain timer if this is a failed session, we restart
550 * the timer if the login is not successful.
551 */
552 spin_lock_bh(&sess->conn_lock);
553 if (sess->session_state == TARG_SESS_STATE_FAILED)
554 atomic_set(&sess->session_continuation, 1);
555 spin_unlock_bh(&sess->conn_lock);
556
557 iscsi_login_set_conn_values(sess, conn, pdu->cid);
558
559 if (iscsi_copy_param_list(&conn->param_list,
560 conn->tpg->param_list, 0) < 0) {
561 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
562 ISCSI_LOGIN_STATUS_NO_RESOURCES);
563 return -1;
564 }
565
566 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
567 iser = true;
568
569 iscsi_set_keys_to_negotiate(conn->param_list, iser);
570 /*
571 * Need to send TargetPortalGroupTag back in first login response
572 * on any iSCSI connection where the Initiator provides TargetName.
573 * See 5.3.1. Login Phase Start
574 *
575 * In our case, we have already located the struct iscsi_tiqn at this point.
576 */
577 if (iscsi_change_param_sprintf(conn, "TargetPortalGroupTag=%hu", sess->tpg->tpgt))
578 return -1;
579
580 return 0;
581 }
582
583 int iscsi_login_post_auth_non_zero_tsih(
584 struct iscsi_conn *conn,
585 u16 cid,
586 u32 exp_statsn)
587 {
588 struct iscsi_conn *conn_ptr = NULL;
589 struct iscsi_conn_recovery *cr = NULL;
590 struct iscsi_session *sess = conn->sess;
591
592 /*
593 * By following item 5 in the login table, if we have found
594 * an existing ISID and a valid/existing TSIH and an existing
595 * CID we do connection reinstatement. Currently we dont not
596 * support it so we send back an non-zero status class to the
597 * initiator and release the new connection.
598 */
599 conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
600 if (conn_ptr) {
601 pr_err("Connection exists with CID %hu for %s,"
602 " performing connection reinstatement.\n",
603 conn_ptr->cid, sess->sess_ops->InitiatorName);
604
605 iscsit_connection_reinstatement_rcfr(conn_ptr);
606 iscsit_dec_conn_usage_count(conn_ptr);
607 }
608
609 /*
610 * Check for any connection recovery entires containing CID.
611 * We use the original ExpStatSN sent in the first login request
612 * to acknowledge commands for the failed connection.
613 *
614 * Also note that an explict logout may have already been sent,
615 * but the response may not be sent due to additional connection
616 * loss.
617 */
618 if (sess->sess_ops->ErrorRecoveryLevel == 2) {
619 cr = iscsit_get_inactive_connection_recovery_entry(
620 sess, cid);
621 if (cr) {
622 pr_debug("Performing implicit logout"
623 " for connection recovery on CID: %hu\n",
624 conn->cid);
625 iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
626 }
627 }
628
629 /*
630 * Else we follow item 4 from the login table in that we have
631 * found an existing ISID and a valid/existing TSIH and a new
632 * CID we go ahead and continue to add a new connection to the
633 * session.
634 */
635 pr_debug("Adding CID %hu to existing session for %s.\n",
636 cid, sess->sess_ops->InitiatorName);
637
638 if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
639 pr_err("Adding additional connection to this session"
640 " would exceed MaxConnections %d, login failed.\n",
641 sess->sess_ops->MaxConnections);
642 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
643 ISCSI_LOGIN_STATUS_ISID_ERROR);
644 return -1;
645 }
646
647 return 0;
648 }
649
650 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
651 {
652 struct iscsi_session *sess = conn->sess;
653 /*
654 * FIXME: Unsolicited NopIN support for ISER
655 */
656 if (conn->conn_transport->transport_type == ISCSI_INFINIBAND)
657 return;
658
659 if (!sess->sess_ops->SessionType)
660 iscsit_start_nopin_timer(conn);
661 }
662
663 int iscsit_start_kthreads(struct iscsi_conn *conn)
664 {
665 int ret = 0;
666
667 spin_lock(&iscsit_global->ts_bitmap_lock);
668 conn->bitmap_id = bitmap_find_free_region(iscsit_global->ts_bitmap,
669 ISCSIT_BITMAP_BITS, get_order(1));
670 spin_unlock(&iscsit_global->ts_bitmap_lock);
671
672 if (conn->bitmap_id < 0) {
673 pr_err("bitmap_find_free_region() failed for"
674 " iscsit_start_kthreads()\n");
675 return -ENOMEM;
676 }
677
678 conn->tx_thread = kthread_run(iscsi_target_tx_thread, conn,
679 "%s", ISCSI_TX_THREAD_NAME);
680 if (IS_ERR(conn->tx_thread)) {
681 pr_err("Unable to start iscsi_target_tx_thread\n");
682 ret = PTR_ERR(conn->tx_thread);
683 goto out_bitmap;
684 }
685 conn->tx_thread_active = true;
686
687 conn->rx_thread = kthread_run(iscsi_target_rx_thread, conn,
688 "%s", ISCSI_RX_THREAD_NAME);
689 if (IS_ERR(conn->rx_thread)) {
690 pr_err("Unable to start iscsi_target_rx_thread\n");
691 ret = PTR_ERR(conn->rx_thread);
692 goto out_tx;
693 }
694 conn->rx_thread_active = true;
695
696 return 0;
697 out_tx:
698 send_sig(SIGINT, conn->tx_thread, 1);
699 kthread_stop(conn->tx_thread);
700 conn->tx_thread_active = false;
701 out_bitmap:
702 spin_lock(&iscsit_global->ts_bitmap_lock);
703 bitmap_release_region(iscsit_global->ts_bitmap, conn->bitmap_id,
704 get_order(1));
705 spin_unlock(&iscsit_global->ts_bitmap_lock);
706 return ret;
707 }
708
709 void iscsi_post_login_handler(
710 struct iscsi_np *np,
711 struct iscsi_conn *conn,
712 u8 zero_tsih)
713 {
714 int stop_timer = 0;
715 struct iscsi_session *sess = conn->sess;
716 struct se_session *se_sess = sess->se_sess;
717 struct iscsi_portal_group *tpg = sess->tpg;
718 struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
719
720 iscsit_inc_conn_usage_count(conn);
721
722 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
723 ISCSI_LOGIN_STATUS_ACCEPT);
724
725 pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
726 conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
727
728 iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
729 /*
730 * SCSI Initiator -> SCSI Target Port Mapping
731 */
732 if (!zero_tsih) {
733 iscsi_set_session_parameters(sess->sess_ops,
734 conn->param_list, 0);
735 iscsi_release_param_list(conn->param_list);
736 conn->param_list = NULL;
737
738 spin_lock_bh(&sess->conn_lock);
739 atomic_set(&sess->session_continuation, 0);
740 if (sess->session_state == TARG_SESS_STATE_FAILED) {
741 pr_debug("Moving to"
742 " TARG_SESS_STATE_LOGGED_IN.\n");
743 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
744 stop_timer = 1;
745 }
746
747 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to"
748 " %pISpc,%hu\n", conn->cid, &conn->login_sockaddr,
749 &conn->local_sockaddr, tpg->tpgt);
750
751 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
752 atomic_inc(&sess->nconn);
753 pr_debug("Incremented iSCSI Connection count to %hu"
754 " from node: %s\n", atomic_read(&sess->nconn),
755 sess->sess_ops->InitiatorName);
756 spin_unlock_bh(&sess->conn_lock);
757
758 iscsi_post_login_start_timers(conn);
759 /*
760 * Determine CPU mask to ensure connection's RX and TX kthreads
761 * are scheduled on the same CPU.
762 */
763 iscsit_thread_get_cpumask(conn);
764 conn->conn_rx_reset_cpumask = 1;
765 conn->conn_tx_reset_cpumask = 1;
766 /*
767 * Wakeup the sleeping iscsi_target_rx_thread() now that
768 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
769 */
770 complete(&conn->rx_login_comp);
771 iscsit_dec_conn_usage_count(conn);
772
773 if (stop_timer) {
774 spin_lock_bh(&se_tpg->session_lock);
775 iscsit_stop_time2retain_timer(sess);
776 spin_unlock_bh(&se_tpg->session_lock);
777 }
778 iscsit_dec_session_usage_count(sess);
779 return;
780 }
781
782 iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
783 iscsi_release_param_list(conn->param_list);
784 conn->param_list = NULL;
785
786 iscsit_determine_maxcmdsn(sess);
787
788 spin_lock_bh(&se_tpg->session_lock);
789 __transport_register_session(&sess->tpg->tpg_se_tpg,
790 se_sess->se_node_acl, se_sess, sess);
791 pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
792 sess->session_state = TARG_SESS_STATE_LOGGED_IN;
793
794 pr_debug("iSCSI Login successful on CID: %hu from %pISpc to %pISpc,%hu\n",
795 conn->cid, &conn->login_sockaddr, &conn->local_sockaddr,
796 tpg->tpgt);
797
798 spin_lock_bh(&sess->conn_lock);
799 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
800 atomic_inc(&sess->nconn);
801 pr_debug("Incremented iSCSI Connection count to %hu from node:"
802 " %s\n", atomic_read(&sess->nconn),
803 sess->sess_ops->InitiatorName);
804 spin_unlock_bh(&sess->conn_lock);
805
806 sess->sid = tpg->sid++;
807 if (!sess->sid)
808 sess->sid = tpg->sid++;
809 pr_debug("Established iSCSI session from node: %s\n",
810 sess->sess_ops->InitiatorName);
811
812 tpg->nsessions++;
813 if (tpg->tpg_tiqn)
814 tpg->tpg_tiqn->tiqn_nsessions++;
815
816 pr_debug("Incremented number of active iSCSI sessions to %u on"
817 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
818 spin_unlock_bh(&se_tpg->session_lock);
819
820 iscsi_post_login_start_timers(conn);
821 /*
822 * Determine CPU mask to ensure connection's RX and TX kthreads
823 * are scheduled on the same CPU.
824 */
825 iscsit_thread_get_cpumask(conn);
826 conn->conn_rx_reset_cpumask = 1;
827 conn->conn_tx_reset_cpumask = 1;
828 /*
829 * Wakeup the sleeping iscsi_target_rx_thread() now that
830 * iscsi_conn is in TARG_CONN_STATE_LOGGED_IN state.
831 */
832 complete(&conn->rx_login_comp);
833 iscsit_dec_conn_usage_count(conn);
834 }
835
836 static void iscsi_handle_login_thread_timeout(unsigned long data)
837 {
838 struct iscsi_np *np = (struct iscsi_np *) data;
839
840 spin_lock_bh(&np->np_thread_lock);
841 pr_err("iSCSI Login timeout on Network Portal %pISpc\n",
842 &np->np_sockaddr);
843
844 if (np->np_login_timer_flags & ISCSI_TF_STOP) {
845 spin_unlock_bh(&np->np_thread_lock);
846 return;
847 }
848
849 if (np->np_thread)
850 send_sig(SIGINT, np->np_thread, 1);
851
852 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
853 spin_unlock_bh(&np->np_thread_lock);
854 }
855
856 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
857 {
858 /*
859 * This used the TA_LOGIN_TIMEOUT constant because at this
860 * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
861 */
862 spin_lock_bh(&np->np_thread_lock);
863 init_timer(&np->np_login_timer);
864 np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
865 np->np_login_timer.data = (unsigned long)np;
866 np->np_login_timer.function = iscsi_handle_login_thread_timeout;
867 np->np_login_timer_flags &= ~ISCSI_TF_STOP;
868 np->np_login_timer_flags |= ISCSI_TF_RUNNING;
869 add_timer(&np->np_login_timer);
870
871 pr_debug("Added timeout timer to iSCSI login request for"
872 " %u seconds.\n", TA_LOGIN_TIMEOUT);
873 spin_unlock_bh(&np->np_thread_lock);
874 }
875
876 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
877 {
878 spin_lock_bh(&np->np_thread_lock);
879 if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
880 spin_unlock_bh(&np->np_thread_lock);
881 return;
882 }
883 np->np_login_timer_flags |= ISCSI_TF_STOP;
884 spin_unlock_bh(&np->np_thread_lock);
885
886 del_timer_sync(&np->np_login_timer);
887
888 spin_lock_bh(&np->np_thread_lock);
889 np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
890 spin_unlock_bh(&np->np_thread_lock);
891 }
892
893 int iscsit_setup_np(
894 struct iscsi_np *np,
895 struct sockaddr_storage *sockaddr)
896 {
897 struct socket *sock = NULL;
898 int backlog = ISCSIT_TCP_BACKLOG, ret, opt = 0, len;
899
900 switch (np->np_network_transport) {
901 case ISCSI_TCP:
902 np->np_ip_proto = IPPROTO_TCP;
903 np->np_sock_type = SOCK_STREAM;
904 break;
905 case ISCSI_SCTP_TCP:
906 np->np_ip_proto = IPPROTO_SCTP;
907 np->np_sock_type = SOCK_STREAM;
908 break;
909 case ISCSI_SCTP_UDP:
910 np->np_ip_proto = IPPROTO_SCTP;
911 np->np_sock_type = SOCK_SEQPACKET;
912 break;
913 default:
914 pr_err("Unsupported network_transport: %d\n",
915 np->np_network_transport);
916 return -EINVAL;
917 }
918
919 np->np_ip_proto = IPPROTO_TCP;
920 np->np_sock_type = SOCK_STREAM;
921
922 ret = sock_create(sockaddr->ss_family, np->np_sock_type,
923 np->np_ip_proto, &sock);
924 if (ret < 0) {
925 pr_err("sock_create() failed.\n");
926 return ret;
927 }
928 np->np_socket = sock;
929 /*
930 * Setup the np->np_sockaddr from the passed sockaddr setup
931 * in iscsi_target_configfs.c code..
932 */
933 memcpy(&np->np_sockaddr, sockaddr,
934 sizeof(struct sockaddr_storage));
935
936 if (sockaddr->ss_family == AF_INET6)
937 len = sizeof(struct sockaddr_in6);
938 else
939 len = sizeof(struct sockaddr_in);
940 /*
941 * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
942 */
943 /* FIXME: Someone please explain why this is endian-safe */
944 opt = 1;
945 if (np->np_network_transport == ISCSI_TCP) {
946 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
947 (char *)&opt, sizeof(opt));
948 if (ret < 0) {
949 pr_err("kernel_setsockopt() for TCP_NODELAY"
950 " failed: %d\n", ret);
951 goto fail;
952 }
953 }
954
955 /* FIXME: Someone please explain why this is endian-safe */
956 ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
957 (char *)&opt, sizeof(opt));
958 if (ret < 0) {
959 pr_err("kernel_setsockopt() for SO_REUSEADDR"
960 " failed\n");
961 goto fail;
962 }
963
964 ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
965 (char *)&opt, sizeof(opt));
966 if (ret < 0) {
967 pr_err("kernel_setsockopt() for IP_FREEBIND"
968 " failed\n");
969 goto fail;
970 }
971
972 ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
973 if (ret < 0) {
974 pr_err("kernel_bind() failed: %d\n", ret);
975 goto fail;
976 }
977
978 ret = kernel_listen(sock, backlog);
979 if (ret != 0) {
980 pr_err("kernel_listen() failed: %d\n", ret);
981 goto fail;
982 }
983
984 return 0;
985 fail:
986 np->np_socket = NULL;
987 sock_release(sock);
988 return ret;
989 }
990
991 int iscsi_target_setup_login_socket(
992 struct iscsi_np *np,
993 struct sockaddr_storage *sockaddr)
994 {
995 struct iscsit_transport *t;
996 int rc;
997
998 t = iscsit_get_transport(np->np_network_transport);
999 if (!t)
1000 return -EINVAL;
1001
1002 rc = t->iscsit_setup_np(np, sockaddr);
1003 if (rc < 0) {
1004 iscsit_put_transport(t);
1005 return rc;
1006 }
1007
1008 np->np_transport = t;
1009 np->enabled = true;
1010 return 0;
1011 }
1012
1013 int iscsit_accept_np(struct iscsi_np *np, struct iscsi_conn *conn)
1014 {
1015 struct socket *new_sock, *sock = np->np_socket;
1016 struct sockaddr_in sock_in;
1017 struct sockaddr_in6 sock_in6;
1018 int rc, err;
1019
1020 rc = kernel_accept(sock, &new_sock, 0);
1021 if (rc < 0)
1022 return rc;
1023
1024 conn->sock = new_sock;
1025 conn->login_family = np->np_sockaddr.ss_family;
1026
1027 if (np->np_sockaddr.ss_family == AF_INET6) {
1028 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
1029
1030 rc = conn->sock->ops->getname(conn->sock,
1031 (struct sockaddr *)&sock_in6, &err, 1);
1032 if (!rc) {
1033 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1034 memcpy(&conn->login_sockaddr, &sock_in6, sizeof(sock_in6));
1035 } else {
1036 /* Pretend to be an ipv4 socket */
1037 sock_in.sin_family = AF_INET;
1038 sock_in.sin_port = sock_in6.sin6_port;
1039 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1040 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1041 }
1042 }
1043
1044 rc = conn->sock->ops->getname(conn->sock,
1045 (struct sockaddr *)&sock_in6, &err, 0);
1046 if (!rc) {
1047 if (!ipv6_addr_v4mapped(&sock_in6.sin6_addr)) {
1048 memcpy(&conn->local_sockaddr, &sock_in6, sizeof(sock_in6));
1049 } else {
1050 /* Pretend to be an ipv4 socket */
1051 sock_in.sin_family = AF_INET;
1052 sock_in.sin_port = sock_in6.sin6_port;
1053 memcpy(&sock_in.sin_addr, &sock_in6.sin6_addr.s6_addr32[3], 4);
1054 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1055 }
1056 }
1057 } else {
1058 memset(&sock_in, 0, sizeof(struct sockaddr_in));
1059
1060 rc = conn->sock->ops->getname(conn->sock,
1061 (struct sockaddr *)&sock_in, &err, 1);
1062 if (!rc)
1063 memcpy(&conn->login_sockaddr, &sock_in, sizeof(sock_in));
1064
1065 rc = conn->sock->ops->getname(conn->sock,
1066 (struct sockaddr *)&sock_in, &err, 0);
1067 if (!rc)
1068 memcpy(&conn->local_sockaddr, &sock_in, sizeof(sock_in));
1069 }
1070
1071 return 0;
1072 }
1073
1074 int iscsit_get_login_rx(struct iscsi_conn *conn, struct iscsi_login *login)
1075 {
1076 struct iscsi_login_req *login_req;
1077 u32 padding = 0, payload_length;
1078
1079 if (iscsi_login_rx_data(conn, login->req, ISCSI_HDR_LEN) < 0)
1080 return -1;
1081
1082 login_req = (struct iscsi_login_req *)login->req;
1083 payload_length = ntoh24(login_req->dlength);
1084 padding = ((-payload_length) & 3);
1085
1086 pr_debug("Got Login Command, Flags 0x%02x, ITT: 0x%08x,"
1087 " CmdSN: 0x%08x, ExpStatSN: 0x%08x, CID: %hu, Length: %u\n",
1088 login_req->flags, login_req->itt, login_req->cmdsn,
1089 login_req->exp_statsn, login_req->cid, payload_length);
1090 /*
1091 * Setup the initial iscsi_login values from the leading
1092 * login request PDU.
1093 */
1094 if (login->first_request) {
1095 login_req = (struct iscsi_login_req *)login->req;
1096 login->leading_connection = (!login_req->tsih) ? 1 : 0;
1097 login->current_stage = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
1098 login->version_min = login_req->min_version;
1099 login->version_max = login_req->max_version;
1100 memcpy(login->isid, login_req->isid, 6);
1101 login->cmd_sn = be32_to_cpu(login_req->cmdsn);
1102 login->init_task_tag = login_req->itt;
1103 login->initial_exp_statsn = be32_to_cpu(login_req->exp_statsn);
1104 login->cid = be16_to_cpu(login_req->cid);
1105 login->tsih = be16_to_cpu(login_req->tsih);
1106 }
1107
1108 if (iscsi_target_check_login_request(conn, login) < 0)
1109 return -1;
1110
1111 memset(login->req_buf, 0, MAX_KEY_VALUE_PAIRS);
1112 if (iscsi_login_rx_data(conn, login->req_buf,
1113 payload_length + padding) < 0)
1114 return -1;
1115
1116 return 0;
1117 }
1118
1119 int iscsit_put_login_tx(struct iscsi_conn *conn, struct iscsi_login *login,
1120 u32 length)
1121 {
1122 if (iscsi_login_tx_data(conn, login->rsp, login->rsp_buf, length) < 0)
1123 return -1;
1124
1125 return 0;
1126 }
1127
1128 static int
1129 iscsit_conn_set_transport(struct iscsi_conn *conn, struct iscsit_transport *t)
1130 {
1131 int rc;
1132
1133 if (!t->owner) {
1134 conn->conn_transport = t;
1135 return 0;
1136 }
1137
1138 rc = try_module_get(t->owner);
1139 if (!rc) {
1140 pr_err("try_module_get() failed for %s\n", t->name);
1141 return -EINVAL;
1142 }
1143
1144 conn->conn_transport = t;
1145 return 0;
1146 }
1147
1148 void iscsi_target_login_sess_out(struct iscsi_conn *conn,
1149 struct iscsi_np *np, bool zero_tsih, bool new_sess)
1150 {
1151 if (!new_sess)
1152 goto old_sess_out;
1153
1154 pr_err("iSCSI Login negotiation failed.\n");
1155 iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1156 ISCSI_LOGIN_STATUS_INIT_ERR);
1157 if (!zero_tsih || !conn->sess)
1158 goto old_sess_out;
1159 if (conn->sess->se_sess)
1160 transport_free_session(conn->sess->se_sess);
1161 if (conn->sess->session_index != 0) {
1162 spin_lock_bh(&sess_idr_lock);
1163 idr_remove(&sess_idr, conn->sess->session_index);
1164 spin_unlock_bh(&sess_idr_lock);
1165 }
1166 kfree(conn->sess->sess_ops);
1167 kfree(conn->sess);
1168 conn->sess = NULL;
1169
1170 old_sess_out:
1171 iscsi_stop_login_thread_timer(np);
1172 /*
1173 * If login negotiation fails check if the Time2Retain timer
1174 * needs to be restarted.
1175 */
1176 if (!zero_tsih && conn->sess) {
1177 spin_lock_bh(&conn->sess->conn_lock);
1178 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1179 struct se_portal_group *se_tpg =
1180 &conn->tpg->tpg_se_tpg;
1181
1182 atomic_set(&conn->sess->session_continuation, 0);
1183 spin_unlock_bh(&conn->sess->conn_lock);
1184 spin_lock_bh(&se_tpg->session_lock);
1185 iscsit_start_time2retain_handler(conn->sess);
1186 spin_unlock_bh(&se_tpg->session_lock);
1187 } else
1188 spin_unlock_bh(&conn->sess->conn_lock);
1189 iscsit_dec_session_usage_count(conn->sess);
1190 }
1191
1192 ahash_request_free(conn->conn_tx_hash);
1193 if (conn->conn_rx_hash) {
1194 struct crypto_ahash *tfm;
1195
1196 tfm = crypto_ahash_reqtfm(conn->conn_rx_hash);
1197 ahash_request_free(conn->conn_rx_hash);
1198 crypto_free_ahash(tfm);
1199 }
1200
1201 free_cpumask_var(conn->conn_cpumask);
1202
1203 kfree(conn->conn_ops);
1204
1205 if (conn->param_list) {
1206 iscsi_release_param_list(conn->param_list);
1207 conn->param_list = NULL;
1208 }
1209 iscsi_target_nego_release(conn);
1210
1211 if (conn->sock) {
1212 sock_release(conn->sock);
1213 conn->sock = NULL;
1214 }
1215
1216 if (conn->conn_transport->iscsit_wait_conn)
1217 conn->conn_transport->iscsit_wait_conn(conn);
1218
1219 if (conn->conn_transport->iscsit_free_conn)
1220 conn->conn_transport->iscsit_free_conn(conn);
1221
1222 iscsit_put_transport(conn->conn_transport);
1223 kfree(conn);
1224 }
1225
1226 static int __iscsi_target_login_thread(struct iscsi_np *np)
1227 {
1228 u8 *buffer, zero_tsih = 0;
1229 int ret = 0, rc;
1230 struct iscsi_conn *conn = NULL;
1231 struct iscsi_login *login;
1232 struct iscsi_portal_group *tpg = NULL;
1233 struct iscsi_login_req *pdu;
1234 struct iscsi_tpg_np *tpg_np;
1235 bool new_sess = false;
1236
1237 flush_signals(current);
1238
1239 spin_lock_bh(&np->np_thread_lock);
1240 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1241 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1242 complete(&np->np_restart_comp);
1243 } else if (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN) {
1244 spin_unlock_bh(&np->np_thread_lock);
1245 goto exit;
1246 } else {
1247 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
1248 }
1249 spin_unlock_bh(&np->np_thread_lock);
1250
1251 conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
1252 if (!conn) {
1253 pr_err("Could not allocate memory for"
1254 " new connection\n");
1255 /* Get another socket */
1256 return 1;
1257 }
1258 pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
1259 conn->conn_state = TARG_CONN_STATE_FREE;
1260
1261 if (iscsit_conn_set_transport(conn, np->np_transport) < 0) {
1262 kfree(conn);
1263 return 1;
1264 }
1265
1266 rc = np->np_transport->iscsit_accept_np(np, conn);
1267 if (rc == -ENOSYS) {
1268 complete(&np->np_restart_comp);
1269 iscsit_put_transport(conn->conn_transport);
1270 kfree(conn);
1271 conn = NULL;
1272 goto exit;
1273 } else if (rc < 0) {
1274 spin_lock_bh(&np->np_thread_lock);
1275 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
1276 spin_unlock_bh(&np->np_thread_lock);
1277 complete(&np->np_restart_comp);
1278 iscsit_put_transport(conn->conn_transport);
1279 kfree(conn);
1280 conn = NULL;
1281 /* Get another socket */
1282 return 1;
1283 }
1284 spin_unlock_bh(&np->np_thread_lock);
1285 iscsit_put_transport(conn->conn_transport);
1286 kfree(conn);
1287 conn = NULL;
1288 goto out;
1289 }
1290 /*
1291 * Perform the remaining iSCSI connection initialization items..
1292 */
1293 login = iscsi_login_init_conn(conn);
1294 if (!login) {
1295 goto new_sess_out;
1296 }
1297
1298 iscsi_start_login_thread_timer(np);
1299
1300 pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
1301 conn->conn_state = TARG_CONN_STATE_XPT_UP;
1302 /*
1303 * This will process the first login request + payload..
1304 */
1305 rc = np->np_transport->iscsit_get_login_rx(conn, login);
1306 if (rc == 1)
1307 return 1;
1308 else if (rc < 0)
1309 goto new_sess_out;
1310
1311 buffer = &login->req[0];
1312 pdu = (struct iscsi_login_req *)buffer;
1313 /*
1314 * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
1315 * when Status-Class != 0.
1316 */
1317 conn->login_itt = pdu->itt;
1318
1319 spin_lock_bh(&np->np_thread_lock);
1320 if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
1321 spin_unlock_bh(&np->np_thread_lock);
1322 pr_err("iSCSI Network Portal on %pISpc currently not"
1323 " active.\n", &np->np_sockaddr);
1324 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1325 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1326 goto new_sess_out;
1327 }
1328 spin_unlock_bh(&np->np_thread_lock);
1329
1330 conn->network_transport = np->np_network_transport;
1331
1332 pr_debug("Received iSCSI login request from %pISpc on %s Network"
1333 " Portal %pISpc\n", &conn->login_sockaddr, np->np_transport->name,
1334 &conn->local_sockaddr);
1335
1336 pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1337 conn->conn_state = TARG_CONN_STATE_IN_LOGIN;
1338
1339 if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1340 pdu->min_version) < 0)
1341 goto new_sess_out;
1342
1343 zero_tsih = (pdu->tsih == 0x0000);
1344 if (zero_tsih) {
1345 /*
1346 * This is the leading connection of a new session.
1347 * We wait until after authentication to check for
1348 * session reinstatement.
1349 */
1350 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1351 goto new_sess_out;
1352 } else {
1353 /*
1354 * Add a new connection to an existing session.
1355 * We check for a non-existant session in
1356 * iscsi_login_non_zero_tsih_s2() below based
1357 * on ISID/TSIH, but wait until after authentication
1358 * to check for connection reinstatement, etc.
1359 */
1360 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1361 goto new_sess_out;
1362 }
1363 /*
1364 * SessionType: Discovery
1365 *
1366 * Locates Default Portal
1367 *
1368 * SessionType: Normal
1369 *
1370 * Locates Target Portal from NP -> Target IQN
1371 */
1372 rc = iscsi_target_locate_portal(np, conn, login);
1373 if (rc < 0) {
1374 tpg = conn->tpg;
1375 goto new_sess_out;
1376 }
1377 login->zero_tsih = zero_tsih;
1378
1379 if (conn->sess)
1380 conn->sess->se_sess->sup_prot_ops =
1381 conn->conn_transport->iscsit_get_sup_prot_ops(conn);
1382
1383 tpg = conn->tpg;
1384 if (!tpg) {
1385 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1386 goto new_sess_out;
1387 }
1388
1389 if (zero_tsih) {
1390 if (iscsi_login_zero_tsih_s2(conn) < 0)
1391 goto new_sess_out;
1392 } else {
1393 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0)
1394 goto old_sess_out;
1395 }
1396
1397 if (conn->conn_transport->iscsit_validate_params) {
1398 ret = conn->conn_transport->iscsit_validate_params(conn);
1399 if (ret < 0) {
1400 if (zero_tsih)
1401 goto new_sess_out;
1402 else
1403 goto old_sess_out;
1404 }
1405 }
1406
1407 ret = iscsi_target_start_negotiation(login, conn);
1408 if (ret < 0)
1409 goto new_sess_out;
1410
1411 iscsi_stop_login_thread_timer(np);
1412
1413 if (ret == 1) {
1414 tpg_np = conn->tpg_np;
1415
1416 iscsi_post_login_handler(np, conn, zero_tsih);
1417 iscsit_deaccess_np(np, tpg, tpg_np);
1418 }
1419
1420 tpg = NULL;
1421 tpg_np = NULL;
1422 /* Get another socket */
1423 return 1;
1424
1425 new_sess_out:
1426 new_sess = true;
1427 old_sess_out:
1428 tpg_np = conn->tpg_np;
1429 iscsi_target_login_sess_out(conn, np, zero_tsih, new_sess);
1430 new_sess = false;
1431
1432 if (tpg) {
1433 iscsit_deaccess_np(np, tpg, tpg_np);
1434 tpg = NULL;
1435 tpg_np = NULL;
1436 }
1437
1438 out:
1439 return 1;
1440
1441 exit:
1442 iscsi_stop_login_thread_timer(np);
1443 spin_lock_bh(&np->np_thread_lock);
1444 np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1445 spin_unlock_bh(&np->np_thread_lock);
1446
1447 return 0;
1448 }
1449
1450 int iscsi_target_login_thread(void *arg)
1451 {
1452 struct iscsi_np *np = arg;
1453 int ret;
1454
1455 allow_signal(SIGINT);
1456
1457 while (1) {
1458 ret = __iscsi_target_login_thread(np);
1459 /*
1460 * We break and exit here unless another sock_accept() call
1461 * is expected.
1462 */
1463 if (ret != 1)
1464 break;
1465 }
1466
1467 return 0;
1468 }