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