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 text_receive(struct connection *conn)
50 struct iscsi_bhs_text_request *bhstr;
52 request = pdu_new(conn);
54 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
55 ISCSI_BHS_OPCODE_TEXT_REQUEST)
56 log_errx(1, "protocol error: received invalid opcode 0x%x",
57 request->pdu_bhs->bhs_opcode);
58 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
60 if ((bhstr->bhstr_flags & ISCSI_BHSTR_FLAGS_FINAL) == 0)
61 log_errx(1, "received Text PDU without the \"F\" flag");
64 * XXX: Implement the C flag some day.
66 if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
67 log_errx(1, "received Text PDU with unsupported \"C\" flag");
68 if (ISCSI_SNLT(ntohl(bhstr->bhstr_cmdsn), conn->conn_cmdsn)) {
69 log_errx(1, "received Text PDU with decreasing CmdSN: "
70 "was %u, is %u", conn->conn_cmdsn, ntohl(bhstr->bhstr_cmdsn));
72 if (ntohl(bhstr->bhstr_expstatsn) != conn->conn_statsn) {
73 log_errx(1, "received Text PDU with wrong ExpStatSN: "
74 "is %u, should be %u", ntohl(bhstr->bhstr_expstatsn),
77 conn->conn_cmdsn = ntohl(bhstr->bhstr_cmdsn);
78 if ((bhstr->bhstr_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
85 text_new_response(struct pdu *request)
88 struct connection *conn;
89 struct iscsi_bhs_text_request *bhstr;
90 struct iscsi_bhs_text_response *bhstr2;
92 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
93 conn = request->pdu_connection;
95 response = pdu_new_response(request);
96 bhstr2 = (struct iscsi_bhs_text_response *)response->pdu_bhs;
97 bhstr2->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_RESPONSE;
98 bhstr2->bhstr_flags = BHSTR_FLAGS_FINAL;
99 bhstr2->bhstr_lun = bhstr->bhstr_lun;
100 bhstr2->bhstr_initiator_task_tag = bhstr->bhstr_initiator_task_tag;
101 bhstr2->bhstr_target_transfer_tag = bhstr->bhstr_target_transfer_tag;
102 bhstr2->bhstr_statsn = htonl(conn->conn_statsn++);
103 bhstr2->bhstr_expcmdsn = htonl(conn->conn_cmdsn);
104 bhstr2->bhstr_maxcmdsn = htonl(conn->conn_cmdsn);
110 logout_receive(struct connection *conn)
113 struct iscsi_bhs_logout_request *bhslr;
115 request = pdu_new(conn);
116 pdu_receive(request);
117 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
118 ISCSI_BHS_OPCODE_LOGOUT_REQUEST)
119 log_errx(1, "protocol error: received invalid opcode 0x%x",
120 request->pdu_bhs->bhs_opcode);
121 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
122 if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION)
123 log_debugx("received Logout PDU with invalid reason 0x%x; "
124 "continuing anyway", bhslr->bhslr_reason & 0x7f);
125 if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
126 log_errx(1, "received Logout PDU with decreasing CmdSN: "
127 "was %u, is %u", conn->conn_cmdsn,
128 ntohl(bhslr->bhslr_cmdsn));
130 if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
131 log_errx(1, "received Logout PDU with wrong ExpStatSN: "
132 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
135 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
136 if ((bhslr->bhslr_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
143 logout_new_response(struct pdu *request)
145 struct pdu *response;
146 struct connection *conn;
147 struct iscsi_bhs_logout_request *bhslr;
148 struct iscsi_bhs_logout_response *bhslr2;
150 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
151 conn = request->pdu_connection;
153 response = pdu_new_response(request);
154 bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
155 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
156 bhslr2->bhslr_flags = 0x80;
157 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
158 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
159 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
160 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
161 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
167 discovery_add_target(struct keys *response_keys, const struct target *targ)
170 struct portal *portal;
172 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
176 keys_add(response_keys, "TargetName", targ->t_name);
177 TAILQ_FOREACH(port, &targ->t_ports, p_ts) {
178 if (port->p_portal_group == NULL)
180 TAILQ_FOREACH(portal, &port->p_portal_group->pg_portals, p_next) {
182 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
183 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
184 NI_NUMERICHOST | NI_NUMERICSERV);
186 log_warnx("getnameinfo: %s", gai_strerror(ret));
189 switch (ai->ai_addr->sa_family) {
191 if (strcmp(hbuf, "0.0.0.0") == 0)
193 ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
194 port->p_portal_group->pg_tag);
197 if (strcmp(hbuf, "::") == 0)
199 ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
200 port->p_portal_group->pg_tag);
206 log_err(1, "asprintf");
207 keys_add(response_keys, "TargetAddress", buf);
214 discovery_target_filtered_out(const struct connection *conn,
215 const struct port *port)
217 const struct auth_group *ag;
218 const struct portal_group *pg;
219 const struct target *targ;
220 const struct auth *auth;
223 targ = port->p_target;
224 ag = port->p_auth_group;
226 ag = targ->t_auth_group;
227 pg = conn->conn_portal->p_portal_group;
229 assert(pg->pg_discovery_auth_group != PG_FILTER_UNKNOWN);
231 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL &&
232 auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
233 log_debugx("initiator does not match initiator portals "
234 "allowed for target \"%s\"; skipping", targ->t_name);
238 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME &&
239 auth_name_check(ag, conn->conn_initiator_name) != 0) {
240 log_debugx("initiator does not match initiator names "
241 "allowed for target \"%s\"; skipping", targ->t_name);
245 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME_AUTH &&
246 ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
247 if (conn->conn_chap == NULL) {
248 assert(pg->pg_discovery_auth_group->ag_type ==
249 AG_TYPE_NO_AUTHENTICATION);
251 log_debugx("initiator didn't authenticate, but target "
252 "\"%s\" requires CHAP; skipping", targ->t_name);
256 assert(conn->conn_user != NULL);
257 auth = auth_find(ag, conn->conn_user);
259 log_debugx("CHAP user \"%s\" doesn't match target "
260 "\"%s\"; skipping", conn->conn_user, targ->t_name);
264 error = chap_authenticate(conn->conn_chap, auth->a_secret);
266 log_debugx("password for CHAP user \"%s\" doesn't "
267 "match target \"%s\"; skipping",
268 conn->conn_user, targ->t_name);
277 discovery(struct connection *conn)
279 struct pdu *request, *response;
280 struct keys *request_keys, *response_keys;
281 const struct port *port;
282 const struct portal_group *pg;
283 const char *send_targets;
285 pg = conn->conn_portal->p_portal_group;
287 log_debugx("beginning discovery session; waiting for Text PDU");
288 request = text_receive(conn);
289 request_keys = keys_new();
290 keys_load(request_keys, request);
292 send_targets = keys_find(request_keys, "SendTargets");
293 if (send_targets == NULL)
294 log_errx(1, "received Text PDU without SendTargets");
296 response = text_new_response(request);
297 response_keys = keys_new();
299 if (strcmp(send_targets, "All") == 0) {
300 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
301 if (discovery_target_filtered_out(conn, port)) {
302 /* Ignore this target. */
305 discovery_add_target(response_keys, port->p_target);
308 port = port_find_in_pg(pg, send_targets);
310 log_debugx("initiator requested information on unknown "
311 "target \"%s\"; returning nothing", send_targets);
313 if (discovery_target_filtered_out(conn, port)) {
314 /* Ignore this target. */
316 discovery_add_target(response_keys, port->p_target);
320 keys_save(response_keys, response);
323 pdu_delete(response);
324 keys_delete(response_keys);
326 keys_delete(request_keys);
328 log_debugx("done sending targets; waiting for Logout PDU");
329 request = logout_receive(conn);
330 response = logout_new_response(request);
333 pdu_delete(response);
336 log_debugx("discovery session done");