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