1009ea47eSEdward Tomasz Napierala /*- 2009ea47eSEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 3009ea47eSEdward Tomasz Napierala * All rights reserved. 4009ea47eSEdward Tomasz Napierala * 5009ea47eSEdward Tomasz Napierala * This software was developed by Edward Tomasz Napierala under sponsorship 6009ea47eSEdward Tomasz Napierala * from the FreeBSD Foundation. 7009ea47eSEdward Tomasz Napierala * 8009ea47eSEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without 9009ea47eSEdward Tomasz Napierala * modification, are permitted provided that the following conditions 10009ea47eSEdward Tomasz Napierala * are met: 11009ea47eSEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright 12009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer. 13009ea47eSEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright 14009ea47eSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the 15009ea47eSEdward Tomasz Napierala * documentation and/or other materials provided with the distribution. 16009ea47eSEdward Tomasz Napierala * 17009ea47eSEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18009ea47eSEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19009ea47eSEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20009ea47eSEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21009ea47eSEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22009ea47eSEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23009ea47eSEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24009ea47eSEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25009ea47eSEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26009ea47eSEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27009ea47eSEdward Tomasz Napierala * SUCH DAMAGE. 28009ea47eSEdward Tomasz Napierala * 29009ea47eSEdward Tomasz Napierala * $FreeBSD$ 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> 38b881b8beSRobert Watson #include <sys/capsicum.h> 39009ea47eSEdward Tomasz Napierala #include <sys/wait.h> 40009ea47eSEdward Tomasz Napierala #include <assert.h> 41009ea47eSEdward Tomasz Napierala #include <errno.h> 42009ea47eSEdward Tomasz Napierala #include <fcntl.h> 43009ea47eSEdward Tomasz Napierala #include <netdb.h> 44009ea47eSEdward Tomasz Napierala #include <signal.h> 45009ea47eSEdward Tomasz Napierala #include <stdbool.h> 46009ea47eSEdward Tomasz Napierala #include <stdint.h> 47009ea47eSEdward Tomasz Napierala #include <stdio.h> 48009ea47eSEdward Tomasz Napierala #include <stdlib.h> 49009ea47eSEdward Tomasz Napierala #include <string.h> 50009ea47eSEdward Tomasz Napierala #include <unistd.h> 51009ea47eSEdward Tomasz Napierala 52009ea47eSEdward Tomasz Napierala #include <libutil.h> 53009ea47eSEdward Tomasz Napierala 54009ea47eSEdward Tomasz Napierala #include "iscsid.h" 55009ea47eSEdward Tomasz Napierala 56009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false; 57009ea47eSEdward Tomasz Napierala 58009ea47eSEdward Tomasz Napierala static int nchildren = 0; 59009ea47eSEdward Tomasz Napierala 60009ea47eSEdward Tomasz Napierala static void 61009ea47eSEdward Tomasz Napierala usage(void) 62009ea47eSEdward Tomasz Napierala { 63009ea47eSEdward Tomasz Napierala 64009ea47eSEdward Tomasz Napierala fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n"); 65009ea47eSEdward Tomasz Napierala exit(1); 66009ea47eSEdward Tomasz Napierala } 67009ea47eSEdward Tomasz Napierala 68009ea47eSEdward Tomasz Napierala char * 69009ea47eSEdward Tomasz Napierala checked_strdup(const char *s) 70009ea47eSEdward Tomasz Napierala { 71009ea47eSEdward Tomasz Napierala char *c; 72009ea47eSEdward Tomasz Napierala 73009ea47eSEdward Tomasz Napierala c = strdup(s); 74009ea47eSEdward Tomasz Napierala if (c == NULL) 75009ea47eSEdward Tomasz Napierala log_err(1, "strdup"); 76009ea47eSEdward Tomasz Napierala return (c); 77009ea47eSEdward Tomasz Napierala } 78009ea47eSEdward Tomasz Napierala 791f90f282SEdward Tomasz Napierala static void 801f90f282SEdward Tomasz Napierala resolve_addr(const struct connection *conn, const char *address, 811f90f282SEdward Tomasz Napierala struct addrinfo **ai, bool initiator_side) 82009ea47eSEdward Tomasz Napierala { 83009ea47eSEdward Tomasz Napierala struct addrinfo hints; 84009ea47eSEdward Tomasz Napierala char *arg, *addr, *ch; 85009ea47eSEdward Tomasz Napierala const char *port; 86009ea47eSEdward Tomasz Napierala int error, colons = 0; 87009ea47eSEdward Tomasz Napierala 88009ea47eSEdward Tomasz Napierala arg = checked_strdup(address); 89009ea47eSEdward Tomasz Napierala 90009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 911f90f282SEdward Tomasz Napierala fail(conn, "empty address"); 921f90f282SEdward Tomasz Napierala log_errx(1, "empty address"); 93009ea47eSEdward Tomasz Napierala } 94009ea47eSEdward Tomasz Napierala if (arg[0] == '[') { 95009ea47eSEdward Tomasz Napierala /* 96009ea47eSEdward Tomasz Napierala * IPv6 address in square brackets, perhaps with port. 97009ea47eSEdward Tomasz Napierala */ 98009ea47eSEdward Tomasz Napierala arg++; 99009ea47eSEdward Tomasz Napierala addr = strsep(&arg, "]"); 100009ea47eSEdward Tomasz Napierala if (arg == NULL) { 1011f90f282SEdward Tomasz Napierala fail(conn, "malformed address"); 1021f90f282SEdward Tomasz Napierala log_errx(1, "malformed address %s", address); 103009ea47eSEdward Tomasz Napierala } 104009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 1051f90f282SEdward Tomasz Napierala port = NULL; 106009ea47eSEdward Tomasz Napierala } else if (arg[0] == ':') { 107009ea47eSEdward Tomasz Napierala port = arg + 1; 108009ea47eSEdward Tomasz Napierala } else { 1091f90f282SEdward Tomasz Napierala fail(conn, "malformed address"); 1101f90f282SEdward Tomasz Napierala log_errx(1, "malformed address %s", address); 111009ea47eSEdward Tomasz Napierala } 112009ea47eSEdward Tomasz Napierala } else { 113009ea47eSEdward Tomasz Napierala /* 114009ea47eSEdward Tomasz Napierala * Either IPv6 address without brackets - and without 115009ea47eSEdward Tomasz Napierala * a port - or IPv4 address. Just count the colons. 116009ea47eSEdward Tomasz Napierala */ 117009ea47eSEdward Tomasz Napierala for (ch = arg; *ch != '\0'; ch++) { 118009ea47eSEdward Tomasz Napierala if (*ch == ':') 119009ea47eSEdward Tomasz Napierala colons++; 120009ea47eSEdward Tomasz Napierala } 121009ea47eSEdward Tomasz Napierala if (colons > 1) { 122009ea47eSEdward Tomasz Napierala addr = arg; 1231f90f282SEdward Tomasz Napierala port = NULL; 124009ea47eSEdward Tomasz Napierala } else { 125009ea47eSEdward Tomasz Napierala addr = strsep(&arg, ":"); 126009ea47eSEdward Tomasz Napierala if (arg == NULL) 1271f90f282SEdward Tomasz Napierala port = NULL; 128009ea47eSEdward Tomasz Napierala else 129009ea47eSEdward Tomasz Napierala port = arg; 130009ea47eSEdward Tomasz Napierala } 131009ea47eSEdward Tomasz Napierala } 132009ea47eSEdward Tomasz Napierala 1331f90f282SEdward Tomasz Napierala if (port == NULL && !initiator_side) 1341f90f282SEdward Tomasz Napierala port = "3260"; 1351f90f282SEdward Tomasz Napierala 136009ea47eSEdward Tomasz Napierala memset(&hints, 0, sizeof(hints)); 137009ea47eSEdward Tomasz Napierala hints.ai_family = PF_UNSPEC; 138009ea47eSEdward Tomasz Napierala hints.ai_socktype = SOCK_STREAM; 1391f90f282SEdward Tomasz Napierala hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; 1401f90f282SEdward Tomasz Napierala if (initiator_side) 1411f90f282SEdward Tomasz Napierala hints.ai_flags |= AI_PASSIVE; 142009ea47eSEdward Tomasz Napierala 143009ea47eSEdward Tomasz Napierala error = getaddrinfo(addr, port, &hints, ai); 144009ea47eSEdward Tomasz Napierala if (error != 0) { 1451f90f282SEdward Tomasz Napierala fail(conn, gai_strerror(error)); 1461f90f282SEdward Tomasz Napierala log_errx(1, "getaddrinfo for %s failed: %s", 147009ea47eSEdward Tomasz Napierala address, gai_strerror(error)); 148009ea47eSEdward Tomasz Napierala } 149009ea47eSEdward Tomasz Napierala } 150009ea47eSEdward Tomasz Napierala 151009ea47eSEdward Tomasz Napierala static struct connection * 152*ffe82e05SAlexander Motin connection_new(unsigned int session_id, const uint8_t isid[8], uint16_t tsih, 153*ffe82e05SAlexander Motin const struct iscsi_session_conf *conf, int iscsi_fd) 154009ea47eSEdward Tomasz Napierala { 155009ea47eSEdward Tomasz Napierala struct connection *conn; 156009ea47eSEdward Tomasz Napierala struct addrinfo *from_ai, *to_ai; 157009ea47eSEdward Tomasz Napierala const char *from_addr, *to_addr; 158009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1597843bd03SEdward Tomasz Napierala struct iscsi_daemon_connect idc; 160009ea47eSEdward Tomasz Napierala #endif 161009ea47eSEdward Tomasz Napierala int error; 162009ea47eSEdward Tomasz Napierala 163009ea47eSEdward Tomasz Napierala conn = calloc(1, sizeof(*conn)); 164009ea47eSEdward Tomasz Napierala if (conn == NULL) 165009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 166009ea47eSEdward Tomasz Napierala 167009ea47eSEdward Tomasz Napierala /* 168009ea47eSEdward Tomasz Napierala * Default values, from RFC 3720, section 12. 169009ea47eSEdward Tomasz Napierala */ 170009ea47eSEdward Tomasz Napierala conn->conn_header_digest = CONN_DIGEST_NONE; 171009ea47eSEdward Tomasz Napierala conn->conn_data_digest = CONN_DIGEST_NONE; 172009ea47eSEdward Tomasz Napierala conn->conn_initial_r2t = true; 173009ea47eSEdward Tomasz Napierala conn->conn_immediate_data = true; 174009ea47eSEdward Tomasz Napierala conn->conn_max_data_segment_length = 8192; 175009ea47eSEdward Tomasz Napierala conn->conn_max_burst_length = 262144; 176009ea47eSEdward Tomasz Napierala conn->conn_first_burst_length = 65536; 177009ea47eSEdward Tomasz Napierala 178009ea47eSEdward Tomasz Napierala conn->conn_session_id = session_id; 179*ffe82e05SAlexander Motin memcpy(&conn->conn_isid, isid, sizeof(conn->conn_isid)); 180*ffe82e05SAlexander Motin conn->conn_tsih = tsih; 1811f90f282SEdward Tomasz Napierala conn->conn_iscsi_fd = iscsi_fd; 1821f90f282SEdward Tomasz Napierala 183009ea47eSEdward Tomasz Napierala /* 184009ea47eSEdward Tomasz Napierala * XXX: Should we sanitize this somehow? 185009ea47eSEdward Tomasz Napierala */ 186009ea47eSEdward Tomasz Napierala memcpy(&conn->conn_conf, conf, sizeof(conn->conn_conf)); 187009ea47eSEdward Tomasz Napierala 188009ea47eSEdward Tomasz Napierala from_addr = conn->conn_conf.isc_initiator_addr; 189009ea47eSEdward Tomasz Napierala to_addr = conn->conn_conf.isc_target_addr; 190009ea47eSEdward Tomasz Napierala 1911f90f282SEdward Tomasz Napierala if (from_addr[0] != '\0') 1921f90f282SEdward Tomasz Napierala resolve_addr(conn, from_addr, &from_ai, true); 1931f90f282SEdward Tomasz Napierala else 194009ea47eSEdward Tomasz Napierala from_ai = NULL; 195009ea47eSEdward Tomasz Napierala 1961f90f282SEdward Tomasz Napierala resolve_addr(conn, to_addr, &to_ai, false); 197009ea47eSEdward Tomasz Napierala 198009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 19957a4f20bSEdward Tomasz Napierala if (conn->conn_conf.isc_iser) { 2007843bd03SEdward Tomasz Napierala memset(&idc, 0, sizeof(idc)); 2017843bd03SEdward Tomasz Napierala idc.idc_session_id = conn->conn_session_id; 202009ea47eSEdward Tomasz Napierala if (conn->conn_conf.isc_iser) 2037843bd03SEdward Tomasz Napierala idc.idc_iser = 1; 2047843bd03SEdward Tomasz Napierala idc.idc_domain = to_ai->ai_family; 2057843bd03SEdward Tomasz Napierala idc.idc_socktype = to_ai->ai_socktype; 2067843bd03SEdward Tomasz Napierala idc.idc_protocol = to_ai->ai_protocol; 207009ea47eSEdward Tomasz Napierala if (from_ai != NULL) { 2087843bd03SEdward Tomasz Napierala idc.idc_from_addr = from_ai->ai_addr; 2097843bd03SEdward Tomasz Napierala idc.idc_from_addrlen = from_ai->ai_addrlen; 210009ea47eSEdward Tomasz Napierala } 2117843bd03SEdward Tomasz Napierala idc.idc_to_addr = to_ai->ai_addr; 2127843bd03SEdward Tomasz Napierala idc.idc_to_addrlen = to_ai->ai_addrlen; 213009ea47eSEdward Tomasz Napierala 214009ea47eSEdward Tomasz Napierala log_debugx("connecting to %s using ICL kernel proxy", to_addr); 2157843bd03SEdward Tomasz Napierala error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc); 216009ea47eSEdward Tomasz Napierala if (error != 0) { 217009ea47eSEdward Tomasz Napierala fail(conn, strerror(errno)); 21857a4f20bSEdward Tomasz Napierala log_err(1, "failed to connect to %s " 21957a4f20bSEdward Tomasz Napierala "using ICL kernel proxy: ISCSIDCONNECT", to_addr); 220009ea47eSEdward Tomasz Napierala } 221009ea47eSEdward Tomasz Napierala 22257a4f20bSEdward Tomasz Napierala return (conn); 22357a4f20bSEdward Tomasz Napierala } 22457a4f20bSEdward Tomasz Napierala #endif /* ICL_KERNEL_PROXY */ 225009ea47eSEdward Tomasz Napierala 2261f90f282SEdward Tomasz Napierala if (conn->conn_conf.isc_iser) { 2271f90f282SEdward Tomasz Napierala fail(conn, "iSER not supported"); 228009ea47eSEdward Tomasz Napierala log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY " 229009ea47eSEdward Tomasz Napierala "does not support iSER"); 2301f90f282SEdward Tomasz Napierala } 231009ea47eSEdward Tomasz Napierala 232009ea47eSEdward Tomasz Napierala conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype, 233009ea47eSEdward Tomasz Napierala to_ai->ai_protocol); 2341f90f282SEdward Tomasz Napierala if (conn->conn_socket < 0) { 2351f90f282SEdward Tomasz Napierala fail(conn, strerror(errno)); 236009ea47eSEdward Tomasz Napierala log_err(1, "failed to create socket for %s", from_addr); 2371f90f282SEdward Tomasz Napierala } 238009ea47eSEdward Tomasz Napierala if (from_ai != NULL) { 239009ea47eSEdward Tomasz Napierala error = bind(conn->conn_socket, from_ai->ai_addr, 240009ea47eSEdward Tomasz Napierala from_ai->ai_addrlen); 2411f90f282SEdward Tomasz Napierala if (error != 0) { 2421f90f282SEdward Tomasz Napierala fail(conn, strerror(errno)); 243009ea47eSEdward Tomasz Napierala log_err(1, "failed to bind to %s", from_addr); 244009ea47eSEdward Tomasz Napierala } 2451f90f282SEdward Tomasz Napierala } 246009ea47eSEdward Tomasz Napierala log_debugx("connecting to %s", to_addr); 247009ea47eSEdward Tomasz Napierala error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); 248009ea47eSEdward Tomasz Napierala if (error != 0) { 249009ea47eSEdward Tomasz Napierala fail(conn, strerror(errno)); 250009ea47eSEdward Tomasz Napierala log_err(1, "failed to connect to %s", to_addr); 251009ea47eSEdward Tomasz Napierala } 252009ea47eSEdward Tomasz Napierala 253009ea47eSEdward Tomasz Napierala return (conn); 254009ea47eSEdward Tomasz Napierala } 255009ea47eSEdward Tomasz Napierala 256009ea47eSEdward Tomasz Napierala static void 257009ea47eSEdward Tomasz Napierala handoff(struct connection *conn) 258009ea47eSEdward Tomasz Napierala { 2597843bd03SEdward Tomasz Napierala struct iscsi_daemon_handoff idh; 260009ea47eSEdward Tomasz Napierala int error; 261009ea47eSEdward Tomasz Napierala 262009ea47eSEdward Tomasz Napierala log_debugx("handing off connection to the kernel"); 263009ea47eSEdward Tomasz Napierala 2647843bd03SEdward Tomasz Napierala memset(&idh, 0, sizeof(idh)); 2657843bd03SEdward Tomasz Napierala idh.idh_session_id = conn->conn_session_id; 2667843bd03SEdward Tomasz Napierala idh.idh_socket = conn->conn_socket; 2677843bd03SEdward Tomasz Napierala strlcpy(idh.idh_target_alias, conn->conn_target_alias, 2687843bd03SEdward Tomasz Napierala sizeof(idh.idh_target_alias)); 269*ffe82e05SAlexander Motin idh.idh_tsih = conn->conn_tsih; 2707843bd03SEdward Tomasz Napierala idh.idh_statsn = conn->conn_statsn; 2717843bd03SEdward Tomasz Napierala idh.idh_header_digest = conn->conn_header_digest; 2727843bd03SEdward Tomasz Napierala idh.idh_data_digest = conn->conn_data_digest; 2737843bd03SEdward Tomasz Napierala idh.idh_initial_r2t = conn->conn_initial_r2t; 2747843bd03SEdward Tomasz Napierala idh.idh_immediate_data = conn->conn_immediate_data; 2757843bd03SEdward Tomasz Napierala idh.idh_max_data_segment_length = conn->conn_max_data_segment_length; 2767843bd03SEdward Tomasz Napierala idh.idh_max_burst_length = conn->conn_max_burst_length; 2777843bd03SEdward Tomasz Napierala idh.idh_first_burst_length = conn->conn_first_burst_length; 278009ea47eSEdward Tomasz Napierala 2797843bd03SEdward Tomasz Napierala error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh); 280009ea47eSEdward Tomasz Napierala if (error != 0) 281009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDHANDOFF"); 282009ea47eSEdward Tomasz Napierala } 283009ea47eSEdward Tomasz Napierala 284009ea47eSEdward Tomasz Napierala void 285009ea47eSEdward Tomasz Napierala fail(const struct connection *conn, const char *reason) 286009ea47eSEdward Tomasz Napierala { 2877843bd03SEdward Tomasz Napierala struct iscsi_daemon_fail idf; 288009ea47eSEdward Tomasz Napierala int error; 289009ea47eSEdward Tomasz Napierala 2907843bd03SEdward Tomasz Napierala memset(&idf, 0, sizeof(idf)); 2917843bd03SEdward Tomasz Napierala idf.idf_session_id = conn->conn_session_id; 2927843bd03SEdward Tomasz Napierala strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason)); 293009ea47eSEdward Tomasz Napierala 2947843bd03SEdward Tomasz Napierala error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf); 295009ea47eSEdward Tomasz Napierala if (error != 0) 296009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDFAIL"); 297009ea47eSEdward Tomasz Napierala } 298009ea47eSEdward Tomasz Napierala 299009ea47eSEdward Tomasz Napierala /* 300009ea47eSEdward Tomasz Napierala * XXX: I CANT INTO LATIN 301009ea47eSEdward Tomasz Napierala */ 302009ea47eSEdward Tomasz Napierala static void 303009ea47eSEdward Tomasz Napierala capsicate(struct connection *conn) 304009ea47eSEdward Tomasz Napierala { 305009ea47eSEdward Tomasz Napierala int error; 306009ea47eSEdward Tomasz Napierala cap_rights_t rights; 307009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 308009ea47eSEdward Tomasz Napierala const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, 30951be90b5SEdward Tomasz Napierala ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; 310009ea47eSEdward Tomasz Napierala #else 311009ea47eSEdward Tomasz Napierala const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, 31251be90b5SEdward Tomasz Napierala ISCSISREMOVE, ISCSISMODIFY }; 313009ea47eSEdward Tomasz Napierala #endif 314009ea47eSEdward Tomasz Napierala 315009ea47eSEdward Tomasz Napierala cap_rights_init(&rights, CAP_IOCTL); 316009ea47eSEdward Tomasz Napierala error = cap_rights_limit(conn->conn_iscsi_fd, &rights); 317009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 318009ea47eSEdward Tomasz Napierala log_err(1, "cap_rights_limit"); 319009ea47eSEdward Tomasz Napierala 320009ea47eSEdward Tomasz Napierala error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, 321009ea47eSEdward Tomasz Napierala sizeof(cmds) / sizeof(cmds[0])); 322009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 323009ea47eSEdward Tomasz Napierala log_err(1, "cap_ioctls_limit"); 324009ea47eSEdward Tomasz Napierala 325009ea47eSEdward Tomasz Napierala error = cap_enter(); 326009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 327009ea47eSEdward Tomasz Napierala log_err(1, "cap_enter"); 328009ea47eSEdward Tomasz Napierala 329009ea47eSEdward Tomasz Napierala if (cap_sandboxed()) 330009ea47eSEdward Tomasz Napierala log_debugx("Capsicum capability mode enabled"); 331009ea47eSEdward Tomasz Napierala else 332009ea47eSEdward Tomasz Napierala log_warnx("Capsicum capability mode not supported"); 333009ea47eSEdward Tomasz Napierala } 334009ea47eSEdward Tomasz Napierala 335009ea47eSEdward Tomasz Napierala bool 336009ea47eSEdward Tomasz Napierala timed_out(void) 337009ea47eSEdward Tomasz Napierala { 338009ea47eSEdward Tomasz Napierala 339009ea47eSEdward Tomasz Napierala return (sigalrm_received); 340009ea47eSEdward Tomasz Napierala } 341009ea47eSEdward Tomasz Napierala 342009ea47eSEdward Tomasz Napierala static void 343009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused) 344009ea47eSEdward Tomasz Napierala { 345009ea47eSEdward Tomasz Napierala /* 346009ea47eSEdward Tomasz Napierala * It would be easiest to just log an error and exit. We can't 347009ea47eSEdward Tomasz Napierala * do this, though, because log_errx() is not signal safe, since 348009ea47eSEdward Tomasz Napierala * it calls syslog(3). Instead, set a flag checked by pdu_send() 349009ea47eSEdward Tomasz Napierala * and pdu_receive(), to call log_errx() there. Should they fail 350009ea47eSEdward Tomasz Napierala * to notice, we'll exit here one second later. 351009ea47eSEdward Tomasz Napierala */ 352009ea47eSEdward Tomasz Napierala if (sigalrm_received) { 353009ea47eSEdward Tomasz Napierala /* 354009ea47eSEdward Tomasz Napierala * Oh well. Just give up and quit. 355009ea47eSEdward Tomasz Napierala */ 356009ea47eSEdward Tomasz Napierala _exit(2); 357009ea47eSEdward Tomasz Napierala } 358009ea47eSEdward Tomasz Napierala 359009ea47eSEdward Tomasz Napierala sigalrm_received = true; 360009ea47eSEdward Tomasz Napierala } 361009ea47eSEdward Tomasz Napierala 362009ea47eSEdward Tomasz Napierala static void 363009ea47eSEdward Tomasz Napierala set_timeout(int timeout) 364009ea47eSEdward Tomasz Napierala { 365009ea47eSEdward Tomasz Napierala struct sigaction sa; 366009ea47eSEdward Tomasz Napierala struct itimerval itv; 367009ea47eSEdward Tomasz Napierala int error; 368009ea47eSEdward Tomasz Napierala 369009ea47eSEdward Tomasz Napierala if (timeout <= 0) { 370009ea47eSEdward Tomasz Napierala log_debugx("session timeout disabled"); 371009ea47eSEdward Tomasz Napierala return; 372009ea47eSEdward Tomasz Napierala } 373009ea47eSEdward Tomasz Napierala 374009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 375009ea47eSEdward Tomasz Napierala sa.sa_handler = sigalrm_handler; 376009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 377009ea47eSEdward Tomasz Napierala error = sigaction(SIGALRM, &sa, NULL); 378009ea47eSEdward Tomasz Napierala if (error != 0) 379009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 380009ea47eSEdward Tomasz Napierala 381009ea47eSEdward Tomasz Napierala /* 382009ea47eSEdward Tomasz Napierala * First SIGALRM will arive after conf_timeout seconds. 383009ea47eSEdward Tomasz Napierala * If we do nothing, another one will arrive a second later. 384009ea47eSEdward Tomasz Napierala */ 385009ea47eSEdward Tomasz Napierala bzero(&itv, sizeof(itv)); 386009ea47eSEdward Tomasz Napierala itv.it_interval.tv_sec = 1; 387009ea47eSEdward Tomasz Napierala itv.it_value.tv_sec = timeout; 388009ea47eSEdward Tomasz Napierala 389009ea47eSEdward Tomasz Napierala log_debugx("setting session timeout to %d seconds", 390009ea47eSEdward Tomasz Napierala timeout); 391009ea47eSEdward Tomasz Napierala error = setitimer(ITIMER_REAL, &itv, NULL); 392009ea47eSEdward Tomasz Napierala if (error != 0) 393009ea47eSEdward Tomasz Napierala log_err(1, "setitimer"); 394009ea47eSEdward Tomasz Napierala } 395009ea47eSEdward Tomasz Napierala 396009ea47eSEdward Tomasz Napierala static void 3971ecb3c58SEdward Tomasz Napierala sigchld_handler(int dummy __unused) 3981ecb3c58SEdward Tomasz Napierala { 3991ecb3c58SEdward Tomasz Napierala 4001ecb3c58SEdward Tomasz Napierala /* 4011ecb3c58SEdward Tomasz Napierala * The only purpose of this handler is to make SIGCHLD 4021ecb3c58SEdward Tomasz Napierala * interrupt the ISCSIDWAIT ioctl(2), so we can call 4031ecb3c58SEdward Tomasz Napierala * wait_for_children(). 4041ecb3c58SEdward Tomasz Napierala */ 4051ecb3c58SEdward Tomasz Napierala } 4061ecb3c58SEdward Tomasz Napierala 4071ecb3c58SEdward Tomasz Napierala static void 4081ecb3c58SEdward Tomasz Napierala register_sigchld(void) 4091ecb3c58SEdward Tomasz Napierala { 4101ecb3c58SEdward Tomasz Napierala struct sigaction sa; 4111ecb3c58SEdward Tomasz Napierala int error; 4121ecb3c58SEdward Tomasz Napierala 4131ecb3c58SEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 4141ecb3c58SEdward Tomasz Napierala sa.sa_handler = sigchld_handler; 4151ecb3c58SEdward Tomasz Napierala sigfillset(&sa.sa_mask); 4161ecb3c58SEdward Tomasz Napierala error = sigaction(SIGCHLD, &sa, NULL); 4171ecb3c58SEdward Tomasz Napierala if (error != 0) 4181ecb3c58SEdward Tomasz Napierala log_err(1, "sigaction"); 4191ecb3c58SEdward Tomasz Napierala 4201ecb3c58SEdward Tomasz Napierala } 4211ecb3c58SEdward Tomasz Napierala 4221ecb3c58SEdward Tomasz Napierala static void 4237843bd03SEdward Tomasz Napierala handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout) 424009ea47eSEdward Tomasz Napierala { 425009ea47eSEdward Tomasz Napierala struct connection *conn; 426009ea47eSEdward Tomasz Napierala 427009ea47eSEdward Tomasz Napierala log_set_peer_addr(request->idr_conf.isc_target_addr); 428009ea47eSEdward Tomasz Napierala if (request->idr_conf.isc_target[0] != '\0') { 429009ea47eSEdward Tomasz Napierala log_set_peer_name(request->idr_conf.isc_target); 430009ea47eSEdward Tomasz Napierala setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target); 431009ea47eSEdward Tomasz Napierala } else { 432009ea47eSEdward Tomasz Napierala setproctitle("%s", request->idr_conf.isc_target_addr); 433009ea47eSEdward Tomasz Napierala } 434009ea47eSEdward Tomasz Napierala 435*ffe82e05SAlexander Motin conn = connection_new(request->idr_session_id, request->idr_isid, 436*ffe82e05SAlexander Motin request->idr_tsih, &request->idr_conf, iscsi_fd); 437009ea47eSEdward Tomasz Napierala set_timeout(timeout); 438009ea47eSEdward Tomasz Napierala capsicate(conn); 439009ea47eSEdward Tomasz Napierala login(conn); 440009ea47eSEdward Tomasz Napierala if (conn->conn_conf.isc_discovery != 0) 441009ea47eSEdward Tomasz Napierala discovery(conn); 442009ea47eSEdward Tomasz Napierala else 443009ea47eSEdward Tomasz Napierala handoff(conn); 444009ea47eSEdward Tomasz Napierala 445009ea47eSEdward Tomasz Napierala log_debugx("nothing more to do; exiting"); 446009ea47eSEdward Tomasz Napierala exit (0); 447009ea47eSEdward Tomasz Napierala } 448009ea47eSEdward Tomasz Napierala 449009ea47eSEdward Tomasz Napierala static int 450009ea47eSEdward Tomasz Napierala wait_for_children(bool block) 451009ea47eSEdward Tomasz Napierala { 452009ea47eSEdward Tomasz Napierala pid_t pid; 453009ea47eSEdward Tomasz Napierala int status; 454009ea47eSEdward Tomasz Napierala int num = 0; 455009ea47eSEdward Tomasz Napierala 456009ea47eSEdward Tomasz Napierala for (;;) { 457009ea47eSEdward Tomasz Napierala /* 458009ea47eSEdward Tomasz Napierala * If "block" is true, wait for at least one process. 459009ea47eSEdward Tomasz Napierala */ 460009ea47eSEdward Tomasz Napierala if (block && num == 0) 461009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, 0, NULL); 462009ea47eSEdward Tomasz Napierala else 463009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, WNOHANG, NULL); 464009ea47eSEdward Tomasz Napierala if (pid <= 0) 465009ea47eSEdward Tomasz Napierala break; 466009ea47eSEdward Tomasz Napierala if (WIFSIGNALED(status)) { 467009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with signal %d", 468009ea47eSEdward Tomasz Napierala pid, WTERMSIG(status)); 469009ea47eSEdward Tomasz Napierala } else if (WEXITSTATUS(status) != 0) { 470009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with exit status %d", 471009ea47eSEdward Tomasz Napierala pid, WEXITSTATUS(status)); 472009ea47eSEdward Tomasz Napierala } else { 473009ea47eSEdward Tomasz Napierala log_debugx("child process %d terminated gracefully", pid); 474009ea47eSEdward Tomasz Napierala } 475009ea47eSEdward Tomasz Napierala num++; 476009ea47eSEdward Tomasz Napierala } 477009ea47eSEdward Tomasz Napierala 478009ea47eSEdward Tomasz Napierala return (num); 479009ea47eSEdward Tomasz Napierala } 480009ea47eSEdward Tomasz Napierala 481009ea47eSEdward Tomasz Napierala int 482009ea47eSEdward Tomasz Napierala main(int argc, char **argv) 483009ea47eSEdward Tomasz Napierala { 484009ea47eSEdward Tomasz Napierala int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, 485009ea47eSEdward Tomasz Napierala timeout = 60; 486009ea47eSEdward Tomasz Napierala bool dont_daemonize = false; 487009ea47eSEdward Tomasz Napierala struct pidfh *pidfh; 488009ea47eSEdward Tomasz Napierala pid_t pid, otherpid; 489009ea47eSEdward Tomasz Napierala const char *pidfile_path = DEFAULT_PIDFILE; 4907843bd03SEdward Tomasz Napierala struct iscsi_daemon_request request; 491009ea47eSEdward Tomasz Napierala 492009ea47eSEdward Tomasz Napierala while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { 493009ea47eSEdward Tomasz Napierala switch (ch) { 494009ea47eSEdward Tomasz Napierala case 'P': 495009ea47eSEdward Tomasz Napierala pidfile_path = optarg; 496009ea47eSEdward Tomasz Napierala break; 497009ea47eSEdward Tomasz Napierala case 'd': 498009ea47eSEdward Tomasz Napierala dont_daemonize = true; 499009ea47eSEdward Tomasz Napierala debug++; 500009ea47eSEdward Tomasz Napierala break; 501009ea47eSEdward Tomasz Napierala case 'l': 502009ea47eSEdward Tomasz Napierala debug = atoi(optarg); 503009ea47eSEdward Tomasz Napierala break; 504009ea47eSEdward Tomasz Napierala case 'm': 505009ea47eSEdward Tomasz Napierala maxproc = atoi(optarg); 506009ea47eSEdward Tomasz Napierala break; 507009ea47eSEdward Tomasz Napierala case 't': 508009ea47eSEdward Tomasz Napierala timeout = atoi(optarg); 509009ea47eSEdward Tomasz Napierala break; 510009ea47eSEdward Tomasz Napierala case '?': 511009ea47eSEdward Tomasz Napierala default: 512009ea47eSEdward Tomasz Napierala usage(); 513009ea47eSEdward Tomasz Napierala } 514009ea47eSEdward Tomasz Napierala } 515009ea47eSEdward Tomasz Napierala argc -= optind; 516009ea47eSEdward Tomasz Napierala if (argc != 0) 517009ea47eSEdward Tomasz Napierala usage(); 518009ea47eSEdward Tomasz Napierala 519009ea47eSEdward Tomasz Napierala log_init(debug); 520009ea47eSEdward Tomasz Napierala 521009ea47eSEdward Tomasz Napierala pidfh = pidfile_open(pidfile_path, 0600, &otherpid); 522009ea47eSEdward Tomasz Napierala if (pidfh == NULL) { 523009ea47eSEdward Tomasz Napierala if (errno == EEXIST) 524009ea47eSEdward Tomasz Napierala log_errx(1, "daemon already running, pid: %jd.", 525009ea47eSEdward Tomasz Napierala (intmax_t)otherpid); 526009ea47eSEdward Tomasz Napierala log_err(1, "cannot open or create pidfile \"%s\"", 527009ea47eSEdward Tomasz Napierala pidfile_path); 528009ea47eSEdward Tomasz Napierala } 529009ea47eSEdward Tomasz Napierala 530009ea47eSEdward Tomasz Napierala iscsi_fd = open(ISCSI_PATH, O_RDWR); 531c76e8a9aSEdward Tomasz Napierala if (iscsi_fd < 0 && errno == ENOENT) { 532009ea47eSEdward Tomasz Napierala saved_errno = errno; 533009ea47eSEdward Tomasz Napierala retval = kldload("iscsi"); 534009ea47eSEdward Tomasz Napierala if (retval != -1) 535009ea47eSEdward Tomasz Napierala iscsi_fd = open(ISCSI_PATH, O_RDWR); 536009ea47eSEdward Tomasz Napierala else 537009ea47eSEdward Tomasz Napierala errno = saved_errno; 538009ea47eSEdward Tomasz Napierala } 539009ea47eSEdward Tomasz Napierala if (iscsi_fd < 0) 540009ea47eSEdward Tomasz Napierala log_err(1, "failed to open %s", ISCSI_PATH); 541009ea47eSEdward Tomasz Napierala 542009ea47eSEdward Tomasz Napierala if (dont_daemonize == false) { 543009ea47eSEdward Tomasz Napierala if (daemon(0, 0) == -1) { 544009ea47eSEdward Tomasz Napierala log_warn("cannot daemonize"); 545009ea47eSEdward Tomasz Napierala pidfile_remove(pidfh); 546009ea47eSEdward Tomasz Napierala exit(1); 547009ea47eSEdward Tomasz Napierala } 548009ea47eSEdward Tomasz Napierala } 549009ea47eSEdward Tomasz Napierala 550009ea47eSEdward Tomasz Napierala pidfile_write(pidfh); 551009ea47eSEdward Tomasz Napierala 5521ecb3c58SEdward Tomasz Napierala register_sigchld(); 5531ecb3c58SEdward Tomasz Napierala 554009ea47eSEdward Tomasz Napierala for (;;) { 555009ea47eSEdward Tomasz Napierala log_debugx("waiting for request from the kernel"); 556009ea47eSEdward Tomasz Napierala 5577843bd03SEdward Tomasz Napierala memset(&request, 0, sizeof(request)); 5587843bd03SEdward Tomasz Napierala error = ioctl(iscsi_fd, ISCSIDWAIT, &request); 559009ea47eSEdward Tomasz Napierala if (error != 0) { 560009ea47eSEdward Tomasz Napierala if (errno == EINTR) { 561009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 562009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 563009ea47eSEdward Tomasz Napierala continue; 564009ea47eSEdward Tomasz Napierala } 565009ea47eSEdward Tomasz Napierala 566009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDWAIT"); 567009ea47eSEdward Tomasz Napierala } 568009ea47eSEdward Tomasz Napierala 569009ea47eSEdward Tomasz Napierala if (dont_daemonize) { 570009ea47eSEdward Tomasz Napierala log_debugx("not forking due to -d flag; " 571009ea47eSEdward Tomasz Napierala "will exit after servicing a single request"); 572009ea47eSEdward Tomasz Napierala } else { 573009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 574009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 575009ea47eSEdward Tomasz Napierala 576009ea47eSEdward Tomasz Napierala while (maxproc > 0 && nchildren >= maxproc) { 577009ea47eSEdward Tomasz Napierala log_debugx("maxproc limit of %d child processes hit; " 578009ea47eSEdward Tomasz Napierala "waiting for child process to exit", maxproc); 579009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(true); 580009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 581009ea47eSEdward Tomasz Napierala } 582009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; forking child process #%d", 583009ea47eSEdward Tomasz Napierala nchildren); 584009ea47eSEdward Tomasz Napierala nchildren++; 585009ea47eSEdward Tomasz Napierala 586009ea47eSEdward Tomasz Napierala pid = fork(); 587009ea47eSEdward Tomasz Napierala if (pid < 0) 588009ea47eSEdward Tomasz Napierala log_err(1, "fork"); 589009ea47eSEdward Tomasz Napierala if (pid > 0) 590009ea47eSEdward Tomasz Napierala continue; 591009ea47eSEdward Tomasz Napierala } 592009ea47eSEdward Tomasz Napierala 593009ea47eSEdward Tomasz Napierala pidfile_close(pidfh); 5947843bd03SEdward Tomasz Napierala handle_request(iscsi_fd, &request, timeout); 595009ea47eSEdward Tomasz Napierala } 596009ea47eSEdward Tomasz Napierala 597009ea47eSEdward Tomasz Napierala return (0); 598009ea47eSEdward Tomasz Napierala } 599