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