]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ctld/login.c
Merge content currently under test from ^/vendor/NetBSD/tests/dist/@r312123
[FreeBSD/FreeBSD.git] / usr.sbin / ctld / login.c
1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Edward Tomasz Napierala under sponsorship
6  * from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <assert.h>
35 #include <stdbool.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <netinet/in.h>
40
41 #include "ctld.h"
42 #include "iscsi_proto.h"
43
44 static void login_send_error(struct pdu *request,
45     char class, char detail);
46
47 static void
48 login_set_nsg(struct pdu *response, int nsg)
49 {
50         struct iscsi_bhs_login_response *bhslr;
51
52         assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
53             nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
54             nsg == BHSLR_STAGE_FULL_FEATURE_PHASE);
55
56         bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
57
58         bhslr->bhslr_flags &= 0xFC;
59         bhslr->bhslr_flags |= nsg;
60         bhslr->bhslr_flags |= BHSLR_FLAGS_TRANSIT;
61 }
62
63 static int
64 login_csg(const struct pdu *request)
65 {
66         struct iscsi_bhs_login_request *bhslr;
67
68         bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
69
70         return ((bhslr->bhslr_flags & 0x0C) >> 2);
71 }
72
73 static void
74 login_set_csg(struct pdu *response, int csg)
75 {
76         struct iscsi_bhs_login_response *bhslr;
77
78         assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION ||
79             csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION ||
80             csg == BHSLR_STAGE_FULL_FEATURE_PHASE);
81
82         bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs;
83
84         bhslr->bhslr_flags &= 0xF3;
85         bhslr->bhslr_flags |= csg << 2;
86 }
87
88 static struct pdu *
89 login_receive(struct connection *conn, bool initial)
90 {
91         struct pdu *request;
92         struct iscsi_bhs_login_request *bhslr;
93
94         request = pdu_new(conn);
95         pdu_receive(request);
96         if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
97             ISCSI_BHS_OPCODE_LOGIN_REQUEST) {
98                 /*
99                  * The first PDU in session is special - if we receive any PDU
100                  * different than login request, we have to drop the connection
101                  * without sending response ("A target receiving any PDU
102                  * except a Login request before the Login Phase is started MUST
103                  * immediately terminate the connection on which the PDU
104                  * was received.")
105                  */
106                 if (initial == false)
107                         login_send_error(request, 0x02, 0x0b);
108                 log_errx(1, "protocol error: received invalid opcode 0x%x",
109                     request->pdu_bhs->bhs_opcode);
110         }
111         bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
112         /*
113          * XXX: Implement the C flag some day.
114          */
115         if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) {
116                 login_send_error(request, 0x03, 0x00);
117                 log_errx(1, "received Login PDU with unsupported \"C\" flag");
118         }
119         if (bhslr->bhslr_version_max != 0x00) {
120                 login_send_error(request, 0x02, 0x05);
121                 log_errx(1, "received Login PDU with unsupported "
122                     "Version-max 0x%x", bhslr->bhslr_version_max);
123         }
124         if (bhslr->bhslr_version_min != 0x00) {
125                 login_send_error(request, 0x02, 0x05);
126                 log_errx(1, "received Login PDU with unsupported "
127                     "Version-min 0x%x", bhslr->bhslr_version_min);
128         }
129         if (initial == false &&
130             ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
131                 login_send_error(request, 0x02, 0x00);
132                 log_errx(1, "received Login PDU with decreasing CmdSN: "
133                     "was %u, is %u", conn->conn_cmdsn,
134                     ntohl(bhslr->bhslr_cmdsn));
135         }
136         if (initial == false &&
137             ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
138                 login_send_error(request, 0x02, 0x00);
139                 log_errx(1, "received Login PDU with wrong ExpStatSN: "
140                     "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
141                     conn->conn_statsn);
142         }
143         conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
144
145         return (request);
146 }
147
148 static struct pdu *
149 login_new_response(struct pdu *request)
150 {
151         struct pdu *response;
152         struct connection *conn;
153         struct iscsi_bhs_login_request *bhslr;
154         struct iscsi_bhs_login_response *bhslr2;
155
156         bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
157         conn = request->pdu_connection;
158
159         response = pdu_new_response(request);
160         bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
161         bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE;
162         login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION);
163         memcpy(bhslr2->bhslr_isid,
164             bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid));
165         bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
166         bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
167         bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
168         bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
169
170         return (response);
171 }
172
173 static void
174 login_send_error(struct pdu *request, char class, char detail)
175 {
176         struct pdu *response;
177         struct iscsi_bhs_login_response *bhslr2;
178
179         log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; "
180             "see next line for reason", class, detail);
181         response = login_new_response(request);
182         bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
183         bhslr2->bhslr_status_class = class;
184         bhslr2->bhslr_status_detail = detail;
185
186         pdu_send(response);
187         pdu_delete(response);
188 }
189
190 static int
191 login_list_contains(const char *list, const char *what)
192 {
193         char *tofree, *str, *token;
194
195         tofree = str = checked_strdup(list);
196
197         while ((token = strsep(&str, ",")) != NULL) {
198                 if (strcmp(token, what) == 0) {
199                         free(tofree);
200                         return (1);
201                 }
202         }
203         free(tofree);
204         return (0);
205 }
206
207 static int
208 login_list_prefers(const char *list,
209     const char *choice1, const char *choice2)
210 {
211         char *tofree, *str, *token;
212
213         tofree = str = checked_strdup(list);
214
215         while ((token = strsep(&str, ",")) != NULL) {
216                 if (strcmp(token, choice1) == 0) {
217                         free(tofree);
218                         return (1);
219                 }
220                 if (strcmp(token, choice2) == 0) {
221                         free(tofree);
222                         return (2);
223                 }
224         }
225         free(tofree);
226         return (-1);
227 }
228
229 static struct pdu *
230 login_receive_chap_a(struct connection *conn)
231 {
232         struct pdu *request;
233         struct keys *request_keys;
234         const char *chap_a;
235
236         request = login_receive(conn, false);
237         request_keys = keys_new();
238         keys_load(request_keys, request);
239
240         chap_a = keys_find(request_keys, "CHAP_A");
241         if (chap_a == NULL) {
242                 login_send_error(request, 0x02, 0x07);
243                 log_errx(1, "received CHAP Login PDU without CHAP_A");
244         }
245         if (login_list_contains(chap_a, "5") == 0) {
246                 login_send_error(request, 0x02, 0x01);
247                 log_errx(1, "received CHAP Login PDU with unsupported CHAP_A "
248                     "\"%s\"", chap_a);
249         }
250         keys_delete(request_keys);
251
252         return (request);
253 }
254
255 static void
256 login_send_chap_c(struct pdu *request, struct chap *chap)
257 {
258         struct pdu *response;
259         struct keys *response_keys;
260         char *chap_c, *chap_i;
261
262         chap_c = chap_get_challenge(chap);
263         chap_i = chap_get_id(chap);
264
265         response = login_new_response(request);
266         response_keys = keys_new();
267         keys_add(response_keys, "CHAP_A", "5");
268         keys_add(response_keys, "CHAP_I", chap_i);
269         keys_add(response_keys, "CHAP_C", chap_c);
270         free(chap_i);
271         free(chap_c);
272         keys_save(response_keys, response);
273         pdu_send(response);
274         pdu_delete(response);
275         keys_delete(response_keys);
276 }
277
278 static struct pdu *
279 login_receive_chap_r(struct connection *conn, struct auth_group *ag,
280     struct chap *chap, const struct auth **authp)
281 {
282         struct pdu *request;
283         struct keys *request_keys;
284         const char *chap_n, *chap_r;
285         const struct auth *auth;
286         int error;
287
288         request = login_receive(conn, false);
289         request_keys = keys_new();
290         keys_load(request_keys, request);
291
292         chap_n = keys_find(request_keys, "CHAP_N");
293         if (chap_n == NULL) {
294                 login_send_error(request, 0x02, 0x07);
295                 log_errx(1, "received CHAP Login PDU without CHAP_N");
296         }
297         chap_r = keys_find(request_keys, "CHAP_R");
298         if (chap_r == NULL) {
299                 login_send_error(request, 0x02, 0x07);
300                 log_errx(1, "received CHAP Login PDU without CHAP_R");
301         }
302         error = chap_receive(chap, chap_r);
303         if (error != 0) {
304                 login_send_error(request, 0x02, 0x07);
305                 log_errx(1, "received CHAP Login PDU with malformed CHAP_R");
306         }
307
308         /*
309          * Verify the response.
310          */
311         assert(ag->ag_type == AG_TYPE_CHAP ||
312             ag->ag_type == AG_TYPE_CHAP_MUTUAL);
313         auth = auth_find(ag, chap_n);
314         if (auth == NULL) {
315                 login_send_error(request, 0x02, 0x01);
316                 log_errx(1, "received CHAP Login with invalid user \"%s\"",
317                     chap_n);
318         }
319
320         assert(auth->a_secret != NULL);
321         assert(strlen(auth->a_secret) > 0);
322
323         error = chap_authenticate(chap, auth->a_secret);
324         if (error != 0) {
325                 login_send_error(request, 0x02, 0x01);
326                 log_errx(1, "CHAP authentication failed for user \"%s\"",
327                     auth->a_user);
328         }
329
330         keys_delete(request_keys);
331
332         *authp = auth;
333         return (request);
334 }
335
336 static void
337 login_send_chap_success(struct pdu *request,
338     const struct auth *auth)
339 {
340         struct pdu *response;
341         struct keys *request_keys, *response_keys;
342         struct rchap *rchap;
343         const char *chap_i, *chap_c;
344         char *chap_r;
345         int error;
346
347         response = login_new_response(request);
348         login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
349
350         /*
351          * Actually, one more thing: mutual authentication.
352          */
353         request_keys = keys_new();
354         keys_load(request_keys, request);
355         chap_i = keys_find(request_keys, "CHAP_I");
356         chap_c = keys_find(request_keys, "CHAP_C");
357         if (chap_i != NULL || chap_c != NULL) {
358                 if (chap_i == NULL) {
359                         login_send_error(request, 0x02, 0x07);
360                         log_errx(1, "initiator requested target "
361                             "authentication, but didn't send CHAP_I");
362                 }
363                 if (chap_c == NULL) {
364                         login_send_error(request, 0x02, 0x07);
365                         log_errx(1, "initiator requested target "
366                             "authentication, but didn't send CHAP_C");
367                 }
368                 if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) {
369                         login_send_error(request, 0x02, 0x01);
370                         log_errx(1, "initiator requests target authentication "
371                             "for user \"%s\", but mutual user/secret "
372                             "is not set", auth->a_user);
373                 }
374
375                 log_debugx("performing mutual authentication as user \"%s\"",
376                     auth->a_mutual_user);
377
378                 rchap = rchap_new(auth->a_mutual_secret);
379                 error = rchap_receive(rchap, chap_i, chap_c);
380                 if (error != 0) {
381                         login_send_error(request, 0x02, 0x07);
382                         log_errx(1, "received CHAP Login PDU with malformed "
383                             "CHAP_I or CHAP_C");
384                 }
385                 chap_r = rchap_get_response(rchap);
386                 rchap_delete(rchap);
387                 response_keys = keys_new();
388                 keys_add(response_keys, "CHAP_N", auth->a_mutual_user);
389                 keys_add(response_keys, "CHAP_R", chap_r);
390                 free(chap_r);
391                 keys_save(response_keys, response);
392                 keys_delete(response_keys);
393         } else {
394                 log_debugx("initiator did not request target authentication");
395         }
396
397         keys_delete(request_keys);
398         pdu_send(response);
399         pdu_delete(response);
400 }
401
402 static void
403 login_chap(struct connection *conn, struct auth_group *ag)
404 {
405         const struct auth *auth;
406         struct chap *chap;
407         struct pdu *request;
408
409         /*
410          * Receive CHAP_A PDU.
411          */
412         log_debugx("beginning CHAP authentication; waiting for CHAP_A");
413         request = login_receive_chap_a(conn);
414
415         /*
416          * Generate the challenge.
417          */
418         chap = chap_new();
419
420         /*
421          * Send the challenge.
422          */
423         log_debugx("sending CHAP_C, binary challenge size is %zd bytes",
424             sizeof(chap->chap_challenge));
425         login_send_chap_c(request, chap);
426         pdu_delete(request);
427
428         /*
429          * Receive CHAP_N/CHAP_R PDU and authenticate.
430          */
431         log_debugx("waiting for CHAP_N/CHAP_R");
432         request = login_receive_chap_r(conn, ag, chap, &auth);
433
434         /*
435          * Yay, authentication succeeded!
436          */
437         log_debugx("authentication succeeded for user \"%s\"; "
438             "transitioning to Negotiation Phase", auth->a_user);
439         login_send_chap_success(request, auth);
440         pdu_delete(request);
441
442         /*
443          * Leave username and CHAP information for discovery().
444          */
445         conn->conn_user = auth->a_user;
446         conn->conn_chap = chap;
447 }
448
449 static void
450 login_negotiate_key(struct pdu *request, const char *name,
451     const char *value, bool skipped_security, struct keys *response_keys)
452 {
453         int which;
454         size_t tmp;
455         struct connection *conn;
456
457         conn = request->pdu_connection;
458
459         if (strcmp(name, "InitiatorName") == 0) {
460                 if (!skipped_security)
461                         log_errx(1, "initiator resent InitiatorName");
462         } else if (strcmp(name, "SessionType") == 0) {
463                 if (!skipped_security)
464                         log_errx(1, "initiator resent SessionType");
465         } else if (strcmp(name, "TargetName") == 0) {
466                 if (!skipped_security)
467                         log_errx(1, "initiator resent TargetName");
468         } else if (strcmp(name, "InitiatorAlias") == 0) {
469                 if (conn->conn_initiator_alias != NULL)
470                         free(conn->conn_initiator_alias);
471                 conn->conn_initiator_alias = checked_strdup(value);
472         } else if (strcmp(value, "Irrelevant") == 0) {
473                 /* Ignore. */
474         } else if (strcmp(name, "HeaderDigest") == 0) {
475                 /*
476                  * We don't handle digests for discovery sessions.
477                  */
478                 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
479                         log_debugx("discovery session; digests disabled");
480                         keys_add(response_keys, name, "None");
481                         return;
482                 }
483
484                 which = login_list_prefers(value, "CRC32C", "None");
485                 switch (which) {
486                 case 1:
487                         log_debugx("initiator prefers CRC32C "
488                             "for header digest; we'll use it");
489                         conn->conn_header_digest = CONN_DIGEST_CRC32C;
490                         keys_add(response_keys, name, "CRC32C");
491                         break;
492                 case 2:
493                         log_debugx("initiator prefers not to do "
494                             "header digest; we'll comply");
495                         keys_add(response_keys, name, "None");
496                         break;
497                 default:
498                         log_warnx("initiator sent unrecognized "
499                             "HeaderDigest value \"%s\"; will use None", value);
500                         keys_add(response_keys, name, "None");
501                         break;
502                 }
503         } else if (strcmp(name, "DataDigest") == 0) {
504                 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
505                         log_debugx("discovery session; digests disabled");
506                         keys_add(response_keys, name, "None");
507                         return;
508                 }
509
510                 which = login_list_prefers(value, "CRC32C", "None");
511                 switch (which) {
512                 case 1:
513                         log_debugx("initiator prefers CRC32C "
514                             "for data digest; we'll use it");
515                         conn->conn_data_digest = CONN_DIGEST_CRC32C;
516                         keys_add(response_keys, name, "CRC32C");
517                         break;
518                 case 2:
519                         log_debugx("initiator prefers not to do "
520                             "data digest; we'll comply");
521                         keys_add(response_keys, name, "None");
522                         break;
523                 default:
524                         log_warnx("initiator sent unrecognized "
525                             "DataDigest value \"%s\"; will use None", value);
526                         keys_add(response_keys, name, "None");
527                         break;
528                 }
529         } else if (strcmp(name, "MaxConnections") == 0) {
530                 keys_add(response_keys, name, "1");
531         } else if (strcmp(name, "InitialR2T") == 0) {
532                 keys_add(response_keys, name, "Yes");
533         } else if (strcmp(name, "ImmediateData") == 0) {
534                 if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) {
535                         log_debugx("discovery session; ImmediateData irrelevant");
536                         keys_add(response_keys, name, "Irrelevant");
537                 } else {
538                         if (strcmp(value, "Yes") == 0) {
539                                 conn->conn_immediate_data = true;
540                                 keys_add(response_keys, name, "Yes");
541                         } else {
542                                 conn->conn_immediate_data = false;
543                                 keys_add(response_keys, name, "No");
544                         }
545                 }
546         } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) {
547                 tmp = strtoul(value, NULL, 10);
548                 if (tmp <= 0) {
549                         login_send_error(request, 0x02, 0x00);
550                         log_errx(1, "received invalid "
551                             "MaxRecvDataSegmentLength");
552                 }
553
554                 /*
555                  * MaxRecvDataSegmentLength is a direction-specific parameter.
556                  * We'll limit our _send_ to what the initiator can handle but
557                  * our MaxRecvDataSegmentLength is not influenced by the
558                  * initiator in any way.
559                  */
560                 if ((int)tmp > conn->conn_max_send_data_segment_length) {
561                         log_debugx("capping max_send_data_segment_length "
562                             "from %zd to %d", tmp,
563                             conn->conn_max_send_data_segment_length);
564                         tmp = conn->conn_max_send_data_segment_length;
565                 }
566                 conn->conn_max_send_data_segment_length = tmp;
567                 keys_add_int(response_keys, name,
568                     conn->conn_max_recv_data_segment_length);
569         } else if (strcmp(name, "MaxBurstLength") == 0) {
570                 tmp = strtoul(value, NULL, 10);
571                 if (tmp <= 0) {
572                         login_send_error(request, 0x02, 0x00);
573                         log_errx(1, "received invalid MaxBurstLength");
574                 }
575                 if ((int)tmp > conn->conn_max_burst_length) {
576                         log_debugx("capping MaxBurstLength from %zd to %d",
577                             tmp, conn->conn_max_burst_length);
578                         tmp = conn->conn_max_burst_length;
579                 }
580                 conn->conn_max_burst_length = tmp;
581                 keys_add_int(response_keys, name, tmp);
582         } else if (strcmp(name, "FirstBurstLength") == 0) {
583                 tmp = strtoul(value, NULL, 10);
584                 if (tmp <= 0) {
585                         login_send_error(request, 0x02, 0x00);
586                         log_errx(1, "received invalid FirstBurstLength");
587                 }
588                 if ((int)tmp > conn->conn_first_burst_length) {
589                         log_debugx("capping FirstBurstLength from %zd to %d",
590                             tmp, conn->conn_first_burst_length);
591                         tmp = conn->conn_first_burst_length;
592                 }
593                 conn->conn_first_burst_length = tmp;
594                 keys_add_int(response_keys, name, tmp);
595         } else if (strcmp(name, "DefaultTime2Wait") == 0) {
596                 keys_add(response_keys, name, value);
597         } else if (strcmp(name, "DefaultTime2Retain") == 0) {
598                 keys_add(response_keys, name, "0");
599         } else if (strcmp(name, "MaxOutstandingR2T") == 0) {
600                 keys_add(response_keys, name, "1");
601         } else if (strcmp(name, "DataPDUInOrder") == 0) {
602                 keys_add(response_keys, name, "Yes");
603         } else if (strcmp(name, "DataSequenceInOrder") == 0) {
604                 keys_add(response_keys, name, "Yes");
605         } else if (strcmp(name, "ErrorRecoveryLevel") == 0) {
606                 keys_add(response_keys, name, "0");
607         } else if (strcmp(name, "OFMarker") == 0) {
608                 keys_add(response_keys, name, "No");
609         } else if (strcmp(name, "IFMarker") == 0) {
610                 keys_add(response_keys, name, "No");
611         } else if (strcmp(name, "iSCSIProtocolLevel") == 0) {
612                 tmp = strtoul(value, NULL, 10);
613                 if (tmp > 2)
614                         tmp = 2;
615                 keys_add_int(response_keys, name, tmp);
616         } else {
617                 log_debugx("unknown key \"%s\"; responding "
618                     "with NotUnderstood", name);
619                 keys_add(response_keys, name, "NotUnderstood");
620         }
621 }
622
623 static void
624 login_redirect(struct pdu *request, const char *target_address)
625 {
626         struct pdu *response;
627         struct iscsi_bhs_login_response *bhslr2;
628         struct keys *response_keys;
629
630         response = login_new_response(request);
631         login_set_csg(response, login_csg(request));
632         bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
633         bhslr2->bhslr_status_class = 0x01;
634         bhslr2->bhslr_status_detail = 0x01;
635
636         response_keys = keys_new();
637         keys_add(response_keys, "TargetAddress", target_address);
638
639         keys_save(response_keys, response);
640         pdu_send(response);
641         pdu_delete(response);
642         keys_delete(response_keys);
643 }
644
645 static bool
646 login_portal_redirect(struct connection *conn, struct pdu *request)
647 {
648         const struct portal_group *pg;
649
650         pg = conn->conn_portal->p_portal_group;
651         if (pg->pg_redirection == NULL)
652                 return (false);
653
654         log_debugx("portal-group \"%s\" configured to redirect to %s",
655             pg->pg_name, pg->pg_redirection);
656         login_redirect(request, pg->pg_redirection);
657
658         return (true);
659 }
660
661 static bool
662 login_target_redirect(struct connection *conn, struct pdu *request)
663 {
664         const char *target_address;
665
666         assert(conn->conn_portal->p_portal_group->pg_redirection == NULL);
667
668         if (conn->conn_target == NULL)
669                 return (false);
670
671         target_address = conn->conn_target->t_redirection;
672         if (target_address == NULL)
673                 return (false);
674
675         log_debugx("target \"%s\" configured to redirect to %s",
676           conn->conn_target->t_name, target_address);
677         login_redirect(request, target_address);
678
679         return (true);
680 }
681
682 static void
683 login_negotiate(struct connection *conn, struct pdu *request)
684 {
685         struct pdu *response;
686         struct iscsi_bhs_login_response *bhslr2;
687         struct keys *request_keys, *response_keys;
688         int i;
689         bool redirected, skipped_security;
690
691         if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
692                 /*
693                  * Query the kernel for various size limits.  In case of
694                  * offload, it depends on hardware capabilities.
695                  */
696                 assert(conn->conn_target != NULL);
697                 kernel_limits(conn->conn_portal->p_portal_group->pg_offload,
698                     &conn->conn_max_recv_data_segment_length,
699                     &conn->conn_max_send_data_segment_length,
700                     &conn->conn_max_burst_length,
701                     &conn->conn_first_burst_length);
702
703                 /* We expect legal, usable values at this point. */
704                 assert(conn->conn_max_recv_data_segment_length >= 512);
705                 assert(conn->conn_max_recv_data_segment_length < (1 << 24));
706                 assert(conn->conn_max_burst_length >= 512);
707                 assert(conn->conn_max_burst_length < (1 << 24));
708                 assert(conn->conn_first_burst_length >= 512);
709                 assert(conn->conn_first_burst_length < (1 << 24));
710                 assert(conn->conn_first_burst_length <=
711                     conn->conn_max_burst_length);
712         } else {
713                 conn->conn_max_recv_data_segment_length =
714                     MAX_DATA_SEGMENT_LENGTH;
715                 conn->conn_max_send_data_segment_length =
716                     MAX_DATA_SEGMENT_LENGTH;
717         }
718
719         if (request == NULL) {
720                 log_debugx("beginning operational parameter negotiation; "
721                     "waiting for Login PDU");
722                 request = login_receive(conn, false);
723                 skipped_security = false;
724         } else
725                 skipped_security = true;
726
727         /*
728          * RFC 3720, 10.13.5.  Status-Class and Status-Detail, says
729          * the redirection SHOULD be accepted by the initiator before
730          * authentication, but MUST be be accepted afterwards; that's
731          * why we're doing it here and not earlier.
732          */
733         redirected = login_target_redirect(conn, request);
734         if (redirected) {
735                 log_debugx("initiator redirected; exiting");
736                 exit(0);
737         }
738
739         request_keys = keys_new();
740         keys_load(request_keys, request);
741
742         response = login_new_response(request);
743         bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs;
744         bhslr2->bhslr_tsih = htons(0xbadd);
745         login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
746         login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE);
747         response_keys = keys_new();
748
749         if (skipped_security &&
750             conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
751                 if (conn->conn_target->t_alias != NULL)
752                         keys_add(response_keys,
753                             "TargetAlias", conn->conn_target->t_alias);
754                 keys_add_int(response_keys, "TargetPortalGroupTag",
755                     conn->conn_portal->p_portal_group->pg_tag);
756         }
757
758         for (i = 0; i < KEYS_MAX; i++) {
759                 if (request_keys->keys_names[i] == NULL)
760                         break;
761
762                 login_negotiate_key(request, request_keys->keys_names[i],
763                     request_keys->keys_values[i], skipped_security,
764                     response_keys);
765         }
766
767         /*
768          * We'd started with usable values at our end.  But a bad initiator
769          * could have presented a large FirstBurstLength and then a smaller
770          * MaxBurstLength (in that order) and because we process the key/value
771          * pairs in the order they are in the request we might have ended up
772          * with illegal values here.
773          */
774         if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL &&
775             conn->conn_first_burst_length > conn->conn_max_burst_length) {
776                 log_errx(1, "initiator sent FirstBurstLength > MaxBurstLength");
777         }
778
779         log_debugx("operational parameter negotiation done; "
780             "transitioning to Full Feature Phase");
781
782         keys_save(response_keys, response);
783         pdu_send(response);
784         pdu_delete(response);
785         keys_delete(response_keys);
786         pdu_delete(request);
787         keys_delete(request_keys);
788 }
789
790 static void
791 login_wait_transition(struct connection *conn)
792 {
793         struct pdu *request, *response;
794         struct iscsi_bhs_login_request *bhslr;
795
796         log_debugx("waiting for state transition request");
797         request = login_receive(conn, false);
798         bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
799         if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) {
800                 login_send_error(request, 0x02, 0x00);
801                 log_errx(1, "got no \"T\" flag after answering AuthMethod");
802         }
803
804         log_debugx("got state transition request");
805         response = login_new_response(request);
806         pdu_delete(request);
807         login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
808         pdu_send(response);
809         pdu_delete(response);
810
811         login_negotiate(conn, NULL);
812 }
813
814 void
815 login(struct connection *conn)
816 {
817         struct pdu *request, *response;
818         struct iscsi_bhs_login_request *bhslr;
819         struct keys *request_keys, *response_keys;
820         struct auth_group *ag;
821         struct portal_group *pg;
822         const char *initiator_name, *initiator_alias, *session_type,
823             *target_name, *auth_method;
824         bool redirected, fail, trans;
825
826         /*
827          * Handle the initial Login Request - figure out required authentication
828          * method and either transition to the next phase, if no authentication
829          * is required, or call appropriate authentication code.
830          */
831         log_debugx("beginning Login Phase; waiting for Login PDU");
832         request = login_receive(conn, true);
833         bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs;
834         if (bhslr->bhslr_tsih != 0) {
835                 login_send_error(request, 0x02, 0x0a);
836                 log_errx(1, "received Login PDU with non-zero TSIH");
837         }
838
839         pg = conn->conn_portal->p_portal_group;
840
841         memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid,
842             sizeof(conn->conn_initiator_isid));
843
844         /*
845          * XXX: Implement the C flag some day.
846          */
847         request_keys = keys_new();
848         keys_load(request_keys, request);
849
850         assert(conn->conn_initiator_name == NULL);
851         initiator_name = keys_find(request_keys, "InitiatorName");
852         if (initiator_name == NULL) {
853                 login_send_error(request, 0x02, 0x07);
854                 log_errx(1, "received Login PDU without InitiatorName");
855         }
856         if (valid_iscsi_name(initiator_name) == false) {
857                 login_send_error(request, 0x02, 0x00);
858                 log_errx(1, "received Login PDU with invalid InitiatorName");
859         }
860         conn->conn_initiator_name = checked_strdup(initiator_name);
861         log_set_peer_name(conn->conn_initiator_name);
862         setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name);
863
864         redirected = login_portal_redirect(conn, request);
865         if (redirected) {
866                 log_debugx("initiator redirected; exiting");
867                 exit(0);
868         }
869
870         initiator_alias = keys_find(request_keys, "InitiatorAlias");
871         if (initiator_alias != NULL)
872                 conn->conn_initiator_alias = checked_strdup(initiator_alias);
873
874         assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE);
875         session_type = keys_find(request_keys, "SessionType");
876         if (session_type != NULL) {
877                 if (strcmp(session_type, "Normal") == 0) {
878                         conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
879                 } else if (strcmp(session_type, "Discovery") == 0) {
880                         conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY;
881                 } else {
882                         login_send_error(request, 0x02, 0x00);
883                         log_errx(1, "received Login PDU with invalid "
884                             "SessionType \"%s\"", session_type);
885                 }
886         } else
887                 conn->conn_session_type = CONN_SESSION_TYPE_NORMAL;
888
889         assert(conn->conn_target == NULL);
890         if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
891                 target_name = keys_find(request_keys, "TargetName");
892                 if (target_name == NULL) {
893                         login_send_error(request, 0x02, 0x07);
894                         log_errx(1, "received Login PDU without TargetName");
895                 }
896
897                 conn->conn_port = port_find_in_pg(pg, target_name);
898                 if (conn->conn_port == NULL) {
899                         login_send_error(request, 0x02, 0x03);
900                         log_errx(1, "requested target \"%s\" not found",
901                             target_name);
902                 }
903                 conn->conn_target = conn->conn_port->p_target;
904         }
905
906         /*
907          * At this point we know what kind of authentication we need.
908          */
909         if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
910                 ag = conn->conn_port->p_auth_group;
911                 if (ag == NULL)
912                         ag = conn->conn_target->t_auth_group;
913                 if (ag->ag_name != NULL) {
914                         log_debugx("initiator requests to connect "
915                             "to target \"%s\"; auth-group \"%s\"",
916                             conn->conn_target->t_name,
917                             ag->ag_name);
918                 } else {
919                         log_debugx("initiator requests to connect "
920                             "to target \"%s\"", conn->conn_target->t_name);
921                 }
922         } else {
923                 assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY);
924                 ag = pg->pg_discovery_auth_group;
925                 if (ag->ag_name != NULL) {
926                         log_debugx("initiator requests "
927                             "discovery session; auth-group \"%s\"", ag->ag_name);
928                 } else {
929                         log_debugx("initiator requests discovery session");
930                 }
931         }
932
933         if (ag->ag_type == AG_TYPE_DENY) {
934                 login_send_error(request, 0x02, 0x01);
935                 log_errx(1, "auth-type is \"deny\"");
936         }
937
938         if (ag->ag_type == AG_TYPE_UNKNOWN) {
939                 /*
940                  * This can happen with empty auth-group.
941                  */
942                 login_send_error(request, 0x02, 0x01);
943                 log_errx(1, "auth-type not set, denying access");
944         }
945
946         /*
947          * Enforce initiator-name and initiator-portal.
948          */
949         if (auth_name_check(ag, initiator_name) != 0) {
950                 login_send_error(request, 0x02, 0x02);
951                 log_errx(1, "initiator does not match allowed initiator names");
952         }
953
954         if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
955                 login_send_error(request, 0x02, 0x02);
956                 log_errx(1, "initiator does not match allowed "
957                     "initiator portals");
958         }
959
960         /*
961          * Let's see if the initiator intends to do any kind of authentication
962          * at all.
963          */
964         if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) {
965                 if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
966                         login_send_error(request, 0x02, 0x01);
967                         log_errx(1, "initiator skipped the authentication, "
968                             "but authentication is required");
969                 }
970
971                 keys_delete(request_keys);
972
973                 log_debugx("initiator skipped the authentication, "
974                     "and we don't need it; proceeding with negotiation");
975                 login_negotiate(conn, request);
976                 return;
977         }
978
979         fail = false;
980         response = login_new_response(request);
981         response_keys = keys_new();
982         trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0;
983         auth_method = keys_find(request_keys, "AuthMethod");
984         if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) {
985                 log_debugx("authentication not required");
986                 if (auth_method == NULL ||
987                     login_list_contains(auth_method, "None")) {
988                         keys_add(response_keys, "AuthMethod", "None");
989                 } else {
990                         log_warnx("initiator requests "
991                             "AuthMethod \"%s\" instead of \"None\"",
992                             auth_method);
993                         keys_add(response_keys, "AuthMethod", "Reject");
994                 }
995                 if (trans)
996                         login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION);
997         } else {
998                 log_debugx("CHAP authentication required");
999                 if (auth_method == NULL ||
1000                     login_list_contains(auth_method, "CHAP")) {
1001                         keys_add(response_keys, "AuthMethod", "CHAP");
1002                 } else {
1003                         log_warnx("initiator requests unsupported "
1004                             "AuthMethod \"%s\" instead of \"CHAP\"",
1005                             auth_method);
1006                         keys_add(response_keys, "AuthMethod", "Reject");
1007                         fail = true;
1008                 }
1009         }
1010         if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) {
1011                 if (conn->conn_target->t_alias != NULL)
1012                         keys_add(response_keys,
1013                             "TargetAlias", conn->conn_target->t_alias);
1014                 keys_add_int(response_keys,
1015                     "TargetPortalGroupTag", pg->pg_tag);
1016         }
1017         keys_save(response_keys, response);
1018
1019         pdu_send(response);
1020         pdu_delete(response);
1021         keys_delete(response_keys);
1022         pdu_delete(request);
1023         keys_delete(request_keys);
1024
1025         if (fail) {
1026                 log_debugx("sent reject for AuthMethod; exiting");
1027                 exit(1);
1028         }
1029
1030         if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
1031                 login_chap(conn, ag);
1032                 login_negotiate(conn, NULL);
1033         } else if (trans) {
1034                 login_negotiate(conn, NULL);
1035         } else {
1036                 login_wait_transition(conn);
1037         }
1038 }