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