2 * SPDX-License-Identifier: BSD-2-Clause
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>
37 #include <netinet/in.h>
39 #include <sys/socket.h>
42 #include "iscsi_proto.h"
45 logout_receive(struct connection *conn)
48 struct iscsi_bhs_logout_request *bhslr;
50 request = pdu_new(conn);
52 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
53 ISCSI_BHS_OPCODE_LOGOUT_REQUEST)
54 log_errx(1, "protocol error: received invalid opcode 0x%x",
55 request->pdu_bhs->bhs_opcode);
56 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
57 if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION)
58 log_debugx("received Logout PDU with invalid reason 0x%x; "
59 "continuing anyway", bhslr->bhslr_reason & 0x7f);
60 if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
61 log_errx(1, "received Logout PDU with decreasing CmdSN: "
62 "was %u, is %u", conn->conn_cmdsn,
63 ntohl(bhslr->bhslr_cmdsn));
65 if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
66 log_errx(1, "received Logout PDU with wrong ExpStatSN: "
67 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
70 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
71 if ((bhslr->bhslr_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
78 logout_new_response(struct pdu *request)
81 struct connection *conn;
82 struct iscsi_bhs_logout_request *bhslr;
83 struct iscsi_bhs_logout_response *bhslr2;
85 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
86 conn = request->pdu_connection;
88 response = pdu_new_response(request);
89 bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
90 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
91 bhslr2->bhslr_flags = 0x80;
92 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
93 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
94 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
95 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
96 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
102 discovery_add_target(struct keys *response_keys, const struct target *targ)
105 struct portal *portal;
107 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
111 keys_add(response_keys, "TargetName", targ->t_name);
112 TAILQ_FOREACH(port, &targ->t_ports, p_ts) {
113 if (port->p_portal_group == NULL)
115 TAILQ_FOREACH(portal, &port->p_portal_group->pg_portals, p_next) {
117 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
118 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
119 NI_NUMERICHOST | NI_NUMERICSERV);
121 log_warnx("getnameinfo: %s", gai_strerror(ret));
124 switch (ai->ai_addr->sa_family) {
126 if (strcmp(hbuf, "0.0.0.0") == 0)
128 ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
129 port->p_portal_group->pg_tag);
132 if (strcmp(hbuf, "::") == 0)
134 ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
135 port->p_portal_group->pg_tag);
141 log_err(1, "asprintf");
142 keys_add(response_keys, "TargetAddress", buf);
149 discovery_target_filtered_out(const struct ctld_connection *conn,
150 const struct port *port)
152 const struct auth_group *ag;
153 const struct portal_group *pg;
154 const struct target *targ;
155 const struct auth *auth;
158 targ = port->p_target;
159 ag = port->p_auth_group;
161 ag = targ->t_auth_group;
162 pg = conn->conn_portal->p_portal_group;
164 assert(pg->pg_discovery_auth_group != PG_FILTER_UNKNOWN);
166 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL &&
167 auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
168 log_debugx("initiator does not match initiator portals "
169 "allowed for target \"%s\"; skipping", targ->t_name);
173 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME &&
174 auth_name_check(ag, conn->conn_initiator_name) != 0) {
175 log_debugx("initiator does not match initiator names "
176 "allowed for target \"%s\"; skipping", targ->t_name);
180 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME_AUTH &&
181 ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
182 if (conn->conn_chap == NULL) {
183 assert(pg->pg_discovery_auth_group->ag_type ==
184 AG_TYPE_NO_AUTHENTICATION);
186 log_debugx("initiator didn't authenticate, but target "
187 "\"%s\" requires CHAP; skipping", targ->t_name);
191 assert(conn->conn_user != NULL);
192 auth = auth_find(ag, conn->conn_user);
194 log_debugx("CHAP user \"%s\" doesn't match target "
195 "\"%s\"; skipping", conn->conn_user, targ->t_name);
199 error = chap_authenticate(conn->conn_chap, auth->a_secret);
201 log_debugx("password for CHAP user \"%s\" doesn't "
202 "match target \"%s\"; skipping",
203 conn->conn_user, targ->t_name);
212 discovery(struct ctld_connection *conn)
214 struct pdu *request, *response;
215 struct keys *request_keys, *response_keys;
216 const struct port *port;
217 const struct portal_group *pg;
218 const char *send_targets;
220 pg = conn->conn_portal->p_portal_group;
222 log_debugx("beginning discovery session; waiting for TextRequest PDU");
223 request_keys = text_read_request(&conn->conn, &request);
225 send_targets = keys_find(request_keys, "SendTargets");
226 if (send_targets == NULL)
227 log_errx(1, "received TextRequest PDU without SendTargets");
229 response_keys = keys_new();
231 if (strcmp(send_targets, "All") == 0) {
232 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
233 if (discovery_target_filtered_out(conn, port)) {
234 /* Ignore this target. */
237 discovery_add_target(response_keys, port->p_target);
240 port = port_find_in_pg(pg, send_targets);
242 log_debugx("initiator requested information on unknown "
243 "target \"%s\"; returning nothing", send_targets);
245 if (discovery_target_filtered_out(conn, port)) {
246 /* Ignore this target. */
248 discovery_add_target(response_keys, port->p_target);
253 text_send_response(request, response_keys);
254 keys_delete(response_keys);
256 keys_delete(request_keys);
258 log_debugx("done sending targets; waiting for Logout PDU");
259 request = logout_receive(&conn->conn);
260 response = logout_new_response(request);
263 pdu_delete(response);
266 log_debugx("discovery session done");