2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
38 #include <netinet/in.h>
40 #include <sys/socket.h>
43 #include "iscsi_proto.h"
46 text_receive(struct connection *conn)
49 struct iscsi_bhs_text_request *bhstr;
51 request = pdu_new(conn);
53 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
54 ISCSI_BHS_OPCODE_TEXT_REQUEST)
55 log_errx(1, "protocol error: received invalid opcode 0x%x",
56 request->pdu_bhs->bhs_opcode);
57 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
59 if ((bhstr->bhstr_flags & ISCSI_BHSTR_FLAGS_FINAL) == 0)
60 log_errx(1, "received Text PDU without the \"F\" flag");
63 * XXX: Implement the C flag some day.
65 if ((bhstr->bhstr_flags & BHSTR_FLAGS_CONTINUE) != 0)
66 log_errx(1, "received Text PDU with unsupported \"C\" flag");
67 if (ISCSI_SNLT(ntohl(bhstr->bhstr_cmdsn), conn->conn_cmdsn)) {
68 log_errx(1, "received Text PDU with decreasing CmdSN: "
69 "was %u, is %u", conn->conn_cmdsn, ntohl(bhstr->bhstr_cmdsn));
71 if (ntohl(bhstr->bhstr_expstatsn) != conn->conn_statsn) {
72 log_errx(1, "received Text PDU with wrong StatSN: "
73 "is %u, should be %u", ntohl(bhstr->bhstr_expstatsn),
76 conn->conn_cmdsn = ntohl(bhstr->bhstr_cmdsn);
77 if ((bhstr->bhstr_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
84 text_new_response(struct pdu *request)
87 struct connection *conn;
88 struct iscsi_bhs_text_request *bhstr;
89 struct iscsi_bhs_text_response *bhstr2;
91 bhstr = (struct iscsi_bhs_text_request *)request->pdu_bhs;
92 conn = request->pdu_connection;
94 response = pdu_new_response(request);
95 bhstr2 = (struct iscsi_bhs_text_response *)response->pdu_bhs;
96 bhstr2->bhstr_opcode = ISCSI_BHS_OPCODE_TEXT_RESPONSE;
97 bhstr2->bhstr_flags = BHSTR_FLAGS_FINAL;
98 bhstr2->bhstr_lun = bhstr->bhstr_lun;
99 bhstr2->bhstr_initiator_task_tag = bhstr->bhstr_initiator_task_tag;
100 bhstr2->bhstr_target_transfer_tag = bhstr->bhstr_target_transfer_tag;
101 bhstr2->bhstr_statsn = htonl(conn->conn_statsn++);
102 bhstr2->bhstr_expcmdsn = htonl(conn->conn_cmdsn);
103 bhstr2->bhstr_maxcmdsn = htonl(conn->conn_cmdsn);
109 logout_receive(struct connection *conn)
112 struct iscsi_bhs_logout_request *bhslr;
114 request = pdu_new(conn);
115 pdu_receive(request);
116 if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
117 ISCSI_BHS_OPCODE_LOGOUT_REQUEST)
118 log_errx(1, "protocol error: received invalid opcode 0x%x",
119 request->pdu_bhs->bhs_opcode);
120 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
121 if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION)
122 log_debugx("received Logout PDU with invalid reason 0x%x; "
123 "continuing anyway", bhslr->bhslr_reason & 0x7f);
124 if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
125 log_errx(1, "received Logout PDU with decreasing CmdSN: "
126 "was %u, is %u", conn->conn_cmdsn,
127 ntohl(bhslr->bhslr_cmdsn));
129 if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
130 log_errx(1, "received Logout PDU with wrong StatSN: "
131 "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
134 conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
135 if ((bhslr->bhslr_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
142 logout_new_response(struct pdu *request)
144 struct pdu *response;
145 struct connection *conn;
146 struct iscsi_bhs_logout_request *bhslr;
147 struct iscsi_bhs_logout_response *bhslr2;
149 bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
150 conn = request->pdu_connection;
152 response = pdu_new_response(request);
153 bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
154 bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
155 bhslr2->bhslr_flags = 0x80;
156 bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
157 bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
158 bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
159 bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
160 bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
166 discovery_add_target(struct keys *response_keys, const struct target *targ)
169 struct portal *portal;
171 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
175 keys_add(response_keys, "TargetName", targ->t_name);
176 TAILQ_FOREACH(port, &targ->t_ports, p_ts) {
177 if (port->p_portal_group == NULL)
179 TAILQ_FOREACH(portal, &port->p_portal_group->pg_portals, p_next) {
181 ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
182 hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
183 NI_NUMERICHOST | NI_NUMERICSERV);
185 log_warnx("getnameinfo: %s", gai_strerror(ret));
188 switch (ai->ai_addr->sa_family) {
190 if (strcmp(hbuf, "0.0.0.0") == 0)
192 ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
193 port->p_portal_group->pg_tag);
196 if (strcmp(hbuf, "::") == 0)
198 ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
199 port->p_portal_group->pg_tag);
205 log_err(1, "asprintf");
206 keys_add(response_keys, "TargetAddress", buf);
213 discovery_target_filtered_out(const struct connection *conn,
214 const struct port *port)
216 const struct auth_group *ag;
217 const struct portal_group *pg;
218 const struct target *targ;
219 const struct auth *auth;
222 targ = port->p_target;
223 ag = port->p_auth_group;
225 ag = targ->t_auth_group;
226 pg = conn->conn_portal->p_portal_group;
228 assert(pg->pg_discovery_auth_group != PG_FILTER_UNKNOWN);
230 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL &&
231 auth_portal_check(ag, &conn->conn_initiator_sa) != 0) {
232 log_debugx("initiator does not match initiator portals "
233 "allowed for target \"%s\"; skipping", targ->t_name);
237 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME &&
238 auth_name_check(ag, conn->conn_initiator_name) != 0) {
239 log_debugx("initiator does not match initiator names "
240 "allowed for target \"%s\"; skipping", targ->t_name);
244 if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME_AUTH &&
245 ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
246 if (conn->conn_chap == NULL) {
247 assert(pg->pg_discovery_auth_group->ag_type ==
248 AG_TYPE_NO_AUTHENTICATION);
250 log_debugx("initiator didn't authenticate, but target "
251 "\"%s\" requires CHAP; skipping", targ->t_name);
255 assert(conn->conn_user != NULL);
256 auth = auth_find(ag, conn->conn_user);
258 log_debugx("CHAP user \"%s\" doesn't match target "
259 "\"%s\"; skipping", conn->conn_user, targ->t_name);
263 error = chap_authenticate(conn->conn_chap, auth->a_secret);
265 log_debugx("password for CHAP user \"%s\" doesn't "
266 "match target \"%s\"; skipping",
267 conn->conn_user, targ->t_name);
276 discovery(struct connection *conn)
278 struct pdu *request, *response;
279 struct keys *request_keys, *response_keys;
280 const struct port *port;
281 const struct portal_group *pg;
282 const char *send_targets;
284 pg = conn->conn_portal->p_portal_group;
286 log_debugx("beginning discovery session; waiting for Text PDU");
287 request = text_receive(conn);
288 request_keys = keys_new();
289 keys_load(request_keys, request);
291 send_targets = keys_find(request_keys, "SendTargets");
292 if (send_targets == NULL)
293 log_errx(1, "received Text PDU without SendTargets");
295 response = text_new_response(request);
296 response_keys = keys_new();
298 if (strcmp(send_targets, "All") == 0) {
299 TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
300 if (discovery_target_filtered_out(conn, port)) {
301 /* Ignore this target. */
304 discovery_add_target(response_keys, port->p_target);
307 port = port_find_in_pg(pg, send_targets);
309 log_debugx("initiator requested information on unknown "
310 "target \"%s\"; returning nothing", send_targets);
312 if (discovery_target_filtered_out(conn, port)) {
313 /* Ignore this target. */
315 discovery_add_target(response_keys, port->p_target);
319 keys_save(response_keys, response);
322 pdu_delete(response);
323 keys_delete(response_keys);
325 keys_delete(request_keys);
327 log_debugx("done sending targets; waiting for Logout PDU");
328 request = logout_receive(conn);
329 response = logout_new_response(request);
332 pdu_delete(response);
335 log_debugx("discovery session done");