Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * This file contains the login functions used by the iSCSI Target driver. |
| 3 | * |
| 4 | * \u00a9 Copyright 2007-2011 RisingTide Systems LLC. |
| 5 | * |
| 6 | * Licensed to the Linux Foundation under the General Public License (GPL) version 2. |
| 7 | * |
| 8 | * Author: Nicholas A. Bellinger <nab@linux-iscsi.org> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | ******************************************************************************/ |
| 20 | |
| 21 | #include <linux/string.h> |
| 22 | #include <linux/kthread.h> |
| 23 | #include <linux/crypto.h> |
Al Viro | 4040153 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 24 | #include <linux/idr.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 25 | #include <scsi/iscsi_proto.h> |
| 26 | #include <target/target_core_base.h> |
Christoph Hellwig | c4795fb | 2011-11-16 09:46:48 -0500 | [diff] [blame] | 27 | #include <target/target_core_fabric.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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" |
| 40 | #include "iscsi_target_parameters.h" |
| 41 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 42 | static int iscsi_login_init_conn(struct iscsi_conn *conn) |
| 43 | { |
Roland Dreier | d5627ac | 2012-10-31 09:16:46 -0700 | [diff] [blame] | 44 | init_waitqueue_head(&conn->queues_wq); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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); |
| 57 | spin_lock_init(&conn->conn_usage_lock); |
| 58 | spin_lock_init(&conn->immed_queue_lock); |
| 59 | spin_lock_init(&conn->nopin_timer_lock); |
| 60 | spin_lock_init(&conn->response_queue_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 | */ |
| 75 | int iscsi_login_setup_crypto(struct iscsi_conn *conn) |
| 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, |
| 84 | CRYPTO_ALG_ASYNC); |
| 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, |
| 92 | CRYPTO_ALG_ASYNC); |
| 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); |
| 111 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, |
| 112 | ISCSI_LOGIN_STATUS_NO_VERSION); |
| 113 | return -1; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn) |
| 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); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 130 | sessiontype_param = iscsi_find_param_from_key( |
| 131 | SESSIONTYPE, conn->param_list); |
Roland Dreier | 1c5c12c | 2012-11-05 18:02:42 -0800 | [diff] [blame] | 132 | if (!initiatorname_param || !sessiontype_param) { |
| 133 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, |
| 134 | ISCSI_LOGIN_STATUS_MISSING_FIELDS); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 135 | return -1; |
Roland Dreier | 1c5c12c | 2012-11-05 18:02:42 -0800 | [diff] [blame] | 136 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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 | |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 144 | sess_p = se_sess->fabric_sess_ptr; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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 | } |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 152 | if (!memcmp(sess_p->isid, conn->sess->isid, 6) && |
| 153 | (!strcmp(sess_p->sess_ops->InitiatorName, |
| 154 | initiatorname_param->value) && |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 155 | (sess_p->sess_ops->SessionType == sessiontype))) { |
| 156 | atomic_set(&sess_p->session_reinstatement, 1); |
| 157 | spin_unlock(&sess_p->conn_lock); |
| 158 | iscsit_inc_session_usage_count(sess_p); |
| 159 | iscsit_stop_time2retain_timer(sess_p); |
| 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); |
| 180 | iscsit_dec_session_usage_count(sess); |
Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 181 | target_put_session(sess->se_sess); |
| 182 | return 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 183 | } |
| 184 | spin_unlock_bh(&sess->conn_lock); |
| 185 | |
| 186 | iscsit_stop_session(sess, 1, 1); |
| 187 | iscsit_dec_session_usage_count(sess); |
| 188 | |
Nicholas Bellinger | 99367f0 | 2012-02-27 01:43:32 -0800 | [diff] [blame] | 189 | target_put_session(sess->se_sess); |
| 190 | return 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void iscsi_login_set_conn_values( |
| 194 | struct iscsi_session *sess, |
| 195 | struct iscsi_conn *conn, |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 196 | __be16 cid) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 197 | { |
| 198 | conn->sess = sess; |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 199 | conn->cid = be16_to_cpu(cid); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 200 | /* |
| 201 | * Generate a random Status sequence number (statsn) for the new |
| 202 | * iSCSI connection. |
| 203 | */ |
| 204 | get_random_bytes(&conn->stat_sn, sizeof(u32)); |
| 205 | |
| 206 | mutex_lock(&auth_id_lock); |
| 207 | conn->auth_id = iscsit_global->auth_id++; |
| 208 | mutex_unlock(&auth_id_lock); |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * This is the leading connection of a new session, |
| 213 | * or session reinstatement. |
| 214 | */ |
| 215 | static int iscsi_login_zero_tsih_s1( |
| 216 | struct iscsi_conn *conn, |
| 217 | unsigned char *buf) |
| 218 | { |
| 219 | struct iscsi_session *sess = NULL; |
| 220 | struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; |
Benjamin Wang | 13b5533 | 2012-08-26 18:04:10 +0800 | [diff] [blame] | 221 | int ret; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 222 | |
| 223 | sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL); |
| 224 | if (!sess) { |
| 225 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 226 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 227 | pr_err("Could not allocate memory for session\n"); |
Nicholas Bellinger | 0957627 | 2011-11-04 11:36:38 -0700 | [diff] [blame] | 228 | return -ENOMEM; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | iscsi_login_set_conn_values(sess, conn, pdu->cid); |
| 232 | sess->init_task_tag = pdu->itt; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 233 | memcpy(&sess->isid, pdu->isid, 6); |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 234 | sess->exp_cmd_sn = be32_to_cpu(pdu->cmdsn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 235 | INIT_LIST_HEAD(&sess->sess_conn_list); |
| 236 | INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list); |
| 237 | INIT_LIST_HEAD(&sess->cr_active_list); |
| 238 | INIT_LIST_HEAD(&sess->cr_inactive_list); |
| 239 | init_completion(&sess->async_msg_comp); |
| 240 | init_completion(&sess->reinstatement_comp); |
| 241 | init_completion(&sess->session_wait_comp); |
| 242 | init_completion(&sess->session_waiting_on_uc_comp); |
| 243 | mutex_init(&sess->cmdsn_mutex); |
| 244 | spin_lock_init(&sess->conn_lock); |
| 245 | spin_lock_init(&sess->cr_a_lock); |
| 246 | spin_lock_init(&sess->cr_i_lock); |
| 247 | spin_lock_init(&sess->session_usage_lock); |
| 248 | spin_lock_init(&sess->ttt_lock); |
| 249 | |
| 250 | if (!idr_pre_get(&sess_idr, GFP_KERNEL)) { |
| 251 | pr_err("idr_pre_get() for sess_idr failed\n"); |
| 252 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 253 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
Nicholas Bellinger | 0957627 | 2011-11-04 11:36:38 -0700 | [diff] [blame] | 254 | kfree(sess); |
| 255 | return -ENOMEM; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 256 | } |
Roland Dreier | 998866b | 2012-11-05 18:02:40 -0800 | [diff] [blame] | 257 | spin_lock_bh(&sess_idr_lock); |
Benjamin Wang | 13b5533 | 2012-08-26 18:04:10 +0800 | [diff] [blame] | 258 | ret = idr_get_new(&sess_idr, NULL, &sess->session_index); |
Roland Dreier | 998866b | 2012-11-05 18:02:40 -0800 | [diff] [blame] | 259 | spin_unlock_bh(&sess_idr_lock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 260 | |
Benjamin Wang | 13b5533 | 2012-08-26 18:04:10 +0800 | [diff] [blame] | 261 | if (ret < 0) { |
| 262 | pr_err("idr_get_new() for sess_idr failed\n"); |
| 263 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 264 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 265 | kfree(sess); |
| 266 | return -ENOMEM; |
| 267 | } |
| 268 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 269 | sess->creation_time = get_jiffies_64(); |
| 270 | spin_lock_init(&sess->session_stats_lock); |
| 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 | */ |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 275 | sess->max_cmd_sn = be32_to_cpu(pdu->cmdsn); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 276 | |
| 277 | sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL); |
| 278 | if (!sess->sess_ops) { |
| 279 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 280 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 281 | pr_err("Unable to allocate memory for" |
| 282 | " struct iscsi_sess_ops.\n"); |
Nicholas Bellinger | 0957627 | 2011-11-04 11:36:38 -0700 | [diff] [blame] | 283 | kfree(sess); |
| 284 | return -ENOMEM; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | sess->se_sess = transport_init_session(); |
Nicholas Bellinger | 0957627 | 2011-11-04 11:36:38 -0700 | [diff] [blame] | 288 | if (IS_ERR(sess->se_sess)) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 289 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 290 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
Nicholas Bellinger | 0957627 | 2011-11-04 11:36:38 -0700 | [diff] [blame] | 291 | kfree(sess); |
| 292 | return -ENOMEM; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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 | */ |
| 318 | if (iscsi_copy_param_list(&conn->param_list, |
| 319 | ISCSI_TPG_C(conn)->param_list, 1) < 0) { |
| 320 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 321 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 322 | return -1; |
| 323 | } |
| 324 | |
| 325 | iscsi_set_keys_to_negotiate(0, conn->param_list); |
| 326 | |
| 327 | if (sess->sess_ops->SessionType) |
| 328 | return iscsi_set_keys_irrelevant_for_discovery( |
| 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) { |
| 343 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 344 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 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) { |
| 356 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 357 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 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 | */ |
| 371 | int iscsi_login_disable_FIM_keys( |
| 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"); |
| 381 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 382 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 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"); |
| 391 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 392 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 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"); |
| 401 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 402 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 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"); |
| 411 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 412 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 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; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 452 | if (!memcmp(sess_p->isid, pdu->isid, 6) && |
Christoph Hellwig | 50e5c87 | 2012-09-26 08:00:40 -0400 | [diff] [blame] | 453 | (sess_p->tsih == be16_to_cpu(pdu->tsih))) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 454 | iscsit_inc_session_usage_count(sess_p); |
| 455 | iscsit_stop_time2retain_timer(sess_p); |
| 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"); |
| 468 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, |
| 469 | ISCSI_LOGIN_STATUS_NO_SESSION); |
| 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); |
| 478 | if (sess->session_state == TARG_SESS_STATE_FAILED) |
| 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 | |
| 484 | if (iscsi_copy_param_list(&conn->param_list, |
| 485 | ISCSI_TPG_C(conn)->param_list, 0) < 0) { |
| 486 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 487 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | iscsi_set_keys_to_negotiate(0, conn->param_list); |
| 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) { |
| 502 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 503 | ISCSI_LOGIN_STATUS_NO_RESOURCES); |
| 504 | return -1; |
| 505 | } |
| 506 | |
| 507 | return iscsi_login_disable_FIM_keys(conn->param_list, conn); |
| 508 | } |
| 509 | |
| 510 | int iscsi_login_post_auth_non_zero_tsih( |
| 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); |
Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 527 | if (conn_ptr) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 528 | pr_err("Connection exists with CID %hu for %s," |
| 529 | " performing connection reinstatement.\n", |
| 530 | conn_ptr->cid, sess->sess_ops->InitiatorName); |
| 531 | |
| 532 | iscsit_connection_reinstatement_rcfr(conn_ptr); |
| 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) { |
| 546 | cr = iscsit_get_inactive_connection_recovery_entry( |
| 547 | sess, cid); |
Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 548 | if (cr) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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); |
| 569 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, |
| 570 | ISCSI_LOGIN_STATUS_ISID_ERROR); |
| 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) |
| 582 | iscsit_start_nopin_timer(conn); |
| 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 | |
| 597 | iscsit_inc_conn_usage_count(conn); |
| 598 | |
| 599 | iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS, |
| 600 | ISCSI_LOGIN_STATUS_ACCEPT); |
| 601 | |
| 602 | pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n"); |
| 603 | conn->conn_state = TARG_CONN_STATE_LOGGED_IN; |
| 604 | |
| 605 | iscsi_set_connection_parameters(conn->conn_ops, conn->param_list); |
| 606 | iscsit_set_sync_and_steering_values(conn); |
| 607 | /* |
| 608 | * SCSI Initiator -> SCSI Target Port Mapping |
| 609 | */ |
| 610 | ts = iscsi_get_thread_set(); |
| 611 | if (!zero_tsih) { |
| 612 | iscsi_set_session_parameters(sess->sess_ops, |
| 613 | conn->param_list, 0); |
| 614 | iscsi_release_param_list(conn->param_list); |
| 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"); |
| 622 | sess->session_state = TARG_SESS_STATE_LOGGED_IN; |
| 623 | stop_timer = 1; |
| 624 | } |
| 625 | |
| 626 | pr_debug("iSCSI Login successful on CID: %hu from %s to" |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 627 | " %s:%hu,%hu\n", conn->cid, conn->login_ip, |
| 628 | conn->local_ip, conn->local_port, tpg->tpgt); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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 | */ |
| 643 | iscsit_thread_get_cpumask(conn); |
| 644 | conn->conn_rx_reset_cpumask = 1; |
| 645 | conn->conn_tx_reset_cpumask = 1; |
| 646 | |
| 647 | iscsit_dec_conn_usage_count(conn); |
| 648 | if (stop_timer) { |
| 649 | spin_lock_bh(&se_tpg->session_lock); |
| 650 | iscsit_stop_time2retain_timer(sess); |
| 651 | spin_unlock_bh(&se_tpg->session_lock); |
| 652 | } |
| 653 | iscsit_dec_session_usage_count(sess); |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1); |
| 658 | iscsi_release_param_list(conn->param_list); |
| 659 | conn->param_list = NULL; |
| 660 | |
| 661 | iscsit_determine_maxcmdsn(sess); |
| 662 | |
| 663 | spin_lock_bh(&se_tpg->session_lock); |
| 664 | __transport_register_session(&sess->tpg->tpg_se_tpg, |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 665 | se_sess->se_node_acl, se_sess, sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 666 | pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n"); |
| 667 | sess->session_state = TARG_SESS_STATE_LOGGED_IN; |
| 668 | |
| 669 | pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n", |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 670 | conn->cid, conn->login_ip, conn->local_ip, conn->local_port, |
| 671 | tpg->tpgt); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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 | */ |
| 701 | iscsit_thread_get_cpumask(conn); |
| 702 | conn->conn_rx_reset_cpumask = 1; |
| 703 | conn->conn_tx_reset_cpumask = 1; |
| 704 | |
| 705 | iscsit_dec_conn_usage_count(conn); |
| 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 | |
| 718 | if (np->np_login_timer_flags & ISCSI_TF_STOP) { |
| 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 | |
| 726 | np->np_login_timer_flags &= ~ISCSI_TF_RUNNING; |
| 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); |
| 737 | init_timer(&np->np_login_timer); |
| 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; |
| 741 | np->np_login_timer_flags &= ~ISCSI_TF_STOP; |
| 742 | np->np_login_timer_flags |= ISCSI_TF_RUNNING; |
| 743 | add_timer(&np->np_login_timer); |
| 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 | } |
| 757 | np->np_login_timer_flags |= ISCSI_TF_STOP; |
| 758 | spin_unlock_bh(&np->np_thread_lock); |
| 759 | |
| 760 | del_timer_sync(&np->np_login_timer); |
| 761 | |
| 762 | spin_lock_bh(&np->np_thread_lock); |
| 763 | np->np_login_timer_flags &= ~ISCSI_TF_RUNNING; |
| 764 | spin_unlock_bh(&np->np_thread_lock); |
| 765 | } |
| 766 | |
| 767 | int iscsi_target_setup_login_socket( |
| 768 | struct iscsi_np *np, |
| 769 | struct __kernel_sockaddr_storage *sockaddr) |
| 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; |
| 777 | np->np_sock_type = SOCK_STREAM; |
| 778 | break; |
| 779 | case ISCSI_SCTP_TCP: |
| 780 | np->np_ip_proto = IPPROTO_SCTP; |
| 781 | np->np_sock_type = SOCK_STREAM; |
| 782 | break; |
| 783 | case ISCSI_SCTP_UDP: |
| 784 | np->np_ip_proto = IPPROTO_SCTP; |
| 785 | np->np_sock_type = SOCK_SEQPACKET; |
| 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", |
| 792 | np->np_network_transport); |
| 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 | /* |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 804 | * Setup the np->np_sockaddr from the passed sockaddr setup |
| 805 | * in iscsi_target_configfs.c code.. |
| 806 | */ |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 807 | memcpy(&np->np_sockaddr, sockaddr, |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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 | */ |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 817 | /* FIXME: Someone please explain why this is endian-safe */ |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 818 | opt = 1; |
| 819 | if (np->np_network_transport == ISCSI_TCP) { |
| 820 | ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, |
| 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 | |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 829 | /* FIXME: Someone please explain why this is endian-safe */ |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 830 | ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
| 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 | |
Dax Kelson | 9f9ef6d | 2012-02-03 23:40:25 -0700 | [diff] [blame] | 838 | ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND, |
| 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 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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; |
Al Viro | bf6932f | 2012-07-21 08:55:18 +0100 | [diff] [blame] | 862 | if (sock) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 863 | sock_release(sock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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; |
Al Viro | bf6932f | 2012-07-21 08:55:18 +0100 | [diff] [blame] | 870 | int err, ret = 0, stop; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 871 | struct iscsi_conn *conn = NULL; |
| 872 | struct iscsi_login *login; |
| 873 | struct iscsi_portal_group *tpg = NULL; |
| 874 | struct socket *new_sock, *sock; |
| 875 | struct kvec iov; |
| 876 | struct iscsi_login_req *pdu; |
| 877 | struct sockaddr_in sock_in; |
| 878 | struct sockaddr_in6 sock_in6; |
| 879 | |
| 880 | flush_signals(current); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 881 | sock = np->np_socket; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 882 | |
| 883 | spin_lock_bh(&np->np_thread_lock); |
| 884 | if (np->np_thread_state == ISCSI_NP_THREAD_RESET) { |
| 885 | np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; |
| 886 | complete(&np->np_restart_comp); |
| 887 | } else { |
| 888 | np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; |
| 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); |
| 894 | if (np->np_thread_state == ISCSI_NP_THREAD_RESET) { |
| 895 | spin_unlock_bh(&np->np_thread_lock); |
| 896 | complete(&np->np_restart_comp); |
| 897 | /* Get another socket */ |
| 898 | return 1; |
| 899 | } |
| 900 | spin_unlock_bh(&np->np_thread_lock); |
| 901 | goto out; |
| 902 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 909 | sock_release(new_sock); |
| 910 | /* Get another socket */ |
| 911 | return 1; |
| 912 | } |
| 913 | |
| 914 | pr_debug("Moving to TARG_CONN_STATE_FREE.\n"); |
| 915 | conn->conn_state = TARG_CONN_STATE_FREE; |
| 916 | conn->sock = new_sock; |
| 917 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 918 | pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n"); |
| 919 | conn->conn_state = TARG_CONN_STATE_XPT_UP; |
| 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; |
Christoph Hellwig | 66c7db6 | 2012-09-26 08:00:39 -0400 | [diff] [blame] | 955 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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); |
| 963 | if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { |
| 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); |
| 967 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 968 | ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); |
| 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"); |
| 979 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 980 | ISCSI_LOGIN_STATUS_TARGET_ERROR); |
| 981 | goto new_sess_out; |
| 982 | } |
Chris Boot | 6626a05 | 2011-08-13 22:10:46 -0700 | [diff] [blame] | 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); |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 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"); |
| 990 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 991 | ISCSI_LOGIN_STATUS_TARGET_ERROR); |
| 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 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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"); |
| 1004 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 1005 | ISCSI_LOGIN_STATUS_TARGET_ERROR); |
| 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); |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 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"); |
| 1014 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
| 1015 | ISCSI_LOGIN_STATUS_TARGET_ERROR); |
| 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); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | conn->network_transport = np->np_network_transport; |
| 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", |
Nicholas Bellinger | 2f9bc89 | 2012-01-16 23:33:48 -0800 | [diff] [blame] | 1027 | conn->local_ip, conn->local_port); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1028 | |
| 1029 | pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n"); |
| 1030 | conn->conn_state = TARG_CONN_STATE_IN_LOGIN; |
| 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); |
Andy Grover | ee1b1b9 | 2012-07-12 17:34:54 -0700 | [diff] [blame] | 1037 | if (zero_tsih) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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"); |
| 1110 | iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, |
| 1111 | ISCSI_LOGIN_STATUS_INIT_ERR); |
| 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 | } |
Sachin Kamat | b07c28a | 2012-11-21 16:14:40 +0530 | [diff] [blame] | 1121 | kfree(conn->sess->sess_ops); |
| 1122 | kfree(conn->sess); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1123 | old_sess_out: |
| 1124 | iscsi_stop_login_thread_timer(np); |
| 1125 | /* |
| 1126 | * If login negotiation fails check if the Time2Retain timer |
| 1127 | * needs to be restarted. |
| 1128 | */ |
| 1129 | if (!zero_tsih && conn->sess) { |
| 1130 | spin_lock_bh(&conn->sess->conn_lock); |
| 1131 | if (conn->sess->session_state == TARG_SESS_STATE_FAILED) { |
| 1132 | struct se_portal_group *se_tpg = |
| 1133 | &ISCSI_TPG_C(conn)->tpg_se_tpg; |
| 1134 | |
| 1135 | atomic_set(&conn->sess->session_continuation, 0); |
| 1136 | spin_unlock_bh(&conn->sess->conn_lock); |
| 1137 | spin_lock_bh(&se_tpg->session_lock); |
| 1138 | iscsit_start_time2retain_handler(conn->sess); |
| 1139 | spin_unlock_bh(&se_tpg->session_lock); |
| 1140 | } else |
| 1141 | spin_unlock_bh(&conn->sess->conn_lock); |
| 1142 | iscsit_dec_session_usage_count(conn->sess); |
| 1143 | } |
| 1144 | |
| 1145 | if (!IS_ERR(conn->conn_rx_hash.tfm)) |
| 1146 | crypto_free_hash(conn->conn_rx_hash.tfm); |
| 1147 | if (!IS_ERR(conn->conn_tx_hash.tfm)) |
| 1148 | crypto_free_hash(conn->conn_tx_hash.tfm); |
| 1149 | |
| 1150 | if (conn->conn_cpumask) |
| 1151 | free_cpumask_var(conn->conn_cpumask); |
| 1152 | |
| 1153 | kfree(conn->conn_ops); |
| 1154 | |
| 1155 | if (conn->param_list) { |
| 1156 | iscsi_release_param_list(conn->param_list); |
| 1157 | conn->param_list = NULL; |
| 1158 | } |
Al Viro | bf6932f | 2012-07-21 08:55:18 +0100 | [diff] [blame] | 1159 | if (conn->sock) |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1160 | sock_release(conn->sock); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1161 | kfree(conn); |
| 1162 | |
| 1163 | if (tpg) { |
| 1164 | iscsit_deaccess_np(np, tpg); |
| 1165 | tpg = NULL; |
| 1166 | } |
| 1167 | |
| 1168 | out: |
| 1169 | stop = kthread_should_stop(); |
| 1170 | if (!stop && signal_pending(current)) { |
| 1171 | spin_lock_bh(&np->np_thread_lock); |
| 1172 | stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN); |
| 1173 | spin_unlock_bh(&np->np_thread_lock); |
| 1174 | } |
| 1175 | /* Wait for another socket.. */ |
| 1176 | if (!stop) |
| 1177 | return 1; |
| 1178 | |
| 1179 | iscsi_stop_login_thread_timer(np); |
| 1180 | spin_lock_bh(&np->np_thread_lock); |
| 1181 | np->np_thread_state = ISCSI_NP_THREAD_EXIT; |
| 1182 | spin_unlock_bh(&np->np_thread_lock); |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | int iscsi_target_login_thread(void *arg) |
| 1187 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 1188 | struct iscsi_np *np = arg; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1189 | int ret; |
| 1190 | |
| 1191 | allow_signal(SIGINT); |
| 1192 | |
| 1193 | while (!kthread_should_stop()) { |
| 1194 | ret = __iscsi_target_login_thread(np); |
| 1195 | /* |
| 1196 | * We break and exit here unless another sock_accept() call |
| 1197 | * is expected. |
| 1198 | */ |
| 1199 | if (ret != 1) |
| 1200 | break; |
| 1201 | } |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |