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