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