xref: /freebsd/usr.sbin/iscsid/iscsid.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1009ea47eSEdward Tomasz Napierala /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4009ea47eSEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
5009ea47eSEdward Tomasz Napierala  *
6009ea47eSEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
7009ea47eSEdward Tomasz Napierala  * from the FreeBSD Foundation.
8009ea47eSEdward Tomasz Napierala  *
9009ea47eSEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
10009ea47eSEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
11009ea47eSEdward Tomasz Napierala  * are met:
12009ea47eSEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
13009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
14009ea47eSEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
15009ea47eSEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
16009ea47eSEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
17009ea47eSEdward Tomasz Napierala  *
18009ea47eSEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19009ea47eSEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20009ea47eSEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21009ea47eSEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22009ea47eSEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23009ea47eSEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24009ea47eSEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25009ea47eSEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26009ea47eSEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27009ea47eSEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28009ea47eSEdward Tomasz Napierala  * SUCH DAMAGE.
29009ea47eSEdward Tomasz Napierala  *
30009ea47eSEdward Tomasz Napierala  */
31009ea47eSEdward Tomasz Napierala 
32009ea47eSEdward Tomasz Napierala #include <sys/types.h>
33009ea47eSEdward Tomasz Napierala #include <sys/time.h>
34009ea47eSEdward Tomasz Napierala #include <sys/ioctl.h>
35009ea47eSEdward Tomasz Napierala #include <sys/param.h>
36009ea47eSEdward Tomasz Napierala #include <sys/linker.h>
37009ea47eSEdward Tomasz Napierala #include <sys/socket.h>
38bd6bb493SRichard Scheffenegger #include <sys/sysctl.h>
39b881b8beSRobert Watson #include <sys/capsicum.h>
40009ea47eSEdward Tomasz Napierala #include <sys/wait.h>
41bfabdadeSRichard Scheffenegger #include <netinet/in.h>
42bd6bb493SRichard Scheffenegger #include <netinet/tcp.h>
43009ea47eSEdward Tomasz Napierala #include <assert.h>
441489776dSMariusz Zaborski #include <capsicum_helpers.h>
45009ea47eSEdward Tomasz Napierala #include <errno.h>
46009ea47eSEdward Tomasz Napierala #include <fcntl.h>
47592d6e85SEdward Tomasz Napierala #include <libutil.h>
48009ea47eSEdward Tomasz Napierala #include <netdb.h>
49009ea47eSEdward Tomasz Napierala #include <signal.h>
50009ea47eSEdward Tomasz Napierala #include <stdbool.h>
51009ea47eSEdward Tomasz Napierala #include <stdint.h>
52009ea47eSEdward Tomasz Napierala #include <stdio.h>
53009ea47eSEdward Tomasz Napierala #include <stdlib.h>
54009ea47eSEdward Tomasz Napierala #include <string.h>
55009ea47eSEdward Tomasz Napierala #include <unistd.h>
56009ea47eSEdward Tomasz Napierala 
57009ea47eSEdward Tomasz Napierala #include "iscsid.h"
58009ea47eSEdward Tomasz Napierala 
5963783933SJohn Baldwin static bool	timed_out(void);
6063783933SJohn Baldwin #ifdef ICL_KERNEL_PROXY
6163783933SJohn Baldwin static void	pdu_receive_proxy(struct pdu *pdu);
6263783933SJohn Baldwin static void	pdu_send_proxy(struct pdu *pdu);
6363783933SJohn Baldwin #endif /* ICL_KERNEL_PROXY */
6463783933SJohn Baldwin 
65009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false;
66009ea47eSEdward Tomasz Napierala 
67009ea47eSEdward Tomasz Napierala static int nchildren = 0;
68009ea47eSEdward Tomasz Napierala 
6963783933SJohn Baldwin static struct connection_ops conn_ops = {
7063783933SJohn Baldwin 	.timed_out = timed_out,
7163783933SJohn Baldwin #ifdef ICL_KERNEL_PROXY
7263783933SJohn Baldwin 	.pdu_receive_proxy = pdu_receive_proxy,
7363783933SJohn Baldwin 	.pdu_send_proxy = pdu_send_proxy,
7463783933SJohn Baldwin #endif
7563783933SJohn Baldwin 	.fail = fail,
7663783933SJohn Baldwin };
7763783933SJohn Baldwin 
78009ea47eSEdward Tomasz Napierala static void
usage(void)79009ea47eSEdward Tomasz Napierala usage(void)
80009ea47eSEdward Tomasz Napierala {
81009ea47eSEdward Tomasz Napierala 
82009ea47eSEdward Tomasz Napierala 	fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n");
83009ea47eSEdward Tomasz Napierala 	exit(1);
84009ea47eSEdward Tomasz Napierala }
85009ea47eSEdward Tomasz Napierala 
8663783933SJohn Baldwin #ifdef ICL_KERNEL_PROXY
87009ea47eSEdward Tomasz Napierala 
8863783933SJohn Baldwin static void
pdu_receive_proxy(struct pdu * pdu)8963783933SJohn Baldwin pdu_receive_proxy(struct pdu *pdu)
9063783933SJohn Baldwin {
9163783933SJohn Baldwin 	struct iscsid_connection *conn;
9263783933SJohn Baldwin 	struct iscsi_daemon_receive idr;
9363783933SJohn Baldwin 	size_t len;
9463783933SJohn Baldwin 	int error;
9563783933SJohn Baldwin 
9663783933SJohn Baldwin 	conn = (struct iscsid_connection *)pdu->pdu_connection;
9763783933SJohn Baldwin 	assert(conn->conn_conf.isc_iser != 0);
9863783933SJohn Baldwin 
9963783933SJohn Baldwin 	pdu->pdu_data = malloc(conn->conn.conn_max_recv_data_segment_length);
10063783933SJohn Baldwin 	if (pdu->pdu_data == NULL)
10163783933SJohn Baldwin 		log_err(1, "malloc");
10263783933SJohn Baldwin 
10363783933SJohn Baldwin 	memset(&idr, 0, sizeof(idr));
10463783933SJohn Baldwin 	idr.idr_session_id = conn->conn_session_id;
10563783933SJohn Baldwin 	idr.idr_bhs = pdu->pdu_bhs;
10663783933SJohn Baldwin 	idr.idr_data_segment_len = conn->conn.conn_max_recv_data_segment_length;
10763783933SJohn Baldwin 	idr.idr_data_segment = pdu->pdu_data;
10863783933SJohn Baldwin 
10963783933SJohn Baldwin 	error = ioctl(conn->conn_iscsi_fd, ISCSIDRECEIVE, &idr);
11063783933SJohn Baldwin 	if (error != 0)
11163783933SJohn Baldwin 		log_err(1, "ISCSIDRECEIVE");
11263783933SJohn Baldwin 
11363783933SJohn Baldwin 	len = pdu_ahs_length(pdu);
11463783933SJohn Baldwin 	if (len > 0)
11563783933SJohn Baldwin 		log_errx(1, "protocol error: non-empty AHS");
11663783933SJohn Baldwin 
11763783933SJohn Baldwin 	len = pdu_data_segment_length(pdu);
11863783933SJohn Baldwin 	assert(len <= (size_t)conn->conn.conn_max_recv_data_segment_length);
11963783933SJohn Baldwin 	pdu->pdu_data_len = len;
120009ea47eSEdward Tomasz Napierala }
121009ea47eSEdward Tomasz Napierala 
1221f90f282SEdward Tomasz Napierala static void
pdu_send_proxy(struct pdu * pdu)12363783933SJohn Baldwin pdu_send_proxy(struct pdu *pdu)
12463783933SJohn Baldwin {
12563783933SJohn Baldwin 	struct iscsid_connection *conn;
12663783933SJohn Baldwin 	struct iscsi_daemon_send ids;
12763783933SJohn Baldwin 	int error;
12863783933SJohn Baldwin 
12963783933SJohn Baldwin 	conn = (struct iscsid_connection *)pdu->pdu_connection;
13063783933SJohn Baldwin 	assert(conn->conn_conf.isc_iser != 0);
13163783933SJohn Baldwin 
13263783933SJohn Baldwin 	pdu_set_data_segment_length(pdu, pdu->pdu_data_len);
13363783933SJohn Baldwin 
13463783933SJohn Baldwin 	memset(&ids, 0, sizeof(ids));
13563783933SJohn Baldwin 	ids.ids_session_id = conn->conn_session_id;
13663783933SJohn Baldwin 	ids.ids_bhs = pdu->pdu_bhs;
13763783933SJohn Baldwin 	ids.ids_data_segment_len = pdu->pdu_data_len;
13863783933SJohn Baldwin 	ids.ids_data_segment = pdu->pdu_data;
13963783933SJohn Baldwin 
14063783933SJohn Baldwin 	error = ioctl(conn->conn_iscsi_fd, ISCSIDSEND, &ids);
14163783933SJohn Baldwin 	if (error != 0)
14263783933SJohn Baldwin 		log_err(1, "ISCSIDSEND");
14363783933SJohn Baldwin }
14463783933SJohn Baldwin 
14563783933SJohn Baldwin #endif /* ICL_KERNEL_PROXY */
14663783933SJohn Baldwin 
14763783933SJohn Baldwin static void
resolve_addr(const struct connection * conn,const char * address,struct addrinfo ** ai,bool initiator_side)1481f90f282SEdward Tomasz Napierala resolve_addr(const struct connection *conn, const char *address,
1491f90f282SEdward Tomasz Napierala     struct addrinfo **ai, bool initiator_side)
150009ea47eSEdward Tomasz Napierala {
151009ea47eSEdward Tomasz Napierala 	struct addrinfo hints;
152c74ab5ceSJohn Baldwin 	char *arg, *addr, *ch, *tofree;
153009ea47eSEdward Tomasz Napierala 	const char *port;
154009ea47eSEdward Tomasz Napierala 	int error, colons = 0;
155009ea47eSEdward Tomasz Napierala 
156c74ab5ceSJohn Baldwin 	tofree = arg = checked_strdup(address);
157009ea47eSEdward Tomasz Napierala 
158009ea47eSEdward Tomasz Napierala 	if (arg[0] == '\0') {
1591f90f282SEdward Tomasz Napierala 		fail(conn, "empty address");
1601f90f282SEdward Tomasz Napierala 		log_errx(1, "empty address");
161009ea47eSEdward Tomasz Napierala 	}
162009ea47eSEdward Tomasz Napierala 	if (arg[0] == '[') {
163009ea47eSEdward Tomasz Napierala 		/*
164009ea47eSEdward Tomasz Napierala 		 * IPv6 address in square brackets, perhaps with port.
165009ea47eSEdward Tomasz Napierala 		 */
166009ea47eSEdward Tomasz Napierala 		arg++;
167009ea47eSEdward Tomasz Napierala 		addr = strsep(&arg, "]");
168009ea47eSEdward Tomasz Napierala 		if (arg == NULL) {
1691f90f282SEdward Tomasz Napierala 			fail(conn, "malformed address");
1701f90f282SEdward Tomasz Napierala 			log_errx(1, "malformed address %s", address);
171009ea47eSEdward Tomasz Napierala 		}
172009ea47eSEdward Tomasz Napierala 		if (arg[0] == '\0') {
1731f90f282SEdward Tomasz Napierala 			port = NULL;
174009ea47eSEdward Tomasz Napierala 		} else if (arg[0] == ':') {
175009ea47eSEdward Tomasz Napierala 			port = arg + 1;
176009ea47eSEdward Tomasz Napierala 		} else {
1771f90f282SEdward Tomasz Napierala 			fail(conn, "malformed address");
1781f90f282SEdward Tomasz Napierala 			log_errx(1, "malformed address %s", address);
179009ea47eSEdward Tomasz Napierala 		}
180009ea47eSEdward Tomasz Napierala 	} else {
181009ea47eSEdward Tomasz Napierala 		/*
182009ea47eSEdward Tomasz Napierala 		 * Either IPv6 address without brackets - and without
183009ea47eSEdward Tomasz Napierala 		 * a port - or IPv4 address.  Just count the colons.
184009ea47eSEdward Tomasz Napierala 		 */
185009ea47eSEdward Tomasz Napierala 		for (ch = arg; *ch != '\0'; ch++) {
186009ea47eSEdward Tomasz Napierala 			if (*ch == ':')
187009ea47eSEdward Tomasz Napierala 				colons++;
188009ea47eSEdward Tomasz Napierala 		}
189009ea47eSEdward Tomasz Napierala 		if (colons > 1) {
190009ea47eSEdward Tomasz Napierala 			addr = arg;
1911f90f282SEdward Tomasz Napierala 			port = NULL;
192009ea47eSEdward Tomasz Napierala 		} else {
193009ea47eSEdward Tomasz Napierala 			addr = strsep(&arg, ":");
194009ea47eSEdward Tomasz Napierala 			if (arg == NULL)
1951f90f282SEdward Tomasz Napierala 				port = NULL;
196009ea47eSEdward Tomasz Napierala 			else
197009ea47eSEdward Tomasz Napierala 				port = arg;
198009ea47eSEdward Tomasz Napierala 		}
199009ea47eSEdward Tomasz Napierala 	}
200009ea47eSEdward Tomasz Napierala 
2011f90f282SEdward Tomasz Napierala 	if (port == NULL && !initiator_side)
2021f90f282SEdward Tomasz Napierala 		port = "3260";
2031f90f282SEdward Tomasz Napierala 
204009ea47eSEdward Tomasz Napierala 	memset(&hints, 0, sizeof(hints));
205009ea47eSEdward Tomasz Napierala 	hints.ai_family = PF_UNSPEC;
206009ea47eSEdward Tomasz Napierala 	hints.ai_socktype = SOCK_STREAM;
2071f90f282SEdward Tomasz Napierala 	hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
2081f90f282SEdward Tomasz Napierala 	if (initiator_side)
2091f90f282SEdward Tomasz Napierala 		hints.ai_flags |= AI_PASSIVE;
210009ea47eSEdward Tomasz Napierala 
211009ea47eSEdward Tomasz Napierala 	error = getaddrinfo(addr, port, &hints, ai);
212009ea47eSEdward Tomasz Napierala 	if (error != 0) {
2131f90f282SEdward Tomasz Napierala 		fail(conn, gai_strerror(error));
2141f90f282SEdward Tomasz Napierala 		log_errx(1, "getaddrinfo for %s failed: %s",
215009ea47eSEdward Tomasz Napierala 		    address, gai_strerror(error));
216009ea47eSEdward Tomasz Napierala 	}
217a7972b49SEdward Tomasz Napierala 
218c74ab5ceSJohn Baldwin 	free(tofree);
219009ea47eSEdward Tomasz Napierala }
220009ea47eSEdward Tomasz Napierala 
22163783933SJohn Baldwin static struct iscsid_connection *
connection_new(int iscsi_fd,const struct iscsi_daemon_request * request)22283f37561SEdward Tomasz Napierala connection_new(int iscsi_fd, const struct iscsi_daemon_request *request)
223009ea47eSEdward Tomasz Napierala {
22463783933SJohn Baldwin 	struct iscsid_connection *conn;
225009ea47eSEdward Tomasz Napierala 	struct addrinfo *from_ai, *to_ai;
226009ea47eSEdward Tomasz Napierala 	const char *from_addr, *to_addr;
227009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
2287843bd03SEdward Tomasz Napierala 	struct iscsi_daemon_connect idc;
229009ea47eSEdward Tomasz Napierala #endif
23091c62d62SJohn Baldwin 	int error, optval;
231009ea47eSEdward Tomasz Napierala 
232009ea47eSEdward Tomasz Napierala 	conn = calloc(1, sizeof(*conn));
233009ea47eSEdward Tomasz Napierala 	if (conn == NULL)
234009ea47eSEdward Tomasz Napierala 		log_err(1, "calloc");
235009ea47eSEdward Tomasz Napierala 
23663783933SJohn Baldwin 	connection_init(&conn->conn, &conn_ops,
23763783933SJohn Baldwin 	    request->idr_conf.isc_iser != 0);
2387dbbd1aeSAlexander Motin 	conn->conn_protocol_level = 0;
239009ea47eSEdward Tomasz Napierala 	conn->conn_initial_r2t = true;
2401f90f282SEdward Tomasz Napierala 	conn->conn_iscsi_fd = iscsi_fd;
2411f90f282SEdward Tomasz Napierala 
24283f37561SEdward Tomasz Napierala 	conn->conn_session_id = request->idr_session_id;
24383f37561SEdward Tomasz Napierala 	memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf));
24463783933SJohn Baldwin 	memcpy(&conn->conn.conn_isid, &request->idr_isid,
24563783933SJohn Baldwin 	    sizeof(conn->conn.conn_isid));
24663783933SJohn Baldwin 	conn->conn.conn_tsih = request->idr_tsih;
24797b84d34SNavdeep Parhar 
248009ea47eSEdward Tomasz Napierala 	from_addr = conn->conn_conf.isc_initiator_addr;
249009ea47eSEdward Tomasz Napierala 	to_addr = conn->conn_conf.isc_target_addr;
250009ea47eSEdward Tomasz Napierala 
2511f90f282SEdward Tomasz Napierala 	if (from_addr[0] != '\0')
25263783933SJohn Baldwin 		resolve_addr(&conn->conn, from_addr, &from_ai, true);
2531f90f282SEdward Tomasz Napierala 	else
254009ea47eSEdward Tomasz Napierala 		from_ai = NULL;
255009ea47eSEdward Tomasz Napierala 
25663783933SJohn Baldwin 	resolve_addr(&conn->conn, to_addr, &to_ai, false);
257009ea47eSEdward Tomasz Napierala 
258009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
25957a4f20bSEdward Tomasz Napierala 	if (conn->conn_conf.isc_iser) {
2607843bd03SEdward Tomasz Napierala 		memset(&idc, 0, sizeof(idc));
2617843bd03SEdward Tomasz Napierala 		idc.idc_session_id = conn->conn_session_id;
262009ea47eSEdward Tomasz Napierala 		if (conn->conn_conf.isc_iser)
2637843bd03SEdward Tomasz Napierala 			idc.idc_iser = 1;
2647843bd03SEdward Tomasz Napierala 		idc.idc_domain = to_ai->ai_family;
2657843bd03SEdward Tomasz Napierala 		idc.idc_socktype = to_ai->ai_socktype;
2667843bd03SEdward Tomasz Napierala 		idc.idc_protocol = to_ai->ai_protocol;
267009ea47eSEdward Tomasz Napierala 		if (from_ai != NULL) {
2687843bd03SEdward Tomasz Napierala 			idc.idc_from_addr = from_ai->ai_addr;
2697843bd03SEdward Tomasz Napierala 			idc.idc_from_addrlen = from_ai->ai_addrlen;
270009ea47eSEdward Tomasz Napierala 		}
2717843bd03SEdward Tomasz Napierala 		idc.idc_to_addr = to_ai->ai_addr;
2727843bd03SEdward Tomasz Napierala 		idc.idc_to_addrlen = to_ai->ai_addrlen;
273009ea47eSEdward Tomasz Napierala 
274009ea47eSEdward Tomasz Napierala 		log_debugx("connecting to %s using ICL kernel proxy", to_addr);
2757843bd03SEdward Tomasz Napierala 		error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc);
276009ea47eSEdward Tomasz Napierala 		if (error != 0) {
27763783933SJohn Baldwin 			fail(&conn->conn, strerror(errno));
27857a4f20bSEdward Tomasz Napierala 			log_err(1, "failed to connect to %s "
27957a4f20bSEdward Tomasz Napierala 			    "using ICL kernel proxy: ISCSIDCONNECT", to_addr);
280009ea47eSEdward Tomasz Napierala 		}
281009ea47eSEdward Tomasz Napierala 
2827db3d97eSEdward Tomasz Napierala 		if (from_ai != NULL)
2837db3d97eSEdward Tomasz Napierala 			freeaddrinfo(from_ai);
2847db3d97eSEdward Tomasz Napierala 		freeaddrinfo(to_ai);
2857db3d97eSEdward Tomasz Napierala 
28657a4f20bSEdward Tomasz Napierala 		return (conn);
28757a4f20bSEdward Tomasz Napierala 	}
28857a4f20bSEdward Tomasz Napierala #endif /* ICL_KERNEL_PROXY */
289009ea47eSEdward Tomasz Napierala 
2901f90f282SEdward Tomasz Napierala 	if (conn->conn_conf.isc_iser) {
29163783933SJohn Baldwin 		fail(&conn->conn, "iSER not supported");
292009ea47eSEdward Tomasz Napierala 		log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY "
293009ea47eSEdward Tomasz Napierala 		    "does not support iSER");
2941f90f282SEdward Tomasz Napierala 	}
295009ea47eSEdward Tomasz Napierala 
29663783933SJohn Baldwin 	conn->conn.conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype,
297009ea47eSEdward Tomasz Napierala 	    to_ai->ai_protocol);
29863783933SJohn Baldwin 	if (conn->conn.conn_socket < 0) {
29963783933SJohn Baldwin 		fail(&conn->conn, strerror(errno));
300009ea47eSEdward Tomasz Napierala 		log_err(1, "failed to create socket for %s", from_addr);
3011f90f282SEdward Tomasz Napierala 	}
30291c62d62SJohn Baldwin 	optval = SOCKBUF_SIZE;
30363783933SJohn Baldwin 	if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_RCVBUF,
30491c62d62SJohn Baldwin 	    &optval, sizeof(optval)) == -1)
3058b94b583SAlexander Motin 		log_warn("setsockopt(SO_RCVBUF) failed");
30691c62d62SJohn Baldwin 	optval = SOCKBUF_SIZE;
30763783933SJohn Baldwin 	if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_SNDBUF,
30891c62d62SJohn Baldwin 	    &optval, sizeof(optval)) == -1)
3098b94b583SAlexander Motin 		log_warn("setsockopt(SO_SNDBUF) failed");
31091c62d62SJohn Baldwin 	optval = 1;
31163783933SJohn Baldwin 	if (setsockopt(conn->conn.conn_socket, SOL_SOCKET, SO_NO_DDP,
31291c62d62SJohn Baldwin 	    &optval, sizeof(optval)) == -1)
31391c62d62SJohn Baldwin 		log_warn("setsockopt(SO_NO_DDP) failed");
314bfabdadeSRichard Scheffenegger 	if (conn->conn_conf.isc_dscp != -1) {
315bfabdadeSRichard Scheffenegger 		int tos = conn->conn_conf.isc_dscp << 2;
316bfabdadeSRichard Scheffenegger 		if (to_ai->ai_family == AF_INET) {
31763783933SJohn Baldwin 			if (setsockopt(conn->conn.conn_socket,
318bfabdadeSRichard Scheffenegger 			    IPPROTO_IP, IP_TOS,
319bfabdadeSRichard Scheffenegger 			    &tos, sizeof(tos)) == -1)
320bfabdadeSRichard Scheffenegger 				log_warn("setsockopt(IP_TOS) "
321bfabdadeSRichard Scheffenegger 				    "failed for %s",
322bfabdadeSRichard Scheffenegger 				    from_addr);
323bfabdadeSRichard Scheffenegger 		} else
324bfabdadeSRichard Scheffenegger 		if (to_ai->ai_family == AF_INET6) {
32563783933SJohn Baldwin 			if (setsockopt(conn->conn.conn_socket,
326bfabdadeSRichard Scheffenegger 			    IPPROTO_IPV6, IPV6_TCLASS,
327bfabdadeSRichard Scheffenegger 			    &tos, sizeof(tos)) == -1)
328bfabdadeSRichard Scheffenegger 				log_warn("setsockopt(IPV6_TCLASS) "
329bfabdadeSRichard Scheffenegger 				    "failed for %s",
330bfabdadeSRichard Scheffenegger 				    from_addr);
331bfabdadeSRichard Scheffenegger 		}
332bfabdadeSRichard Scheffenegger 	}
3334dfbcffbSRichard Scheffenegger 	if (conn->conn_conf.isc_pcp != -1) {
3344dfbcffbSRichard Scheffenegger 		int pcp = conn->conn_conf.isc_pcp;
3354dfbcffbSRichard Scheffenegger 		if (to_ai->ai_family == AF_INET) {
33663783933SJohn Baldwin 			if (setsockopt(conn->conn.conn_socket,
3374dfbcffbSRichard Scheffenegger 			    IPPROTO_IP, IP_VLAN_PCP,
3384dfbcffbSRichard Scheffenegger 			    &pcp, sizeof(pcp)) == -1)
3394dfbcffbSRichard Scheffenegger 				log_warn("setsockopt(IP_VLAN_PCP) "
3404dfbcffbSRichard Scheffenegger 				    "failed for %s",
3414dfbcffbSRichard Scheffenegger 				    from_addr);
3424dfbcffbSRichard Scheffenegger 		} else
3434dfbcffbSRichard Scheffenegger 		if (to_ai->ai_family == AF_INET6) {
34463783933SJohn Baldwin 			if (setsockopt(conn->conn.conn_socket,
3454dfbcffbSRichard Scheffenegger 			    IPPROTO_IPV6, IPV6_VLAN_PCP,
3464dfbcffbSRichard Scheffenegger 			    &pcp, sizeof(pcp)) == -1)
3474dfbcffbSRichard Scheffenegger 				log_warn("setsockopt(IPV6_VLAN_PCP) "
3484dfbcffbSRichard Scheffenegger 				    "failed for %s",
3494dfbcffbSRichard Scheffenegger 				    from_addr);
3504dfbcffbSRichard Scheffenegger 		}
3514dfbcffbSRichard Scheffenegger 	}
352bd6bb493SRichard Scheffenegger 	/*
353bd6bb493SRichard Scheffenegger 	 * Reduce TCP SYN_SENT timeout while
354bd6bb493SRichard Scheffenegger 	 * no connectivity exists, to allow
355bd6bb493SRichard Scheffenegger 	 * rapid reuse of the available slots.
356bd6bb493SRichard Scheffenegger 	 */
357bd6bb493SRichard Scheffenegger 	int keepinit = 0;
358bd6bb493SRichard Scheffenegger 	if (conn->conn_conf.isc_login_timeout > 0) {
359bd6bb493SRichard Scheffenegger 		keepinit = conn->conn_conf.isc_login_timeout;
360bd6bb493SRichard Scheffenegger 		log_debugx("session specific LoginTimeout at %d sec",
361bd6bb493SRichard Scheffenegger 			keepinit);
362bd6bb493SRichard Scheffenegger 	}
363bd6bb493SRichard Scheffenegger 	if (conn->conn_conf.isc_login_timeout == -1) {
364407c34e7SRichard Scheffenegger 		int value;
365bd6bb493SRichard Scheffenegger 		size_t size = sizeof(value);
366407c34e7SRichard Scheffenegger 		if (sysctlbyname("kern.iscsi.login_timeout",
367407c34e7SRichard Scheffenegger 		    &value, &size, NULL, 0) == 0) {
368407c34e7SRichard Scheffenegger 			keepinit = value;
369407c34e7SRichard Scheffenegger 			log_debugx("global login_timeout at %d sec",
370407c34e7SRichard Scheffenegger 				keepinit);
371407c34e7SRichard Scheffenegger 		}
372bd6bb493SRichard Scheffenegger 	}
373bd6bb493SRichard Scheffenegger 	if (keepinit > 0) {
374bd6bb493SRichard Scheffenegger 		if (setsockopt(conn->conn.conn_socket,
375bd6bb493SRichard Scheffenegger 		    IPPROTO_TCP, TCP_KEEPINIT,
376bd6bb493SRichard Scheffenegger 		    &keepinit, sizeof(keepinit)) == -1)
377bd6bb493SRichard Scheffenegger 			log_warnx("setsockopt(TCP_KEEPINIT) "
378bd6bb493SRichard Scheffenegger 			    "failed for %s", to_addr);
379bd6bb493SRichard Scheffenegger 	}
380009ea47eSEdward Tomasz Napierala 	if (from_ai != NULL) {
38163783933SJohn Baldwin 		error = bind(conn->conn.conn_socket, from_ai->ai_addr,
382009ea47eSEdward Tomasz Napierala 		    from_ai->ai_addrlen);
3831f90f282SEdward Tomasz Napierala 		if (error != 0) {
38463783933SJohn Baldwin 			fail(&conn->conn, strerror(errno));
385009ea47eSEdward Tomasz Napierala 			log_err(1, "failed to bind to %s", from_addr);
386009ea47eSEdward Tomasz Napierala 		}
3871f90f282SEdward Tomasz Napierala 	}
388009ea47eSEdward Tomasz Napierala 	log_debugx("connecting to %s", to_addr);
38963783933SJohn Baldwin 	error = connect(conn->conn.conn_socket, to_ai->ai_addr,
39063783933SJohn Baldwin 	    to_ai->ai_addrlen);
391009ea47eSEdward Tomasz Napierala 	if (error != 0) {
39263783933SJohn Baldwin 		fail(&conn->conn, strerror(errno));
393009ea47eSEdward Tomasz Napierala 		log_err(1, "failed to connect to %s", to_addr);
394009ea47eSEdward Tomasz Napierala 	}
395009ea47eSEdward Tomasz Napierala 
3967db3d97eSEdward Tomasz Napierala 	if (from_ai != NULL)
3977db3d97eSEdward Tomasz Napierala 		freeaddrinfo(from_ai);
3987db3d97eSEdward Tomasz Napierala 	freeaddrinfo(to_ai);
3997db3d97eSEdward Tomasz Napierala 
400009ea47eSEdward Tomasz Napierala 	return (conn);
401009ea47eSEdward Tomasz Napierala }
402009ea47eSEdward Tomasz Napierala 
403009ea47eSEdward Tomasz Napierala static void
limits(struct iscsid_connection * conn)4047b02c1e8SJohn Baldwin limits(struct iscsid_connection *conn)
4057b02c1e8SJohn Baldwin {
4067b02c1e8SJohn Baldwin 	struct iscsi_daemon_limits idl;
4077b02c1e8SJohn Baldwin 	struct iscsi_session_limits *isl;
4087b02c1e8SJohn Baldwin 	int error;
4097b02c1e8SJohn Baldwin 
4107b02c1e8SJohn Baldwin 	log_debugx("fetching limits from the kernel");
4117b02c1e8SJohn Baldwin 
4127b02c1e8SJohn Baldwin 	memset(&idl, 0, sizeof(idl));
4137b02c1e8SJohn Baldwin 	idl.idl_session_id = conn->conn_session_id;
4147b02c1e8SJohn Baldwin 	idl.idl_socket = conn->conn.conn_socket;
4157b02c1e8SJohn Baldwin 
4167b02c1e8SJohn Baldwin 	error = ioctl(conn->conn_iscsi_fd, ISCSIDLIMITS, &idl);
4177b02c1e8SJohn Baldwin 	if (error != 0)
4187b02c1e8SJohn Baldwin 		log_err(1, "ISCSIDLIMITS");
4197b02c1e8SJohn Baldwin 
4207b02c1e8SJohn Baldwin 	/*
4217b02c1e8SJohn Baldwin 	 * Read the driver limits and provide reasonable defaults for the ones
4227b02c1e8SJohn Baldwin 	 * the driver doesn't care about.  If a max_snd_dsl is not explicitly
4237b02c1e8SJohn Baldwin 	 * provided by the driver then we'll make sure both conn->max_snd_dsl
4247b02c1e8SJohn Baldwin 	 * and isl->max_snd_dsl are set to the rcv_dsl.  This preserves historic
4257b02c1e8SJohn Baldwin 	 * behavior.
4267b02c1e8SJohn Baldwin 	 */
4277b02c1e8SJohn Baldwin 	isl = &conn->conn_limits;
4287b02c1e8SJohn Baldwin 	memcpy(isl, &idl.idl_limits, sizeof(*isl));
4297b02c1e8SJohn Baldwin 	if (isl->isl_max_recv_data_segment_length == 0)
4307b02c1e8SJohn Baldwin 		isl->isl_max_recv_data_segment_length = (1 << 24) - 1;
4317b02c1e8SJohn Baldwin 	if (isl->isl_max_send_data_segment_length == 0)
4327b02c1e8SJohn Baldwin 		isl->isl_max_send_data_segment_length =
4337b02c1e8SJohn Baldwin 		    isl->isl_max_recv_data_segment_length;
4347b02c1e8SJohn Baldwin 	if (isl->isl_max_burst_length == 0)
4357b02c1e8SJohn Baldwin 		isl->isl_max_burst_length = (1 << 24) - 1;
4367b02c1e8SJohn Baldwin 	if (isl->isl_first_burst_length == 0)
4377b02c1e8SJohn Baldwin 		isl->isl_first_burst_length = (1 << 24) - 1;
4387b02c1e8SJohn Baldwin 	if (isl->isl_first_burst_length > isl->isl_max_burst_length)
4397b02c1e8SJohn Baldwin 		isl->isl_first_burst_length = isl->isl_max_burst_length;
4407b02c1e8SJohn Baldwin 
4417b02c1e8SJohn Baldwin 	/*
4427b02c1e8SJohn Baldwin 	 * Limit default send length in case it won't be negotiated.
4437b02c1e8SJohn Baldwin 	 * We can't do it for other limits, since they may affect both
4447b02c1e8SJohn Baldwin 	 * sender and receiver operation, and we must obey defaults.
4457b02c1e8SJohn Baldwin 	 */
4467b02c1e8SJohn Baldwin 	if (conn->conn.conn_max_send_data_segment_length >
4477b02c1e8SJohn Baldwin 	    isl->isl_max_send_data_segment_length) {
4487b02c1e8SJohn Baldwin 		conn->conn.conn_max_send_data_segment_length =
4497b02c1e8SJohn Baldwin 		    isl->isl_max_send_data_segment_length;
4507b02c1e8SJohn Baldwin 	}
4517b02c1e8SJohn Baldwin }
4527b02c1e8SJohn Baldwin 
4537b02c1e8SJohn Baldwin static void
handoff(struct iscsid_connection * conn)45463783933SJohn Baldwin handoff(struct iscsid_connection *conn)
455009ea47eSEdward Tomasz Napierala {
4567843bd03SEdward Tomasz Napierala 	struct iscsi_daemon_handoff idh;
457009ea47eSEdward Tomasz Napierala 	int error;
458009ea47eSEdward Tomasz Napierala 
459009ea47eSEdward Tomasz Napierala 	log_debugx("handing off connection to the kernel");
460009ea47eSEdward Tomasz Napierala 
4617843bd03SEdward Tomasz Napierala 	memset(&idh, 0, sizeof(idh));
4627843bd03SEdward Tomasz Napierala 	idh.idh_session_id = conn->conn_session_id;
46363783933SJohn Baldwin 	idh.idh_socket = conn->conn.conn_socket;
4647843bd03SEdward Tomasz Napierala 	strlcpy(idh.idh_target_alias, conn->conn_target_alias,
4657843bd03SEdward Tomasz Napierala 	    sizeof(idh.idh_target_alias));
46663783933SJohn Baldwin 	idh.idh_tsih = conn->conn.conn_tsih;
46763783933SJohn Baldwin 	idh.idh_statsn = conn->conn.conn_statsn;
4687dbbd1aeSAlexander Motin 	idh.idh_protocol_level = conn->conn_protocol_level;
46963783933SJohn Baldwin 	idh.idh_header_digest = conn->conn.conn_header_digest;
47063783933SJohn Baldwin 	idh.idh_data_digest = conn->conn.conn_data_digest;
4717843bd03SEdward Tomasz Napierala 	idh.idh_initial_r2t = conn->conn_initial_r2t;
47263783933SJohn Baldwin 	idh.idh_immediate_data = conn->conn.conn_immediate_data;
47397b84d34SNavdeep Parhar 	idh.idh_max_recv_data_segment_length =
47463783933SJohn Baldwin 	    conn->conn.conn_max_recv_data_segment_length;
47597b84d34SNavdeep Parhar 	idh.idh_max_send_data_segment_length =
47663783933SJohn Baldwin 	    conn->conn.conn_max_send_data_segment_length;
47763783933SJohn Baldwin 	idh.idh_max_burst_length = conn->conn.conn_max_burst_length;
47863783933SJohn Baldwin 	idh.idh_first_burst_length = conn->conn.conn_first_burst_length;
479009ea47eSEdward Tomasz Napierala 
4807843bd03SEdward Tomasz Napierala 	error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh);
481009ea47eSEdward Tomasz Napierala 	if (error != 0)
482009ea47eSEdward Tomasz Napierala 		log_err(1, "ISCSIDHANDOFF");
483009ea47eSEdward Tomasz Napierala }
484009ea47eSEdward Tomasz Napierala 
485009ea47eSEdward Tomasz Napierala void
fail(const struct connection * base_conn,const char * reason)48663783933SJohn Baldwin fail(const struct connection *base_conn, const char *reason)
487009ea47eSEdward Tomasz Napierala {
48863783933SJohn Baldwin 	const struct iscsid_connection *conn;
4897843bd03SEdward Tomasz Napierala 	struct iscsi_daemon_fail idf;
490535bd9beSEdward Tomasz Napierala 	int error, saved_errno;
491535bd9beSEdward Tomasz Napierala 
49263783933SJohn Baldwin 	conn = (const struct iscsid_connection *)base_conn;
493535bd9beSEdward Tomasz Napierala 	saved_errno = errno;
494009ea47eSEdward Tomasz Napierala 
4957843bd03SEdward Tomasz Napierala 	memset(&idf, 0, sizeof(idf));
4967843bd03SEdward Tomasz Napierala 	idf.idf_session_id = conn->conn_session_id;
4977843bd03SEdward Tomasz Napierala 	strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason));
498009ea47eSEdward Tomasz Napierala 
4997843bd03SEdward Tomasz Napierala 	error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf);
500009ea47eSEdward Tomasz Napierala 	if (error != 0)
501009ea47eSEdward Tomasz Napierala 		log_err(1, "ISCSIDFAIL");
502535bd9beSEdward Tomasz Napierala 
503535bd9beSEdward Tomasz Napierala 	errno = saved_errno;
504009ea47eSEdward Tomasz Napierala }
505009ea47eSEdward Tomasz Napierala 
506009ea47eSEdward Tomasz Napierala /*
507009ea47eSEdward Tomasz Napierala  * XXX: I CANT INTO LATIN
508009ea47eSEdward Tomasz Napierala  */
509009ea47eSEdward Tomasz Napierala static void
capsicate(struct iscsid_connection * conn)51063783933SJohn Baldwin capsicate(struct iscsid_connection *conn)
511009ea47eSEdward Tomasz Napierala {
512009ea47eSEdward Tomasz Napierala 	cap_rights_t rights;
513f0df7227SJohn Baldwin 	const unsigned long cmds[] = {
514009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
515f0df7227SJohn Baldwin 		ISCSIDCONNECT,
516f0df7227SJohn Baldwin 		ISCSIDSEND,
517f0df7227SJohn Baldwin 		ISCSIDRECEIVE,
518009ea47eSEdward Tomasz Napierala #endif
5197b02c1e8SJohn Baldwin 		ISCSIDLIMITS,
520f0df7227SJohn Baldwin 		ISCSIDHANDOFF,
521f0df7227SJohn Baldwin 		ISCSIDFAIL,
522f0df7227SJohn Baldwin 		ISCSISADD,
523f0df7227SJohn Baldwin 		ISCSISREMOVE,
524f0df7227SJohn Baldwin 		ISCSISMODIFY
525f0df7227SJohn Baldwin 	};
526009ea47eSEdward Tomasz Napierala 
527009ea47eSEdward Tomasz Napierala 	cap_rights_init(&rights, CAP_IOCTL);
5281489776dSMariusz Zaborski 	if (caph_rights_limit(conn->conn_iscsi_fd, &rights) < 0)
529009ea47eSEdward Tomasz Napierala 		log_err(1, "cap_rights_limit");
530009ea47eSEdward Tomasz Napierala 
5311489776dSMariusz Zaborski 	if (caph_ioctls_limit(conn->conn_iscsi_fd, cmds, nitems(cmds)) < 0)
532009ea47eSEdward Tomasz Napierala 		log_err(1, "cap_ioctls_limit");
533009ea47eSEdward Tomasz Napierala 
5341489776dSMariusz Zaborski 	if (caph_enter() != 0)
535009ea47eSEdward Tomasz Napierala 		log_err(1, "cap_enter");
536009ea47eSEdward Tomasz Napierala 
537009ea47eSEdward Tomasz Napierala 	if (cap_sandboxed())
538009ea47eSEdward Tomasz Napierala 		log_debugx("Capsicum capability mode enabled");
539009ea47eSEdward Tomasz Napierala 	else
540009ea47eSEdward Tomasz Napierala 		log_warnx("Capsicum capability mode not supported");
541009ea47eSEdward Tomasz Napierala }
542009ea47eSEdward Tomasz Napierala 
54363783933SJohn Baldwin static bool
timed_out(void)544009ea47eSEdward Tomasz Napierala timed_out(void)
545009ea47eSEdward Tomasz Napierala {
546009ea47eSEdward Tomasz Napierala 
547009ea47eSEdward Tomasz Napierala 	return (sigalrm_received);
548009ea47eSEdward Tomasz Napierala }
549009ea47eSEdward Tomasz Napierala 
550009ea47eSEdward Tomasz Napierala static void
sigalrm_handler(int dummy __unused)551009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused)
552009ea47eSEdward Tomasz Napierala {
553009ea47eSEdward Tomasz Napierala 	/*
554009ea47eSEdward Tomasz Napierala 	 * It would be easiest to just log an error and exit.  We can't
555009ea47eSEdward Tomasz Napierala 	 * do this, though, because log_errx() is not signal safe, since
556009ea47eSEdward Tomasz Napierala 	 * it calls syslog(3).  Instead, set a flag checked by pdu_send()
557009ea47eSEdward Tomasz Napierala 	 * and pdu_receive(), to call log_errx() there.  Should they fail
558009ea47eSEdward Tomasz Napierala 	 * to notice, we'll exit here one second later.
559009ea47eSEdward Tomasz Napierala 	 */
560009ea47eSEdward Tomasz Napierala 	if (sigalrm_received) {
561009ea47eSEdward Tomasz Napierala 		/*
562009ea47eSEdward Tomasz Napierala 		 * Oh well.  Just give up and quit.
563009ea47eSEdward Tomasz Napierala 		 */
564009ea47eSEdward Tomasz Napierala 		_exit(2);
565009ea47eSEdward Tomasz Napierala 	}
566009ea47eSEdward Tomasz Napierala 
567009ea47eSEdward Tomasz Napierala 	sigalrm_received = true;
568009ea47eSEdward Tomasz Napierala }
569009ea47eSEdward Tomasz Napierala 
570009ea47eSEdward Tomasz Napierala static void
set_timeout(int timeout)571009ea47eSEdward Tomasz Napierala set_timeout(int timeout)
572009ea47eSEdward Tomasz Napierala {
573009ea47eSEdward Tomasz Napierala 	struct sigaction sa;
574009ea47eSEdward Tomasz Napierala 	struct itimerval itv;
575009ea47eSEdward Tomasz Napierala 	int error;
576009ea47eSEdward Tomasz Napierala 
577009ea47eSEdward Tomasz Napierala 	if (timeout <= 0) {
578009ea47eSEdward Tomasz Napierala 		log_debugx("session timeout disabled");
579009ea47eSEdward Tomasz Napierala 		return;
580009ea47eSEdward Tomasz Napierala 	}
581009ea47eSEdward Tomasz Napierala 
582009ea47eSEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
583009ea47eSEdward Tomasz Napierala 	sa.sa_handler = sigalrm_handler;
584009ea47eSEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
585009ea47eSEdward Tomasz Napierala 	error = sigaction(SIGALRM, &sa, NULL);
586009ea47eSEdward Tomasz Napierala 	if (error != 0)
587009ea47eSEdward Tomasz Napierala 		log_err(1, "sigaction");
588009ea47eSEdward Tomasz Napierala 
589009ea47eSEdward Tomasz Napierala 	/*
590009ea47eSEdward Tomasz Napierala 	 * First SIGALRM will arive after conf_timeout seconds.
591009ea47eSEdward Tomasz Napierala 	 * If we do nothing, another one will arrive a second later.
592009ea47eSEdward Tomasz Napierala 	 */
593009ea47eSEdward Tomasz Napierala 	bzero(&itv, sizeof(itv));
594009ea47eSEdward Tomasz Napierala 	itv.it_interval.tv_sec = 1;
595009ea47eSEdward Tomasz Napierala 	itv.it_value.tv_sec = timeout;
596009ea47eSEdward Tomasz Napierala 
597009ea47eSEdward Tomasz Napierala 	log_debugx("setting session timeout to %d seconds",
598009ea47eSEdward Tomasz Napierala 	    timeout);
599009ea47eSEdward Tomasz Napierala 	error = setitimer(ITIMER_REAL, &itv, NULL);
600009ea47eSEdward Tomasz Napierala 	if (error != 0)
601009ea47eSEdward Tomasz Napierala 		log_err(1, "setitimer");
602009ea47eSEdward Tomasz Napierala }
603009ea47eSEdward Tomasz Napierala 
604009ea47eSEdward Tomasz Napierala static void
sigchld_handler(int dummy __unused)6051ecb3c58SEdward Tomasz Napierala sigchld_handler(int dummy __unused)
6061ecb3c58SEdward Tomasz Napierala {
6071ecb3c58SEdward Tomasz Napierala 
6081ecb3c58SEdward Tomasz Napierala 	/*
6091ecb3c58SEdward Tomasz Napierala 	 * The only purpose of this handler is to make SIGCHLD
6101ecb3c58SEdward Tomasz Napierala 	 * interrupt the ISCSIDWAIT ioctl(2), so we can call
6111ecb3c58SEdward Tomasz Napierala 	 * wait_for_children().
6121ecb3c58SEdward Tomasz Napierala 	 */
6131ecb3c58SEdward Tomasz Napierala }
6141ecb3c58SEdward Tomasz Napierala 
6151ecb3c58SEdward Tomasz Napierala static void
register_sigchld(void)6161ecb3c58SEdward Tomasz Napierala register_sigchld(void)
6171ecb3c58SEdward Tomasz Napierala {
6181ecb3c58SEdward Tomasz Napierala 	struct sigaction sa;
6191ecb3c58SEdward Tomasz Napierala 	int error;
6201ecb3c58SEdward Tomasz Napierala 
6211ecb3c58SEdward Tomasz Napierala 	bzero(&sa, sizeof(sa));
6221ecb3c58SEdward Tomasz Napierala 	sa.sa_handler = sigchld_handler;
6231ecb3c58SEdward Tomasz Napierala 	sigfillset(&sa.sa_mask);
6241ecb3c58SEdward Tomasz Napierala 	error = sigaction(SIGCHLD, &sa, NULL);
6251ecb3c58SEdward Tomasz Napierala 	if (error != 0)
6261ecb3c58SEdward Tomasz Napierala 		log_err(1, "sigaction");
6271ecb3c58SEdward Tomasz Napierala 
6281ecb3c58SEdward Tomasz Napierala }
6291ecb3c58SEdward Tomasz Napierala 
6301ecb3c58SEdward Tomasz Napierala static void
handle_request(int iscsi_fd,const struct iscsi_daemon_request * request,int timeout)6317843bd03SEdward Tomasz Napierala handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout)
632009ea47eSEdward Tomasz Napierala {
63363783933SJohn Baldwin 	struct iscsid_connection *conn;
634009ea47eSEdward Tomasz Napierala 
635009ea47eSEdward Tomasz Napierala 	log_set_peer_addr(request->idr_conf.isc_target_addr);
636009ea47eSEdward Tomasz Napierala 	if (request->idr_conf.isc_target[0] != '\0') {
637009ea47eSEdward Tomasz Napierala 		log_set_peer_name(request->idr_conf.isc_target);
638009ea47eSEdward Tomasz Napierala 		setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target);
639009ea47eSEdward Tomasz Napierala 	} else {
640009ea47eSEdward Tomasz Napierala 		setproctitle("%s", request->idr_conf.isc_target_addr);
641009ea47eSEdward Tomasz Napierala 	}
642009ea47eSEdward Tomasz Napierala 
64383f37561SEdward Tomasz Napierala 	conn = connection_new(iscsi_fd, request);
644009ea47eSEdward Tomasz Napierala 	capsicate(conn);
6457b02c1e8SJohn Baldwin 	limits(conn);
6467b02c1e8SJohn Baldwin 	set_timeout(timeout);
647009ea47eSEdward Tomasz Napierala 	login(conn);
648009ea47eSEdward Tomasz Napierala 	if (conn->conn_conf.isc_discovery != 0)
649009ea47eSEdward Tomasz Napierala 		discovery(conn);
650009ea47eSEdward Tomasz Napierala 	else
651009ea47eSEdward Tomasz Napierala 		handoff(conn);
652009ea47eSEdward Tomasz Napierala 
653009ea47eSEdward Tomasz Napierala 	log_debugx("nothing more to do; exiting");
654009ea47eSEdward Tomasz Napierala 	exit (0);
655009ea47eSEdward Tomasz Napierala }
656009ea47eSEdward Tomasz Napierala 
657009ea47eSEdward Tomasz Napierala static int
wait_for_children(bool block)658009ea47eSEdward Tomasz Napierala wait_for_children(bool block)
659009ea47eSEdward Tomasz Napierala {
660009ea47eSEdward Tomasz Napierala 	pid_t pid;
661009ea47eSEdward Tomasz Napierala 	int status;
662009ea47eSEdward Tomasz Napierala 	int num = 0;
663009ea47eSEdward Tomasz Napierala 
664009ea47eSEdward Tomasz Napierala 	for (;;) {
665009ea47eSEdward Tomasz Napierala 		/*
666009ea47eSEdward Tomasz Napierala 		 * If "block" is true, wait for at least one process.
667009ea47eSEdward Tomasz Napierala 		 */
668009ea47eSEdward Tomasz Napierala 		if (block && num == 0)
669009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, 0, NULL);
670009ea47eSEdward Tomasz Napierala 		else
671009ea47eSEdward Tomasz Napierala 			pid = wait4(-1, &status, WNOHANG, NULL);
672009ea47eSEdward Tomasz Napierala 		if (pid <= 0)
673009ea47eSEdward Tomasz Napierala 			break;
674009ea47eSEdward Tomasz Napierala 		if (WIFSIGNALED(status)) {
675009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with signal %d",
676009ea47eSEdward Tomasz Napierala 			    pid, WTERMSIG(status));
677009ea47eSEdward Tomasz Napierala 		} else if (WEXITSTATUS(status) != 0) {
678009ea47eSEdward Tomasz Napierala 			log_warnx("child process %d terminated with exit status %d",
679009ea47eSEdward Tomasz Napierala 			    pid, WEXITSTATUS(status));
680009ea47eSEdward Tomasz Napierala 		} else {
681009ea47eSEdward Tomasz Napierala 			log_debugx("child process %d terminated gracefully", pid);
682009ea47eSEdward Tomasz Napierala 		}
683009ea47eSEdward Tomasz Napierala 		num++;
684009ea47eSEdward Tomasz Napierala 	}
685009ea47eSEdward Tomasz Napierala 
686009ea47eSEdward Tomasz Napierala 	return (num);
687009ea47eSEdward Tomasz Napierala }
688009ea47eSEdward Tomasz Napierala 
689009ea47eSEdward Tomasz Napierala int
main(int argc,char ** argv)690009ea47eSEdward Tomasz Napierala main(int argc, char **argv)
691009ea47eSEdward Tomasz Napierala {
692009ea47eSEdward Tomasz Napierala 	int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno,
693009ea47eSEdward Tomasz Napierala 	    timeout = 60;
694009ea47eSEdward Tomasz Napierala 	bool dont_daemonize = false;
695009ea47eSEdward Tomasz Napierala 	struct pidfh *pidfh;
696009ea47eSEdward Tomasz Napierala 	pid_t pid, otherpid;
697009ea47eSEdward Tomasz Napierala 	const char *pidfile_path = DEFAULT_PIDFILE;
6987843bd03SEdward Tomasz Napierala 	struct iscsi_daemon_request request;
699009ea47eSEdward Tomasz Napierala 
700009ea47eSEdward Tomasz Napierala 	while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) {
701009ea47eSEdward Tomasz Napierala 		switch (ch) {
702009ea47eSEdward Tomasz Napierala 		case 'P':
703009ea47eSEdward Tomasz Napierala 			pidfile_path = optarg;
704009ea47eSEdward Tomasz Napierala 			break;
705009ea47eSEdward Tomasz Napierala 		case 'd':
706009ea47eSEdward Tomasz Napierala 			dont_daemonize = true;
707009ea47eSEdward Tomasz Napierala 			debug++;
708009ea47eSEdward Tomasz Napierala 			break;
709009ea47eSEdward Tomasz Napierala 		case 'l':
710009ea47eSEdward Tomasz Napierala 			debug = atoi(optarg);
711009ea47eSEdward Tomasz Napierala 			break;
712009ea47eSEdward Tomasz Napierala 		case 'm':
713009ea47eSEdward Tomasz Napierala 			maxproc = atoi(optarg);
714009ea47eSEdward Tomasz Napierala 			break;
715009ea47eSEdward Tomasz Napierala 		case 't':
716009ea47eSEdward Tomasz Napierala 			timeout = atoi(optarg);
717009ea47eSEdward Tomasz Napierala 			break;
718009ea47eSEdward Tomasz Napierala 		case '?':
719009ea47eSEdward Tomasz Napierala 		default:
720009ea47eSEdward Tomasz Napierala 			usage();
721009ea47eSEdward Tomasz Napierala 		}
722009ea47eSEdward Tomasz Napierala 	}
723009ea47eSEdward Tomasz Napierala 	argc -= optind;
724009ea47eSEdward Tomasz Napierala 	if (argc != 0)
725009ea47eSEdward Tomasz Napierala 		usage();
726009ea47eSEdward Tomasz Napierala 
727009ea47eSEdward Tomasz Napierala 	log_init(debug);
728009ea47eSEdward Tomasz Napierala 
729009ea47eSEdward Tomasz Napierala 	pidfh = pidfile_open(pidfile_path, 0600, &otherpid);
730009ea47eSEdward Tomasz Napierala 	if (pidfh == NULL) {
731009ea47eSEdward Tomasz Napierala 		if (errno == EEXIST)
732009ea47eSEdward Tomasz Napierala 			log_errx(1, "daemon already running, pid: %jd.",
733009ea47eSEdward Tomasz Napierala 			    (intmax_t)otherpid);
734009ea47eSEdward Tomasz Napierala 		log_err(1, "cannot open or create pidfile \"%s\"",
735009ea47eSEdward Tomasz Napierala 		    pidfile_path);
736009ea47eSEdward Tomasz Napierala 	}
737009ea47eSEdward Tomasz Napierala 
738009ea47eSEdward Tomasz Napierala 	iscsi_fd = open(ISCSI_PATH, O_RDWR);
739c76e8a9aSEdward Tomasz Napierala 	if (iscsi_fd < 0 && errno == ENOENT) {
740009ea47eSEdward Tomasz Napierala 		saved_errno = errno;
741009ea47eSEdward Tomasz Napierala 		retval = kldload("iscsi");
742009ea47eSEdward Tomasz Napierala 		if (retval != -1)
743009ea47eSEdward Tomasz Napierala 			iscsi_fd = open(ISCSI_PATH, O_RDWR);
744009ea47eSEdward Tomasz Napierala 		else
745009ea47eSEdward Tomasz Napierala 			errno = saved_errno;
746009ea47eSEdward Tomasz Napierala 	}
747009ea47eSEdward Tomasz Napierala 	if (iscsi_fd < 0)
748009ea47eSEdward Tomasz Napierala 		log_err(1, "failed to open %s", ISCSI_PATH);
749009ea47eSEdward Tomasz Napierala 
750009ea47eSEdward Tomasz Napierala 	if (dont_daemonize == false) {
751009ea47eSEdward Tomasz Napierala 		if (daemon(0, 0) == -1) {
752009ea47eSEdward Tomasz Napierala 			log_warn("cannot daemonize");
753009ea47eSEdward Tomasz Napierala 			pidfile_remove(pidfh);
754009ea47eSEdward Tomasz Napierala 			exit(1);
755009ea47eSEdward Tomasz Napierala 		}
756009ea47eSEdward Tomasz Napierala 	}
757009ea47eSEdward Tomasz Napierala 
758009ea47eSEdward Tomasz Napierala 	pidfile_write(pidfh);
759009ea47eSEdward Tomasz Napierala 
7601ecb3c58SEdward Tomasz Napierala 	register_sigchld();
7611ecb3c58SEdward Tomasz Napierala 
762009ea47eSEdward Tomasz Napierala 	for (;;) {
763009ea47eSEdward Tomasz Napierala 		log_debugx("waiting for request from the kernel");
764009ea47eSEdward Tomasz Napierala 
7657843bd03SEdward Tomasz Napierala 		memset(&request, 0, sizeof(request));
7667843bd03SEdward Tomasz Napierala 		error = ioctl(iscsi_fd, ISCSIDWAIT, &request);
767009ea47eSEdward Tomasz Napierala 		if (error != 0) {
768009ea47eSEdward Tomasz Napierala 			if (errno == EINTR) {
769009ea47eSEdward Tomasz Napierala 				nchildren -= wait_for_children(false);
770009ea47eSEdward Tomasz Napierala 				assert(nchildren >= 0);
771009ea47eSEdward Tomasz Napierala 				continue;
772009ea47eSEdward Tomasz Napierala 			}
773009ea47eSEdward Tomasz Napierala 
774009ea47eSEdward Tomasz Napierala 			log_err(1, "ISCSIDWAIT");
775009ea47eSEdward Tomasz Napierala 		}
776009ea47eSEdward Tomasz Napierala 
777009ea47eSEdward Tomasz Napierala 		if (dont_daemonize) {
778009ea47eSEdward Tomasz Napierala 			log_debugx("not forking due to -d flag; "
779009ea47eSEdward Tomasz Napierala 			    "will exit after servicing a single request");
780009ea47eSEdward Tomasz Napierala 		} else {
781009ea47eSEdward Tomasz Napierala 			nchildren -= wait_for_children(false);
782009ea47eSEdward Tomasz Napierala 			assert(nchildren >= 0);
783009ea47eSEdward Tomasz Napierala 
784009ea47eSEdward Tomasz Napierala 			while (maxproc > 0 && nchildren >= maxproc) {
785009ea47eSEdward Tomasz Napierala 				log_debugx("maxproc limit of %d child processes hit; "
786009ea47eSEdward Tomasz Napierala 				    "waiting for child process to exit", maxproc);
787009ea47eSEdward Tomasz Napierala 				nchildren -= wait_for_children(true);
788009ea47eSEdward Tomasz Napierala 				assert(nchildren >= 0);
789009ea47eSEdward Tomasz Napierala 			}
790009ea47eSEdward Tomasz Napierala 			log_debugx("incoming connection; forking child process #%d",
791009ea47eSEdward Tomasz Napierala 			    nchildren);
792009ea47eSEdward Tomasz Napierala 			nchildren++;
793009ea47eSEdward Tomasz Napierala 
794009ea47eSEdward Tomasz Napierala 			pid = fork();
795009ea47eSEdward Tomasz Napierala 			if (pid < 0)
796009ea47eSEdward Tomasz Napierala 				log_err(1, "fork");
797009ea47eSEdward Tomasz Napierala 			if (pid > 0)
798009ea47eSEdward Tomasz Napierala 				continue;
799009ea47eSEdward Tomasz Napierala 		}
800009ea47eSEdward Tomasz Napierala 
801009ea47eSEdward Tomasz Napierala 		pidfile_close(pidfh);
8027843bd03SEdward Tomasz Napierala 		handle_request(iscsi_fd, &request, timeout);
803009ea47eSEdward Tomasz Napierala 	}
804009ea47eSEdward Tomasz Napierala 
805009ea47eSEdward Tomasz Napierala 	return (0);
806009ea47eSEdward Tomasz Napierala }
807