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