iscsi-target: Always send a response before terminating iSCSI connection
[pandora-kernel.git] / drivers / target / iscsi / iscsi_target_login.c
1 /*******************************************************************************
2  * This file contains the login functions used by the iSCSI Target driver.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20
21 #include <linux/string.h>
22 #include <linux/kthread.h>
23 #include <linux/crypto.h>
24 #include <scsi/iscsi_proto.h>
25 #include <target/target_core_base.h>
26 #include <target/target_core_transport.h>
27
28 #include "iscsi_target_core.h"
29 #include "iscsi_target_tq.h"
30 #include "iscsi_target_device.h"
31 #include "iscsi_target_nego.h"
32 #include "iscsi_target_erl0.h"
33 #include "iscsi_target_erl2.h"
34 #include "iscsi_target_login.h"
35 #include "iscsi_target_stat.h"
36 #include "iscsi_target_tpg.h"
37 #include "iscsi_target_util.h"
38 #include "iscsi_target.h"
39 #include "iscsi_target_parameters.h"
40
41 extern struct idr sess_idr;
42 extern struct mutex auth_id_lock;
43 extern spinlock_t sess_idr_lock;
44
45 static int iscsi_login_init_conn(struct iscsi_conn *conn)
46 {
47         init_waitqueue_head(&conn->queues_wq);
48         INIT_LIST_HEAD(&conn->conn_list);
49         INIT_LIST_HEAD(&conn->conn_cmd_list);
50         INIT_LIST_HEAD(&conn->immed_queue_list);
51         INIT_LIST_HEAD(&conn->response_queue_list);
52         init_completion(&conn->conn_post_wait_comp);
53         init_completion(&conn->conn_wait_comp);
54         init_completion(&conn->conn_wait_rcfr_comp);
55         init_completion(&conn->conn_waiting_on_uc_comp);
56         init_completion(&conn->conn_logout_comp);
57         init_completion(&conn->rx_half_close_comp);
58         init_completion(&conn->tx_half_close_comp);
59         spin_lock_init(&conn->cmd_lock);
60         spin_lock_init(&conn->conn_usage_lock);
61         spin_lock_init(&conn->immed_queue_lock);
62         spin_lock_init(&conn->nopin_timer_lock);
63         spin_lock_init(&conn->response_queue_lock);
64         spin_lock_init(&conn->state_lock);
65
66         if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) {
67                 pr_err("Unable to allocate conn->conn_cpumask\n");
68                 return -ENOMEM;
69         }
70
71         return 0;
72 }
73
74 /*
75  * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup
76  * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel
77  */
78 int iscsi_login_setup_crypto(struct iscsi_conn *conn)
79 {
80         /*
81          * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts
82          * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback
83          * to software 1x8 byte slicing from crc32c.ko
84          */
85         conn->conn_rx_hash.flags = 0;
86         conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0,
87                                                 CRYPTO_ALG_ASYNC);
88         if (IS_ERR(conn->conn_rx_hash.tfm)) {
89                 pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n");
90                 return -ENOMEM;
91         }
92
93         conn->conn_tx_hash.flags = 0;
94         conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0,
95                                                 CRYPTO_ALG_ASYNC);
96         if (IS_ERR(conn->conn_tx_hash.tfm)) {
97                 pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n");
98                 crypto_free_hash(conn->conn_rx_hash.tfm);
99                 return -ENOMEM;
100         }
101
102         return 0;
103 }
104
105 static int iscsi_login_check_initiator_version(
106         struct iscsi_conn *conn,
107         u8 version_max,
108         u8 version_min)
109 {
110         if ((version_max != 0x00) || (version_min != 0x00)) {
111                 pr_err("Unsupported iSCSI IETF Pre-RFC Revision,"
112                         " version Min/Max 0x%02x/0x%02x, rejecting login.\n",
113                         version_min, version_max);
114                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
115                                 ISCSI_LOGIN_STATUS_NO_VERSION);
116                 return -1;
117         }
118
119         return 0;
120 }
121
122 int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn)
123 {
124         int sessiontype;
125         struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL;
126         struct iscsi_portal_group *tpg = conn->tpg;
127         struct iscsi_session *sess = NULL, *sess_p = NULL;
128         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
129         struct se_session *se_sess, *se_sess_tmp;
130
131         initiatorname_param = iscsi_find_param_from_key(
132                         INITIATORNAME, conn->param_list);
133         sessiontype_param = iscsi_find_param_from_key(
134                         SESSIONTYPE, conn->param_list);
135         if (!initiatorname_param || !sessiontype_param) {
136                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
137                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
138                 return -1;
139         }
140
141         sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0;
142
143         spin_lock_bh(&se_tpg->session_lock);
144         list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
145                         sess_list) {
146
147                 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
148                 spin_lock(&sess_p->conn_lock);
149                 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
150                     atomic_read(&sess_p->session_logout) ||
151                     (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) {
152                         spin_unlock(&sess_p->conn_lock);
153                         continue;
154                 }
155                 if (!memcmp((void *)sess_p->isid, (void *)conn->sess->isid, 6) &&
156                    (!strcmp((void *)sess_p->sess_ops->InitiatorName,
157                             (void *)initiatorname_param->value) &&
158                    (sess_p->sess_ops->SessionType == sessiontype))) {
159                         atomic_set(&sess_p->session_reinstatement, 1);
160                         spin_unlock(&sess_p->conn_lock);
161                         iscsit_inc_session_usage_count(sess_p);
162                         iscsit_stop_time2retain_timer(sess_p);
163                         sess = sess_p;
164                         break;
165                 }
166                 spin_unlock(&sess_p->conn_lock);
167         }
168         spin_unlock_bh(&se_tpg->session_lock);
169         /*
170          * If the Time2Retain handler has expired, the session is already gone.
171          */
172         if (!sess)
173                 return 0;
174
175         pr_debug("%s iSCSI Session SID %u is still active for %s,"
176                 " preforming session reinstatement.\n", (sessiontype) ?
177                 "Discovery" : "Normal", sess->sid,
178                 sess->sess_ops->InitiatorName);
179
180         spin_lock_bh(&sess->conn_lock);
181         if (sess->session_state == TARG_SESS_STATE_FAILED) {
182                 spin_unlock_bh(&sess->conn_lock);
183                 iscsit_dec_session_usage_count(sess);
184                 return iscsit_close_session(sess);
185         }
186         spin_unlock_bh(&sess->conn_lock);
187
188         iscsit_stop_session(sess, 1, 1);
189         iscsit_dec_session_usage_count(sess);
190
191         return iscsit_close_session(sess);
192 }
193
194 static void iscsi_login_set_conn_values(
195         struct iscsi_session *sess,
196         struct iscsi_conn *conn,
197         u16 cid)
198 {
199         conn->sess              = sess;
200         conn->cid               = cid;
201         /*
202          * Generate a random Status sequence number (statsn) for the new
203          * iSCSI connection.
204          */
205         get_random_bytes(&conn->stat_sn, sizeof(u32));
206
207         mutex_lock(&auth_id_lock);
208         conn->auth_id           = iscsit_global->auth_id++;
209         mutex_unlock(&auth_id_lock);
210 }
211
212 /*
213  *      This is the leading connection of a new session,
214  *      or session reinstatement.
215  */
216 static int iscsi_login_zero_tsih_s1(
217         struct iscsi_conn *conn,
218         unsigned char *buf)
219 {
220         struct iscsi_session *sess = NULL;
221         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
222
223         sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL);
224         if (!sess) {
225                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
226                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
227                 pr_err("Could not allocate memory for session\n");
228                 return -ENOMEM;
229         }
230
231         iscsi_login_set_conn_values(sess, conn, pdu->cid);
232         sess->init_task_tag     = pdu->itt;
233         memcpy((void *)&sess->isid, (void *)pdu->isid, 6);
234         sess->exp_cmd_sn        = pdu->cmdsn;
235         INIT_LIST_HEAD(&sess->sess_conn_list);
236         INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list);
237         INIT_LIST_HEAD(&sess->cr_active_list);
238         INIT_LIST_HEAD(&sess->cr_inactive_list);
239         init_completion(&sess->async_msg_comp);
240         init_completion(&sess->reinstatement_comp);
241         init_completion(&sess->session_wait_comp);
242         init_completion(&sess->session_waiting_on_uc_comp);
243         mutex_init(&sess->cmdsn_mutex);
244         spin_lock_init(&sess->conn_lock);
245         spin_lock_init(&sess->cr_a_lock);
246         spin_lock_init(&sess->cr_i_lock);
247         spin_lock_init(&sess->session_usage_lock);
248         spin_lock_init(&sess->ttt_lock);
249
250         if (!idr_pre_get(&sess_idr, GFP_KERNEL)) {
251                 pr_err("idr_pre_get() for sess_idr failed\n");
252                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
253                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
254                 kfree(sess);
255                 return -ENOMEM;
256         }
257         spin_lock(&sess_idr_lock);
258         idr_get_new(&sess_idr, NULL, &sess->session_index);
259         spin_unlock(&sess_idr_lock);
260
261         sess->creation_time = get_jiffies_64();
262         spin_lock_init(&sess->session_stats_lock);
263         /*
264          * The FFP CmdSN window values will be allocated from the TPG's
265          * Initiator Node's ACL once the login has been successfully completed.
266          */
267         sess->max_cmd_sn        = pdu->cmdsn;
268
269         sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL);
270         if (!sess->sess_ops) {
271                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
272                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
273                 pr_err("Unable to allocate memory for"
274                                 " struct iscsi_sess_ops.\n");
275                 kfree(sess);
276                 return -ENOMEM;
277         }
278
279         sess->se_sess = transport_init_session();
280         if (IS_ERR(sess->se_sess)) {
281                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
282                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
283                 kfree(sess);
284                 return -ENOMEM;
285         }
286
287         return 0;
288 }
289
290 static int iscsi_login_zero_tsih_s2(
291         struct iscsi_conn *conn)
292 {
293         struct iscsi_node_attrib *na;
294         struct iscsi_session *sess = conn->sess;
295         unsigned char buf[32];
296
297         sess->tpg = conn->tpg;
298
299         /*
300          * Assign a new TPG Session Handle.  Note this is protected with
301          * struct iscsi_portal_group->np_login_sem from iscsit_access_np().
302          */
303         sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
304         if (!sess->tsih)
305                 sess->tsih = ++ISCSI_TPG_S(sess)->ntsih;
306
307         /*
308          * Create the default params from user defined values..
309          */
310         if (iscsi_copy_param_list(&conn->param_list,
311                                 ISCSI_TPG_C(conn)->param_list, 1) < 0) {
312                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
313                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
314                 return -1;
315         }
316
317         iscsi_set_keys_to_negotiate(0, conn->param_list);
318
319         if (sess->sess_ops->SessionType)
320                 return iscsi_set_keys_irrelevant_for_discovery(
321                                 conn->param_list);
322
323         na = iscsit_tpg_get_node_attrib(sess);
324
325         /*
326          * Need to send TargetPortalGroupTag back in first login response
327          * on any iSCSI connection where the Initiator provides TargetName.
328          * See 5.3.1.  Login Phase Start
329          *
330          * In our case, we have already located the struct iscsi_tiqn at this point.
331          */
332         memset(buf, 0, 32);
333         sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
334         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
335                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
336                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
337                 return -1;
338         }
339
340         /*
341          * Workaround for Initiators that have broken connection recovery logic.
342          *
343          * "We would really like to get rid of this." Linux-iSCSI.org team
344          */
345         memset(buf, 0, 32);
346         sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl);
347         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
348                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
349                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
350                 return -1;
351         }
352
353         if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0)
354                 return -1;
355
356         return 0;
357 }
358
359 /*
360  * Remove PSTATE_NEGOTIATE for the four FIM related keys.
361  * The Initiator node will be able to enable FIM by proposing them itself.
362  */
363 int iscsi_login_disable_FIM_keys(
364         struct iscsi_param_list *param_list,
365         struct iscsi_conn *conn)
366 {
367         struct iscsi_param *param;
368
369         param = iscsi_find_param_from_key("OFMarker", param_list);
370         if (!param) {
371                 pr_err("iscsi_find_param_from_key() for"
372                                 " OFMarker failed\n");
373                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
374                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
375                 return -1;
376         }
377         param->state &= ~PSTATE_NEGOTIATE;
378
379         param = iscsi_find_param_from_key("OFMarkInt", param_list);
380         if (!param) {
381                 pr_err("iscsi_find_param_from_key() for"
382                                 " IFMarker failed\n");
383                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
384                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
385                 return -1;
386         }
387         param->state &= ~PSTATE_NEGOTIATE;
388
389         param = iscsi_find_param_from_key("IFMarker", param_list);
390         if (!param) {
391                 pr_err("iscsi_find_param_from_key() for"
392                                 " IFMarker failed\n");
393                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
394                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
395                 return -1;
396         }
397         param->state &= ~PSTATE_NEGOTIATE;
398
399         param = iscsi_find_param_from_key("IFMarkInt", param_list);
400         if (!param) {
401                 pr_err("iscsi_find_param_from_key() for"
402                                 " IFMarker failed\n");
403                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
404                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
405                 return -1;
406         }
407         param->state &= ~PSTATE_NEGOTIATE;
408
409         return 0;
410 }
411
412 static int iscsi_login_non_zero_tsih_s1(
413         struct iscsi_conn *conn,
414         unsigned char *buf)
415 {
416         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
417
418         iscsi_login_set_conn_values(NULL, conn, pdu->cid);
419         return 0;
420 }
421
422 /*
423  *      Add a new connection to an existing session.
424  */
425 static int iscsi_login_non_zero_tsih_s2(
426         struct iscsi_conn *conn,
427         unsigned char *buf)
428 {
429         struct iscsi_portal_group *tpg = conn->tpg;
430         struct iscsi_session *sess = NULL, *sess_p = NULL;
431         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
432         struct se_session *se_sess, *se_sess_tmp;
433         struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf;
434
435         spin_lock_bh(&se_tpg->session_lock);
436         list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list,
437                         sess_list) {
438
439                 sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr;
440                 if (atomic_read(&sess_p->session_fall_back_to_erl0) ||
441                     atomic_read(&sess_p->session_logout) ||
442                    (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED))
443                         continue;
444                 if (!memcmp((const void *)sess_p->isid,
445                      (const void *)pdu->isid, 6) &&
446                      (sess_p->tsih == pdu->tsih)) {
447                         iscsit_inc_session_usage_count(sess_p);
448                         iscsit_stop_time2retain_timer(sess_p);
449                         sess = sess_p;
450                         break;
451                 }
452         }
453         spin_unlock_bh(&se_tpg->session_lock);
454
455         /*
456          * If the Time2Retain handler has expired, the session is already gone.
457          */
458         if (!sess) {
459                 pr_err("Initiator attempting to add a connection to"
460                         " a non-existent session, rejecting iSCSI Login.\n");
461                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
462                                 ISCSI_LOGIN_STATUS_NO_SESSION);
463                 return -1;
464         }
465
466         /*
467          * Stop the Time2Retain timer if this is a failed session, we restart
468          * the timer if the login is not successful.
469          */
470         spin_lock_bh(&sess->conn_lock);
471         if (sess->session_state == TARG_SESS_STATE_FAILED)
472                 atomic_set(&sess->session_continuation, 1);
473         spin_unlock_bh(&sess->conn_lock);
474
475         iscsi_login_set_conn_values(sess, conn, pdu->cid);
476
477         if (iscsi_copy_param_list(&conn->param_list,
478                         ISCSI_TPG_C(conn)->param_list, 0) < 0) {
479                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
480                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
481                 return -1;
482         }
483
484         iscsi_set_keys_to_negotiate(0, conn->param_list);
485         /*
486          * Need to send TargetPortalGroupTag back in first login response
487          * on any iSCSI connection where the Initiator provides TargetName.
488          * See 5.3.1.  Login Phase Start
489          *
490          * In our case, we have already located the struct iscsi_tiqn at this point.
491          */
492         memset(buf, 0, 32);
493         sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt);
494         if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) {
495                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
496                                 ISCSI_LOGIN_STATUS_NO_RESOURCES);
497                 return -1;
498         }
499
500         return iscsi_login_disable_FIM_keys(conn->param_list, conn);
501 }
502
503 int iscsi_login_post_auth_non_zero_tsih(
504         struct iscsi_conn *conn,
505         u16 cid,
506         u32 exp_statsn)
507 {
508         struct iscsi_conn *conn_ptr = NULL;
509         struct iscsi_conn_recovery *cr = NULL;
510         struct iscsi_session *sess = conn->sess;
511
512         /*
513          * By following item 5 in the login table,  if we have found
514          * an existing ISID and a valid/existing TSIH and an existing
515          * CID we do connection reinstatement.  Currently we dont not
516          * support it so we send back an non-zero status class to the
517          * initiator and release the new connection.
518          */
519         conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid);
520         if ((conn_ptr)) {
521                 pr_err("Connection exists with CID %hu for %s,"
522                         " performing connection reinstatement.\n",
523                         conn_ptr->cid, sess->sess_ops->InitiatorName);
524
525                 iscsit_connection_reinstatement_rcfr(conn_ptr);
526                 iscsit_dec_conn_usage_count(conn_ptr);
527         }
528
529         /*
530          * Check for any connection recovery entires containing CID.
531          * We use the original ExpStatSN sent in the first login request
532          * to acknowledge commands for the failed connection.
533          *
534          * Also note that an explict logout may have already been sent,
535          * but the response may not be sent due to additional connection
536          * loss.
537          */
538         if (sess->sess_ops->ErrorRecoveryLevel == 2) {
539                 cr = iscsit_get_inactive_connection_recovery_entry(
540                                 sess, cid);
541                 if ((cr)) {
542                         pr_debug("Performing implicit logout"
543                                 " for connection recovery on CID: %hu\n",
544                                         conn->cid);
545                         iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn);
546                 }
547         }
548
549         /*
550          * Else we follow item 4 from the login table in that we have
551          * found an existing ISID and a valid/existing TSIH and a new
552          * CID we go ahead and continue to add a new connection to the
553          * session.
554          */
555         pr_debug("Adding CID %hu to existing session for %s.\n",
556                         cid, sess->sess_ops->InitiatorName);
557
558         if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) {
559                 pr_err("Adding additional connection to this session"
560                         " would exceed MaxConnections %d, login failed.\n",
561                                 sess->sess_ops->MaxConnections);
562                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
563                                 ISCSI_LOGIN_STATUS_ISID_ERROR);
564                 return -1;
565         }
566
567         return 0;
568 }
569
570 static void iscsi_post_login_start_timers(struct iscsi_conn *conn)
571 {
572         struct iscsi_session *sess = conn->sess;
573
574         if (!sess->sess_ops->SessionType)
575                 iscsit_start_nopin_timer(conn);
576 }
577
578 static int iscsi_post_login_handler(
579         struct iscsi_np *np,
580         struct iscsi_conn *conn,
581         u8 zero_tsih)
582 {
583         int stop_timer = 0;
584         struct iscsi_session *sess = conn->sess;
585         struct se_session *se_sess = sess->se_sess;
586         struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess);
587         struct se_portal_group *se_tpg = &tpg->tpg_se_tpg;
588         struct iscsi_thread_set *ts;
589
590         iscsit_inc_conn_usage_count(conn);
591
592         iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS,
593                         ISCSI_LOGIN_STATUS_ACCEPT);
594
595         pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n");
596         conn->conn_state = TARG_CONN_STATE_LOGGED_IN;
597
598         iscsi_set_connection_parameters(conn->conn_ops, conn->param_list);
599         iscsit_set_sync_and_steering_values(conn);
600         /*
601          * SCSI Initiator -> SCSI Target Port Mapping
602          */
603         ts = iscsi_get_thread_set();
604         if (!zero_tsih) {
605                 iscsi_set_session_parameters(sess->sess_ops,
606                                 conn->param_list, 0);
607                 iscsi_release_param_list(conn->param_list);
608                 conn->param_list = NULL;
609
610                 spin_lock_bh(&sess->conn_lock);
611                 atomic_set(&sess->session_continuation, 0);
612                 if (sess->session_state == TARG_SESS_STATE_FAILED) {
613                         pr_debug("Moving to"
614                                         " TARG_SESS_STATE_LOGGED_IN.\n");
615                         sess->session_state = TARG_SESS_STATE_LOGGED_IN;
616                         stop_timer = 1;
617                 }
618
619                 pr_debug("iSCSI Login successful on CID: %hu from %s to"
620                         " %s:%hu,%hu\n", conn->cid, conn->login_ip,
621                         conn->local_ip, conn->local_port, tpg->tpgt);
622
623                 list_add_tail(&conn->conn_list, &sess->sess_conn_list);
624                 atomic_inc(&sess->nconn);
625                 pr_debug("Incremented iSCSI Connection count to %hu"
626                         " from node: %s\n", atomic_read(&sess->nconn),
627                         sess->sess_ops->InitiatorName);
628                 spin_unlock_bh(&sess->conn_lock);
629
630                 iscsi_post_login_start_timers(conn);
631                 iscsi_activate_thread_set(conn, ts);
632                 /*
633                  * Determine CPU mask to ensure connection's RX and TX kthreads
634                  * are scheduled on the same CPU.
635                  */
636                 iscsit_thread_get_cpumask(conn);
637                 conn->conn_rx_reset_cpumask = 1;
638                 conn->conn_tx_reset_cpumask = 1;
639
640                 iscsit_dec_conn_usage_count(conn);
641                 if (stop_timer) {
642                         spin_lock_bh(&se_tpg->session_lock);
643                         iscsit_stop_time2retain_timer(sess);
644                         spin_unlock_bh(&se_tpg->session_lock);
645                 }
646                 iscsit_dec_session_usage_count(sess);
647                 return 0;
648         }
649
650         iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1);
651         iscsi_release_param_list(conn->param_list);
652         conn->param_list = NULL;
653
654         iscsit_determine_maxcmdsn(sess);
655
656         spin_lock_bh(&se_tpg->session_lock);
657         __transport_register_session(&sess->tpg->tpg_se_tpg,
658                         se_sess->se_node_acl, se_sess, (void *)sess);
659         pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n");
660         sess->session_state = TARG_SESS_STATE_LOGGED_IN;
661
662         pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n",
663                 conn->cid, conn->login_ip, conn->local_ip, conn->local_port,
664                 tpg->tpgt);
665
666         spin_lock_bh(&sess->conn_lock);
667         list_add_tail(&conn->conn_list, &sess->sess_conn_list);
668         atomic_inc(&sess->nconn);
669         pr_debug("Incremented iSCSI Connection count to %hu from node:"
670                 " %s\n", atomic_read(&sess->nconn),
671                 sess->sess_ops->InitiatorName);
672         spin_unlock_bh(&sess->conn_lock);
673
674         sess->sid = tpg->sid++;
675         if (!sess->sid)
676                 sess->sid = tpg->sid++;
677         pr_debug("Established iSCSI session from node: %s\n",
678                         sess->sess_ops->InitiatorName);
679
680         tpg->nsessions++;
681         if (tpg->tpg_tiqn)
682                 tpg->tpg_tiqn->tiqn_nsessions++;
683
684         pr_debug("Incremented number of active iSCSI sessions to %u on"
685                 " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt);
686         spin_unlock_bh(&se_tpg->session_lock);
687
688         iscsi_post_login_start_timers(conn);
689         iscsi_activate_thread_set(conn, ts);
690         /*
691          * Determine CPU mask to ensure connection's RX and TX kthreads
692          * are scheduled on the same CPU.
693          */
694         iscsit_thread_get_cpumask(conn);
695         conn->conn_rx_reset_cpumask = 1;
696         conn->conn_tx_reset_cpumask = 1;
697
698         iscsit_dec_conn_usage_count(conn);
699
700         return 0;
701 }
702
703 static void iscsi_handle_login_thread_timeout(unsigned long data)
704 {
705         struct iscsi_np *np = (struct iscsi_np *) data;
706
707         spin_lock_bh(&np->np_thread_lock);
708         pr_err("iSCSI Login timeout on Network Portal %s:%hu\n",
709                         np->np_ip, np->np_port);
710
711         if (np->np_login_timer_flags & ISCSI_TF_STOP) {
712                 spin_unlock_bh(&np->np_thread_lock);
713                 return;
714         }
715
716         if (np->np_thread)
717                 send_sig(SIGINT, np->np_thread, 1);
718
719         np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
720         spin_unlock_bh(&np->np_thread_lock);
721 }
722
723 static void iscsi_start_login_thread_timer(struct iscsi_np *np)
724 {
725         /*
726          * This used the TA_LOGIN_TIMEOUT constant because at this
727          * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout
728          */
729         spin_lock_bh(&np->np_thread_lock);
730         init_timer(&np->np_login_timer);
731         np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
732         np->np_login_timer.data = (unsigned long)np;
733         np->np_login_timer.function = iscsi_handle_login_thread_timeout;
734         np->np_login_timer_flags &= ~ISCSI_TF_STOP;
735         np->np_login_timer_flags |= ISCSI_TF_RUNNING;
736         add_timer(&np->np_login_timer);
737
738         pr_debug("Added timeout timer to iSCSI login request for"
739                         " %u seconds.\n", TA_LOGIN_TIMEOUT);
740         spin_unlock_bh(&np->np_thread_lock);
741 }
742
743 static void iscsi_stop_login_thread_timer(struct iscsi_np *np)
744 {
745         spin_lock_bh(&np->np_thread_lock);
746         if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) {
747                 spin_unlock_bh(&np->np_thread_lock);
748                 return;
749         }
750         np->np_login_timer_flags |= ISCSI_TF_STOP;
751         spin_unlock_bh(&np->np_thread_lock);
752
753         del_timer_sync(&np->np_login_timer);
754
755         spin_lock_bh(&np->np_thread_lock);
756         np->np_login_timer_flags &= ~ISCSI_TF_RUNNING;
757         spin_unlock_bh(&np->np_thread_lock);
758 }
759
760 int iscsi_target_setup_login_socket(
761         struct iscsi_np *np,
762         struct __kernel_sockaddr_storage *sockaddr)
763 {
764         struct socket *sock;
765         int backlog = 5, ret, opt = 0, len;
766
767         switch (np->np_network_transport) {
768         case ISCSI_TCP:
769                 np->np_ip_proto = IPPROTO_TCP;
770                 np->np_sock_type = SOCK_STREAM;
771                 break;
772         case ISCSI_SCTP_TCP:
773                 np->np_ip_proto = IPPROTO_SCTP;
774                 np->np_sock_type = SOCK_STREAM;
775                 break;
776         case ISCSI_SCTP_UDP:
777                 np->np_ip_proto = IPPROTO_SCTP;
778                 np->np_sock_type = SOCK_SEQPACKET;
779                 break;
780         case ISCSI_IWARP_TCP:
781         case ISCSI_IWARP_SCTP:
782         case ISCSI_INFINIBAND:
783         default:
784                 pr_err("Unsupported network_transport: %d\n",
785                                 np->np_network_transport);
786                 return -EINVAL;
787         }
788
789         ret = sock_create(sockaddr->ss_family, np->np_sock_type,
790                         np->np_ip_proto, &sock);
791         if (ret < 0) {
792                 pr_err("sock_create() failed.\n");
793                 return ret;
794         }
795         np->np_socket = sock;
796         /*
797          * Setup the np->np_sockaddr from the passed sockaddr setup
798          * in iscsi_target_configfs.c code..
799          */
800         memcpy((void *)&np->np_sockaddr, (void *)sockaddr,
801                         sizeof(struct __kernel_sockaddr_storage));
802
803         if (sockaddr->ss_family == AF_INET6)
804                 len = sizeof(struct sockaddr_in6);
805         else
806                 len = sizeof(struct sockaddr_in);
807         /*
808          * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY.
809          */
810         opt = 1;
811         if (np->np_network_transport == ISCSI_TCP) {
812                 ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
813                                 (char *)&opt, sizeof(opt));
814                 if (ret < 0) {
815                         pr_err("kernel_setsockopt() for TCP_NODELAY"
816                                 " failed: %d\n", ret);
817                         goto fail;
818                 }
819         }
820
821         ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
822                         (char *)&opt, sizeof(opt));
823         if (ret < 0) {
824                 pr_err("kernel_setsockopt() for SO_REUSEADDR"
825                         " failed\n");
826                 goto fail;
827         }
828
829         ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len);
830         if (ret < 0) {
831                 pr_err("kernel_bind() failed: %d\n", ret);
832                 goto fail;
833         }
834
835         ret = kernel_listen(sock, backlog);
836         if (ret != 0) {
837                 pr_err("kernel_listen() failed: %d\n", ret);
838                 goto fail;
839         }
840
841         return 0;
842
843 fail:
844         np->np_socket = NULL;
845         if (sock)
846                 sock_release(sock);
847         return ret;
848 }
849
850 static int __iscsi_target_login_thread(struct iscsi_np *np)
851 {
852         u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0;
853         int err, ret = 0, ip_proto, sock_type, stop;
854         struct iscsi_conn *conn = NULL;
855         struct iscsi_login *login;
856         struct iscsi_portal_group *tpg = NULL;
857         struct socket *new_sock, *sock;
858         struct kvec iov;
859         struct iscsi_login_req *pdu;
860         struct sockaddr_in sock_in;
861         struct sockaddr_in6 sock_in6;
862
863         flush_signals(current);
864         sock = np->np_socket;
865         ip_proto = np->np_ip_proto;
866         sock_type = np->np_sock_type;
867
868         spin_lock_bh(&np->np_thread_lock);
869         if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
870                 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
871                 complete(&np->np_restart_comp);
872         } else {
873                 np->np_thread_state = ISCSI_NP_THREAD_ACTIVE;
874         }
875         spin_unlock_bh(&np->np_thread_lock);
876
877         if (kernel_accept(sock, &new_sock, 0) < 0) {
878                 spin_lock_bh(&np->np_thread_lock);
879                 if (np->np_thread_state == ISCSI_NP_THREAD_RESET) {
880                         spin_unlock_bh(&np->np_thread_lock);
881                         complete(&np->np_restart_comp);
882                         /* Get another socket */
883                         return 1;
884                 }
885                 spin_unlock_bh(&np->np_thread_lock);
886                 goto out;
887         }
888         iscsi_start_login_thread_timer(np);
889
890         conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL);
891         if (!conn) {
892                 pr_err("Could not allocate memory for"
893                         " new connection\n");
894                 sock_release(new_sock);
895                 /* Get another socket */
896                 return 1;
897         }
898
899         pr_debug("Moving to TARG_CONN_STATE_FREE.\n");
900         conn->conn_state = TARG_CONN_STATE_FREE;
901         conn->sock = new_sock;
902
903         pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n");
904         conn->conn_state = TARG_CONN_STATE_XPT_UP;
905
906         /*
907          * Allocate conn->conn_ops early as a failure calling
908          * iscsit_tx_login_rsp() below will call tx_data().
909          */
910         conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL);
911         if (!conn->conn_ops) {
912                 pr_err("Unable to allocate memory for"
913                         " struct iscsi_conn_ops.\n");
914                 goto new_sess_out;
915         }
916         /*
917          * Perform the remaining iSCSI connection initialization items..
918          */
919         if (iscsi_login_init_conn(conn) < 0)
920                 goto new_sess_out;
921
922         memset(buffer, 0, ISCSI_HDR_LEN);
923         memset(&iov, 0, sizeof(struct kvec));
924         iov.iov_base    = buffer;
925         iov.iov_len     = ISCSI_HDR_LEN;
926
927         if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) {
928                 pr_err("rx_data() returned an error.\n");
929                 goto new_sess_out;
930         }
931
932         iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK);
933         if (!(iscsi_opcode & ISCSI_OP_LOGIN)) {
934                 pr_err("First opcode is not login request,"
935                         " failing login request.\n");
936                 goto new_sess_out;
937         }
938
939         pdu                     = (struct iscsi_login_req *) buffer;
940         pdu->cid                = be16_to_cpu(pdu->cid);
941         pdu->tsih               = be16_to_cpu(pdu->tsih);
942         pdu->itt                = be32_to_cpu(pdu->itt);
943         pdu->cmdsn              = be32_to_cpu(pdu->cmdsn);
944         pdu->exp_statsn         = be32_to_cpu(pdu->exp_statsn);
945         /*
946          * Used by iscsit_tx_login_rsp() for Login Resonses PDUs
947          * when Status-Class != 0.
948         */
949         conn->login_itt         = pdu->itt;
950
951         spin_lock_bh(&np->np_thread_lock);
952         if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) {
953                 spin_unlock_bh(&np->np_thread_lock);
954                 pr_err("iSCSI Network Portal on %s:%hu currently not"
955                         " active.\n", np->np_ip, np->np_port);
956                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
957                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
958                 goto new_sess_out;
959         }
960         spin_unlock_bh(&np->np_thread_lock);
961
962         if (np->np_sockaddr.ss_family == AF_INET6) {
963                 memset(&sock_in6, 0, sizeof(struct sockaddr_in6));
964
965                 if (conn->sock->ops->getname(conn->sock,
966                                 (struct sockaddr *)&sock_in6, &err, 1) < 0) {
967                         pr_err("sock_ops->getname() failed.\n");
968                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
969                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
970                         goto new_sess_out;
971                 }
972                 snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c",
973                                 &sock_in6.sin6_addr.in6_u);
974                 conn->login_port = ntohs(sock_in6.sin6_port);
975
976                 if (conn->sock->ops->getname(conn->sock,
977                                 (struct sockaddr *)&sock_in6, &err, 0) < 0) {
978                         pr_err("sock_ops->getname() failed.\n");
979                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
980                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
981                         goto new_sess_out;
982                 }
983                 snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c",
984                                 &sock_in6.sin6_addr.in6_u);
985                 conn->local_port = ntohs(sock_in6.sin6_port);
986
987         } else {
988                 memset(&sock_in, 0, sizeof(struct sockaddr_in));
989
990                 if (conn->sock->ops->getname(conn->sock,
991                                 (struct sockaddr *)&sock_in, &err, 1) < 0) {
992                         pr_err("sock_ops->getname() failed.\n");
993                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
994                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
995                         goto new_sess_out;
996                 }
997                 sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr);
998                 conn->login_port = ntohs(sock_in.sin_port);
999
1000                 if (conn->sock->ops->getname(conn->sock,
1001                                 (struct sockaddr *)&sock_in, &err, 0) < 0) {
1002                         pr_err("sock_ops->getname() failed.\n");
1003                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1004                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
1005                         goto new_sess_out;
1006                 }
1007                 sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr);
1008                 conn->local_port = ntohs(sock_in.sin_port);
1009         }
1010
1011         conn->network_transport = np->np_network_transport;
1012
1013         pr_debug("Received iSCSI login request from %s on %s Network"
1014                         " Portal %s:%hu\n", conn->login_ip,
1015                 (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP",
1016                         conn->local_ip, conn->local_port);
1017
1018         pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n");
1019         conn->conn_state        = TARG_CONN_STATE_IN_LOGIN;
1020
1021         if (iscsi_login_check_initiator_version(conn, pdu->max_version,
1022                         pdu->min_version) < 0)
1023                 goto new_sess_out;
1024
1025         zero_tsih = (pdu->tsih == 0x0000);
1026         if ((zero_tsih)) {
1027                 /*
1028                  * This is the leading connection of a new session.
1029                  * We wait until after authentication to check for
1030                  * session reinstatement.
1031                  */
1032                 if (iscsi_login_zero_tsih_s1(conn, buffer) < 0)
1033                         goto new_sess_out;
1034         } else {
1035                 /*
1036                  * Add a new connection to an existing session.
1037                  * We check for a non-existant session in
1038                  * iscsi_login_non_zero_tsih_s2() below based
1039                  * on ISID/TSIH, but wait until after authentication
1040                  * to check for connection reinstatement, etc.
1041                  */
1042                 if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0)
1043                         goto new_sess_out;
1044         }
1045
1046         /*
1047          * This will process the first login request, and call
1048          * iscsi_target_locate_portal(), and return a valid struct iscsi_login.
1049          */
1050         login = iscsi_target_init_negotiation(np, conn, buffer);
1051         if (!login) {
1052                 tpg = conn->tpg;
1053                 goto new_sess_out;
1054         }
1055
1056         tpg = conn->tpg;
1057         if (!tpg) {
1058                 pr_err("Unable to locate struct iscsi_conn->tpg\n");
1059                 goto new_sess_out;
1060         }
1061
1062         if (zero_tsih) {
1063                 if (iscsi_login_zero_tsih_s2(conn) < 0) {
1064                         iscsi_target_nego_release(login, conn);
1065                         goto new_sess_out;
1066                 }
1067         } else {
1068                 if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) {
1069                         iscsi_target_nego_release(login, conn);
1070                         goto old_sess_out;
1071                 }
1072         }
1073
1074         if (iscsi_target_start_negotiation(login, conn) < 0)
1075                 goto new_sess_out;
1076
1077         if (!conn->sess) {
1078                 pr_err("struct iscsi_conn session pointer is NULL!\n");
1079                 goto new_sess_out;
1080         }
1081
1082         iscsi_stop_login_thread_timer(np);
1083
1084         if (signal_pending(current))
1085                 goto new_sess_out;
1086
1087         ret = iscsi_post_login_handler(np, conn, zero_tsih);
1088
1089         if (ret < 0)
1090                 goto new_sess_out;
1091
1092         iscsit_deaccess_np(np, tpg);
1093         tpg = NULL;
1094         /* Get another socket */
1095         return 1;
1096
1097 new_sess_out:
1098         pr_err("iSCSI Login negotiation failed.\n");
1099         iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1100                                   ISCSI_LOGIN_STATUS_INIT_ERR);
1101         if (!zero_tsih || !conn->sess)
1102                 goto old_sess_out;
1103         if (conn->sess->se_sess)
1104                 transport_free_session(conn->sess->se_sess);
1105         if (conn->sess->session_index != 0) {
1106                 spin_lock_bh(&sess_idr_lock);
1107                 idr_remove(&sess_idr, conn->sess->session_index);
1108                 spin_unlock_bh(&sess_idr_lock);
1109         }
1110         if (conn->sess->sess_ops)
1111                 kfree(conn->sess->sess_ops);
1112         if (conn->sess)
1113                 kfree(conn->sess);
1114 old_sess_out:
1115         iscsi_stop_login_thread_timer(np);
1116         /*
1117          * If login negotiation fails check if the Time2Retain timer
1118          * needs to be restarted.
1119          */
1120         if (!zero_tsih && conn->sess) {
1121                 spin_lock_bh(&conn->sess->conn_lock);
1122                 if (conn->sess->session_state == TARG_SESS_STATE_FAILED) {
1123                         struct se_portal_group *se_tpg =
1124                                         &ISCSI_TPG_C(conn)->tpg_se_tpg;
1125
1126                         atomic_set(&conn->sess->session_continuation, 0);
1127                         spin_unlock_bh(&conn->sess->conn_lock);
1128                         spin_lock_bh(&se_tpg->session_lock);
1129                         iscsit_start_time2retain_handler(conn->sess);
1130                         spin_unlock_bh(&se_tpg->session_lock);
1131                 } else
1132                         spin_unlock_bh(&conn->sess->conn_lock);
1133                 iscsit_dec_session_usage_count(conn->sess);
1134         }
1135
1136         if (!IS_ERR(conn->conn_rx_hash.tfm))
1137                 crypto_free_hash(conn->conn_rx_hash.tfm);
1138         if (!IS_ERR(conn->conn_tx_hash.tfm))
1139                 crypto_free_hash(conn->conn_tx_hash.tfm);
1140
1141         if (conn->conn_cpumask)
1142                 free_cpumask_var(conn->conn_cpumask);
1143
1144         kfree(conn->conn_ops);
1145
1146         if (conn->param_list) {
1147                 iscsi_release_param_list(conn->param_list);
1148                 conn->param_list = NULL;
1149         }
1150         if (conn->sock)
1151                 sock_release(conn->sock);
1152         kfree(conn);
1153
1154         if (tpg) {
1155                 iscsit_deaccess_np(np, tpg);
1156                 tpg = NULL;
1157         }
1158
1159 out:
1160         stop = kthread_should_stop();
1161         if (!stop && signal_pending(current)) {
1162                 spin_lock_bh(&np->np_thread_lock);
1163                 stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN);
1164                 spin_unlock_bh(&np->np_thread_lock);
1165         }
1166         /* Wait for another socket.. */
1167         if (!stop)
1168                 return 1;
1169
1170         iscsi_stop_login_thread_timer(np);
1171         spin_lock_bh(&np->np_thread_lock);
1172         np->np_thread_state = ISCSI_NP_THREAD_EXIT;
1173         spin_unlock_bh(&np->np_thread_lock);
1174         return 0;
1175 }
1176
1177 int iscsi_target_login_thread(void *arg)
1178 {
1179         struct iscsi_np *np = (struct iscsi_np *)arg;
1180         int ret;
1181
1182         allow_signal(SIGINT);
1183
1184         while (!kthread_should_stop()) {
1185                 ret = __iscsi_target_login_thread(np);
1186                 /*
1187                  * We break and exit here unless another sock_accept() call
1188                  * is expected.
1189                  */
1190                 if (ret != 1)
1191                         break;
1192         }
1193
1194         return 0;
1195 }