discovery.c revision 267606
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: head/usr.sbin/ctld/discovery.c 267606 2014-06-18 12:26:02Z mav $ 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> 38267606Smav#include <netdb.h> 39267606Smav#include <sys/socket.h> 40255570Strasz 41255570Strasz#include "ctld.h" 42255570Strasz#include "iscsi_proto.h" 43255570Strasz 44255570Straszstatic struct pdu * 45255570Strasztext_receive(struct connection *conn) 46255570Strasz{ 47255570Strasz struct pdu *request; 48255570Strasz struct iscsi_bhs_text_request *bhstr; 49255570Strasz 50255570Strasz request = pdu_new(conn); 51255570Strasz pdu_receive(request); 52255570Strasz if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) != 53255570Strasz ISCSI_BHS_OPCODE_TEXT_REQUEST) 54255570Strasz log_errx(1, "protocol error: received invalid opcode 0x%x", 55255570Strasz request->pdu_bhs->bhs_opcode); 56255570Strasz bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs; 57255570Strasz#if 0 58255570Strasz if ((bhstr->bhstr_flags & ISCSI_BHSTR_FLAGS_FINAL) == 0) 59255570Strasz log_errx(1, "received Text PDU without the \"F\" flag"); 60255570Strasz#endif 61255570Strasz /* 62255570Strasz * XXX: Implement the C flag some day. 63255570Strasz */ 64255570Strasz if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0) 65255570Strasz log_errx(1, "received Text PDU with unsupported \"C\" flag"); 66255570Strasz if (ntohl(bhstr->bhstr_cmdsn) < conn->conn_cmdsn) { 67255570Strasz log_errx(1, "received Text PDU with decreasing CmdSN: " 68255570Strasz "was %d, is %d", conn->conn_cmdsn, ntohl(bhstr->bhstr_cmdsn)); 69255570Strasz } 70255570Strasz if (ntohl(bhstr->bhstr_expstatsn) != conn->conn_statsn) { 71255570Strasz log_errx(1, "received Text PDU with wrong StatSN: " 72255570Strasz "is %d, should be %d", ntohl(bhstr->bhstr_expstatsn), 73255570Strasz conn->conn_statsn); 74255570Strasz } 75255570Strasz conn->conn_cmdsn = ntohl(bhstr->bhstr_cmdsn); 76255570Strasz 77255570Strasz return (request); 78255570Strasz} 79255570Strasz 80255570Straszstatic struct pdu * 81255570Strasztext_new_response(struct pdu *request) 82255570Strasz{ 83255570Strasz struct pdu *response; 84255570Strasz struct connection *conn; 85255570Strasz struct iscsi_bhs_text_request *bhstr; 86255570Strasz struct iscsi_bhs_text_response *bhstr2; 87255570Strasz 88255570Strasz bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs; 89255570Strasz conn = request->pdu_connection; 90255570Strasz 91255570Strasz response = pdu_new_response(request); 92255570Strasz bhstr2 = (struct iscsi_bhs_text_response *)response->pdu_bhs; 93255570Strasz bhstr2->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_RESPONSE; 94255570Strasz bhstr2->bhstr_flags = BHSTR_FLAGS_FINAL; 95255570Strasz bhstr2->bhstr_lun = bhstr->bhstr_lun; 96255570Strasz bhstr2->bhstr_initiator_task_tag = bhstr->bhstr_initiator_task_tag; 97255570Strasz bhstr2->bhstr_target_transfer_tag = bhstr->bhstr_target_transfer_tag; 98255570Strasz bhstr2->bhstr_statsn = htonl(conn->conn_statsn++); 99255570Strasz bhstr2->bhstr_expcmdsn = htonl(conn->conn_cmdsn); 100255570Strasz bhstr2->bhstr_maxcmdsn = htonl(conn->conn_cmdsn); 101255570Strasz 102255570Strasz return (response); 103255570Strasz} 104255570Strasz 105255570Straszstatic struct pdu * 106255570Straszlogout_receive(struct connection *conn) 107255570Strasz{ 108255570Strasz struct pdu *request; 109255570Strasz struct iscsi_bhs_logout_request *bhslr; 110255570Strasz 111255570Strasz request = pdu_new(conn); 112255570Strasz pdu_receive(request); 113255570Strasz if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) != 114255570Strasz ISCSI_BHS_OPCODE_LOGOUT_REQUEST) 115255570Strasz log_errx(1, "protocol error: received invalid opcode 0x%x", 116255570Strasz request->pdu_bhs->bhs_opcode); 117255570Strasz bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs; 118255570Strasz if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION) 119255570Strasz log_debugx("received Logout PDU with invalid reason 0x%x; " 120255570Strasz "continuing anyway", bhslr->bhslr_reason & 0x7f); 121255570Strasz if (ntohl(bhslr->bhslr_cmdsn) < conn->conn_cmdsn) { 122255570Strasz log_errx(1, "received Logout PDU with decreasing CmdSN: " 123255570Strasz "was %d, is %d", conn->conn_cmdsn, 124255570Strasz ntohl(bhslr->bhslr_cmdsn)); 125255570Strasz } 126255570Strasz if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) { 127255570Strasz log_errx(1, "received Logout PDU with wrong StatSN: " 128255570Strasz "is %d, should be %d", ntohl(bhslr->bhslr_expstatsn), 129255570Strasz conn->conn_statsn); 130255570Strasz } 131255570Strasz conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn); 132255570Strasz 133255570Strasz return (request); 134255570Strasz} 135255570Strasz 136255570Straszstatic struct pdu * 137255570Straszlogout_new_response(struct pdu *request) 138255570Strasz{ 139255570Strasz struct pdu *response; 140255570Strasz struct connection *conn; 141255570Strasz struct iscsi_bhs_logout_request *bhslr; 142255570Strasz struct iscsi_bhs_logout_response *bhslr2; 143255570Strasz 144255570Strasz bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs; 145255570Strasz conn = request->pdu_connection; 146255570Strasz 147255570Strasz response = pdu_new_response(request); 148255570Strasz bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs; 149255570Strasz bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE; 150255570Strasz bhslr2->bhslr_flags = 0x80; 151255570Strasz bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY; 152255570Strasz bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag; 153255570Strasz bhslr2->bhslr_statsn = htonl(conn->conn_statsn++); 154255570Strasz bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn); 155255570Strasz bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn); 156255570Strasz 157255570Strasz return (response); 158255570Strasz} 159255570Strasz 160267606Smavstatic void 161267606Smavdiscovery_add_target(struct keys *response_keys, struct target *targ) 162267606Smav{ 163267606Smav struct portal *portal; 164267606Smav char *buf; 165267606Smav char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; 166267606Smav struct addrinfo *ai; 167267606Smav int ret; 168267606Smav 169267606Smav keys_add(response_keys, "TargetName", targ->t_name); 170267606Smav TAILQ_FOREACH(portal, &targ->t_portal_group->pg_portals, p_next) { 171267606Smav ai = portal->p_ai; 172267606Smav ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, 173267606Smav hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), 174267606Smav NI_NUMERICHOST | NI_NUMERICSERV); 175267606Smav if (ret != 0) { 176267606Smav log_warnx("getnameinfo: %s", gai_strerror(ret)); 177267606Smav continue; 178267606Smav } 179267606Smav switch (ai->ai_addr->sa_family) { 180267606Smav case AF_INET: 181267606Smav if (strcmp(hbuf, "0.0.0.0") == 0) 182267606Smav continue; 183267606Smav ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf, 184267606Smav targ->t_portal_group->pg_tag); 185267606Smav break; 186267606Smav case AF_INET6: 187267606Smav if (strcmp(hbuf, "::") == 0) 188267606Smav continue; 189267606Smav ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf, 190267606Smav targ->t_portal_group->pg_tag); 191267606Smav break; 192267606Smav default: 193267606Smav continue; 194267606Smav } 195267606Smav if (ret <= 0) 196267606Smav log_err(1, "asprintf"); 197267606Smav keys_add(response_keys, "TargetAddress", buf); 198267606Smav free(buf); 199267606Smav } 200267606Smav} 201267606Smav 202255570Straszvoid 203255570Straszdiscovery(struct connection *conn) 204255570Strasz{ 205255570Strasz struct pdu *request, *response; 206255570Strasz struct keys *request_keys, *response_keys; 207255570Strasz struct target *targ; 208255570Strasz const char *send_targets; 209255570Strasz 210255570Strasz log_debugx("beginning discovery session; waiting for Text PDU"); 211255570Strasz request = text_receive(conn); 212255570Strasz request_keys = keys_new(); 213255570Strasz keys_load(request_keys, request); 214255570Strasz 215255570Strasz send_targets = keys_find(request_keys, "SendTargets"); 216255570Strasz if (send_targets == NULL) 217255570Strasz log_errx(1, "received Text PDU without SendTargets"); 218255570Strasz 219255570Strasz response = text_new_response(request); 220255570Strasz response_keys = keys_new(); 221255570Strasz 222255570Strasz if (strcmp(send_targets, "All") == 0) { 223255570Strasz TAILQ_FOREACH(targ, 224255570Strasz &conn->conn_portal->p_portal_group->pg_conf->conf_targets, 225255570Strasz t_next) { 226255570Strasz if (targ->t_portal_group != 227255570Strasz conn->conn_portal->p_portal_group) { 228255570Strasz log_debugx("not returning target \"%s\"; " 229255570Strasz "belongs to a different portal group", 230261757Strasz targ->t_name); 231255570Strasz continue; 232255570Strasz } 233267606Smav discovery_add_target(response_keys, targ); 234255570Strasz } 235255570Strasz } else { 236255570Strasz targ = target_find(conn->conn_portal->p_portal_group->pg_conf, 237255570Strasz send_targets); 238255570Strasz if (targ == NULL) { 239255570Strasz log_debugx("initiator requested information on unknown " 240255570Strasz "target \"%s\"; returning nothing", send_targets); 241267606Smav } else 242267606Smav discovery_add_target(response_keys, targ); 243255570Strasz } 244255570Strasz keys_save(response_keys, response); 245255570Strasz 246255570Strasz pdu_send(response); 247255570Strasz pdu_delete(response); 248255570Strasz keys_delete(response_keys); 249255570Strasz pdu_delete(request); 250255570Strasz keys_delete(request_keys); 251255570Strasz 252255570Strasz log_debugx("done sending targets; waiting for Logout PDU"); 253255570Strasz request = logout_receive(conn); 254255570Strasz response = logout_new_response(request); 255255570Strasz 256255570Strasz pdu_send(response); 257255570Strasz pdu_delete(response); 258255570Strasz pdu_delete(request); 259255570Strasz 260255570Strasz log_debugx("discovery session done"); 261255570Strasz} 262