xref: /freebsd/usr.sbin/ctld/discovery.cc (revision 839d0755fea8ef761698b50fa5f293546ae832a8)
1*839d0755SJohn Baldwin /*-
2*839d0755SJohn Baldwin  * SPDX-License-Identifier: BSD-2-Clause
3*839d0755SJohn Baldwin  *
4*839d0755SJohn Baldwin  * Copyright (c) 2012 The FreeBSD Foundation
5*839d0755SJohn Baldwin  *
6*839d0755SJohn Baldwin  * This software was developed by Edward Tomasz Napierala under sponsorship
7*839d0755SJohn Baldwin  * from the FreeBSD Foundation.
8*839d0755SJohn Baldwin  *
9*839d0755SJohn Baldwin  * Redistribution and use in source and binary forms, with or without
10*839d0755SJohn Baldwin  * modification, are permitted provided that the following conditions
11*839d0755SJohn Baldwin  * are met:
12*839d0755SJohn Baldwin  * 1. Redistributions of source code must retain the above copyright
13*839d0755SJohn Baldwin  *    notice, this list of conditions and the following disclaimer.
14*839d0755SJohn Baldwin  * 2. Redistributions in binary form must reproduce the above copyright
15*839d0755SJohn Baldwin  *    notice, this list of conditions and the following disclaimer in the
16*839d0755SJohn Baldwin  *    documentation and/or other materials provided with the distribution.
17*839d0755SJohn Baldwin  *
18*839d0755SJohn Baldwin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19*839d0755SJohn Baldwin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*839d0755SJohn Baldwin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*839d0755SJohn Baldwin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22*839d0755SJohn Baldwin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*839d0755SJohn Baldwin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*839d0755SJohn Baldwin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*839d0755SJohn Baldwin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*839d0755SJohn Baldwin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*839d0755SJohn Baldwin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*839d0755SJohn Baldwin  * SUCH DAMAGE.
29*839d0755SJohn Baldwin  *
30*839d0755SJohn Baldwin  */
31*839d0755SJohn Baldwin 
32*839d0755SJohn Baldwin #include <sys/cdefs.h>
33*839d0755SJohn Baldwin #include <assert.h>
34*839d0755SJohn Baldwin #include <stdio.h>
35*839d0755SJohn Baldwin #include <stdlib.h>
36*839d0755SJohn Baldwin #include <string.h>
37*839d0755SJohn Baldwin #include <netinet/in.h>
38*839d0755SJohn Baldwin #include <netdb.h>
39*839d0755SJohn Baldwin #include <sys/socket.h>
40*839d0755SJohn Baldwin 
41*839d0755SJohn Baldwin #include "ctld.h"
42*839d0755SJohn Baldwin #include "iscsi_proto.h"
43*839d0755SJohn Baldwin 
44*839d0755SJohn Baldwin static struct pdu *
logout_receive(struct connection * conn)45*839d0755SJohn Baldwin logout_receive(struct connection *conn)
46*839d0755SJohn Baldwin {
47*839d0755SJohn Baldwin 	struct pdu *request;
48*839d0755SJohn Baldwin 	struct iscsi_bhs_logout_request *bhslr;
49*839d0755SJohn Baldwin 
50*839d0755SJohn Baldwin 	request = pdu_new(conn);
51*839d0755SJohn Baldwin 	pdu_receive(request);
52*839d0755SJohn Baldwin 	if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) !=
53*839d0755SJohn Baldwin 	    ISCSI_BHS_OPCODE_LOGOUT_REQUEST)
54*839d0755SJohn Baldwin 		log_errx(1, "protocol error: received invalid opcode 0x%x",
55*839d0755SJohn Baldwin 		    request->pdu_bhs->bhs_opcode);
56*839d0755SJohn Baldwin 	bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
57*839d0755SJohn Baldwin 	if ((bhslr->bhslr_reason & 0x7f) != BHSLR_REASON_CLOSE_SESSION)
58*839d0755SJohn Baldwin 		log_debugx("received Logout PDU with invalid reason 0x%x; "
59*839d0755SJohn Baldwin 		    "continuing anyway", bhslr->bhslr_reason & 0x7f);
60*839d0755SJohn Baldwin 	if (ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) {
61*839d0755SJohn Baldwin 		log_errx(1, "received Logout PDU with decreasing CmdSN: "
62*839d0755SJohn Baldwin 		    "was %u, is %u", conn->conn_cmdsn,
63*839d0755SJohn Baldwin 		    ntohl(bhslr->bhslr_cmdsn));
64*839d0755SJohn Baldwin 	}
65*839d0755SJohn Baldwin 	if (ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) {
66*839d0755SJohn Baldwin 		log_errx(1, "received Logout PDU with wrong ExpStatSN: "
67*839d0755SJohn Baldwin 		    "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn),
68*839d0755SJohn Baldwin 		    conn->conn_statsn);
69*839d0755SJohn Baldwin 	}
70*839d0755SJohn Baldwin 	conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn);
71*839d0755SJohn Baldwin 	if ((bhslr->bhslr_opcode & ISCSI_BHS_OPCODE_IMMEDIATE) == 0)
72*839d0755SJohn Baldwin 		conn->conn_cmdsn++;
73*839d0755SJohn Baldwin 
74*839d0755SJohn Baldwin 	return (request);
75*839d0755SJohn Baldwin }
76*839d0755SJohn Baldwin 
77*839d0755SJohn Baldwin static struct pdu *
logout_new_response(struct pdu * request)78*839d0755SJohn Baldwin logout_new_response(struct pdu *request)
79*839d0755SJohn Baldwin {
80*839d0755SJohn Baldwin 	struct pdu *response;
81*839d0755SJohn Baldwin 	struct connection *conn;
82*839d0755SJohn Baldwin 	struct iscsi_bhs_logout_request *bhslr;
83*839d0755SJohn Baldwin 	struct iscsi_bhs_logout_response *bhslr2;
84*839d0755SJohn Baldwin 
85*839d0755SJohn Baldwin 	bhslr = (struct iscsi_bhs_logout_request *)request->pdu_bhs;
86*839d0755SJohn Baldwin 	conn = request->pdu_connection;
87*839d0755SJohn Baldwin 
88*839d0755SJohn Baldwin 	response = pdu_new_response(request);
89*839d0755SJohn Baldwin 	bhslr2 = (struct iscsi_bhs_logout_response *)response->pdu_bhs;
90*839d0755SJohn Baldwin 	bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGOUT_RESPONSE;
91*839d0755SJohn Baldwin 	bhslr2->bhslr_flags = 0x80;
92*839d0755SJohn Baldwin 	bhslr2->bhslr_response = BHSLR_RESPONSE_CLOSED_SUCCESSFULLY;
93*839d0755SJohn Baldwin 	bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag;
94*839d0755SJohn Baldwin 	bhslr2->bhslr_statsn = htonl(conn->conn_statsn++);
95*839d0755SJohn Baldwin 	bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn);
96*839d0755SJohn Baldwin 	bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn);
97*839d0755SJohn Baldwin 
98*839d0755SJohn Baldwin 	return (response);
99*839d0755SJohn Baldwin }
100*839d0755SJohn Baldwin 
101*839d0755SJohn Baldwin static void
discovery_add_target(struct keys * response_keys,const struct target * targ)102*839d0755SJohn Baldwin discovery_add_target(struct keys *response_keys, const struct target *targ)
103*839d0755SJohn Baldwin {
104*839d0755SJohn Baldwin 	struct port *port;
105*839d0755SJohn Baldwin 	struct portal *portal;
106*839d0755SJohn Baldwin 	char *buf;
107*839d0755SJohn Baldwin 	char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
108*839d0755SJohn Baldwin 	struct addrinfo *ai;
109*839d0755SJohn Baldwin 	int ret;
110*839d0755SJohn Baldwin 
111*839d0755SJohn Baldwin 	keys_add(response_keys, "TargetName", targ->t_name);
112*839d0755SJohn Baldwin 	TAILQ_FOREACH(port, &targ->t_ports, p_ts) {
113*839d0755SJohn Baldwin 	    if (port->p_portal_group == NULL)
114*839d0755SJohn Baldwin 		continue;
115*839d0755SJohn Baldwin 	    TAILQ_FOREACH(portal, &port->p_portal_group->pg_portals, p_next) {
116*839d0755SJohn Baldwin 		ai = portal->p_ai;
117*839d0755SJohn Baldwin 		ret = getnameinfo(ai->ai_addr, ai->ai_addrlen,
118*839d0755SJohn Baldwin 		    hbuf, sizeof(hbuf), sbuf, sizeof(sbuf),
119*839d0755SJohn Baldwin 		    NI_NUMERICHOST | NI_NUMERICSERV);
120*839d0755SJohn Baldwin 		if (ret != 0) {
121*839d0755SJohn Baldwin 			log_warnx("getnameinfo: %s", gai_strerror(ret));
122*839d0755SJohn Baldwin 			continue;
123*839d0755SJohn Baldwin 		}
124*839d0755SJohn Baldwin 		switch (ai->ai_addr->sa_family) {
125*839d0755SJohn Baldwin 		case AF_INET:
126*839d0755SJohn Baldwin 			if (strcmp(hbuf, "0.0.0.0") == 0)
127*839d0755SJohn Baldwin 				continue;
128*839d0755SJohn Baldwin 			ret = asprintf(&buf, "%s:%s,%d", hbuf, sbuf,
129*839d0755SJohn Baldwin 			    port->p_portal_group->pg_tag);
130*839d0755SJohn Baldwin 			break;
131*839d0755SJohn Baldwin 		case AF_INET6:
132*839d0755SJohn Baldwin 			if (strcmp(hbuf, "::") == 0)
133*839d0755SJohn Baldwin 				continue;
134*839d0755SJohn Baldwin 			ret = asprintf(&buf, "[%s]:%s,%d", hbuf, sbuf,
135*839d0755SJohn Baldwin 			    port->p_portal_group->pg_tag);
136*839d0755SJohn Baldwin 			break;
137*839d0755SJohn Baldwin 		default:
138*839d0755SJohn Baldwin 			continue;
139*839d0755SJohn Baldwin 		}
140*839d0755SJohn Baldwin 		if (ret <= 0)
141*839d0755SJohn Baldwin 		    log_err(1, "asprintf");
142*839d0755SJohn Baldwin 		keys_add(response_keys, "TargetAddress", buf);
143*839d0755SJohn Baldwin 		free(buf);
144*839d0755SJohn Baldwin 	    }
145*839d0755SJohn Baldwin 	}
146*839d0755SJohn Baldwin }
147*839d0755SJohn Baldwin 
148*839d0755SJohn Baldwin static bool
discovery_target_filtered_out(const struct ctld_connection * conn,const struct port * port)149*839d0755SJohn Baldwin discovery_target_filtered_out(const struct ctld_connection *conn,
150*839d0755SJohn Baldwin     const struct port *port)
151*839d0755SJohn Baldwin {
152*839d0755SJohn Baldwin 	const struct auth_group *ag;
153*839d0755SJohn Baldwin 	const struct portal_group *pg;
154*839d0755SJohn Baldwin 	const struct target *targ;
155*839d0755SJohn Baldwin 	const struct auth *auth;
156*839d0755SJohn Baldwin 	int error;
157*839d0755SJohn Baldwin 
158*839d0755SJohn Baldwin 	targ = port->p_target;
159*839d0755SJohn Baldwin 	ag = port->p_auth_group;
160*839d0755SJohn Baldwin 	if (ag == NULL)
161*839d0755SJohn Baldwin 		ag = targ->t_auth_group;
162*839d0755SJohn Baldwin 	pg = conn->conn_portal->p_portal_group;
163*839d0755SJohn Baldwin 
164*839d0755SJohn Baldwin 	assert(pg->pg_discovery_filter != PG_FILTER_UNKNOWN);
165*839d0755SJohn Baldwin 
166*839d0755SJohn Baldwin 	if (pg->pg_discovery_filter >= PG_FILTER_PORTAL &&
167*839d0755SJohn Baldwin 	    !auth_portal_check(ag, &conn->conn_initiator_sa)) {
168*839d0755SJohn Baldwin 		log_debugx("initiator does not match initiator portals "
169*839d0755SJohn Baldwin 		    "allowed for target \"%s\"; skipping", targ->t_name);
170*839d0755SJohn Baldwin 		return (true);
171*839d0755SJohn Baldwin 	}
172*839d0755SJohn Baldwin 
173*839d0755SJohn Baldwin 	if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME &&
174*839d0755SJohn Baldwin 	    !auth_name_check(ag, conn->conn_initiator_name)) {
175*839d0755SJohn Baldwin 		log_debugx("initiator does not match initiator names "
176*839d0755SJohn Baldwin 		    "allowed for target \"%s\"; skipping", targ->t_name);
177*839d0755SJohn Baldwin 		return (true);
178*839d0755SJohn Baldwin 	}
179*839d0755SJohn Baldwin 
180*839d0755SJohn Baldwin 	if (pg->pg_discovery_filter >= PG_FILTER_PORTAL_NAME_AUTH &&
181*839d0755SJohn Baldwin 	    ag->ag_type != AG_TYPE_NO_AUTHENTICATION) {
182*839d0755SJohn Baldwin 		if (conn->conn_chap == NULL) {
183*839d0755SJohn Baldwin 			assert(pg->pg_discovery_auth_group->ag_type ==
184*839d0755SJohn Baldwin 			    AG_TYPE_NO_AUTHENTICATION);
185*839d0755SJohn Baldwin 
186*839d0755SJohn Baldwin 			log_debugx("initiator didn't authenticate, but target "
187*839d0755SJohn Baldwin 			    "\"%s\" requires CHAP; skipping", targ->t_name);
188*839d0755SJohn Baldwin 			return (true);
189*839d0755SJohn Baldwin 		}
190*839d0755SJohn Baldwin 
191*839d0755SJohn Baldwin 		assert(conn->conn_user != NULL);
192*839d0755SJohn Baldwin 		auth = auth_find(ag, conn->conn_user);
193*839d0755SJohn Baldwin 		if (auth == NULL) {
194*839d0755SJohn Baldwin 			log_debugx("CHAP user \"%s\" doesn't match target "
195*839d0755SJohn Baldwin 			    "\"%s\"; skipping", conn->conn_user, targ->t_name);
196*839d0755SJohn Baldwin 			return (true);
197*839d0755SJohn Baldwin 		}
198*839d0755SJohn Baldwin 
199*839d0755SJohn Baldwin 		error = chap_authenticate(conn->conn_chap, auth->a_secret);
200*839d0755SJohn Baldwin 		if (error != 0) {
201*839d0755SJohn Baldwin 			log_debugx("password for CHAP user \"%s\" doesn't "
202*839d0755SJohn Baldwin 			    "match target \"%s\"; skipping",
203*839d0755SJohn Baldwin 			    conn->conn_user, targ->t_name);
204*839d0755SJohn Baldwin 			return (true);
205*839d0755SJohn Baldwin 		}
206*839d0755SJohn Baldwin 	}
207*839d0755SJohn Baldwin 
208*839d0755SJohn Baldwin 	return (false);
209*839d0755SJohn Baldwin }
210*839d0755SJohn Baldwin 
211*839d0755SJohn Baldwin void
discovery(struct ctld_connection * conn)212*839d0755SJohn Baldwin discovery(struct ctld_connection *conn)
213*839d0755SJohn Baldwin {
214*839d0755SJohn Baldwin 	struct pdu *request, *response;
215*839d0755SJohn Baldwin 	struct keys *request_keys, *response_keys;
216*839d0755SJohn Baldwin 	const struct port *port;
217*839d0755SJohn Baldwin 	const struct portal_group *pg;
218*839d0755SJohn Baldwin 	const char *send_targets;
219*839d0755SJohn Baldwin 
220*839d0755SJohn Baldwin 	pg = conn->conn_portal->p_portal_group;
221*839d0755SJohn Baldwin 
222*839d0755SJohn Baldwin 	log_debugx("beginning discovery session; waiting for TextRequest PDU");
223*839d0755SJohn Baldwin 	request_keys = text_read_request(&conn->conn, &request);
224*839d0755SJohn Baldwin 
225*839d0755SJohn Baldwin 	send_targets = keys_find(request_keys, "SendTargets");
226*839d0755SJohn Baldwin 	if (send_targets == NULL)
227*839d0755SJohn Baldwin 		log_errx(1, "received TextRequest PDU without SendTargets");
228*839d0755SJohn Baldwin 
229*839d0755SJohn Baldwin 	response_keys = keys_new();
230*839d0755SJohn Baldwin 
231*839d0755SJohn Baldwin 	if (strcmp(send_targets, "All") == 0) {
232*839d0755SJohn Baldwin 		TAILQ_FOREACH(port, &pg->pg_ports, p_pgs) {
233*839d0755SJohn Baldwin 			if (discovery_target_filtered_out(conn, port)) {
234*839d0755SJohn Baldwin 				/* Ignore this target. */
235*839d0755SJohn Baldwin 				continue;
236*839d0755SJohn Baldwin 			}
237*839d0755SJohn Baldwin 			discovery_add_target(response_keys, port->p_target);
238*839d0755SJohn Baldwin 		}
239*839d0755SJohn Baldwin 	} else {
240*839d0755SJohn Baldwin 		port = port_find_in_pg(pg, send_targets);
241*839d0755SJohn Baldwin 		if (port == NULL) {
242*839d0755SJohn Baldwin 			log_debugx("initiator requested information on unknown "
243*839d0755SJohn Baldwin 			    "target \"%s\"; returning nothing", send_targets);
244*839d0755SJohn Baldwin 		} else {
245*839d0755SJohn Baldwin 			if (discovery_target_filtered_out(conn, port)) {
246*839d0755SJohn Baldwin 				/* Ignore this target. */
247*839d0755SJohn Baldwin 			} else {
248*839d0755SJohn Baldwin 				discovery_add_target(response_keys, port->p_target);
249*839d0755SJohn Baldwin 			}
250*839d0755SJohn Baldwin 		}
251*839d0755SJohn Baldwin 	}
252*839d0755SJohn Baldwin 
253*839d0755SJohn Baldwin 	text_send_response(request, response_keys);
254*839d0755SJohn Baldwin 	keys_delete(response_keys);
255*839d0755SJohn Baldwin 	pdu_delete(request);
256*839d0755SJohn Baldwin 	keys_delete(request_keys);
257*839d0755SJohn Baldwin 
258*839d0755SJohn Baldwin 	log_debugx("done sending targets; waiting for Logout PDU");
259*839d0755SJohn Baldwin 	request = logout_receive(&conn->conn);
260*839d0755SJohn Baldwin 	response = logout_new_response(request);
261*839d0755SJohn Baldwin 
262*839d0755SJohn Baldwin 	pdu_send(response);
263*839d0755SJohn Baldwin 	pdu_delete(response);
264*839d0755SJohn Baldwin 	pdu_delete(request);
265*839d0755SJohn Baldwin 
266*839d0755SJohn Baldwin 	log_debugx("discovery session done");
267*839d0755SJohn Baldwin }
268