1255570Strasz/*- 2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation 3255570Strasz * All rights reserved. 4255570Strasz * 5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship 6255570Strasz * from the FreeBSD Foundation. 7255570Strasz * 8255570Strasz * Redistribution and use in source and binary forms, with or without 9255570Strasz * modification, are permitted provided that the following conditions 10255570Strasz * are met: 11255570Strasz * 1. Redistributions of source code must retain the above copyright 12255570Strasz * notice, this list of conditions and the following disclaimer. 13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright 14255570Strasz * notice, this list of conditions and the following disclaimer in the 15255570Strasz * documentation and/or other materials provided with the distribution. 16255570Strasz * 17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20255570Strasz * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27255570Strasz * SUCH DAMAGE. 28255570Strasz * 29255570Strasz * $FreeBSD$ 30255570Strasz */ 31255570Strasz 32255570Strasz#include <assert.h> 33255570Strasz#include <stdint.h> 34255570Strasz#include <stdio.h> 35255570Strasz#include <stdlib.h> 36255570Strasz#include <string.h> 37255570Strasz#include <netinet/in.h> 38255570Strasz 39255570Strasz#include "ctld.h" 40255570Strasz#include "iscsi_proto.h" 41255570Strasz 42255570Straszstatic struct pdu * 43255570Strasztext_receive(struct connection *conn) 44255570Strasz{ 45255570Strasz struct pdu *request; 46255570Strasz struct iscsi_bhs_text_request *bhstr; 47255570Strasz 48255570Strasz request = pdu_new(conn); 49255570Strasz pdu_receive(request); 50255570Strasz if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) != 51255570Strasz ISCSI_BHS_OPCODE_TEXT_REQUEST) 52255570Strasz log_errx(1, "protocol error: received invalid opcode 0x%x", 53255570Strasz request->pdu_bhs->bhs_opcode); 54255570Strasz bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs; 55255570Strasz#if 0 56255570Strasz if ((bhstr->bhstr_flags & ISCSI_BHSTR_FLAGS_FINAL) == 0) 57255570Strasz log_errx(1, "received Text PDU without the \"F\" flag"); 58255570Strasz#endif 59255570Strasz /* 60255570Strasz * XXX: Implement the C flag some day. 61255570Strasz */ 62255570Strasz if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0) 63255570Strasz log_errx(1, "received Text PDU with unsupported \"C\" flag"); 64255570Strasz if (request->pdu_data_len == 0) 65255570Strasz log_errx(1, "received Text PDU with empty data segment"); 66255570Strasz 67255570Strasz if (ntohl(bhstr->bhstr_cmdsn) < conn->conn_cmdsn) { 68255570Strasz log_errx(1, "received Text PDU with decreasing CmdSN: " 69255570Strasz "was %d, is %d", conn->conn_cmdsn, ntohl(bhstr->bhstr_cmdsn)); 70255570Strasz } 71255570Strasz if (ntohl(bhstr->bhstr_expstatsn) != conn->conn_statsn) { 72255570Strasz log_errx(1, "received Text PDU with wrong StatSN: " 73255570Strasz "is %d, should be %d", ntohl(bhstr->bhstr_expstatsn), 74255570Strasz conn->conn_statsn); 75255570Strasz } 76255570Strasz conn->conn_cmdsn = ntohl(bhstr->bhstr_cmdsn); 77255570Strasz 78255570Strasz return (request); 79255570Strasz} 80255570Strasz 81255570Straszstatic struct pdu * 82255570Strasztext_new_response(struct pdu *request) 83255570Strasz{ 84255570Strasz struct pdu *response; 85255570Strasz struct connection *conn; 86255570Strasz struct iscsi_bhs_text_request *bhstr; 87255570Strasz struct iscsi_bhs_text_response *bhstr2; 88255570Strasz 89255570Strasz bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs; 90255570Strasz conn = request->pdu_connection; 91255570Strasz 92255570Strasz response = pdu_new_response(request); 93255570Strasz bhstr2 = (struct iscsi_bhs_text_response *)response->pdu_bhs; 94255570Strasz bhstr2->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_RESPONSE; 95255570Strasz bhstr2->bhstr_flags = BHSTR_FLAGS_FINAL; 96255570Strasz bhstr2->bhstr_lun = bhstr->bhstr_lun; 97255570Strasz bhstr2->bhstr_initiator_task_tag = bhstr->bhstr_initiator_task_tag; 98255570Strasz bhstr2->bhstr_target_transfer_tag = bhstr->bhstr_target_transfer_tag; 99255570Strasz bhstr2->bhstr_statsn = htonl(conn->conn_statsn++); 100255570Strasz bhstr2->bhstr_expcmdsn = htonl(conn->conn_cmdsn); 101255570Strasz bhstr2->bhstr_maxcmdsn = htonl(conn->conn_cmdsn); 102255570Strasz 103255570Strasz return (response); 104255570Strasz} 105255570Strasz 106255570Straszstatic struct pdu * 107255570Straszlogout_receive(struct connection *conn) 108255570Strasz{ 109255570Strasz struct pdu *request; 110255570Strasz struct iscsi_bhs_logout_request *bhslr; 111255570Strasz 112255570Strasz request = pdu_new(conn); 113255570Strasz pdu_receive(request); 114255570Strasz if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) != 115255570Strasz ISCSI_BHS_OPCODE_LOGOUT_REQUEST) 116255570Strasz log_errx(1, "protocol error: received invalid opcode 0x%x", 117255570Strasz request->pdu_bhs->bhs_opcode); 118255570Strasz bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs; 119255570Strasz if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION) 120255570Strasz log_debugx("received Logout PDU with invalid reason 0x%x; " 121255570Strasz "continuing anyway", bhslr->bhslr_reason & 0x7f); 122255570Strasz if (ntohl(bhslr->bhslr_cmdsn) < conn->conn_cmdsn) { 123255570Strasz log_errx(1, "received Logout PDU with decreasing CmdSN: " 124255570Strasz "was %d, is %d", conn->conn_cmdsn, 125255570Strasz ntohl(bhslr->bhslr_cmdsn)); 126255570Strasz } 127255570Strasz if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) { 128255570Strasz log_errx(1, "received Logout PDU with wrong StatSN: " 129255570Strasz "is %d, should be %d", ntohl(bhslr->bhslr_expstatsn), 130255570Strasz conn->conn_statsn); 131255570Strasz } 132255570Strasz conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn); 133255570Strasz 134255570Strasz return (request); 135255570Strasz} 136255570Strasz 137255570Straszstatic struct pdu * 138255570Straszlogout_new_response(struct pdu *request) 139255570Strasz{ 140255570Strasz struct pdu *response; 141255570Strasz struct connection *conn; 142255570Strasz struct iscsi_bhs_logout_request *bhslr; 143255570Strasz struct iscsi_bhs_logout_response *bhslr2; 144255570Strasz 145255570Strasz bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs; 146255570Strasz conn = request->pdu_connection; 147255570Strasz 148255570Strasz response = pdu_new_response(request); 149255570Strasz bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs; 150255570Strasz bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE; 151255570Strasz bhslr2->bhslr_flags = 0x80; 152255570Strasz bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY; 153255570Strasz bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag; 154255570Strasz bhslr2->bhslr_statsn = htonl(conn->conn_statsn++); 155255570Strasz bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn); 156255570Strasz bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn); 157255570Strasz 158255570Strasz return (response); 159255570Strasz} 160255570Strasz 161255570Straszvoid 162255570Straszdiscovery(struct connection *conn) 163255570Strasz{ 164255570Strasz struct pdu *request, *response; 165255570Strasz struct keys *request_keys, *response_keys; 166255570Strasz struct target *targ; 167255570Strasz const char *send_targets; 168255570Strasz 169255570Strasz log_debugx("beginning discovery session; waiting for Text PDU"); 170255570Strasz request = text_receive(conn); 171255570Strasz request_keys = keys_new(); 172255570Strasz keys_load(request_keys, request); 173255570Strasz 174255570Strasz send_targets = keys_find(request_keys, "SendTargets"); 175255570Strasz if (send_targets == NULL) 176255570Strasz log_errx(1, "received Text PDU without SendTargets"); 177255570Strasz 178255570Strasz response = text_new_response(request); 179255570Strasz response_keys = keys_new(); 180255570Strasz 181255570Strasz if (strcmp(send_targets, "All") == 0) { 182255570Strasz TAILQ_FOREACH(targ, 183255570Strasz &conn->conn_portal->p_portal_group->pg_conf->conf_targets, 184255570Strasz t_next) { 185255570Strasz if (targ->t_portal_group != 186255570Strasz conn->conn_portal->p_portal_group) { 187255570Strasz log_debugx("not returning target \"%s\"; " 188255570Strasz "belongs to a different portal group", 189255570Strasz targ->t_iqn); 190255570Strasz continue; 191255570Strasz } 192255570Strasz keys_add(response_keys, "TargetName", targ->t_iqn); 193255570Strasz } 194255570Strasz } else { 195255570Strasz targ = target_find(conn->conn_portal->p_portal_group->pg_conf, 196255570Strasz send_targets); 197255570Strasz if (targ == NULL) { 198255570Strasz log_debugx("initiator requested information on unknown " 199255570Strasz "target \"%s\"; returning nothing", send_targets); 200255570Strasz } else { 201255570Strasz keys_add(response_keys, "TargetName", targ->t_iqn); 202255570Strasz } 203255570Strasz } 204255570Strasz keys_save(response_keys, response); 205255570Strasz 206255570Strasz pdu_send(response); 207255570Strasz pdu_delete(response); 208255570Strasz keys_delete(response_keys); 209255570Strasz pdu_delete(request); 210255570Strasz keys_delete(request_keys); 211255570Strasz 212255570Strasz log_debugx("done sending targets; waiting for Logout PDU"); 213255570Strasz request = logout_receive(conn); 214255570Strasz response = logout_new_response(request); 215255570Strasz 216255570Strasz pdu_send(response); 217255570Strasz pdu_delete(response); 218255570Strasz pdu_delete(request); 219255570Strasz 220255570Strasz log_debugx("discovery session done"); 221255570Strasz} 222