]> Pileus Git - ~andy/linux/blob - drivers/target/iscsi/iscsi_target_nego.c
iscsi-target: Remove left-over iscsi_target_do_login_io
[~andy/linux] / drivers / target / iscsi / iscsi_target_nego.c
1 /*******************************************************************************
2  * This file contains main functions related to iSCSI Parameter negotiation.
3  *
4  * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
5  *
6  * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
7  *
8  * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  ******************************************************************************/
20
21 #include <linux/ctype.h>
22 #include <scsi/iscsi_proto.h>
23 #include <target/target_core_base.h>
24 #include <target/target_core_fabric.h>
25 #include <target/iscsi/iscsi_transport.h>
26
27 #include "iscsi_target_core.h"
28 #include "iscsi_target_parameters.h"
29 #include "iscsi_target_login.h"
30 #include "iscsi_target_nego.h"
31 #include "iscsi_target_tpg.h"
32 #include "iscsi_target_util.h"
33 #include "iscsi_target.h"
34 #include "iscsi_target_auth.h"
35
36 #define MAX_LOGIN_PDUS  7
37 #define TEXT_LEN        4096
38
39 void convert_null_to_semi(char *buf, int len)
40 {
41         int i;
42
43         for (i = 0; i < len; i++)
44                 if (buf[i] == '\0')
45                         buf[i] = ';';
46 }
47
48 static int strlen_semi(char *buf)
49 {
50         int i = 0;
51
52         while (buf[i] != '\0') {
53                 if (buf[i] == ';')
54                         return i;
55                 i++;
56         }
57
58         return -1;
59 }
60
61 int extract_param(
62         const char *in_buf,
63         const char *pattern,
64         unsigned int max_length,
65         char *out_buf,
66         unsigned char *type)
67 {
68         char *ptr;
69         int len;
70
71         if (!in_buf || !pattern || !out_buf || !type)
72                 return -1;
73
74         ptr = strstr(in_buf, pattern);
75         if (!ptr)
76                 return -1;
77
78         ptr = strstr(ptr, "=");
79         if (!ptr)
80                 return -1;
81
82         ptr += 1;
83         if (*ptr == '0' && (*(ptr+1) == 'x' || *(ptr+1) == 'X')) {
84                 ptr += 2; /* skip 0x */
85                 *type = HEX;
86         } else
87                 *type = DECIMAL;
88
89         len = strlen_semi(ptr);
90         if (len < 0)
91                 return -1;
92
93         if (len > max_length) {
94                 pr_err("Length of input: %d exceeds max_length:"
95                         " %d\n", len, max_length);
96                 return -1;
97         }
98         memcpy(out_buf, ptr, len);
99         out_buf[len] = '\0';
100
101         return 0;
102 }
103
104 static u32 iscsi_handle_authentication(
105         struct iscsi_conn *conn,
106         char *in_buf,
107         char *out_buf,
108         int in_length,
109         int *out_length,
110         unsigned char *authtype)
111 {
112         struct iscsi_session *sess = conn->sess;
113         struct iscsi_node_auth *auth;
114         struct iscsi_node_acl *iscsi_nacl;
115         struct iscsi_portal_group *iscsi_tpg;
116         struct se_node_acl *se_nacl;
117
118         if (!sess->sess_ops->SessionType) {
119                 /*
120                  * For SessionType=Normal
121                  */
122                 se_nacl = conn->sess->se_sess->se_node_acl;
123                 if (!se_nacl) {
124                         pr_err("Unable to locate struct se_node_acl for"
125                                         " CHAP auth\n");
126                         return -1;
127                 }
128                 iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
129                                 se_node_acl);
130                 if (!iscsi_nacl) {
131                         pr_err("Unable to locate struct iscsi_node_acl for"
132                                         " CHAP auth\n");
133                         return -1;
134                 }
135
136                 if (se_nacl->dynamic_node_acl) {
137                         iscsi_tpg = container_of(se_nacl->se_tpg,
138                                         struct iscsi_portal_group, tpg_se_tpg);
139
140                         auth = &iscsi_tpg->tpg_demo_auth;
141                 } else {
142                         iscsi_nacl = container_of(se_nacl, struct iscsi_node_acl,
143                                                   se_node_acl);
144
145                         auth = ISCSI_NODE_AUTH(iscsi_nacl);
146                 }
147         } else {
148                 /*
149                  * For SessionType=Discovery
150                  */
151                 auth = &iscsit_global->discovery_acl.node_auth;
152         }
153
154         if (strstr("CHAP", authtype))
155                 strcpy(conn->sess->auth_type, "CHAP");
156         else
157                 strcpy(conn->sess->auth_type, NONE);
158
159         if (strstr("None", authtype))
160                 return 1;
161 #ifdef CANSRP
162         else if (strstr("SRP", authtype))
163                 return srp_main_loop(conn, auth, in_buf, out_buf,
164                                 &in_length, out_length);
165 #endif
166         else if (strstr("CHAP", authtype))
167                 return chap_main_loop(conn, auth, in_buf, out_buf,
168                                 &in_length, out_length);
169         else if (strstr("SPKM1", authtype))
170                 return 2;
171         else if (strstr("SPKM2", authtype))
172                 return 2;
173         else if (strstr("KRB5", authtype))
174                 return 2;
175         else
176                 return 2;
177 }
178
179 static void iscsi_remove_failed_auth_entry(struct iscsi_conn *conn)
180 {
181         kfree(conn->auth_protocol);
182 }
183
184 int iscsi_target_check_login_request(
185         struct iscsi_conn *conn,
186         struct iscsi_login *login)
187 {
188         int req_csg, req_nsg;
189         u32 payload_length;
190         struct iscsi_login_req *login_req;
191
192         login_req = (struct iscsi_login_req *) login->req;
193         payload_length = ntoh24(login_req->dlength);
194
195         switch (login_req->opcode & ISCSI_OPCODE_MASK) {
196         case ISCSI_OP_LOGIN:
197                 break;
198         default:
199                 pr_err("Received unknown opcode 0x%02x.\n",
200                                 login_req->opcode & ISCSI_OPCODE_MASK);
201                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
202                                 ISCSI_LOGIN_STATUS_INIT_ERR);
203                 return -1;
204         }
205
206         if ((login_req->flags & ISCSI_FLAG_LOGIN_CONTINUE) &&
207             (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
208                 pr_err("Login request has both ISCSI_FLAG_LOGIN_CONTINUE"
209                         " and ISCSI_FLAG_LOGIN_TRANSIT set, protocol error.\n");
210                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
211                                 ISCSI_LOGIN_STATUS_INIT_ERR);
212                 return -1;
213         }
214
215         req_csg = ISCSI_LOGIN_CURRENT_STAGE(login_req->flags);
216         req_nsg = ISCSI_LOGIN_NEXT_STAGE(login_req->flags);
217
218         if (req_csg != login->current_stage) {
219                 pr_err("Initiator unexpectedly changed login stage"
220                         " from %d to %d, login failed.\n", login->current_stage,
221                         req_csg);
222                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
223                                 ISCSI_LOGIN_STATUS_INIT_ERR);
224                 return -1;
225         }
226
227         if ((req_nsg == 2) || (req_csg >= 2) ||
228            ((login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT) &&
229             (req_nsg <= req_csg))) {
230                 pr_err("Illegal login_req->flags Combination, CSG: %d,"
231                         " NSG: %d, ISCSI_FLAG_LOGIN_TRANSIT: %d.\n", req_csg,
232                         req_nsg, (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT));
233                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
234                                 ISCSI_LOGIN_STATUS_INIT_ERR);
235                 return -1;
236         }
237
238         if ((login_req->max_version != login->version_max) ||
239             (login_req->min_version != login->version_min)) {
240                 pr_err("Login request changed Version Max/Nin"
241                         " unexpectedly to 0x%02x/0x%02x, protocol error\n",
242                         login_req->max_version, login_req->min_version);
243                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
244                                 ISCSI_LOGIN_STATUS_INIT_ERR);
245                 return -1;
246         }
247
248         if (memcmp(login_req->isid, login->isid, 6) != 0) {
249                 pr_err("Login request changed ISID unexpectedly,"
250                                 " protocol error.\n");
251                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
252                                 ISCSI_LOGIN_STATUS_INIT_ERR);
253                 return -1;
254         }
255
256         if (login_req->itt != login->init_task_tag) {
257                 pr_err("Login request changed ITT unexpectedly to"
258                         " 0x%08x, protocol error.\n", login_req->itt);
259                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
260                                 ISCSI_LOGIN_STATUS_INIT_ERR);
261                 return -1;
262         }
263
264         if (payload_length > MAX_KEY_VALUE_PAIRS) {
265                 pr_err("Login request payload exceeds default"
266                         " MaxRecvDataSegmentLength: %u, protocol error.\n",
267                                 MAX_KEY_VALUE_PAIRS);
268                 return -1;
269         }
270
271         return 0;
272 }
273
274 static int iscsi_target_check_first_request(
275         struct iscsi_conn *conn,
276         struct iscsi_login *login)
277 {
278         struct iscsi_param *param = NULL;
279         struct se_node_acl *se_nacl;
280
281         login->first_request = 0;
282
283         list_for_each_entry(param, &conn->param_list->param_list, p_list) {
284                 if (!strncmp(param->name, SESSIONTYPE, 11)) {
285                         if (!IS_PSTATE_ACCEPTOR(param)) {
286                                 pr_err("SessionType key not received"
287                                         " in first login request.\n");
288                                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
289                                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
290                                 return -1;
291                         }
292                         if (!strncmp(param->value, DISCOVERY, 9))
293                                 return 0;
294                 }
295
296                 if (!strncmp(param->name, INITIATORNAME, 13)) {
297                         if (!IS_PSTATE_ACCEPTOR(param)) {
298                                 if (!login->leading_connection)
299                                         continue;
300
301                                 pr_err("InitiatorName key not received"
302                                         " in first login request.\n");
303                                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
304                                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
305                                 return -1;
306                         }
307
308                         /*
309                          * For non-leading connections, double check that the
310                          * received InitiatorName matches the existing session's
311                          * struct iscsi_node_acl.
312                          */
313                         if (!login->leading_connection) {
314                                 se_nacl = conn->sess->se_sess->se_node_acl;
315                                 if (!se_nacl) {
316                                         pr_err("Unable to locate"
317                                                 " struct se_node_acl\n");
318                                         iscsit_tx_login_rsp(conn,
319                                                         ISCSI_STATUS_CLS_INITIATOR_ERR,
320                                                         ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
321                                         return -1;
322                                 }
323
324                                 if (strcmp(param->value,
325                                                 se_nacl->initiatorname)) {
326                                         pr_err("Incorrect"
327                                                 " InitiatorName: %s for this"
328                                                 " iSCSI Initiator Node.\n",
329                                                 param->value);
330                                         iscsit_tx_login_rsp(conn,
331                                                         ISCSI_STATUS_CLS_INITIATOR_ERR,
332                                                         ISCSI_LOGIN_STATUS_TGT_NOT_FOUND);
333                                         return -1;
334                                 }
335                         }
336                 }
337         }
338
339         return 0;
340 }
341
342 static int iscsi_target_do_tx_login_io(struct iscsi_conn *conn, struct iscsi_login *login)
343 {
344         u32 padding = 0;
345         struct iscsi_session *sess = conn->sess;
346         struct iscsi_login_rsp *login_rsp;
347
348         login_rsp = (struct iscsi_login_rsp *) login->rsp;
349
350         login_rsp->opcode               = ISCSI_OP_LOGIN_RSP;
351         hton24(login_rsp->dlength, login->rsp_length);
352         memcpy(login_rsp->isid, login->isid, 6);
353         login_rsp->tsih                 = cpu_to_be16(login->tsih);
354         login_rsp->itt                  = login->init_task_tag;
355         login_rsp->statsn               = cpu_to_be32(conn->stat_sn++);
356         login_rsp->exp_cmdsn            = cpu_to_be32(conn->sess->exp_cmd_sn);
357         login_rsp->max_cmdsn            = cpu_to_be32(conn->sess->max_cmd_sn);
358
359         pr_debug("Sending Login Response, Flags: 0x%02x, ITT: 0x%08x,"
360                 " ExpCmdSN; 0x%08x, MaxCmdSN: 0x%08x, StatSN: 0x%08x, Length:"
361                 " %u\n", login_rsp->flags, (__force u32)login_rsp->itt,
362                 ntohl(login_rsp->exp_cmdsn), ntohl(login_rsp->max_cmdsn),
363                 ntohl(login_rsp->statsn), login->rsp_length);
364
365         padding = ((-login->rsp_length) & 3);
366
367         if (conn->conn_transport->iscsit_put_login_tx(conn, login,
368                                         login->rsp_length + padding) < 0)
369                 return -1;
370
371         login->rsp_length               = 0;
372         mutex_lock(&sess->cmdsn_mutex);
373         login_rsp->exp_cmdsn            = cpu_to_be32(sess->exp_cmd_sn);
374         login_rsp->max_cmdsn            = cpu_to_be32(sess->max_cmd_sn);
375         mutex_unlock(&sess->cmdsn_mutex);
376
377         return 0;
378 }
379
380 static void iscsi_target_sk_data_ready(struct sock *sk, int count)
381 {
382         struct iscsi_conn *conn = sk->sk_user_data;
383         bool rc;
384
385         pr_debug("Entering iscsi_target_sk_data_ready: conn: %p\n", conn);
386
387         write_lock_bh(&sk->sk_callback_lock);
388         if (!sk->sk_user_data) {
389                 write_unlock_bh(&sk->sk_callback_lock);
390                 return;
391         }
392         if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
393                 write_unlock_bh(&sk->sk_callback_lock);
394                 pr_debug("Got LOGIN_FLAGS_READY=0, conn: %p >>>>\n", conn);
395                 return;
396         }
397         if (test_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
398                 write_unlock_bh(&sk->sk_callback_lock);
399                 pr_debug("Got LOGIN_FLAGS_CLOSED=1, conn: %p >>>>\n", conn);
400                 return;
401         }
402         if (test_and_set_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
403                 write_unlock_bh(&sk->sk_callback_lock);
404                 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1, conn: %p >>>>\n", conn);
405                 return;
406         }
407
408         rc = schedule_delayed_work(&conn->login_work, 0);
409         if (rc == false) {
410                 pr_debug("iscsi_target_sk_data_ready, schedule_delayed_work"
411                          " got false\n");
412         }
413         write_unlock_bh(&sk->sk_callback_lock);
414 }
415
416 static void iscsi_target_sk_state_change(struct sock *);
417
418 static void iscsi_target_set_sock_callbacks(struct iscsi_conn *conn)
419 {
420         struct sock *sk;
421
422         if (!conn->sock)
423                 return;
424
425         sk = conn->sock->sk;
426         pr_debug("Entering iscsi_target_set_sock_callbacks: conn: %p\n", conn);
427
428         write_lock_bh(&sk->sk_callback_lock);
429         sk->sk_user_data = conn;
430         conn->orig_data_ready = sk->sk_data_ready;
431         conn->orig_state_change = sk->sk_state_change;
432         sk->sk_data_ready = iscsi_target_sk_data_ready;
433         sk->sk_state_change = iscsi_target_sk_state_change;
434         write_unlock_bh(&sk->sk_callback_lock);
435
436         sk->sk_sndtimeo = TA_LOGIN_TIMEOUT * HZ;
437         sk->sk_rcvtimeo = TA_LOGIN_TIMEOUT * HZ;
438 }
439
440 static void iscsi_target_restore_sock_callbacks(struct iscsi_conn *conn)
441 {
442         struct sock *sk;
443
444         if (!conn->sock)
445                 return;
446
447         sk = conn->sock->sk;
448         pr_debug("Entering iscsi_target_restore_sock_callbacks: conn: %p\n", conn);
449
450         write_lock_bh(&sk->sk_callback_lock);
451         if (!sk->sk_user_data) {
452                 write_unlock_bh(&sk->sk_callback_lock);
453                 return;
454         }
455         sk->sk_user_data = NULL;
456         sk->sk_data_ready = conn->orig_data_ready;
457         sk->sk_state_change = conn->orig_state_change;
458         write_unlock_bh(&sk->sk_callback_lock);
459
460         sk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
461         sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
462 }
463
464 static int iscsi_target_do_login(struct iscsi_conn *, struct iscsi_login *);
465
466 static bool iscsi_target_sk_state_check(struct sock *sk)
467 {
468         if (sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) {
469                 pr_debug("iscsi_target_sk_state_check: TCP_CLOSE_WAIT|TCP_CLOSE,"
470                         "returning FALSE\n");
471                 return false;
472         }
473         return true;
474 }
475
476 static void iscsi_target_login_drop(struct iscsi_conn *conn, struct iscsi_login *login)
477 {
478         struct iscsi_np *np = login->np;
479         bool zero_tsih = login->zero_tsih;
480
481         iscsi_remove_failed_auth_entry(conn);
482         iscsi_target_nego_release(conn);
483         iscsi_target_login_sess_out(conn, np, zero_tsih, true);
484 }
485
486 static void iscsi_target_login_timeout(unsigned long data)
487 {
488         struct iscsi_conn *conn = (struct iscsi_conn *)data;
489
490         pr_debug("Entering iscsi_target_login_timeout >>>>>>>>>>>>>>>>>>>\n");
491
492         if (conn->login_kworker) {
493                 pr_debug("Sending SIGINT to conn->login_kworker %s/%d\n",
494                          conn->login_kworker->comm, conn->login_kworker->pid);
495                 send_sig(SIGINT, conn->login_kworker, 1);
496         }
497 }
498
499 static void iscsi_target_do_login_rx(struct work_struct *work)
500 {
501         struct iscsi_conn *conn = container_of(work,
502                                 struct iscsi_conn, login_work.work);
503         struct iscsi_login *login = conn->login;
504         struct iscsi_np *np = login->np;
505         struct iscsi_portal_group *tpg = conn->tpg;
506         struct iscsi_tpg_np *tpg_np = conn->tpg_np;
507         struct timer_list login_timer;
508         int rc, zero_tsih = login->zero_tsih;
509         bool state;
510
511         pr_debug("entering iscsi_target_do_login_rx, conn: %p, %s:%d\n",
512                         conn, current->comm, current->pid);
513
514         spin_lock(&tpg->tpg_state_lock);
515         state = (tpg->tpg_state == TPG_STATE_ACTIVE);
516         spin_unlock(&tpg->tpg_state_lock);
517
518         if (state == false) {
519                 pr_debug("iscsi_target_do_login_rx: tpg_state != TPG_STATE_ACTIVE\n");
520                 iscsi_target_restore_sock_callbacks(conn);
521                 iscsi_target_login_drop(conn, login);
522                 iscsit_deaccess_np(np, tpg, tpg_np);
523                 return;
524         }
525
526         if (conn->sock) {
527                 struct sock *sk = conn->sock->sk;
528
529                 read_lock_bh(&sk->sk_callback_lock);
530                 state = iscsi_target_sk_state_check(sk);
531                 read_unlock_bh(&sk->sk_callback_lock);
532
533                 if (state == false) {
534                         pr_debug("iscsi_target_do_login_rx, TCP state CLOSE\n");
535                         iscsi_target_restore_sock_callbacks(conn);
536                         iscsi_target_login_drop(conn, login);
537                         iscsit_deaccess_np(np, tpg, tpg_np);
538                         return;
539                 }
540         }
541
542         conn->login_kworker = current;
543         allow_signal(SIGINT);
544
545         init_timer(&login_timer);
546         login_timer.expires = (get_jiffies_64() + TA_LOGIN_TIMEOUT * HZ);
547         login_timer.data = (unsigned long)conn;
548         login_timer.function = iscsi_target_login_timeout;
549         add_timer(&login_timer);
550         pr_debug("Starting login_timer for %s/%d\n", current->comm, current->pid);
551
552         rc = conn->conn_transport->iscsit_get_login_rx(conn, login);
553         del_timer_sync(&login_timer);
554         flush_signals(current);
555         conn->login_kworker = NULL;
556
557         if (rc < 0) {
558                 iscsi_target_restore_sock_callbacks(conn);
559                 iscsi_target_login_drop(conn, login);
560                 iscsit_deaccess_np(np, tpg, tpg_np);
561                 return;
562         }
563
564         pr_debug("iscsi_target_do_login_rx after rx_login_io, %p, %s:%d\n",
565                         conn, current->comm, current->pid);
566
567         rc = iscsi_target_do_login(conn, login);
568         if (rc < 0) {
569                 iscsi_target_restore_sock_callbacks(conn);
570                 iscsi_target_login_drop(conn, login);
571                 iscsit_deaccess_np(np, tpg, tpg_np);
572         } else if (!rc) {
573                 if (conn->sock) {
574                         struct sock *sk = conn->sock->sk;
575
576                         write_lock_bh(&sk->sk_callback_lock);
577                         clear_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags);
578                         write_unlock_bh(&sk->sk_callback_lock);
579                 }
580         } else if (rc == 1) {
581                 iscsi_target_nego_release(conn);
582                 iscsi_post_login_handler(np, conn, zero_tsih);
583                 iscsit_deaccess_np(np, tpg, tpg_np);
584         }
585 }
586
587 static void iscsi_target_do_cleanup(struct work_struct *work)
588 {
589         struct iscsi_conn *conn = container_of(work,
590                                 struct iscsi_conn, login_cleanup_work.work);
591         struct sock *sk = conn->sock->sk;
592         struct iscsi_login *login = conn->login;
593         struct iscsi_np *np = login->np;
594         struct iscsi_portal_group *tpg = conn->tpg;
595         struct iscsi_tpg_np *tpg_np = conn->tpg_np;
596
597         pr_debug("Entering iscsi_target_do_cleanup\n");
598
599         cancel_delayed_work_sync(&conn->login_work);
600         conn->orig_state_change(sk);
601
602         iscsi_target_restore_sock_callbacks(conn);
603         iscsi_target_login_drop(conn, login);
604         iscsit_deaccess_np(np, tpg, tpg_np);
605
606         pr_debug("iscsi_target_do_cleanup done()\n");
607 }
608
609 static void iscsi_target_sk_state_change(struct sock *sk)
610 {
611         struct iscsi_conn *conn;
612         void (*orig_state_change)(struct sock *);
613         bool state;
614
615         pr_debug("Entering iscsi_target_sk_state_change\n");
616
617         write_lock_bh(&sk->sk_callback_lock);
618         conn = sk->sk_user_data;
619         if (!conn) {
620                 write_unlock_bh(&sk->sk_callback_lock);
621                 return;
622         }
623         orig_state_change = conn->orig_state_change;
624
625         if (!test_bit(LOGIN_FLAGS_READY, &conn->login_flags)) {
626                 pr_debug("Got LOGIN_FLAGS_READY=0 sk_state_change conn: %p\n",
627                          conn);
628                 write_unlock_bh(&sk->sk_callback_lock);
629                 orig_state_change(sk);
630                 return;
631         }
632         if (test_bit(LOGIN_FLAGS_READ_ACTIVE, &conn->login_flags)) {
633                 pr_debug("Got LOGIN_FLAGS_READ_ACTIVE=1 sk_state_change"
634                          " conn: %p\n", conn);
635                 write_unlock_bh(&sk->sk_callback_lock);
636                 orig_state_change(sk);
637                 return;
638         }
639         if (test_and_set_bit(LOGIN_FLAGS_CLOSED, &conn->login_flags)) {
640                 pr_debug("Got LOGIN_FLAGS_CLOSED=1 sk_state_change conn: %p\n",
641                          conn);
642                 write_unlock_bh(&sk->sk_callback_lock);
643                 orig_state_change(sk);
644                 return;
645         }
646
647         state = iscsi_target_sk_state_check(sk);
648         write_unlock_bh(&sk->sk_callback_lock);
649
650         pr_debug("iscsi_target_sk_state_change: state: %d\n", state);
651
652         if (!state) {
653                 pr_debug("iscsi_target_sk_state_change got failed state\n");
654                 schedule_delayed_work(&conn->login_cleanup_work, 0);
655                 return;
656         }
657         orig_state_change(sk);
658 }
659
660 /*
661  *      NOTE: We check for existing sessions or connections AFTER the initiator
662  *      has been successfully authenticated in order to protect against faked
663  *      ISID/TSIH combinations.
664  */
665 static int iscsi_target_check_for_existing_instances(
666         struct iscsi_conn *conn,
667         struct iscsi_login *login)
668 {
669         if (login->checked_for_existing)
670                 return 0;
671
672         login->checked_for_existing = 1;
673
674         if (!login->tsih)
675                 return iscsi_check_for_session_reinstatement(conn);
676         else
677                 return iscsi_login_post_auth_non_zero_tsih(conn, login->cid,
678                                 login->initial_exp_statsn);
679 }
680
681 static int iscsi_target_do_authentication(
682         struct iscsi_conn *conn,
683         struct iscsi_login *login)
684 {
685         int authret;
686         u32 payload_length;
687         struct iscsi_param *param;
688         struct iscsi_login_req *login_req;
689         struct iscsi_login_rsp *login_rsp;
690
691         login_req = (struct iscsi_login_req *) login->req;
692         login_rsp = (struct iscsi_login_rsp *) login->rsp;
693         payload_length = ntoh24(login_req->dlength);
694
695         param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
696         if (!param)
697                 return -1;
698
699         authret = iscsi_handle_authentication(
700                         conn,
701                         login->req_buf,
702                         login->rsp_buf,
703                         payload_length,
704                         &login->rsp_length,
705                         param->value);
706         switch (authret) {
707         case 0:
708                 pr_debug("Received OK response"
709                 " from LIO Authentication, continuing.\n");
710                 break;
711         case 1:
712                 pr_debug("iSCSI security negotiation"
713                         " completed successfully.\n");
714                 login->auth_complete = 1;
715                 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
716                     (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
717                         login_rsp->flags |= (ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
718                                              ISCSI_FLAG_LOGIN_TRANSIT);
719                         login->current_stage = 1;
720                 }
721                 return iscsi_target_check_for_existing_instances(
722                                 conn, login);
723         case 2:
724                 pr_err("Security negotiation"
725                         " failed.\n");
726                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
727                                 ISCSI_LOGIN_STATUS_AUTH_FAILED);
728                 return -1;
729         default:
730                 pr_err("Received unknown error %d from LIO"
731                                 " Authentication\n", authret);
732                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
733                                 ISCSI_LOGIN_STATUS_TARGET_ERROR);
734                 return -1;
735         }
736
737         return 0;
738 }
739
740 static int iscsi_target_handle_csg_zero(
741         struct iscsi_conn *conn,
742         struct iscsi_login *login)
743 {
744         int ret;
745         u32 payload_length;
746         struct iscsi_param *param;
747         struct iscsi_login_req *login_req;
748         struct iscsi_login_rsp *login_rsp;
749
750         login_req = (struct iscsi_login_req *) login->req;
751         login_rsp = (struct iscsi_login_rsp *) login->rsp;
752         payload_length = ntoh24(login_req->dlength);
753
754         param = iscsi_find_param_from_key(AUTHMETHOD, conn->param_list);
755         if (!param)
756                 return -1;
757
758         ret = iscsi_decode_text_input(
759                         PHASE_SECURITY|PHASE_DECLARATIVE,
760                         SENDER_INITIATOR|SENDER_RECEIVER,
761                         login->req_buf,
762                         payload_length,
763                         conn);
764         if (ret < 0)
765                 return -1;
766
767         if (ret > 0) {
768                 if (login->auth_complete) {
769                         pr_err("Initiator has already been"
770                                 " successfully authenticated, but is still"
771                                 " sending %s keys.\n", param->value);
772                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
773                                         ISCSI_LOGIN_STATUS_INIT_ERR);
774                         return -1;
775                 }
776
777                 goto do_auth;
778         }
779
780         if (login->first_request)
781                 if (iscsi_target_check_first_request(conn, login) < 0)
782                         return -1;
783
784         ret = iscsi_encode_text_output(
785                         PHASE_SECURITY|PHASE_DECLARATIVE,
786                         SENDER_TARGET,
787                         login->rsp_buf,
788                         &login->rsp_length,
789                         conn->param_list);
790         if (ret < 0)
791                 return -1;
792
793         if (!iscsi_check_negotiated_keys(conn->param_list)) {
794                 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
795                     !strncmp(param->value, NONE, 4)) {
796                         pr_err("Initiator sent AuthMethod=None but"
797                                 " Target is enforcing iSCSI Authentication,"
798                                         " login failed.\n");
799                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
800                                         ISCSI_LOGIN_STATUS_AUTH_FAILED);
801                         return -1;
802                 }
803
804                 if (ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication &&
805                     !login->auth_complete)
806                         return 0;
807
808                 if (strncmp(param->value, NONE, 4) && !login->auth_complete)
809                         return 0;
810
811                 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE1) &&
812                     (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT)) {
813                         login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE1 |
814                                             ISCSI_FLAG_LOGIN_TRANSIT;
815                         login->current_stage = 1;
816                 }
817         }
818
819         return 0;
820 do_auth:
821         return iscsi_target_do_authentication(conn, login);
822 }
823
824 static int iscsi_target_handle_csg_one(struct iscsi_conn *conn, struct iscsi_login *login)
825 {
826         int ret;
827         u32 payload_length;
828         struct iscsi_login_req *login_req;
829         struct iscsi_login_rsp *login_rsp;
830
831         login_req = (struct iscsi_login_req *) login->req;
832         login_rsp = (struct iscsi_login_rsp *) login->rsp;
833         payload_length = ntoh24(login_req->dlength);
834
835         ret = iscsi_decode_text_input(
836                         PHASE_OPERATIONAL|PHASE_DECLARATIVE,
837                         SENDER_INITIATOR|SENDER_RECEIVER,
838                         login->req_buf,
839                         payload_length,
840                         conn);
841         if (ret < 0) {
842                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
843                                 ISCSI_LOGIN_STATUS_INIT_ERR);
844                 return -1;
845         }
846
847         if (login->first_request)
848                 if (iscsi_target_check_first_request(conn, login) < 0)
849                         return -1;
850
851         if (iscsi_target_check_for_existing_instances(conn, login) < 0)
852                 return -1;
853
854         ret = iscsi_encode_text_output(
855                         PHASE_OPERATIONAL|PHASE_DECLARATIVE,
856                         SENDER_TARGET,
857                         login->rsp_buf,
858                         &login->rsp_length,
859                         conn->param_list);
860         if (ret < 0) {
861                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
862                                 ISCSI_LOGIN_STATUS_INIT_ERR);
863                 return -1;
864         }
865
866         if (!login->auth_complete &&
867              ISCSI_TPG_ATTRIB(ISCSI_TPG_C(conn))->authentication) {
868                 pr_err("Initiator is requesting CSG: 1, has not been"
869                          " successfully authenticated, and the Target is"
870                         " enforcing iSCSI Authentication, login failed.\n");
871                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
872                                 ISCSI_LOGIN_STATUS_AUTH_FAILED);
873                 return -1;
874         }
875
876         if (!iscsi_check_negotiated_keys(conn->param_list))
877                 if ((login_req->flags & ISCSI_FLAG_LOGIN_NEXT_STAGE3) &&
878                     (login_req->flags & ISCSI_FLAG_LOGIN_TRANSIT))
879                         login_rsp->flags |= ISCSI_FLAG_LOGIN_NEXT_STAGE3 |
880                                             ISCSI_FLAG_LOGIN_TRANSIT;
881
882         return 0;
883 }
884
885 static int iscsi_target_do_login(struct iscsi_conn *conn, struct iscsi_login *login)
886 {
887         int pdu_count = 0;
888         struct iscsi_login_req *login_req;
889         struct iscsi_login_rsp *login_rsp;
890
891         login_req = (struct iscsi_login_req *) login->req;
892         login_rsp = (struct iscsi_login_rsp *) login->rsp;
893
894         while (1) {
895                 if (++pdu_count > MAX_LOGIN_PDUS) {
896                         pr_err("MAX_LOGIN_PDUS count reached.\n");
897                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
898                                         ISCSI_LOGIN_STATUS_TARGET_ERROR);
899                         return -1;
900                 }
901
902                 switch (ISCSI_LOGIN_CURRENT_STAGE(login_req->flags)) {
903                 case 0:
904                         login_rsp->flags &= ~ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK;
905                         if (iscsi_target_handle_csg_zero(conn, login) < 0)
906                                 return -1;
907                         break;
908                 case 1:
909                         login_rsp->flags |= ISCSI_FLAG_LOGIN_CURRENT_STAGE1;
910                         if (iscsi_target_handle_csg_one(conn, login) < 0)
911                                 return -1;
912                         if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
913                                 login->tsih = conn->sess->tsih;
914                                 login->login_complete = 1;
915                                 iscsi_target_restore_sock_callbacks(conn);
916                                 if (iscsi_target_do_tx_login_io(conn,
917                                                 login) < 0)
918                                         return -1;
919                                 return 1;
920                         }
921                         break;
922                 default:
923                         pr_err("Illegal CSG: %d received from"
924                                 " Initiator, protocol error.\n",
925                                 ISCSI_LOGIN_CURRENT_STAGE(login_req->flags));
926                         break;
927                 }
928
929                 if (iscsi_target_do_tx_login_io(conn, login) < 0)
930                         return -1;
931
932                 if (login_rsp->flags & ISCSI_FLAG_LOGIN_TRANSIT) {
933                         login_rsp->flags &= ~ISCSI_FLAG_LOGIN_TRANSIT;
934                         login_rsp->flags &= ~ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK;
935                 }
936                 break;
937         }
938
939         if (conn->sock) {
940                 struct sock *sk = conn->sock->sk;
941                 bool state;
942
943                 read_lock_bh(&sk->sk_callback_lock);
944                 state = iscsi_target_sk_state_check(sk);
945                 read_unlock_bh(&sk->sk_callback_lock);
946
947                 if (!state) {
948                         pr_debug("iscsi_target_do_login() failed state for"
949                                  " conn: %p\n", conn);
950                         return -1;
951                 }
952         }
953
954         return 0;
955 }
956
957 static void iscsi_initiatorname_tolower(
958         char *param_buf)
959 {
960         char *c;
961         u32 iqn_size = strlen(param_buf), i;
962
963         for (i = 0; i < iqn_size; i++) {
964                 c = &param_buf[i];
965                 if (!isupper(*c))
966                         continue;
967
968                 *c = tolower(*c);
969         }
970 }
971
972 /*
973  * Processes the first Login Request..
974  */
975 int iscsi_target_locate_portal(
976         struct iscsi_np *np,
977         struct iscsi_conn *conn,
978         struct iscsi_login *login)
979 {
980         char *i_buf = NULL, *s_buf = NULL, *t_buf = NULL;
981         char *tmpbuf, *start = NULL, *end = NULL, *key, *value;
982         struct iscsi_session *sess = conn->sess;
983         struct iscsi_tiqn *tiqn;
984         struct iscsi_tpg_np *tpg_np = NULL;
985         struct iscsi_login_req *login_req;
986         u32 payload_length;
987         int sessiontype = 0, ret = 0;
988
989         INIT_DELAYED_WORK(&conn->login_work, iscsi_target_do_login_rx);
990         INIT_DELAYED_WORK(&conn->login_cleanup_work, iscsi_target_do_cleanup);
991         iscsi_target_set_sock_callbacks(conn);
992
993         login->np = np;
994
995         login_req = (struct iscsi_login_req *) login->req;
996         payload_length = ntoh24(login_req->dlength);
997
998         tmpbuf = kzalloc(payload_length + 1, GFP_KERNEL);
999         if (!tmpbuf) {
1000                 pr_err("Unable to allocate memory for tmpbuf.\n");
1001                 return -1;
1002         }
1003
1004         memcpy(tmpbuf, login->req_buf, payload_length);
1005         tmpbuf[payload_length] = '\0';
1006         start = tmpbuf;
1007         end = (start + payload_length);
1008
1009         /*
1010          * Locate the initial keys expected from the Initiator node in
1011          * the first login request in order to progress with the login phase.
1012          */
1013         while (start < end) {
1014                 if (iscsi_extract_key_value(start, &key, &value) < 0) {
1015                         ret = -1;
1016                         goto out;
1017                 }
1018
1019                 if (!strncmp(key, "InitiatorName", 13))
1020                         i_buf = value;
1021                 else if (!strncmp(key, "SessionType", 11))
1022                         s_buf = value;
1023                 else if (!strncmp(key, "TargetName", 10))
1024                         t_buf = value;
1025
1026                 start += strlen(key) + strlen(value) + 2;
1027         }
1028         /*
1029          * See 5.3.  Login Phase.
1030          */
1031         if (!i_buf) {
1032                 pr_err("InitiatorName key not received"
1033                         " in first login request.\n");
1034                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1035                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1036                 ret = -1;
1037                 goto out;
1038         }
1039         /*
1040          * Convert the incoming InitiatorName to lowercase following
1041          * RFC-3720 3.2.6.1. section c) that says that iSCSI IQNs
1042          * are NOT case sensitive.
1043          */
1044         iscsi_initiatorname_tolower(i_buf);
1045
1046         if (!s_buf) {
1047                 if (!login->leading_connection)
1048                         goto get_target;
1049
1050                 pr_err("SessionType key not received"
1051                         " in first login request.\n");
1052                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1053                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1054                 ret = -1;
1055                 goto out;
1056         }
1057
1058         /*
1059          * Use default portal group for discovery sessions.
1060          */
1061         sessiontype = strncmp(s_buf, DISCOVERY, 9);
1062         if (!sessiontype) {
1063                 conn->tpg = iscsit_global->discovery_tpg;
1064                 if (!login->leading_connection)
1065                         goto get_target;
1066
1067                 sess->sess_ops->SessionType = 1;
1068                 /*
1069                  * Setup crc32c modules from libcrypto
1070                  */
1071                 if (iscsi_login_setup_crypto(conn) < 0) {
1072                         pr_err("iscsi_login_setup_crypto() failed\n");
1073                         ret = -1;
1074                         goto out;
1075                 }
1076                 /*
1077                  * Serialize access across the discovery struct iscsi_portal_group to
1078                  * process login attempt.
1079                  */
1080                 if (iscsit_access_np(np, conn->tpg) < 0) {
1081                         iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1082                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1083                         ret = -1;
1084                         goto out;
1085                 }
1086                 ret = 0;
1087                 goto out;
1088         }
1089
1090 get_target:
1091         if (!t_buf) {
1092                 pr_err("TargetName key not received"
1093                         " in first login request while"
1094                         " SessionType=Normal.\n");
1095                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1096                         ISCSI_LOGIN_STATUS_MISSING_FIELDS);
1097                 ret = -1;
1098                 goto out;
1099         }
1100
1101         /*
1102          * Locate Target IQN from Storage Node.
1103          */
1104         tiqn = iscsit_get_tiqn_for_login(t_buf);
1105         if (!tiqn) {
1106                 pr_err("Unable to locate Target IQN: %s in"
1107                         " Storage Node\n", t_buf);
1108                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1109                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1110                 ret = -1;
1111                 goto out;
1112         }
1113         pr_debug("Located Storage Object: %s\n", tiqn->tiqn);
1114
1115         /*
1116          * Locate Target Portal Group from Storage Node.
1117          */
1118         conn->tpg = iscsit_get_tpg_from_np(tiqn, np, &tpg_np);
1119         if (!conn->tpg) {
1120                 pr_err("Unable to locate Target Portal Group"
1121                                 " on %s\n", tiqn->tiqn);
1122                 iscsit_put_tiqn_for_login(tiqn);
1123                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1124                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1125                 ret = -1;
1126                 goto out;
1127         }
1128         conn->tpg_np = tpg_np;
1129         pr_debug("Located Portal Group Object: %hu\n", conn->tpg->tpgt);
1130         /*
1131          * Setup crc32c modules from libcrypto
1132          */
1133         if (iscsi_login_setup_crypto(conn) < 0) {
1134                 pr_err("iscsi_login_setup_crypto() failed\n");
1135                 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1136                 iscsit_put_tiqn_for_login(tiqn);
1137                 conn->tpg = NULL;
1138                 ret = -1;
1139                 goto out;
1140         }
1141         /*
1142          * Serialize access across the struct iscsi_portal_group to
1143          * process login attempt.
1144          */
1145         if (iscsit_access_np(np, conn->tpg) < 0) {
1146                 kref_put(&tpg_np->tpg_np_kref, iscsit_login_kref_put);
1147                 iscsit_put_tiqn_for_login(tiqn);
1148                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_TARGET_ERR,
1149                                 ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE);
1150                 conn->tpg = NULL;
1151                 ret = -1;
1152                 goto out;
1153         }
1154
1155         /*
1156          * conn->sess->node_acl will be set when the referenced
1157          * struct iscsi_session is located from received ISID+TSIH in
1158          * iscsi_login_non_zero_tsih_s2().
1159          */
1160         if (!login->leading_connection) {
1161                 ret = 0;
1162                 goto out;
1163         }
1164
1165         /*
1166          * This value is required in iscsi_login_zero_tsih_s2()
1167          */
1168         sess->sess_ops->SessionType = 0;
1169
1170         /*
1171          * Locate incoming Initiator IQN reference from Storage Node.
1172          */
1173         sess->se_sess->se_node_acl = core_tpg_check_initiator_node_acl(
1174                         &conn->tpg->tpg_se_tpg, i_buf);
1175         if (!sess->se_sess->se_node_acl) {
1176                 pr_err("iSCSI Initiator Node: %s is not authorized to"
1177                         " access iSCSI target portal group: %hu.\n",
1178                                 i_buf, conn->tpg->tpgt);
1179                 iscsit_tx_login_rsp(conn, ISCSI_STATUS_CLS_INITIATOR_ERR,
1180                                 ISCSI_LOGIN_STATUS_TGT_FORBIDDEN);
1181                 ret = -1;
1182                 goto out;
1183         }
1184
1185         ret = 0;
1186 out:
1187         kfree(tmpbuf);
1188         return ret;
1189 }
1190
1191 int iscsi_target_start_negotiation(
1192         struct iscsi_login *login,
1193         struct iscsi_conn *conn)
1194 {
1195         int ret;
1196
1197         ret = iscsi_target_do_login(conn, login);
1198         if (!ret) {
1199                 if (conn->sock) {
1200                         struct sock *sk = conn->sock->sk;
1201
1202                         write_lock_bh(&sk->sk_callback_lock);
1203                         set_bit(LOGIN_FLAGS_READY, &conn->login_flags);
1204                         write_unlock_bh(&sk->sk_callback_lock);
1205                 }
1206         } else if (ret < 0) {
1207                 cancel_delayed_work_sync(&conn->login_work);
1208                 cancel_delayed_work_sync(&conn->login_cleanup_work);
1209                 iscsi_target_restore_sock_callbacks(conn);
1210                 iscsi_remove_failed_auth_entry(conn);
1211         }
1212         if (ret != 0)
1213                 iscsi_target_nego_release(conn);
1214
1215         return ret;
1216 }
1217
1218 void iscsi_target_nego_release(struct iscsi_conn *conn)
1219 {
1220         struct iscsi_login *login = conn->conn_login;
1221
1222         if (!login)
1223                 return;
1224
1225         kfree(login->req_buf);
1226         kfree(login->rsp_buf);
1227         kfree(login);
1228
1229         conn->conn_login = NULL;
1230 }