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 * 152009ea47eSEdward Tomasz Napierala connection_new(unsigned int session_id, const struct iscsi_session_conf *conf, 153009ea47eSEdward Tomasz Napierala 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; 1791f90f282SEdward Tomasz Napierala conn->conn_iscsi_fd = iscsi_fd; 1801f90f282SEdward Tomasz Napierala 181009ea47eSEdward Tomasz Napierala /* 182009ea47eSEdward Tomasz Napierala * XXX: Should we sanitize this somehow? 183009ea47eSEdward Tomasz Napierala */ 184009ea47eSEdward Tomasz Napierala memcpy(&conn->conn_conf, conf, sizeof(conn->conn_conf)); 185009ea47eSEdward Tomasz Napierala 186009ea47eSEdward Tomasz Napierala from_addr = conn->conn_conf.isc_initiator_addr; 187009ea47eSEdward Tomasz Napierala to_addr = conn->conn_conf.isc_target_addr; 188009ea47eSEdward Tomasz Napierala 1891f90f282SEdward Tomasz Napierala if (from_addr[0] != '\0') 1901f90f282SEdward Tomasz Napierala resolve_addr(conn, from_addr, &from_ai, true); 1911f90f282SEdward Tomasz Napierala else 192009ea47eSEdward Tomasz Napierala from_ai = NULL; 193009ea47eSEdward Tomasz Napierala 1941f90f282SEdward Tomasz Napierala resolve_addr(conn, to_addr, &to_ai, false); 195009ea47eSEdward Tomasz Napierala 196009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 19757a4f20bSEdward Tomasz Napierala if (conn->conn_conf.isc_iser) { 1987843bd03SEdward Tomasz Napierala memset(&idc, 0, sizeof(idc)); 1997843bd03SEdward Tomasz Napierala idc.idc_session_id = conn->conn_session_id; 200009ea47eSEdward Tomasz Napierala if (conn->conn_conf.isc_iser) 2017843bd03SEdward Tomasz Napierala idc.idc_iser = 1; 2027843bd03SEdward Tomasz Napierala idc.idc_domain = to_ai->ai_family; 2037843bd03SEdward Tomasz Napierala idc.idc_socktype = to_ai->ai_socktype; 2047843bd03SEdward Tomasz Napierala idc.idc_protocol = to_ai->ai_protocol; 205009ea47eSEdward Tomasz Napierala if (from_ai != NULL) { 2067843bd03SEdward Tomasz Napierala idc.idc_from_addr = from_ai->ai_addr; 2077843bd03SEdward Tomasz Napierala idc.idc_from_addrlen = from_ai->ai_addrlen; 208009ea47eSEdward Tomasz Napierala } 2097843bd03SEdward Tomasz Napierala idc.idc_to_addr = to_ai->ai_addr; 2107843bd03SEdward Tomasz Napierala idc.idc_to_addrlen = to_ai->ai_addrlen; 211009ea47eSEdward Tomasz Napierala 212009ea47eSEdward Tomasz Napierala log_debugx("connecting to %s using ICL kernel proxy", to_addr); 2137843bd03SEdward Tomasz Napierala error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc); 214009ea47eSEdward Tomasz Napierala if (error != 0) { 215009ea47eSEdward Tomasz Napierala fail(conn, strerror(errno)); 21657a4f20bSEdward Tomasz Napierala log_err(1, "failed to connect to %s " 21757a4f20bSEdward Tomasz Napierala "using ICL kernel proxy: ISCSIDCONNECT", to_addr); 218009ea47eSEdward Tomasz Napierala } 219009ea47eSEdward Tomasz Napierala 22057a4f20bSEdward Tomasz Napierala return (conn); 22157a4f20bSEdward Tomasz Napierala } 22257a4f20bSEdward Tomasz Napierala #endif /* ICL_KERNEL_PROXY */ 223009ea47eSEdward Tomasz Napierala 2241f90f282SEdward Tomasz Napierala if (conn->conn_conf.isc_iser) { 2251f90f282SEdward Tomasz Napierala fail(conn, "iSER not supported"); 226009ea47eSEdward Tomasz Napierala log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY " 227009ea47eSEdward Tomasz Napierala "does not support iSER"); 2281f90f282SEdward Tomasz Napierala } 229009ea47eSEdward Tomasz Napierala 230009ea47eSEdward Tomasz Napierala conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype, 231009ea47eSEdward Tomasz Napierala to_ai->ai_protocol); 2321f90f282SEdward Tomasz Napierala if (conn->conn_socket < 0) { 2331f90f282SEdward Tomasz Napierala fail(conn, strerror(errno)); 234009ea47eSEdward Tomasz Napierala log_err(1, "failed to create socket for %s", from_addr); 2351f90f282SEdward Tomasz Napierala } 236009ea47eSEdward Tomasz Napierala if (from_ai != NULL) { 237009ea47eSEdward Tomasz Napierala error = bind(conn->conn_socket, from_ai->ai_addr, 238009ea47eSEdward Tomasz Napierala from_ai->ai_addrlen); 2391f90f282SEdward Tomasz Napierala if (error != 0) { 2401f90f282SEdward Tomasz Napierala fail(conn, strerror(errno)); 241009ea47eSEdward Tomasz Napierala log_err(1, "failed to bind to %s", from_addr); 242009ea47eSEdward Tomasz Napierala } 2431f90f282SEdward Tomasz Napierala } 244009ea47eSEdward Tomasz Napierala log_debugx("connecting to %s", to_addr); 245009ea47eSEdward Tomasz Napierala error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); 246009ea47eSEdward Tomasz Napierala if (error != 0) { 247009ea47eSEdward Tomasz Napierala fail(conn, strerror(errno)); 248009ea47eSEdward Tomasz Napierala log_err(1, "failed to connect to %s", to_addr); 249009ea47eSEdward Tomasz Napierala } 250009ea47eSEdward Tomasz Napierala 251009ea47eSEdward Tomasz Napierala return (conn); 252009ea47eSEdward Tomasz Napierala } 253009ea47eSEdward Tomasz Napierala 254009ea47eSEdward Tomasz Napierala static void 255009ea47eSEdward Tomasz Napierala handoff(struct connection *conn) 256009ea47eSEdward Tomasz Napierala { 2577843bd03SEdward Tomasz Napierala struct iscsi_daemon_handoff idh; 258009ea47eSEdward Tomasz Napierala int error; 259009ea47eSEdward Tomasz Napierala 260009ea47eSEdward Tomasz Napierala log_debugx("handing off connection to the kernel"); 261009ea47eSEdward Tomasz Napierala 2627843bd03SEdward Tomasz Napierala memset(&idh, 0, sizeof(idh)); 2637843bd03SEdward Tomasz Napierala idh.idh_session_id = conn->conn_session_id; 2647843bd03SEdward Tomasz Napierala idh.idh_socket = conn->conn_socket; 2657843bd03SEdward Tomasz Napierala strlcpy(idh.idh_target_alias, conn->conn_target_alias, 2667843bd03SEdward Tomasz Napierala sizeof(idh.idh_target_alias)); 2677843bd03SEdward Tomasz Napierala memcpy(idh.idh_isid, conn->conn_isid, sizeof(idh.idh_isid)); 2687843bd03SEdward Tomasz Napierala idh.idh_statsn = conn->conn_statsn; 2697843bd03SEdward Tomasz Napierala idh.idh_header_digest = conn->conn_header_digest; 2707843bd03SEdward Tomasz Napierala idh.idh_data_digest = conn->conn_data_digest; 2717843bd03SEdward Tomasz Napierala idh.idh_initial_r2t = conn->conn_initial_r2t; 2727843bd03SEdward Tomasz Napierala idh.idh_immediate_data = conn->conn_immediate_data; 2737843bd03SEdward Tomasz Napierala idh.idh_max_data_segment_length = conn->conn_max_data_segment_length; 2747843bd03SEdward Tomasz Napierala idh.idh_max_burst_length = conn->conn_max_burst_length; 2757843bd03SEdward Tomasz Napierala idh.idh_first_burst_length = conn->conn_first_burst_length; 276009ea47eSEdward Tomasz Napierala 2777843bd03SEdward Tomasz Napierala error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh); 278009ea47eSEdward Tomasz Napierala if (error != 0) 279009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDHANDOFF"); 280009ea47eSEdward Tomasz Napierala } 281009ea47eSEdward Tomasz Napierala 282009ea47eSEdward Tomasz Napierala void 283009ea47eSEdward Tomasz Napierala fail(const struct connection *conn, const char *reason) 284009ea47eSEdward Tomasz Napierala { 2857843bd03SEdward Tomasz Napierala struct iscsi_daemon_fail idf; 286009ea47eSEdward Tomasz Napierala int error; 287009ea47eSEdward Tomasz Napierala 2887843bd03SEdward Tomasz Napierala memset(&idf, 0, sizeof(idf)); 2897843bd03SEdward Tomasz Napierala idf.idf_session_id = conn->conn_session_id; 2907843bd03SEdward Tomasz Napierala strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason)); 291009ea47eSEdward Tomasz Napierala 2927843bd03SEdward Tomasz Napierala error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf); 293009ea47eSEdward Tomasz Napierala if (error != 0) 294009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDFAIL"); 295009ea47eSEdward Tomasz Napierala } 296009ea47eSEdward Tomasz Napierala 297009ea47eSEdward Tomasz Napierala /* 298009ea47eSEdward Tomasz Napierala * XXX: I CANT INTO LATIN 299009ea47eSEdward Tomasz Napierala */ 300009ea47eSEdward Tomasz Napierala static void 301009ea47eSEdward Tomasz Napierala capsicate(struct connection *conn) 302009ea47eSEdward Tomasz Napierala { 303009ea47eSEdward Tomasz Napierala int error; 304009ea47eSEdward Tomasz Napierala cap_rights_t rights; 305009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 306009ea47eSEdward Tomasz Napierala const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, 307*51be90b5SEdward Tomasz Napierala ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; 308009ea47eSEdward Tomasz Napierala #else 309009ea47eSEdward Tomasz Napierala const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, 310*51be90b5SEdward Tomasz Napierala ISCSISREMOVE, ISCSISMODIFY }; 311009ea47eSEdward Tomasz Napierala #endif 312009ea47eSEdward Tomasz Napierala 313009ea47eSEdward Tomasz Napierala cap_rights_init(&rights, CAP_IOCTL); 314009ea47eSEdward Tomasz Napierala error = cap_rights_limit(conn->conn_iscsi_fd, &rights); 315009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 316009ea47eSEdward Tomasz Napierala log_err(1, "cap_rights_limit"); 317009ea47eSEdward Tomasz Napierala 318009ea47eSEdward Tomasz Napierala error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, 319009ea47eSEdward Tomasz Napierala sizeof(cmds) / sizeof(cmds[0])); 320009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 321009ea47eSEdward Tomasz Napierala log_err(1, "cap_ioctls_limit"); 322009ea47eSEdward Tomasz Napierala 323009ea47eSEdward Tomasz Napierala error = cap_enter(); 324009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 325009ea47eSEdward Tomasz Napierala log_err(1, "cap_enter"); 326009ea47eSEdward Tomasz Napierala 327009ea47eSEdward Tomasz Napierala if (cap_sandboxed()) 328009ea47eSEdward Tomasz Napierala log_debugx("Capsicum capability mode enabled"); 329009ea47eSEdward Tomasz Napierala else 330009ea47eSEdward Tomasz Napierala log_warnx("Capsicum capability mode not supported"); 331009ea47eSEdward Tomasz Napierala } 332009ea47eSEdward Tomasz Napierala 333009ea47eSEdward Tomasz Napierala bool 334009ea47eSEdward Tomasz Napierala timed_out(void) 335009ea47eSEdward Tomasz Napierala { 336009ea47eSEdward Tomasz Napierala 337009ea47eSEdward Tomasz Napierala return (sigalrm_received); 338009ea47eSEdward Tomasz Napierala } 339009ea47eSEdward Tomasz Napierala 340009ea47eSEdward Tomasz Napierala static void 341009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused) 342009ea47eSEdward Tomasz Napierala { 343009ea47eSEdward Tomasz Napierala /* 344009ea47eSEdward Tomasz Napierala * It would be easiest to just log an error and exit. We can't 345009ea47eSEdward Tomasz Napierala * do this, though, because log_errx() is not signal safe, since 346009ea47eSEdward Tomasz Napierala * it calls syslog(3). Instead, set a flag checked by pdu_send() 347009ea47eSEdward Tomasz Napierala * and pdu_receive(), to call log_errx() there. Should they fail 348009ea47eSEdward Tomasz Napierala * to notice, we'll exit here one second later. 349009ea47eSEdward Tomasz Napierala */ 350009ea47eSEdward Tomasz Napierala if (sigalrm_received) { 351009ea47eSEdward Tomasz Napierala /* 352009ea47eSEdward Tomasz Napierala * Oh well. Just give up and quit. 353009ea47eSEdward Tomasz Napierala */ 354009ea47eSEdward Tomasz Napierala _exit(2); 355009ea47eSEdward Tomasz Napierala } 356009ea47eSEdward Tomasz Napierala 357009ea47eSEdward Tomasz Napierala sigalrm_received = true; 358009ea47eSEdward Tomasz Napierala } 359009ea47eSEdward Tomasz Napierala 360009ea47eSEdward Tomasz Napierala static void 361009ea47eSEdward Tomasz Napierala set_timeout(int timeout) 362009ea47eSEdward Tomasz Napierala { 363009ea47eSEdward Tomasz Napierala struct sigaction sa; 364009ea47eSEdward Tomasz Napierala struct itimerval itv; 365009ea47eSEdward Tomasz Napierala int error; 366009ea47eSEdward Tomasz Napierala 367009ea47eSEdward Tomasz Napierala if (timeout <= 0) { 368009ea47eSEdward Tomasz Napierala log_debugx("session timeout disabled"); 369009ea47eSEdward Tomasz Napierala return; 370009ea47eSEdward Tomasz Napierala } 371009ea47eSEdward Tomasz Napierala 372009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 373009ea47eSEdward Tomasz Napierala sa.sa_handler = sigalrm_handler; 374009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 375009ea47eSEdward Tomasz Napierala error = sigaction(SIGALRM, &sa, NULL); 376009ea47eSEdward Tomasz Napierala if (error != 0) 377009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 378009ea47eSEdward Tomasz Napierala 379009ea47eSEdward Tomasz Napierala /* 380009ea47eSEdward Tomasz Napierala * First SIGALRM will arive after conf_timeout seconds. 381009ea47eSEdward Tomasz Napierala * If we do nothing, another one will arrive a second later. 382009ea47eSEdward Tomasz Napierala */ 383009ea47eSEdward Tomasz Napierala bzero(&itv, sizeof(itv)); 384009ea47eSEdward Tomasz Napierala itv.it_interval.tv_sec = 1; 385009ea47eSEdward Tomasz Napierala itv.it_value.tv_sec = timeout; 386009ea47eSEdward Tomasz Napierala 387009ea47eSEdward Tomasz Napierala log_debugx("setting session timeout to %d seconds", 388009ea47eSEdward Tomasz Napierala timeout); 389009ea47eSEdward Tomasz Napierala error = setitimer(ITIMER_REAL, &itv, NULL); 390009ea47eSEdward Tomasz Napierala if (error != 0) 391009ea47eSEdward Tomasz Napierala log_err(1, "setitimer"); 392009ea47eSEdward Tomasz Napierala } 393009ea47eSEdward Tomasz Napierala 394009ea47eSEdward Tomasz Napierala static void 3951ecb3c58SEdward Tomasz Napierala sigchld_handler(int dummy __unused) 3961ecb3c58SEdward Tomasz Napierala { 3971ecb3c58SEdward Tomasz Napierala 3981ecb3c58SEdward Tomasz Napierala /* 3991ecb3c58SEdward Tomasz Napierala * The only purpose of this handler is to make SIGCHLD 4001ecb3c58SEdward Tomasz Napierala * interrupt the ISCSIDWAIT ioctl(2), so we can call 4011ecb3c58SEdward Tomasz Napierala * wait_for_children(). 4021ecb3c58SEdward Tomasz Napierala */ 4031ecb3c58SEdward Tomasz Napierala } 4041ecb3c58SEdward Tomasz Napierala 4051ecb3c58SEdward Tomasz Napierala static void 4061ecb3c58SEdward Tomasz Napierala register_sigchld(void) 4071ecb3c58SEdward Tomasz Napierala { 4081ecb3c58SEdward Tomasz Napierala struct sigaction sa; 4091ecb3c58SEdward Tomasz Napierala int error; 4101ecb3c58SEdward Tomasz Napierala 4111ecb3c58SEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 4121ecb3c58SEdward Tomasz Napierala sa.sa_handler = sigchld_handler; 4131ecb3c58SEdward Tomasz Napierala sigfillset(&sa.sa_mask); 4141ecb3c58SEdward Tomasz Napierala error = sigaction(SIGCHLD, &sa, NULL); 4151ecb3c58SEdward Tomasz Napierala if (error != 0) 4161ecb3c58SEdward Tomasz Napierala log_err(1, "sigaction"); 4171ecb3c58SEdward Tomasz Napierala 4181ecb3c58SEdward Tomasz Napierala } 4191ecb3c58SEdward Tomasz Napierala 4201ecb3c58SEdward Tomasz Napierala static void 4217843bd03SEdward Tomasz Napierala handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout) 422009ea47eSEdward Tomasz Napierala { 423009ea47eSEdward Tomasz Napierala struct connection *conn; 424009ea47eSEdward Tomasz Napierala 425009ea47eSEdward Tomasz Napierala log_set_peer_addr(request->idr_conf.isc_target_addr); 426009ea47eSEdward Tomasz Napierala if (request->idr_conf.isc_target[0] != '\0') { 427009ea47eSEdward Tomasz Napierala log_set_peer_name(request->idr_conf.isc_target); 428009ea47eSEdward Tomasz Napierala setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target); 429009ea47eSEdward Tomasz Napierala } else { 430009ea47eSEdward Tomasz Napierala setproctitle("%s", request->idr_conf.isc_target_addr); 431009ea47eSEdward Tomasz Napierala } 432009ea47eSEdward Tomasz Napierala 433009ea47eSEdward Tomasz Napierala conn = connection_new(request->idr_session_id, &request->idr_conf, iscsi_fd); 434009ea47eSEdward Tomasz Napierala set_timeout(timeout); 435009ea47eSEdward Tomasz Napierala capsicate(conn); 436009ea47eSEdward Tomasz Napierala login(conn); 437009ea47eSEdward Tomasz Napierala if (conn->conn_conf.isc_discovery != 0) 438009ea47eSEdward Tomasz Napierala discovery(conn); 439009ea47eSEdward Tomasz Napierala else 440009ea47eSEdward Tomasz Napierala handoff(conn); 441009ea47eSEdward Tomasz Napierala 442009ea47eSEdward Tomasz Napierala log_debugx("nothing more to do; exiting"); 443009ea47eSEdward Tomasz Napierala exit (0); 444009ea47eSEdward Tomasz Napierala } 445009ea47eSEdward Tomasz Napierala 446009ea47eSEdward Tomasz Napierala static int 447009ea47eSEdward Tomasz Napierala wait_for_children(bool block) 448009ea47eSEdward Tomasz Napierala { 449009ea47eSEdward Tomasz Napierala pid_t pid; 450009ea47eSEdward Tomasz Napierala int status; 451009ea47eSEdward Tomasz Napierala int num = 0; 452009ea47eSEdward Tomasz Napierala 453009ea47eSEdward Tomasz Napierala for (;;) { 454009ea47eSEdward Tomasz Napierala /* 455009ea47eSEdward Tomasz Napierala * If "block" is true, wait for at least one process. 456009ea47eSEdward Tomasz Napierala */ 457009ea47eSEdward Tomasz Napierala if (block && num == 0) 458009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, 0, NULL); 459009ea47eSEdward Tomasz Napierala else 460009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, WNOHANG, NULL); 461009ea47eSEdward Tomasz Napierala if (pid <= 0) 462009ea47eSEdward Tomasz Napierala break; 463009ea47eSEdward Tomasz Napierala if (WIFSIGNALED(status)) { 464009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with signal %d", 465009ea47eSEdward Tomasz Napierala pid, WTERMSIG(status)); 466009ea47eSEdward Tomasz Napierala } else if (WEXITSTATUS(status) != 0) { 467009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with exit status %d", 468009ea47eSEdward Tomasz Napierala pid, WEXITSTATUS(status)); 469009ea47eSEdward Tomasz Napierala } else { 470009ea47eSEdward Tomasz Napierala log_debugx("child process %d terminated gracefully", pid); 471009ea47eSEdward Tomasz Napierala } 472009ea47eSEdward Tomasz Napierala num++; 473009ea47eSEdward Tomasz Napierala } 474009ea47eSEdward Tomasz Napierala 475009ea47eSEdward Tomasz Napierala return (num); 476009ea47eSEdward Tomasz Napierala } 477009ea47eSEdward Tomasz Napierala 478009ea47eSEdward Tomasz Napierala int 479009ea47eSEdward Tomasz Napierala main(int argc, char **argv) 480009ea47eSEdward Tomasz Napierala { 481009ea47eSEdward Tomasz Napierala int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, 482009ea47eSEdward Tomasz Napierala timeout = 60; 483009ea47eSEdward Tomasz Napierala bool dont_daemonize = false; 484009ea47eSEdward Tomasz Napierala struct pidfh *pidfh; 485009ea47eSEdward Tomasz Napierala pid_t pid, otherpid; 486009ea47eSEdward Tomasz Napierala const char *pidfile_path = DEFAULT_PIDFILE; 4877843bd03SEdward Tomasz Napierala struct iscsi_daemon_request request; 488009ea47eSEdward Tomasz Napierala 489009ea47eSEdward Tomasz Napierala while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { 490009ea47eSEdward Tomasz Napierala switch (ch) { 491009ea47eSEdward Tomasz Napierala case 'P': 492009ea47eSEdward Tomasz Napierala pidfile_path = optarg; 493009ea47eSEdward Tomasz Napierala break; 494009ea47eSEdward Tomasz Napierala case 'd': 495009ea47eSEdward Tomasz Napierala dont_daemonize = true; 496009ea47eSEdward Tomasz Napierala debug++; 497009ea47eSEdward Tomasz Napierala break; 498009ea47eSEdward Tomasz Napierala case 'l': 499009ea47eSEdward Tomasz Napierala debug = atoi(optarg); 500009ea47eSEdward Tomasz Napierala break; 501009ea47eSEdward Tomasz Napierala case 'm': 502009ea47eSEdward Tomasz Napierala maxproc = atoi(optarg); 503009ea47eSEdward Tomasz Napierala break; 504009ea47eSEdward Tomasz Napierala case 't': 505009ea47eSEdward Tomasz Napierala timeout = atoi(optarg); 506009ea47eSEdward Tomasz Napierala break; 507009ea47eSEdward Tomasz Napierala case '?': 508009ea47eSEdward Tomasz Napierala default: 509009ea47eSEdward Tomasz Napierala usage(); 510009ea47eSEdward Tomasz Napierala } 511009ea47eSEdward Tomasz Napierala } 512009ea47eSEdward Tomasz Napierala argc -= optind; 513009ea47eSEdward Tomasz Napierala if (argc != 0) 514009ea47eSEdward Tomasz Napierala usage(); 515009ea47eSEdward Tomasz Napierala 516009ea47eSEdward Tomasz Napierala log_init(debug); 517009ea47eSEdward Tomasz Napierala 518009ea47eSEdward Tomasz Napierala pidfh = pidfile_open(pidfile_path, 0600, &otherpid); 519009ea47eSEdward Tomasz Napierala if (pidfh == NULL) { 520009ea47eSEdward Tomasz Napierala if (errno == EEXIST) 521009ea47eSEdward Tomasz Napierala log_errx(1, "daemon already running, pid: %jd.", 522009ea47eSEdward Tomasz Napierala (intmax_t)otherpid); 523009ea47eSEdward Tomasz Napierala log_err(1, "cannot open or create pidfile \"%s\"", 524009ea47eSEdward Tomasz Napierala pidfile_path); 525009ea47eSEdward Tomasz Napierala } 526009ea47eSEdward Tomasz Napierala 527009ea47eSEdward Tomasz Napierala iscsi_fd = open(ISCSI_PATH, O_RDWR); 528c76e8a9aSEdward Tomasz Napierala if (iscsi_fd < 0 && errno == ENOENT) { 529009ea47eSEdward Tomasz Napierala saved_errno = errno; 530009ea47eSEdward Tomasz Napierala retval = kldload("iscsi"); 531009ea47eSEdward Tomasz Napierala if (retval != -1) 532009ea47eSEdward Tomasz Napierala iscsi_fd = open(ISCSI_PATH, O_RDWR); 533009ea47eSEdward Tomasz Napierala else 534009ea47eSEdward Tomasz Napierala errno = saved_errno; 535009ea47eSEdward Tomasz Napierala } 536009ea47eSEdward Tomasz Napierala if (iscsi_fd < 0) 537009ea47eSEdward Tomasz Napierala log_err(1, "failed to open %s", ISCSI_PATH); 538009ea47eSEdward Tomasz Napierala 539009ea47eSEdward Tomasz Napierala if (dont_daemonize == false) { 540009ea47eSEdward Tomasz Napierala if (daemon(0, 0) == -1) { 541009ea47eSEdward Tomasz Napierala log_warn("cannot daemonize"); 542009ea47eSEdward Tomasz Napierala pidfile_remove(pidfh); 543009ea47eSEdward Tomasz Napierala exit(1); 544009ea47eSEdward Tomasz Napierala } 545009ea47eSEdward Tomasz Napierala } 546009ea47eSEdward Tomasz Napierala 547009ea47eSEdward Tomasz Napierala pidfile_write(pidfh); 548009ea47eSEdward Tomasz Napierala 5491ecb3c58SEdward Tomasz Napierala register_sigchld(); 5501ecb3c58SEdward Tomasz Napierala 551009ea47eSEdward Tomasz Napierala for (;;) { 552009ea47eSEdward Tomasz Napierala log_debugx("waiting for request from the kernel"); 553009ea47eSEdward Tomasz Napierala 5547843bd03SEdward Tomasz Napierala memset(&request, 0, sizeof(request)); 5557843bd03SEdward Tomasz Napierala error = ioctl(iscsi_fd, ISCSIDWAIT, &request); 556009ea47eSEdward Tomasz Napierala if (error != 0) { 557009ea47eSEdward Tomasz Napierala if (errno == EINTR) { 558009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 559009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 560009ea47eSEdward Tomasz Napierala continue; 561009ea47eSEdward Tomasz Napierala } 562009ea47eSEdward Tomasz Napierala 563009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDWAIT"); 564009ea47eSEdward Tomasz Napierala } 565009ea47eSEdward Tomasz Napierala 566009ea47eSEdward Tomasz Napierala if (dont_daemonize) { 567009ea47eSEdward Tomasz Napierala log_debugx("not forking due to -d flag; " 568009ea47eSEdward Tomasz Napierala "will exit after servicing a single request"); 569009ea47eSEdward Tomasz Napierala } else { 570009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 571009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 572009ea47eSEdward Tomasz Napierala 573009ea47eSEdward Tomasz Napierala while (maxproc > 0 && nchildren >= maxproc) { 574009ea47eSEdward Tomasz Napierala log_debugx("maxproc limit of %d child processes hit; " 575009ea47eSEdward Tomasz Napierala "waiting for child process to exit", maxproc); 576009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(true); 577009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 578009ea47eSEdward Tomasz Napierala } 579009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; forking child process #%d", 580009ea47eSEdward Tomasz Napierala nchildren); 581009ea47eSEdward Tomasz Napierala nchildren++; 582009ea47eSEdward Tomasz Napierala 583009ea47eSEdward Tomasz Napierala pid = fork(); 584009ea47eSEdward Tomasz Napierala if (pid < 0) 585009ea47eSEdward Tomasz Napierala log_err(1, "fork"); 586009ea47eSEdward Tomasz Napierala if (pid > 0) 587009ea47eSEdward Tomasz Napierala continue; 588009ea47eSEdward Tomasz Napierala } 589009ea47eSEdward Tomasz Napierala 590009ea47eSEdward Tomasz Napierala pidfile_close(pidfh); 5917843bd03SEdward Tomasz Napierala handle_request(iscsi_fd, &request, timeout); 592009ea47eSEdward Tomasz Napierala } 593009ea47eSEdward Tomasz Napierala 594009ea47eSEdward Tomasz Napierala return (0); 595009ea47eSEdward Tomasz Napierala } 596