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 | ||
42 | extern struct idr sess_idr; | |
43 | extern struct mutex auth_id_lock; | |
44 | extern spinlock_t sess_idr_lock; | |
45 | ||
46 | static int iscsi_login_init_conn(struct iscsi_conn *conn) | |
47 | { | |
48 | INIT_LIST_HEAD(&conn->conn_list); | |
49 | INIT_LIST_HEAD(&conn->conn_cmd_list); | |
50 | INIT_LIST_HEAD(&conn->immed_queue_list); | |
51 | INIT_LIST_HEAD(&conn->response_queue_list); | |
52 | init_completion(&conn->conn_post_wait_comp); | |
53 | init_completion(&conn->conn_wait_comp); | |
54 | init_completion(&conn->conn_wait_rcfr_comp); | |
55 | init_completion(&conn->conn_waiting_on_uc_comp); | |
56 | init_completion(&conn->conn_logout_comp); | |
57 | init_completion(&conn->rx_half_close_comp); | |
58 | init_completion(&conn->tx_half_close_comp); | |
59 | spin_lock_init(&conn->cmd_lock); | |
60 | spin_lock_init(&conn->conn_usage_lock); | |
61 | spin_lock_init(&conn->immed_queue_lock); | |
62 | spin_lock_init(&conn->nopin_timer_lock); | |
63 | spin_lock_init(&conn->response_queue_lock); | |
64 | spin_lock_init(&conn->state_lock); | |
65 | ||
66 | if (!zalloc_cpumask_var(&conn->conn_cpumask, GFP_KERNEL)) { | |
67 | pr_err("Unable to allocate conn->conn_cpumask\n"); | |
68 | return -ENOMEM; | |
69 | } | |
70 | ||
71 | return 0; | |
72 | } | |
73 | ||
74 | /* | |
75 | * Used by iscsi_target_nego.c:iscsi_target_locate_portal() to setup | |
76 | * per struct iscsi_conn libcrypto contexts for crc32c and crc32-intel | |
77 | */ | |
78 | int iscsi_login_setup_crypto(struct iscsi_conn *conn) | |
79 | { | |
80 | /* | |
81 | * Setup slicing by CRC32C algorithm for RX and TX libcrypto contexts | |
82 | * which will default to crc32c_intel.ko for cpu_has_xmm4_2, or fallback | |
83 | * to software 1x8 byte slicing from crc32c.ko | |
84 | */ | |
85 | conn->conn_rx_hash.flags = 0; | |
86 | conn->conn_rx_hash.tfm = crypto_alloc_hash("crc32c", 0, | |
87 | CRYPTO_ALG_ASYNC); | |
88 | if (IS_ERR(conn->conn_rx_hash.tfm)) { | |
89 | pr_err("crypto_alloc_hash() failed for conn_rx_tfm\n"); | |
90 | return -ENOMEM; | |
91 | } | |
92 | ||
93 | conn->conn_tx_hash.flags = 0; | |
94 | conn->conn_tx_hash.tfm = crypto_alloc_hash("crc32c", 0, | |
95 | CRYPTO_ALG_ASYNC); | |
96 | if (IS_ERR(conn->conn_tx_hash.tfm)) { | |
97 | pr_err("crypto_alloc_hash() failed for conn_tx_tfm\n"); | |
98 | crypto_free_hash(conn->conn_rx_hash.tfm); | |
99 | return -ENOMEM; | |
100 | } | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | static int iscsi_login_check_initiator_version( | |
106 | struct iscsi_conn *conn, | |
107 | u8 version_max, | |
108 | u8 version_min) | |
109 | { | |
110 | if ((version_max != 0x00) || (version_min != 0x00)) { | |
111 | pr_err("Unsupported iSCSI IETF Pre-RFC Revision," | |
112 | " version Min/Max 0x%02x/0x%02x, rejecting login.\n", | |
113 | version_min, version_max); | |
114 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, | |
115 | ISCSI_LOGIN_STATUS_NO_VERSION); | |
116 | return -1; | |
117 | } | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
122 | int iscsi_check_for_session_reinstatement(struct iscsi_conn *conn) | |
123 | { | |
124 | int sessiontype; | |
125 | struct iscsi_param *initiatorname_param = NULL, *sessiontype_param = NULL; | |
126 | struct iscsi_portal_group *tpg = conn->tpg; | |
127 | struct iscsi_session *sess = NULL, *sess_p = NULL; | |
128 | struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; | |
129 | struct se_session *se_sess, *se_sess_tmp; | |
130 | ||
131 | initiatorname_param = iscsi_find_param_from_key( | |
132 | INITIATORNAME, conn->param_list); | |
133 | if (!initiatorname_param) | |
134 | return -1; | |
135 | ||
136 | sessiontype_param = iscsi_find_param_from_key( | |
137 | SESSIONTYPE, conn->param_list); | |
138 | if (!sessiontype_param) | |
139 | return -1; | |
140 | ||
141 | sessiontype = (strncmp(sessiontype_param->value, NORMAL, 6)) ? 1 : 0; | |
142 | ||
143 | spin_lock_bh(&se_tpg->session_lock); | |
144 | list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, | |
145 | sess_list) { | |
146 | ||
8359cf43 | 147 | sess_p = se_sess->fabric_sess_ptr; |
e48354ce NB |
148 | spin_lock(&sess_p->conn_lock); |
149 | if (atomic_read(&sess_p->session_fall_back_to_erl0) || | |
150 | atomic_read(&sess_p->session_logout) || | |
151 | (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) { | |
152 | spin_unlock(&sess_p->conn_lock); | |
153 | continue; | |
154 | } | |
8359cf43 JE |
155 | if (!memcmp(sess_p->isid, conn->sess->isid, 6) && |
156 | (!strcmp(sess_p->sess_ops->InitiatorName, | |
157 | initiatorname_param->value) && | |
e48354ce NB |
158 | (sess_p->sess_ops->SessionType == sessiontype))) { |
159 | atomic_set(&sess_p->session_reinstatement, 1); | |
160 | spin_unlock(&sess_p->conn_lock); | |
161 | iscsit_inc_session_usage_count(sess_p); | |
162 | iscsit_stop_time2retain_timer(sess_p); | |
163 | sess = sess_p; | |
164 | break; | |
165 | } | |
166 | spin_unlock(&sess_p->conn_lock); | |
167 | } | |
168 | spin_unlock_bh(&se_tpg->session_lock); | |
169 | /* | |
170 | * If the Time2Retain handler has expired, the session is already gone. | |
171 | */ | |
172 | if (!sess) | |
173 | return 0; | |
174 | ||
175 | pr_debug("%s iSCSI Session SID %u is still active for %s," | |
176 | " preforming session reinstatement.\n", (sessiontype) ? | |
177 | "Discovery" : "Normal", sess->sid, | |
178 | sess->sess_ops->InitiatorName); | |
179 | ||
180 | spin_lock_bh(&sess->conn_lock); | |
181 | if (sess->session_state == TARG_SESS_STATE_FAILED) { | |
182 | spin_unlock_bh(&sess->conn_lock); | |
183 | iscsit_dec_session_usage_count(sess); | |
184 | return iscsit_close_session(sess); | |
185 | } | |
186 | spin_unlock_bh(&sess->conn_lock); | |
187 | ||
188 | iscsit_stop_session(sess, 1, 1); | |
189 | iscsit_dec_session_usage_count(sess); | |
190 | ||
191 | return iscsit_close_session(sess); | |
192 | } | |
193 | ||
194 | static void iscsi_login_set_conn_values( | |
195 | struct iscsi_session *sess, | |
196 | struct iscsi_conn *conn, | |
197 | u16 cid) | |
198 | { | |
199 | conn->sess = sess; | |
200 | conn->cid = cid; | |
201 | /* | |
202 | * Generate a random Status sequence number (statsn) for the new | |
203 | * iSCSI connection. | |
204 | */ | |
205 | get_random_bytes(&conn->stat_sn, sizeof(u32)); | |
206 | ||
207 | mutex_lock(&auth_id_lock); | |
208 | conn->auth_id = iscsit_global->auth_id++; | |
209 | mutex_unlock(&auth_id_lock); | |
210 | } | |
211 | ||
212 | /* | |
213 | * This is the leading connection of a new session, | |
214 | * or session reinstatement. | |
215 | */ | |
216 | static int iscsi_login_zero_tsih_s1( | |
217 | struct iscsi_conn *conn, | |
218 | unsigned char *buf) | |
219 | { | |
220 | struct iscsi_session *sess = NULL; | |
221 | struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; | |
222 | ||
223 | sess = kzalloc(sizeof(struct iscsi_session), GFP_KERNEL); | |
224 | if (!sess) { | |
225 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
226 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
227 | pr_err("Could not allocate memory for session\n"); | |
0957627a | 228 | return -ENOMEM; |
e48354ce NB |
229 | } |
230 | ||
231 | iscsi_login_set_conn_values(sess, conn, pdu->cid); | |
232 | sess->init_task_tag = pdu->itt; | |
8359cf43 | 233 | memcpy(&sess->isid, pdu->isid, 6); |
e48354ce NB |
234 | sess->exp_cmd_sn = pdu->cmdsn; |
235 | INIT_LIST_HEAD(&sess->sess_conn_list); | |
236 | INIT_LIST_HEAD(&sess->sess_ooo_cmdsn_list); | |
237 | INIT_LIST_HEAD(&sess->cr_active_list); | |
238 | INIT_LIST_HEAD(&sess->cr_inactive_list); | |
239 | init_completion(&sess->async_msg_comp); | |
240 | init_completion(&sess->reinstatement_comp); | |
241 | init_completion(&sess->session_wait_comp); | |
242 | init_completion(&sess->session_waiting_on_uc_comp); | |
243 | mutex_init(&sess->cmdsn_mutex); | |
244 | spin_lock_init(&sess->conn_lock); | |
245 | spin_lock_init(&sess->cr_a_lock); | |
246 | spin_lock_init(&sess->cr_i_lock); | |
247 | spin_lock_init(&sess->session_usage_lock); | |
248 | spin_lock_init(&sess->ttt_lock); | |
249 | ||
250 | if (!idr_pre_get(&sess_idr, GFP_KERNEL)) { | |
251 | pr_err("idr_pre_get() for sess_idr failed\n"); | |
252 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
253 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
0957627a NB |
254 | kfree(sess); |
255 | return -ENOMEM; | |
e48354ce NB |
256 | } |
257 | spin_lock(&sess_idr_lock); | |
258 | idr_get_new(&sess_idr, NULL, &sess->session_index); | |
259 | spin_unlock(&sess_idr_lock); | |
260 | ||
261 | sess->creation_time = get_jiffies_64(); | |
262 | spin_lock_init(&sess->session_stats_lock); | |
263 | /* | |
264 | * The FFP CmdSN window values will be allocated from the TPG's | |
265 | * Initiator Node's ACL once the login has been successfully completed. | |
266 | */ | |
267 | sess->max_cmd_sn = pdu->cmdsn; | |
268 | ||
269 | sess->sess_ops = kzalloc(sizeof(struct iscsi_sess_ops), GFP_KERNEL); | |
270 | if (!sess->sess_ops) { | |
271 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
272 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
273 | pr_err("Unable to allocate memory for" | |
274 | " struct iscsi_sess_ops.\n"); | |
0957627a NB |
275 | kfree(sess); |
276 | return -ENOMEM; | |
e48354ce NB |
277 | } |
278 | ||
279 | sess->se_sess = transport_init_session(); | |
0957627a | 280 | if (IS_ERR(sess->se_sess)) { |
e48354ce NB |
281 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, |
282 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
0957627a NB |
283 | kfree(sess); |
284 | return -ENOMEM; | |
e48354ce NB |
285 | } |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
290 | static int iscsi_login_zero_tsih_s2( | |
291 | struct iscsi_conn *conn) | |
292 | { | |
293 | struct iscsi_node_attrib *na; | |
294 | struct iscsi_session *sess = conn->sess; | |
295 | unsigned char buf[32]; | |
296 | ||
297 | sess->tpg = conn->tpg; | |
298 | ||
299 | /* | |
300 | * Assign a new TPG Session Handle. Note this is protected with | |
301 | * struct iscsi_portal_group->np_login_sem from iscsit_access_np(). | |
302 | */ | |
303 | sess->tsih = ++ISCSI_TPG_S(sess)->ntsih; | |
304 | if (!sess->tsih) | |
305 | sess->tsih = ++ISCSI_TPG_S(sess)->ntsih; | |
306 | ||
307 | /* | |
308 | * Create the default params from user defined values.. | |
309 | */ | |
310 | if (iscsi_copy_param_list(&conn->param_list, | |
311 | ISCSI_TPG_C(conn)->param_list, 1) < 0) { | |
312 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
313 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
314 | return -1; | |
315 | } | |
316 | ||
317 | iscsi_set_keys_to_negotiate(0, conn->param_list); | |
318 | ||
319 | if (sess->sess_ops->SessionType) | |
320 | return iscsi_set_keys_irrelevant_for_discovery( | |
321 | conn->param_list); | |
322 | ||
323 | na = iscsit_tpg_get_node_attrib(sess); | |
324 | ||
325 | /* | |
326 | * Need to send TargetPortalGroupTag back in first login response | |
327 | * on any iSCSI connection where the Initiator provides TargetName. | |
328 | * See 5.3.1. Login Phase Start | |
329 | * | |
330 | * In our case, we have already located the struct iscsi_tiqn at this point. | |
331 | */ | |
332 | memset(buf, 0, 32); | |
333 | sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt); | |
334 | if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) { | |
335 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
336 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
337 | return -1; | |
338 | } | |
339 | ||
340 | /* | |
341 | * Workaround for Initiators that have broken connection recovery logic. | |
342 | * | |
343 | * "We would really like to get rid of this." Linux-iSCSI.org team | |
344 | */ | |
345 | memset(buf, 0, 32); | |
346 | sprintf(buf, "ErrorRecoveryLevel=%d", na->default_erl); | |
347 | if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) { | |
348 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
349 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
350 | return -1; | |
351 | } | |
352 | ||
353 | if (iscsi_login_disable_FIM_keys(conn->param_list, conn) < 0) | |
354 | return -1; | |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
359 | /* | |
360 | * Remove PSTATE_NEGOTIATE for the four FIM related keys. | |
361 | * The Initiator node will be able to enable FIM by proposing them itself. | |
362 | */ | |
363 | int iscsi_login_disable_FIM_keys( | |
364 | struct iscsi_param_list *param_list, | |
365 | struct iscsi_conn *conn) | |
366 | { | |
367 | struct iscsi_param *param; | |
368 | ||
369 | param = iscsi_find_param_from_key("OFMarker", param_list); | |
370 | if (!param) { | |
371 | pr_err("iscsi_find_param_from_key() for" | |
372 | " OFMarker failed\n"); | |
373 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
374 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
375 | return -1; | |
376 | } | |
377 | param->state &= ~PSTATE_NEGOTIATE; | |
378 | ||
379 | param = iscsi_find_param_from_key("OFMarkInt", param_list); | |
380 | if (!param) { | |
381 | pr_err("iscsi_find_param_from_key() for" | |
382 | " IFMarker failed\n"); | |
383 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
384 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
385 | return -1; | |
386 | } | |
387 | param->state &= ~PSTATE_NEGOTIATE; | |
388 | ||
389 | param = iscsi_find_param_from_key("IFMarker", param_list); | |
390 | if (!param) { | |
391 | pr_err("iscsi_find_param_from_key() for" | |
392 | " IFMarker failed\n"); | |
393 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
394 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
395 | return -1; | |
396 | } | |
397 | param->state &= ~PSTATE_NEGOTIATE; | |
398 | ||
399 | param = iscsi_find_param_from_key("IFMarkInt", param_list); | |
400 | if (!param) { | |
401 | pr_err("iscsi_find_param_from_key() for" | |
402 | " IFMarker failed\n"); | |
403 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
404 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
405 | return -1; | |
406 | } | |
407 | param->state &= ~PSTATE_NEGOTIATE; | |
408 | ||
409 | return 0; | |
410 | } | |
411 | ||
412 | static int iscsi_login_non_zero_tsih_s1( | |
413 | struct iscsi_conn *conn, | |
414 | unsigned char *buf) | |
415 | { | |
416 | struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; | |
417 | ||
418 | iscsi_login_set_conn_values(NULL, conn, pdu->cid); | |
419 | return 0; | |
420 | } | |
421 | ||
422 | /* | |
423 | * Add a new connection to an existing session. | |
424 | */ | |
425 | static int iscsi_login_non_zero_tsih_s2( | |
426 | struct iscsi_conn *conn, | |
427 | unsigned char *buf) | |
428 | { | |
429 | struct iscsi_portal_group *tpg = conn->tpg; | |
430 | struct iscsi_session *sess = NULL, *sess_p = NULL; | |
431 | struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; | |
432 | struct se_session *se_sess, *se_sess_tmp; | |
433 | struct iscsi_login_req *pdu = (struct iscsi_login_req *)buf; | |
434 | ||
435 | spin_lock_bh(&se_tpg->session_lock); | |
436 | list_for_each_entry_safe(se_sess, se_sess_tmp, &se_tpg->tpg_sess_list, | |
437 | sess_list) { | |
438 | ||
439 | sess_p = (struct iscsi_session *)se_sess->fabric_sess_ptr; | |
440 | if (atomic_read(&sess_p->session_fall_back_to_erl0) || | |
441 | atomic_read(&sess_p->session_logout) || | |
442 | (sess_p->time2retain_timer_flags & ISCSI_TF_EXPIRED)) | |
443 | continue; | |
8359cf43 | 444 | if (!memcmp(sess_p->isid, pdu->isid, 6) && |
e48354ce NB |
445 | (sess_p->tsih == pdu->tsih)) { |
446 | iscsit_inc_session_usage_count(sess_p); | |
447 | iscsit_stop_time2retain_timer(sess_p); | |
448 | sess = sess_p; | |
449 | break; | |
450 | } | |
451 | } | |
452 | spin_unlock_bh(&se_tpg->session_lock); | |
453 | ||
454 | /* | |
455 | * If the Time2Retain handler has expired, the session is already gone. | |
456 | */ | |
457 | if (!sess) { | |
458 | pr_err("Initiator attempting to add a connection to" | |
459 | " a non-existent session, rejecting iSCSI Login.\n"); | |
460 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, | |
461 | ISCSI_LOGIN_STATUS_NO_SESSION); | |
462 | return -1; | |
463 | } | |
464 | ||
465 | /* | |
466 | * Stop the Time2Retain timer if this is a failed session, we restart | |
467 | * the timer if the login is not successful. | |
468 | */ | |
469 | spin_lock_bh(&sess->conn_lock); | |
470 | if (sess->session_state == TARG_SESS_STATE_FAILED) | |
471 | atomic_set(&sess->session_continuation, 1); | |
472 | spin_unlock_bh(&sess->conn_lock); | |
473 | ||
474 | iscsi_login_set_conn_values(sess, conn, pdu->cid); | |
475 | ||
476 | if (iscsi_copy_param_list(&conn->param_list, | |
477 | ISCSI_TPG_C(conn)->param_list, 0) < 0) { | |
478 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
479 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
480 | return -1; | |
481 | } | |
482 | ||
483 | iscsi_set_keys_to_negotiate(0, conn->param_list); | |
484 | /* | |
485 | * Need to send TargetPortalGroupTag back in first login response | |
486 | * on any iSCSI connection where the Initiator provides TargetName. | |
487 | * See 5.3.1. Login Phase Start | |
488 | * | |
489 | * In our case, we have already located the struct iscsi_tiqn at this point. | |
490 | */ | |
491 | memset(buf, 0, 32); | |
492 | sprintf(buf, "TargetPortalGroupTag=%hu", ISCSI_TPG_S(sess)->tpgt); | |
493 | if (iscsi_change_param_value(buf, conn->param_list, 0) < 0) { | |
494 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
495 | ISCSI_LOGIN_STATUS_NO_RESOURCES); | |
496 | return -1; | |
497 | } | |
498 | ||
499 | return iscsi_login_disable_FIM_keys(conn->param_list, conn); | |
500 | } | |
501 | ||
502 | int iscsi_login_post_auth_non_zero_tsih( | |
503 | struct iscsi_conn *conn, | |
504 | u16 cid, | |
505 | u32 exp_statsn) | |
506 | { | |
507 | struct iscsi_conn *conn_ptr = NULL; | |
508 | struct iscsi_conn_recovery *cr = NULL; | |
509 | struct iscsi_session *sess = conn->sess; | |
510 | ||
511 | /* | |
512 | * By following item 5 in the login table, if we have found | |
513 | * an existing ISID and a valid/existing TSIH and an existing | |
514 | * CID we do connection reinstatement. Currently we dont not | |
515 | * support it so we send back an non-zero status class to the | |
516 | * initiator and release the new connection. | |
517 | */ | |
518 | conn_ptr = iscsit_get_conn_from_cid_rcfr(sess, cid); | |
519 | if ((conn_ptr)) { | |
520 | pr_err("Connection exists with CID %hu for %s," | |
521 | " performing connection reinstatement.\n", | |
522 | conn_ptr->cid, sess->sess_ops->InitiatorName); | |
523 | ||
524 | iscsit_connection_reinstatement_rcfr(conn_ptr); | |
525 | iscsit_dec_conn_usage_count(conn_ptr); | |
526 | } | |
527 | ||
528 | /* | |
529 | * Check for any connection recovery entires containing CID. | |
530 | * We use the original ExpStatSN sent in the first login request | |
531 | * to acknowledge commands for the failed connection. | |
532 | * | |
533 | * Also note that an explict logout may have already been sent, | |
534 | * but the response may not be sent due to additional connection | |
535 | * loss. | |
536 | */ | |
537 | if (sess->sess_ops->ErrorRecoveryLevel == 2) { | |
538 | cr = iscsit_get_inactive_connection_recovery_entry( | |
539 | sess, cid); | |
540 | if ((cr)) { | |
541 | pr_debug("Performing implicit logout" | |
542 | " for connection recovery on CID: %hu\n", | |
543 | conn->cid); | |
544 | iscsit_discard_cr_cmds_by_expstatsn(cr, exp_statsn); | |
545 | } | |
546 | } | |
547 | ||
548 | /* | |
549 | * Else we follow item 4 from the login table in that we have | |
550 | * found an existing ISID and a valid/existing TSIH and a new | |
551 | * CID we go ahead and continue to add a new connection to the | |
552 | * session. | |
553 | */ | |
554 | pr_debug("Adding CID %hu to existing session for %s.\n", | |
555 | cid, sess->sess_ops->InitiatorName); | |
556 | ||
557 | if ((atomic_read(&sess->nconn) + 1) > sess->sess_ops->MaxConnections) { | |
558 | pr_err("Adding additional connection to this session" | |
559 | " would exceed MaxConnections %d, login failed.\n", | |
560 | sess->sess_ops->MaxConnections); | |
561 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, | |
562 | ISCSI_LOGIN_STATUS_ISID_ERROR); | |
563 | return -1; | |
564 | } | |
565 | ||
566 | return 0; | |
567 | } | |
568 | ||
569 | static void iscsi_post_login_start_timers(struct iscsi_conn *conn) | |
570 | { | |
571 | struct iscsi_session *sess = conn->sess; | |
572 | ||
573 | if (!sess->sess_ops->SessionType) | |
574 | iscsit_start_nopin_timer(conn); | |
575 | } | |
576 | ||
577 | static int iscsi_post_login_handler( | |
578 | struct iscsi_np *np, | |
579 | struct iscsi_conn *conn, | |
580 | u8 zero_tsih) | |
581 | { | |
582 | int stop_timer = 0; | |
583 | struct iscsi_session *sess = conn->sess; | |
584 | struct se_session *se_sess = sess->se_sess; | |
585 | struct iscsi_portal_group *tpg = ISCSI_TPG_S(sess); | |
586 | struct se_portal_group *se_tpg = &tpg->tpg_se_tpg; | |
587 | struct iscsi_thread_set *ts; | |
588 | ||
589 | iscsit_inc_conn_usage_count(conn); | |
590 | ||
591 | iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_SUCCESS, | |
592 | ISCSI_LOGIN_STATUS_ACCEPT); | |
593 | ||
594 | pr_debug("Moving to TARG_CONN_STATE_LOGGED_IN.\n"); | |
595 | conn->conn_state = TARG_CONN_STATE_LOGGED_IN; | |
596 | ||
597 | iscsi_set_connection_parameters(conn->conn_ops, conn->param_list); | |
598 | iscsit_set_sync_and_steering_values(conn); | |
599 | /* | |
600 | * SCSI Initiator -> SCSI Target Port Mapping | |
601 | */ | |
602 | ts = iscsi_get_thread_set(); | |
603 | if (!zero_tsih) { | |
604 | iscsi_set_session_parameters(sess->sess_ops, | |
605 | conn->param_list, 0); | |
606 | iscsi_release_param_list(conn->param_list); | |
607 | conn->param_list = NULL; | |
608 | ||
609 | spin_lock_bh(&sess->conn_lock); | |
610 | atomic_set(&sess->session_continuation, 0); | |
611 | if (sess->session_state == TARG_SESS_STATE_FAILED) { | |
612 | pr_debug("Moving to" | |
613 | " TARG_SESS_STATE_LOGGED_IN.\n"); | |
614 | sess->session_state = TARG_SESS_STATE_LOGGED_IN; | |
615 | stop_timer = 1; | |
616 | } | |
617 | ||
618 | pr_debug("iSCSI Login successful on CID: %hu from %s to" | |
2f9bc894 NB |
619 | " %s:%hu,%hu\n", conn->cid, conn->login_ip, |
620 | conn->local_ip, conn->local_port, tpg->tpgt); | |
e48354ce NB |
621 | |
622 | list_add_tail(&conn->conn_list, &sess->sess_conn_list); | |
623 | atomic_inc(&sess->nconn); | |
624 | pr_debug("Incremented iSCSI Connection count to %hu" | |
625 | " from node: %s\n", atomic_read(&sess->nconn), | |
626 | sess->sess_ops->InitiatorName); | |
627 | spin_unlock_bh(&sess->conn_lock); | |
628 | ||
629 | iscsi_post_login_start_timers(conn); | |
630 | iscsi_activate_thread_set(conn, ts); | |
631 | /* | |
632 | * Determine CPU mask to ensure connection's RX and TX kthreads | |
633 | * are scheduled on the same CPU. | |
634 | */ | |
635 | iscsit_thread_get_cpumask(conn); | |
636 | conn->conn_rx_reset_cpumask = 1; | |
637 | conn->conn_tx_reset_cpumask = 1; | |
638 | ||
639 | iscsit_dec_conn_usage_count(conn); | |
640 | if (stop_timer) { | |
641 | spin_lock_bh(&se_tpg->session_lock); | |
642 | iscsit_stop_time2retain_timer(sess); | |
643 | spin_unlock_bh(&se_tpg->session_lock); | |
644 | } | |
645 | iscsit_dec_session_usage_count(sess); | |
646 | return 0; | |
647 | } | |
648 | ||
649 | iscsi_set_session_parameters(sess->sess_ops, conn->param_list, 1); | |
650 | iscsi_release_param_list(conn->param_list); | |
651 | conn->param_list = NULL; | |
652 | ||
653 | iscsit_determine_maxcmdsn(sess); | |
654 | ||
655 | spin_lock_bh(&se_tpg->session_lock); | |
656 | __transport_register_session(&sess->tpg->tpg_se_tpg, | |
8359cf43 | 657 | se_sess->se_node_acl, se_sess, sess); |
e48354ce NB |
658 | pr_debug("Moving to TARG_SESS_STATE_LOGGED_IN.\n"); |
659 | sess->session_state = TARG_SESS_STATE_LOGGED_IN; | |
660 | ||
661 | pr_debug("iSCSI Login successful on CID: %hu from %s to %s:%hu,%hu\n", | |
2f9bc894 NB |
662 | conn->cid, conn->login_ip, conn->local_ip, conn->local_port, |
663 | tpg->tpgt); | |
e48354ce NB |
664 | |
665 | spin_lock_bh(&sess->conn_lock); | |
666 | list_add_tail(&conn->conn_list, &sess->sess_conn_list); | |
667 | atomic_inc(&sess->nconn); | |
668 | pr_debug("Incremented iSCSI Connection count to %hu from node:" | |
669 | " %s\n", atomic_read(&sess->nconn), | |
670 | sess->sess_ops->InitiatorName); | |
671 | spin_unlock_bh(&sess->conn_lock); | |
672 | ||
673 | sess->sid = tpg->sid++; | |
674 | if (!sess->sid) | |
675 | sess->sid = tpg->sid++; | |
676 | pr_debug("Established iSCSI session from node: %s\n", | |
677 | sess->sess_ops->InitiatorName); | |
678 | ||
679 | tpg->nsessions++; | |
680 | if (tpg->tpg_tiqn) | |
681 | tpg->tpg_tiqn->tiqn_nsessions++; | |
682 | ||
683 | pr_debug("Incremented number of active iSCSI sessions to %u on" | |
684 | " iSCSI Target Portal Group: %hu\n", tpg->nsessions, tpg->tpgt); | |
685 | spin_unlock_bh(&se_tpg->session_lock); | |
686 | ||
687 | iscsi_post_login_start_timers(conn); | |
688 | iscsi_activate_thread_set(conn, ts); | |
689 | /* | |
690 | * Determine CPU mask to ensure connection's RX and TX kthreads | |
691 | * are scheduled on the same CPU. | |
692 | */ | |
693 | iscsit_thread_get_cpumask(conn); | |
694 | conn->conn_rx_reset_cpumask = 1; | |
695 | conn->conn_tx_reset_cpumask = 1; | |
696 | ||
697 | iscsit_dec_conn_usage_count(conn); | |
698 | ||
699 | return 0; | |
700 | } | |
701 | ||
702 | static void iscsi_handle_login_thread_timeout(unsigned long data) | |
703 | { | |
704 | struct iscsi_np *np = (struct iscsi_np *) data; | |
705 | ||
706 | spin_lock_bh(&np->np_thread_lock); | |
707 | pr_err("iSCSI Login timeout on Network Portal %s:%hu\n", | |
708 | np->np_ip, np->np_port); | |
709 | ||
710 | if (np->np_login_timer_flags & ISCSI_TF_STOP) { | |
711 | spin_unlock_bh(&np->np_thread_lock); | |
712 | return; | |
713 | } | |
714 | ||
715 | if (np->np_thread) | |
716 | send_sig(SIGINT, np->np_thread, 1); | |
717 | ||
718 | np->np_login_timer_flags &= ~ISCSI_TF_RUNNING; | |
719 | spin_unlock_bh(&np->np_thread_lock); | |
720 | } | |
721 | ||
722 | static void iscsi_start_login_thread_timer(struct iscsi_np *np) | |
723 | { | |
724 | /* | |
725 | * This used the TA_LOGIN_TIMEOUT constant because at this | |
726 | * point we do not have access to ISCSI_TPG_ATTRIB(tpg)->login_timeout | |
727 | */ | |
728 | spin_lock_bh(&np->np_thread_lock); | |
729 | init_timer(&np->np_login_timer); | |
730 | np->np_login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ); | |
731 | np->np_login_timer.data = (unsigned long)np; | |
732 | np->np_login_timer.function = iscsi_handle_login_thread_timeout; | |
733 | np->np_login_timer_flags &= ~ISCSI_TF_STOP; | |
734 | np->np_login_timer_flags |= ISCSI_TF_RUNNING; | |
735 | add_timer(&np->np_login_timer); | |
736 | ||
737 | pr_debug("Added timeout timer to iSCSI login request for" | |
738 | " %u seconds.\n", TA_LOGIN_TIMEOUT); | |
739 | spin_unlock_bh(&np->np_thread_lock); | |
740 | } | |
741 | ||
742 | static void iscsi_stop_login_thread_timer(struct iscsi_np *np) | |
743 | { | |
744 | spin_lock_bh(&np->np_thread_lock); | |
745 | if (!(np->np_login_timer_flags & ISCSI_TF_RUNNING)) { | |
746 | spin_unlock_bh(&np->np_thread_lock); | |
747 | return; | |
748 | } | |
749 | np->np_login_timer_flags |= ISCSI_TF_STOP; | |
750 | spin_unlock_bh(&np->np_thread_lock); | |
751 | ||
752 | del_timer_sync(&np->np_login_timer); | |
753 | ||
754 | spin_lock_bh(&np->np_thread_lock); | |
755 | np->np_login_timer_flags &= ~ISCSI_TF_RUNNING; | |
756 | spin_unlock_bh(&np->np_thread_lock); | |
757 | } | |
758 | ||
759 | int iscsi_target_setup_login_socket( | |
760 | struct iscsi_np *np, | |
761 | struct __kernel_sockaddr_storage *sockaddr) | |
762 | { | |
763 | struct socket *sock; | |
764 | int backlog = 5, ret, opt = 0, len; | |
765 | ||
766 | switch (np->np_network_transport) { | |
767 | case ISCSI_TCP: | |
768 | np->np_ip_proto = IPPROTO_TCP; | |
769 | np->np_sock_type = SOCK_STREAM; | |
770 | break; | |
771 | case ISCSI_SCTP_TCP: | |
772 | np->np_ip_proto = IPPROTO_SCTP; | |
773 | np->np_sock_type = SOCK_STREAM; | |
774 | break; | |
775 | case ISCSI_SCTP_UDP: | |
776 | np->np_ip_proto = IPPROTO_SCTP; | |
777 | np->np_sock_type = SOCK_SEQPACKET; | |
778 | break; | |
779 | case ISCSI_IWARP_TCP: | |
780 | case ISCSI_IWARP_SCTP: | |
781 | case ISCSI_INFINIBAND: | |
782 | default: | |
783 | pr_err("Unsupported network_transport: %d\n", | |
784 | np->np_network_transport); | |
785 | return -EINVAL; | |
786 | } | |
787 | ||
788 | ret = sock_create(sockaddr->ss_family, np->np_sock_type, | |
789 | np->np_ip_proto, &sock); | |
790 | if (ret < 0) { | |
791 | pr_err("sock_create() failed.\n"); | |
792 | return ret; | |
793 | } | |
794 | np->np_socket = sock; | |
795 | /* | |
796 | * The SCTP stack needs struct socket->file. | |
797 | */ | |
798 | if ((np->np_network_transport == ISCSI_SCTP_TCP) || | |
799 | (np->np_network_transport == ISCSI_SCTP_UDP)) { | |
800 | if (!sock->file) { | |
801 | sock->file = kzalloc(sizeof(struct file), GFP_KERNEL); | |
802 | if (!sock->file) { | |
803 | pr_err("Unable to allocate struct" | |
804 | " file for SCTP\n"); | |
805 | ret = -ENOMEM; | |
806 | goto fail; | |
807 | } | |
808 | np->np_flags |= NPF_SCTP_STRUCT_FILE; | |
809 | } | |
810 | } | |
811 | /* | |
812 | * Setup the np->np_sockaddr from the passed sockaddr setup | |
813 | * in iscsi_target_configfs.c code.. | |
814 | */ | |
8359cf43 | 815 | memcpy(&np->np_sockaddr, sockaddr, |
e48354ce NB |
816 | sizeof(struct __kernel_sockaddr_storage)); |
817 | ||
818 | if (sockaddr->ss_family == AF_INET6) | |
819 | len = sizeof(struct sockaddr_in6); | |
820 | else | |
821 | len = sizeof(struct sockaddr_in); | |
822 | /* | |
823 | * Set SO_REUSEADDR, and disable Nagel Algorithm with TCP_NODELAY. | |
824 | */ | |
8359cf43 | 825 | /* FIXME: Someone please explain why this is endian-safe */ |
e48354ce NB |
826 | opt = 1; |
827 | if (np->np_network_transport == ISCSI_TCP) { | |
828 | ret = kernel_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, | |
829 | (char *)&opt, sizeof(opt)); | |
830 | if (ret < 0) { | |
831 | pr_err("kernel_setsockopt() for TCP_NODELAY" | |
832 | " failed: %d\n", ret); | |
833 | goto fail; | |
834 | } | |
835 | } | |
836 | ||
8359cf43 | 837 | /* FIXME: Someone please explain why this is endian-safe */ |
e48354ce NB |
838 | ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, |
839 | (char *)&opt, sizeof(opt)); | |
840 | if (ret < 0) { | |
841 | pr_err("kernel_setsockopt() for SO_REUSEADDR" | |
842 | " failed\n"); | |
843 | goto fail; | |
844 | } | |
845 | ||
9f9ef6d3 DK |
846 | ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND, |
847 | (char *)&opt, sizeof(opt)); | |
848 | if (ret < 0) { | |
849 | pr_err("kernel_setsockopt() for IP_FREEBIND" | |
850 | " failed\n"); | |
851 | goto fail; | |
852 | } | |
853 | ||
e48354ce NB |
854 | ret = kernel_bind(sock, (struct sockaddr *)&np->np_sockaddr, len); |
855 | if (ret < 0) { | |
856 | pr_err("kernel_bind() failed: %d\n", ret); | |
857 | goto fail; | |
858 | } | |
859 | ||
860 | ret = kernel_listen(sock, backlog); | |
861 | if (ret != 0) { | |
862 | pr_err("kernel_listen() failed: %d\n", ret); | |
863 | goto fail; | |
864 | } | |
865 | ||
866 | return 0; | |
867 | ||
868 | fail: | |
869 | np->np_socket = NULL; | |
870 | if (sock) { | |
871 | if (np->np_flags & NPF_SCTP_STRUCT_FILE) { | |
872 | kfree(sock->file); | |
873 | sock->file = NULL; | |
874 | } | |
875 | ||
876 | sock_release(sock); | |
877 | } | |
878 | return ret; | |
879 | } | |
880 | ||
881 | static int __iscsi_target_login_thread(struct iscsi_np *np) | |
882 | { | |
883 | u8 buffer[ISCSI_HDR_LEN], iscsi_opcode, zero_tsih = 0; | |
884 | int err, ret = 0, ip_proto, sock_type, set_sctp_conn_flag, stop; | |
885 | struct iscsi_conn *conn = NULL; | |
886 | struct iscsi_login *login; | |
887 | struct iscsi_portal_group *tpg = NULL; | |
888 | struct socket *new_sock, *sock; | |
889 | struct kvec iov; | |
890 | struct iscsi_login_req *pdu; | |
891 | struct sockaddr_in sock_in; | |
892 | struct sockaddr_in6 sock_in6; | |
893 | ||
894 | flush_signals(current); | |
895 | set_sctp_conn_flag = 0; | |
896 | sock = np->np_socket; | |
897 | ip_proto = np->np_ip_proto; | |
898 | sock_type = np->np_sock_type; | |
899 | ||
900 | spin_lock_bh(&np->np_thread_lock); | |
901 | if (np->np_thread_state == ISCSI_NP_THREAD_RESET) { | |
902 | np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; | |
903 | complete(&np->np_restart_comp); | |
904 | } else { | |
905 | np->np_thread_state = ISCSI_NP_THREAD_ACTIVE; | |
906 | } | |
907 | spin_unlock_bh(&np->np_thread_lock); | |
908 | ||
909 | if (kernel_accept(sock, &new_sock, 0) < 0) { | |
910 | spin_lock_bh(&np->np_thread_lock); | |
911 | if (np->np_thread_state == ISCSI_NP_THREAD_RESET) { | |
912 | spin_unlock_bh(&np->np_thread_lock); | |
913 | complete(&np->np_restart_comp); | |
914 | /* Get another socket */ | |
915 | return 1; | |
916 | } | |
917 | spin_unlock_bh(&np->np_thread_lock); | |
918 | goto out; | |
919 | } | |
920 | /* | |
921 | * The SCTP stack needs struct socket->file. | |
922 | */ | |
923 | if ((np->np_network_transport == ISCSI_SCTP_TCP) || | |
924 | (np->np_network_transport == ISCSI_SCTP_UDP)) { | |
925 | if (!new_sock->file) { | |
926 | new_sock->file = kzalloc( | |
927 | sizeof(struct file), GFP_KERNEL); | |
928 | if (!new_sock->file) { | |
929 | pr_err("Unable to allocate struct" | |
930 | " file for SCTP\n"); | |
931 | sock_release(new_sock); | |
932 | /* Get another socket */ | |
933 | return 1; | |
934 | } | |
935 | set_sctp_conn_flag = 1; | |
936 | } | |
937 | } | |
938 | ||
939 | iscsi_start_login_thread_timer(np); | |
940 | ||
941 | conn = kzalloc(sizeof(struct iscsi_conn), GFP_KERNEL); | |
942 | if (!conn) { | |
943 | pr_err("Could not allocate memory for" | |
944 | " new connection\n"); | |
945 | if (set_sctp_conn_flag) { | |
946 | kfree(new_sock->file); | |
947 | new_sock->file = NULL; | |
948 | } | |
949 | sock_release(new_sock); | |
950 | /* Get another socket */ | |
951 | return 1; | |
952 | } | |
953 | ||
954 | pr_debug("Moving to TARG_CONN_STATE_FREE.\n"); | |
955 | conn->conn_state = TARG_CONN_STATE_FREE; | |
956 | conn->sock = new_sock; | |
957 | ||
958 | if (set_sctp_conn_flag) | |
959 | conn->conn_flags |= CONNFLAG_SCTP_STRUCT_FILE; | |
960 | ||
961 | pr_debug("Moving to TARG_CONN_STATE_XPT_UP.\n"); | |
962 | conn->conn_state = TARG_CONN_STATE_XPT_UP; | |
963 | ||
964 | /* | |
965 | * Allocate conn->conn_ops early as a failure calling | |
966 | * iscsit_tx_login_rsp() below will call tx_data(). | |
967 | */ | |
968 | conn->conn_ops = kzalloc(sizeof(struct iscsi_conn_ops), GFP_KERNEL); | |
969 | if (!conn->conn_ops) { | |
970 | pr_err("Unable to allocate memory for" | |
971 | " struct iscsi_conn_ops.\n"); | |
972 | goto new_sess_out; | |
973 | } | |
974 | /* | |
975 | * Perform the remaining iSCSI connection initialization items.. | |
976 | */ | |
977 | if (iscsi_login_init_conn(conn) < 0) | |
978 | goto new_sess_out; | |
979 | ||
980 | memset(buffer, 0, ISCSI_HDR_LEN); | |
981 | memset(&iov, 0, sizeof(struct kvec)); | |
982 | iov.iov_base = buffer; | |
983 | iov.iov_len = ISCSI_HDR_LEN; | |
984 | ||
985 | if (rx_data(conn, &iov, 1, ISCSI_HDR_LEN) <= 0) { | |
986 | pr_err("rx_data() returned an error.\n"); | |
987 | goto new_sess_out; | |
988 | } | |
989 | ||
990 | iscsi_opcode = (buffer[0] & ISCSI_OPCODE_MASK); | |
991 | if (!(iscsi_opcode & ISCSI_OP_LOGIN)) { | |
992 | pr_err("First opcode is not login request," | |
993 | " failing login request.\n"); | |
994 | goto new_sess_out; | |
995 | } | |
996 | ||
997 | pdu = (struct iscsi_login_req *) buffer; | |
998 | pdu->cid = be16_to_cpu(pdu->cid); | |
999 | pdu->tsih = be16_to_cpu(pdu->tsih); | |
1000 | pdu->itt = be32_to_cpu(pdu->itt); | |
1001 | pdu->cmdsn = be32_to_cpu(pdu->cmdsn); | |
1002 | pdu->exp_statsn = be32_to_cpu(pdu->exp_statsn); | |
1003 | /* | |
1004 | * Used by iscsit_tx_login_rsp() for Login Resonses PDUs | |
1005 | * when Status-Class != 0. | |
1006 | */ | |
1007 | conn->login_itt = pdu->itt; | |
1008 | ||
1009 | spin_lock_bh(&np->np_thread_lock); | |
1010 | if (np->np_thread_state != ISCSI_NP_THREAD_ACTIVE) { | |
1011 | spin_unlock_bh(&np->np_thread_lock); | |
1012 | pr_err("iSCSI Network Portal on %s:%hu currently not" | |
1013 | " active.\n", np->np_ip, np->np_port); | |
1014 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
1015 | ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE); | |
1016 | goto new_sess_out; | |
1017 | } | |
1018 | spin_unlock_bh(&np->np_thread_lock); | |
1019 | ||
1020 | if (np->np_sockaddr.ss_family == AF_INET6) { | |
1021 | memset(&sock_in6, 0, sizeof(struct sockaddr_in6)); | |
1022 | ||
1023 | if (conn->sock->ops->getname(conn->sock, | |
1024 | (struct sockaddr *)&sock_in6, &err, 1) < 0) { | |
1025 | pr_err("sock_ops->getname() failed.\n"); | |
1026 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
1027 | ISCSI_LOGIN_STATUS_TARGET_ERROR); | |
1028 | goto new_sess_out; | |
1029 | } | |
6626a057 CB |
1030 | snprintf(conn->login_ip, sizeof(conn->login_ip), "%pI6c", |
1031 | &sock_in6.sin6_addr.in6_u); | |
1032 | conn->login_port = ntohs(sock_in6.sin6_port); | |
2f9bc894 NB |
1033 | |
1034 | if (conn->sock->ops->getname(conn->sock, | |
1035 | (struct sockaddr *)&sock_in6, &err, 0) < 0) { | |
1036 | pr_err("sock_ops->getname() failed.\n"); | |
1037 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
1038 | ISCSI_LOGIN_STATUS_TARGET_ERROR); | |
1039 | goto new_sess_out; | |
1040 | } | |
1041 | snprintf(conn->local_ip, sizeof(conn->local_ip), "%pI6c", | |
1042 | &sock_in6.sin6_addr.in6_u); | |
1043 | conn->local_port = ntohs(sock_in6.sin6_port); | |
1044 | ||
e48354ce NB |
1045 | } else { |
1046 | memset(&sock_in, 0, sizeof(struct sockaddr_in)); | |
1047 | ||
1048 | if (conn->sock->ops->getname(conn->sock, | |
1049 | (struct sockaddr *)&sock_in, &err, 1) < 0) { | |
1050 | pr_err("sock_ops->getname() failed.\n"); | |
1051 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
1052 | ISCSI_LOGIN_STATUS_TARGET_ERROR); | |
1053 | goto new_sess_out; | |
1054 | } | |
1055 | sprintf(conn->login_ip, "%pI4", &sock_in.sin_addr.s_addr); | |
1056 | conn->login_port = ntohs(sock_in.sin_port); | |
2f9bc894 NB |
1057 | |
1058 | if (conn->sock->ops->getname(conn->sock, | |
1059 | (struct sockaddr *)&sock_in, &err, 0) < 0) { | |
1060 | pr_err("sock_ops->getname() failed.\n"); | |
1061 | iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR, | |
1062 | ISCSI_LOGIN_STATUS_TARGET_ERROR); | |
1063 | goto new_sess_out; | |
1064 | } | |
1065 | sprintf(conn->local_ip, "%pI4", &sock_in.sin_addr.s_addr); | |
1066 | conn->local_port = ntohs(sock_in.sin_port); | |
e48354ce NB |
1067 | } |
1068 | ||
1069 | conn->network_transport = np->np_network_transport; | |
1070 | ||
1071 | pr_debug("Received iSCSI login request from %s on %s Network" | |
1072 | " Portal %s:%hu\n", conn->login_ip, | |
1073 | (conn->network_transport == ISCSI_TCP) ? "TCP" : "SCTP", | |
2f9bc894 | 1074 | conn->local_ip, conn->local_port); |
e48354ce NB |
1075 | |
1076 | pr_debug("Moving to TARG_CONN_STATE_IN_LOGIN.\n"); | |
1077 | conn->conn_state = TARG_CONN_STATE_IN_LOGIN; | |
1078 | ||
1079 | if (iscsi_login_check_initiator_version(conn, pdu->max_version, | |
1080 | pdu->min_version) < 0) | |
1081 | goto new_sess_out; | |
1082 | ||
1083 | zero_tsih = (pdu->tsih == 0x0000); | |
1084 | if ((zero_tsih)) { | |
1085 | /* | |
1086 | * This is the leading connection of a new session. | |
1087 | * We wait until after authentication to check for | |
1088 | * session reinstatement. | |
1089 | */ | |
1090 | if (iscsi_login_zero_tsih_s1(conn, buffer) < 0) | |
1091 | goto new_sess_out; | |
1092 | } else { | |
1093 | /* | |
1094 | * Add a new connection to an existing session. | |
1095 | * We check for a non-existant session in | |
1096 | * iscsi_login_non_zero_tsih_s2() below based | |
1097 | * on ISID/TSIH, but wait until after authentication | |
1098 | * to check for connection reinstatement, etc. | |
1099 | */ | |
1100 | if (iscsi_login_non_zero_tsih_s1(conn, buffer) < 0) | |
1101 | goto new_sess_out; | |
1102 | } | |
1103 | ||
1104 | /* | |
1105 | * This will process the first login request, and call | |
1106 | * iscsi_target_locate_portal(), and return a valid struct iscsi_login. | |
1107 | */ | |
1108 | login = iscsi_target_init_negotiation(np, conn, buffer); | |
1109 | if (!login) { | |
1110 | tpg = conn->tpg; | |
1111 | goto new_sess_out; | |
1112 | } | |
1113 | ||
1114 | tpg = conn->tpg; | |
1115 | if (!tpg) { | |
1116 | pr_err("Unable to locate struct iscsi_conn->tpg\n"); | |
1117 | goto new_sess_out; | |
1118 | } | |
1119 | ||
1120 | if (zero_tsih) { | |
1121 | if (iscsi_login_zero_tsih_s2(conn) < 0) { | |
1122 | iscsi_target_nego_release(login, conn); | |
1123 | goto new_sess_out; | |
1124 | } | |
1125 | } else { | |
1126 | if (iscsi_login_non_zero_tsih_s2(conn, buffer) < 0) { | |
1127 | iscsi_target_nego_release(login, conn); | |
1128 | goto old_sess_out; | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | if (iscsi_target_start_negotiation(login, conn) < 0) | |
1133 | goto new_sess_out; | |
1134 | ||
1135 | if (!conn->sess) { | |
1136 | pr_err("struct iscsi_conn session pointer is NULL!\n"); | |
1137 | goto new_sess_out; | |
1138 | } | |
1139 | ||
1140 | iscsi_stop_login_thread_timer(np); | |
1141 | ||
1142 | if (signal_pending(current)) | |
1143 | goto new_sess_out; | |
1144 | ||
1145 | ret = iscsi_post_login_handler(np, conn, zero_tsih); | |
1146 | ||
1147 | if (ret < 0) | |
1148 | goto new_sess_out; | |
1149 | ||
1150 | iscsit_deaccess_np(np, tpg); | |
1151 | tpg = NULL; | |
1152 | /* Get another socket */ | |
1153 | return 1; | |
1154 | ||
1155 | new_sess_out: | |
1156 | pr_err("iSCSI Login negotiation failed.\n"); | |
1157 | iscsit_collect_login_stats(conn, ISCSI_STATUS_CLS_INITIATOR_ERR, | |
1158 | ISCSI_LOGIN_STATUS_INIT_ERR); | |
1159 | if (!zero_tsih || !conn->sess) | |
1160 | goto old_sess_out; | |
1161 | if (conn->sess->se_sess) | |
1162 | transport_free_session(conn->sess->se_sess); | |
1163 | if (conn->sess->session_index != 0) { | |
1164 | spin_lock_bh(&sess_idr_lock); | |
1165 | idr_remove(&sess_idr, conn->sess->session_index); | |
1166 | spin_unlock_bh(&sess_idr_lock); | |
1167 | } | |
1168 | if (conn->sess->sess_ops) | |
1169 | kfree(conn->sess->sess_ops); | |
1170 | if (conn->sess) | |
1171 | kfree(conn->sess); | |
1172 | old_sess_out: | |
1173 | iscsi_stop_login_thread_timer(np); | |
1174 | /* | |
1175 | * If login negotiation fails check if the Time2Retain timer | |
1176 | * needs to be restarted. | |
1177 | */ | |
1178 | if (!zero_tsih && conn->sess) { | |
1179 | spin_lock_bh(&conn->sess->conn_lock); | |
1180 | if (conn->sess->session_state == TARG_SESS_STATE_FAILED) { | |
1181 | struct se_portal_group *se_tpg = | |
1182 | &ISCSI_TPG_C(conn)->tpg_se_tpg; | |
1183 | ||
1184 | atomic_set(&conn->sess->session_continuation, 0); | |
1185 | spin_unlock_bh(&conn->sess->conn_lock); | |
1186 | spin_lock_bh(&se_tpg->session_lock); | |
1187 | iscsit_start_time2retain_handler(conn->sess); | |
1188 | spin_unlock_bh(&se_tpg->session_lock); | |
1189 | } else | |
1190 | spin_unlock_bh(&conn->sess->conn_lock); | |
1191 | iscsit_dec_session_usage_count(conn->sess); | |
1192 | } | |
1193 | ||
1194 | if (!IS_ERR(conn->conn_rx_hash.tfm)) | |
1195 | crypto_free_hash(conn->conn_rx_hash.tfm); | |
1196 | if (!IS_ERR(conn->conn_tx_hash.tfm)) | |
1197 | crypto_free_hash(conn->conn_tx_hash.tfm); | |
1198 | ||
1199 | if (conn->conn_cpumask) | |
1200 | free_cpumask_var(conn->conn_cpumask); | |
1201 | ||
1202 | kfree(conn->conn_ops); | |
1203 | ||
1204 | if (conn->param_list) { | |
1205 | iscsi_release_param_list(conn->param_list); | |
1206 | conn->param_list = NULL; | |
1207 | } | |
1208 | if (conn->sock) { | |
1209 | if (conn->conn_flags & CONNFLAG_SCTP_STRUCT_FILE) { | |
1210 | kfree(conn->sock->file); | |
1211 | conn->sock->file = NULL; | |
1212 | } | |
1213 | sock_release(conn->sock); | |
1214 | } | |
1215 | kfree(conn); | |
1216 | ||
1217 | if (tpg) { | |
1218 | iscsit_deaccess_np(np, tpg); | |
1219 | tpg = NULL; | |
1220 | } | |
1221 | ||
1222 | out: | |
1223 | stop = kthread_should_stop(); | |
1224 | if (!stop && signal_pending(current)) { | |
1225 | spin_lock_bh(&np->np_thread_lock); | |
1226 | stop = (np->np_thread_state == ISCSI_NP_THREAD_SHUTDOWN); | |
1227 | spin_unlock_bh(&np->np_thread_lock); | |
1228 | } | |
1229 | /* Wait for another socket.. */ | |
1230 | if (!stop) | |
1231 | return 1; | |
1232 | ||
1233 | iscsi_stop_login_thread_timer(np); | |
1234 | spin_lock_bh(&np->np_thread_lock); | |
1235 | np->np_thread_state = ISCSI_NP_THREAD_EXIT; | |
1236 | spin_unlock_bh(&np->np_thread_lock); | |
1237 | return 0; | |
1238 | } | |
1239 | ||
1240 | int iscsi_target_login_thread(void *arg) | |
1241 | { | |
8359cf43 | 1242 | struct iscsi_np *np = arg; |
e48354ce NB |
1243 | int ret; |
1244 | ||
1245 | allow_signal(SIGINT); | |
1246 | ||
1247 | while (!kthread_should_stop()) { | |
1248 | ret = __iscsi_target_login_thread(np); | |
1249 | /* | |
1250 | * We break and exit here unless another sock_accept() call | |
1251 | * is expected. | |
1252 | */ | |
1253 | if (ret != 1) | |
1254 | break; | |
1255 | } | |
1256 | ||
1257 | return 0; | |
1258 | } |