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