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