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