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