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