2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
39 #include <netinet/in.h>
41 #include <sys/socket.h>
44 #include "iscsi_proto.h"
47 logout_receive(struct connection *conn)
50 struct iscsi_bhs_logout_request *bhslr;
52 request = pdu_new(conn);
54 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
55 ISCSI_BHS_OPCODE_LOGOUT_REQUEST)
56 log_errx(1, "protocol error: received invalid opcode 0x%x",
57 request->pdu_bhs->bhs_opcode);
58 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
59 if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION)
60 log_debugx("received Logout PDU with invalid reason 0x%x; "
61 "continuing anyway", bhslr->bhslr_reason & 0x7f);
62 if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
63 log_errx(1, "received Logout PDU with decreasing CmdSN: "
64 "was %u, is %u", conn->conn_cmdsn,
65 ntohl(bhslr->bhslr_cmdsn));
67 if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
68 log_errx(1, "received Logout PDU with wrong ExpStatSN: "
69 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
72 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
73 if ((bhslr->bhslr_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
80 logout_new_response(struct pdu *request)
83 struct connection *conn;
84 struct iscsi_bhs_logout_request *bhslr;
85 struct iscsi_bhs_logout_response *bhslr2;
87 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
88 conn = request->pdu_connection;
90 response = pdu_new_response(request);
91 bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
92 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
93 bhslr2->bhslr_flags = 0x80;
94 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
95 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
96 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
97 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
98 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
104 discovery_add_target(struct keys *response_keys, const struct target *targ)
107 struct portal *portal;
109 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
113 keys_add(response_keys, "TargetName", targ->t_name);
114 TAILQ_FOREACH(port, &targ->t_ports, p_ts) {
115 if (port->p_portal_group == NULL)
117 TAILQ_FOREACH(portal, &port->p_portal_group->pg_portals, p_next) {
119 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
120 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
121 NI_NUMERICHOST | NI_NUMERICSERV);
123 log_warnx("getnameinfo: %s", gai_strerror(ret));
126 switch (ai->ai_addr->sa_family) {
128 if (strcmp(hbuf, "0.0.0.0") == 0)
130 ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
131 port->p_portal_group->pg_tag);
134 if (strcmp(hbuf, "::") == 0)
136 ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
137 port->p_portal_group->pg_tag);
143 log_err(1, "asprintf");
144 keys_add(response_keys, "TargetAddress", buf);
151 discovery_target_filtered_out(const struct ctld_connection *conn,
152 const struct port *port)
154 const struct auth_group *ag;
155 const struct portal_group *pg;
156 const struct target *targ;
157 const struct auth *auth;
160 targ = port->p_target;
161 ag = port->p_auth_group;
163 ag = targ->t_auth_group;
164 pg = conn->conn_portal->p_portal_group;
166 assert(pg->pg_discovery_auth_group != PG_FILTER_UNKNOWN);
168 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL &&
169 auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
170 log_debugx("initiator does not match initiator portals "
171 "allowed for target \"%s\"; skipping", targ->t_name);
175 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME &&
176 auth_name_check(ag, conn->conn_initiator_name) != 0) {
177 log_debugx("initiator does not match initiator names "
178 "allowed for target \"%s\"; skipping", targ->t_name);
182 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME_AUTH &&
183 ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
184 if (conn->conn_chap == NULL) {
185 assert(pg->pg_discovery_auth_group->ag_type ==
186 AG_TYPE_NO_AUTHENTICATION);
188 log_debugx("initiator didn't authenticate, but target "
189 "\"%s\" requires CHAP; skipping", targ->t_name);
193 assert(conn->conn_user != NULL);
194 auth = auth_find(ag, conn->conn_user);
196 log_debugx("CHAP user \"%s\" doesn't match target "
197 "\"%s\"; skipping", conn->conn_user, targ->t_name);
201 error = chap_authenticate(conn->conn_chap, auth->a_secret);
203 log_debugx("password for CHAP user \"%s\" doesn't "
204 "match target \"%s\"; skipping",
205 conn->conn_user, targ->t_name);
214 discovery(struct ctld_connection *conn)
216 struct pdu *request, *response;
217 struct keys *request_keys, *response_keys;
218 const struct port *port;
219 const struct portal_group *pg;
220 const char *send_targets;
222 pg = conn->conn_portal->p_portal_group;
224 log_debugx("beginning discovery session; waiting for TextRequest PDU");
225 request_keys = text_read_request(&conn->conn, &request);
227 send_targets = keys_find(request_keys, "SendTargets");
228 if (send_targets == NULL)
229 log_errx(1, "received TextRequest PDU without SendTargets");
231 response_keys = keys_new();
233 if (strcmp(send_targets, "All") == 0) {
234 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
235 if (discovery_target_filtered_out(conn, port)) {
236 /* Ignore this target. */
239 discovery_add_target(response_keys, port->p_target);
242 port = port_find_in_pg(pg, send_targets);
244 log_debugx("initiator requested information on unknown "
245 "target \"%s\"; returning nothing", send_targets);
247 if (discovery_target_filtered_out(conn, port)) {
248 /* Ignore this target. */
250 discovery_add_target(response_keys, port->p_target);
255 text_send_response(request, response_keys);
256 keys_delete(response_keys);
258 keys_delete(request_keys);
260 log_debugx("done sending targets; waiting for Logout PDU");
261 request = logout_receive(&conn->conn);
262 response = logout_new_response(request);
265 pdu_delete(response);
268 log_debugx("discovery session done");