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 */ 30009ea47eSEdward Tomasz Napierala 31b7a65e39SEdward Tomasz Napierala #include <sys/cdefs.h> 32b7a65e39SEdward Tomasz Napierala __FBSDID("$FreeBSD$"); 33b7a65e39SEdward Tomasz Napierala 34009ea47eSEdward Tomasz Napierala #include <sys/types.h> 35009ea47eSEdward Tomasz Napierala #include <sys/time.h> 36009ea47eSEdward Tomasz Napierala #include <sys/ioctl.h> 37009ea47eSEdward Tomasz Napierala #include <sys/param.h> 38009ea47eSEdward Tomasz Napierala #include <sys/linker.h> 39009ea47eSEdward Tomasz Napierala #include <sys/socket.h> 40b881b8beSRobert Watson #include <sys/capsicum.h> 41009ea47eSEdward Tomasz Napierala #include <sys/wait.h> 42009ea47eSEdward Tomasz Napierala #include <assert.h> 43009ea47eSEdward Tomasz Napierala #include <errno.h> 44009ea47eSEdward Tomasz Napierala #include <fcntl.h> 45009ea47eSEdward Tomasz Napierala #include <netdb.h> 46009ea47eSEdward Tomasz Napierala #include <signal.h> 47009ea47eSEdward Tomasz Napierala #include <stdbool.h> 48009ea47eSEdward Tomasz Napierala #include <stdint.h> 49009ea47eSEdward Tomasz Napierala #include <stdio.h> 50009ea47eSEdward Tomasz Napierala #include <stdlib.h> 51009ea47eSEdward Tomasz Napierala #include <string.h> 52009ea47eSEdward Tomasz Napierala #include <unistd.h> 53009ea47eSEdward Tomasz Napierala 54009ea47eSEdward Tomasz Napierala #include <libutil.h> 55009ea47eSEdward Tomasz Napierala 56009ea47eSEdward Tomasz Napierala #include "iscsid.h" 57009ea47eSEdward Tomasz Napierala 58009ea47eSEdward Tomasz Napierala static volatile bool sigalrm_received = false; 59009ea47eSEdward Tomasz Napierala 60009ea47eSEdward Tomasz Napierala static int nchildren = 0; 61009ea47eSEdward Tomasz Napierala 62009ea47eSEdward Tomasz Napierala static void 63009ea47eSEdward Tomasz Napierala usage(void) 64009ea47eSEdward Tomasz Napierala { 65009ea47eSEdward Tomasz Napierala 66009ea47eSEdward Tomasz Napierala fprintf(stderr, "usage: iscsid [-P pidfile][-d][-m maxproc][-t timeout]\n"); 67009ea47eSEdward Tomasz Napierala exit(1); 68009ea47eSEdward Tomasz Napierala } 69009ea47eSEdward Tomasz Napierala 70009ea47eSEdward Tomasz Napierala char * 71009ea47eSEdward Tomasz Napierala checked_strdup(const char *s) 72009ea47eSEdward Tomasz Napierala { 73009ea47eSEdward Tomasz Napierala char *c; 74009ea47eSEdward Tomasz Napierala 75009ea47eSEdward Tomasz Napierala c = strdup(s); 76009ea47eSEdward Tomasz Napierala if (c == NULL) 77009ea47eSEdward Tomasz Napierala log_err(1, "strdup"); 78009ea47eSEdward Tomasz Napierala return (c); 79009ea47eSEdward Tomasz Napierala } 80009ea47eSEdward Tomasz Napierala 811f90f282SEdward Tomasz Napierala static void 821f90f282SEdward Tomasz Napierala resolve_addr(const struct connection *conn, const char *address, 831f90f282SEdward Tomasz Napierala struct addrinfo **ai, bool initiator_side) 84009ea47eSEdward Tomasz Napierala { 85009ea47eSEdward Tomasz Napierala struct addrinfo hints; 86009ea47eSEdward Tomasz Napierala char *arg, *addr, *ch; 87009ea47eSEdward Tomasz Napierala const char *port; 88009ea47eSEdward Tomasz Napierala int error, colons = 0; 89009ea47eSEdward Tomasz Napierala 90009ea47eSEdward Tomasz Napierala arg = checked_strdup(address); 91009ea47eSEdward Tomasz Napierala 92009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 931f90f282SEdward Tomasz Napierala fail(conn, "empty address"); 941f90f282SEdward Tomasz Napierala log_errx(1, "empty address"); 95009ea47eSEdward Tomasz Napierala } 96009ea47eSEdward Tomasz Napierala if (arg[0] == '[') { 97009ea47eSEdward Tomasz Napierala /* 98009ea47eSEdward Tomasz Napierala * IPv6 address in square brackets, perhaps with port. 99009ea47eSEdward Tomasz Napierala */ 100009ea47eSEdward Tomasz Napierala arg++; 101009ea47eSEdward Tomasz Napierala addr = strsep(&arg, "]"); 102009ea47eSEdward Tomasz Napierala if (arg == NULL) { 1031f90f282SEdward Tomasz Napierala fail(conn, "malformed address"); 1041f90f282SEdward Tomasz Napierala log_errx(1, "malformed address %s", address); 105009ea47eSEdward Tomasz Napierala } 106009ea47eSEdward Tomasz Napierala if (arg[0] == '\0') { 1071f90f282SEdward Tomasz Napierala port = NULL; 108009ea47eSEdward Tomasz Napierala } else if (arg[0] == ':') { 109009ea47eSEdward Tomasz Napierala port = arg + 1; 110009ea47eSEdward Tomasz Napierala } else { 1111f90f282SEdward Tomasz Napierala fail(conn, "malformed address"); 1121f90f282SEdward Tomasz Napierala log_errx(1, "malformed address %s", address); 113009ea47eSEdward Tomasz Napierala } 114009ea47eSEdward Tomasz Napierala } else { 115009ea47eSEdward Tomasz Napierala /* 116009ea47eSEdward Tomasz Napierala * Either IPv6 address without brackets - and without 117009ea47eSEdward Tomasz Napierala * a port - or IPv4 address. Just count the colons. 118009ea47eSEdward Tomasz Napierala */ 119009ea47eSEdward Tomasz Napierala for (ch = arg; *ch != '\0'; ch++) { 120009ea47eSEdward Tomasz Napierala if (*ch == ':') 121009ea47eSEdward Tomasz Napierala colons++; 122009ea47eSEdward Tomasz Napierala } 123009ea47eSEdward Tomasz Napierala if (colons > 1) { 124009ea47eSEdward Tomasz Napierala addr = arg; 1251f90f282SEdward Tomasz Napierala port = NULL; 126009ea47eSEdward Tomasz Napierala } else { 127009ea47eSEdward Tomasz Napierala addr = strsep(&arg, ":"); 128009ea47eSEdward Tomasz Napierala if (arg == NULL) 1291f90f282SEdward Tomasz Napierala port = NULL; 130009ea47eSEdward Tomasz Napierala else 131009ea47eSEdward Tomasz Napierala port = arg; 132009ea47eSEdward Tomasz Napierala } 133009ea47eSEdward Tomasz Napierala } 134009ea47eSEdward Tomasz Napierala 1351f90f282SEdward Tomasz Napierala if (port == NULL && !initiator_side) 1361f90f282SEdward Tomasz Napierala port = "3260"; 1371f90f282SEdward Tomasz Napierala 138009ea47eSEdward Tomasz Napierala memset(&hints, 0, sizeof(hints)); 139009ea47eSEdward Tomasz Napierala hints.ai_family = PF_UNSPEC; 140009ea47eSEdward Tomasz Napierala hints.ai_socktype = SOCK_STREAM; 1411f90f282SEdward Tomasz Napierala hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; 1421f90f282SEdward Tomasz Napierala if (initiator_side) 1431f90f282SEdward Tomasz Napierala hints.ai_flags |= AI_PASSIVE; 144009ea47eSEdward Tomasz Napierala 145009ea47eSEdward Tomasz Napierala error = getaddrinfo(addr, port, &hints, ai); 146009ea47eSEdward Tomasz Napierala if (error != 0) { 1471f90f282SEdward Tomasz Napierala fail(conn, gai_strerror(error)); 1481f90f282SEdward Tomasz Napierala log_errx(1, "getaddrinfo for %s failed: %s", 149009ea47eSEdward Tomasz Napierala address, gai_strerror(error)); 150009ea47eSEdward Tomasz Napierala } 151009ea47eSEdward Tomasz Napierala } 152009ea47eSEdward Tomasz Napierala 153009ea47eSEdward Tomasz Napierala static struct connection * 154*83f37561SEdward Tomasz Napierala connection_new(int iscsi_fd, const struct iscsi_daemon_request *request) 155009ea47eSEdward Tomasz Napierala { 156009ea47eSEdward Tomasz Napierala struct connection *conn; 157009ea47eSEdward Tomasz Napierala struct addrinfo *from_ai, *to_ai; 158009ea47eSEdward Tomasz Napierala const char *from_addr, *to_addr; 159009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1607843bd03SEdward Tomasz Napierala struct iscsi_daemon_connect idc; 161009ea47eSEdward Tomasz Napierala #endif 1628b94b583SAlexander Motin int error, sockbuf; 163009ea47eSEdward Tomasz Napierala 164009ea47eSEdward Tomasz Napierala conn = calloc(1, sizeof(*conn)); 165009ea47eSEdward Tomasz Napierala if (conn == NULL) 166009ea47eSEdward Tomasz Napierala log_err(1, "calloc"); 167009ea47eSEdward Tomasz Napierala 168009ea47eSEdward Tomasz Napierala /* 169009ea47eSEdward Tomasz Napierala * Default values, from RFC 3720, section 12. 170009ea47eSEdward Tomasz Napierala */ 171009ea47eSEdward Tomasz Napierala conn->conn_header_digest = CONN_DIGEST_NONE; 172009ea47eSEdward Tomasz Napierala conn->conn_data_digest = CONN_DIGEST_NONE; 173009ea47eSEdward Tomasz Napierala conn->conn_initial_r2t = true; 174009ea47eSEdward Tomasz Napierala conn->conn_immediate_data = true; 175009ea47eSEdward Tomasz Napierala conn->conn_max_data_segment_length = 8192; 176009ea47eSEdward Tomasz Napierala conn->conn_max_burst_length = 262144; 177009ea47eSEdward Tomasz Napierala conn->conn_first_burst_length = 65536; 1781f90f282SEdward Tomasz Napierala conn->conn_iscsi_fd = iscsi_fd; 1791f90f282SEdward Tomasz Napierala 180*83f37561SEdward Tomasz Napierala conn->conn_session_id = request->idr_session_id; 181*83f37561SEdward Tomasz Napierala memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf)); 182*83f37561SEdward Tomasz Napierala memcpy(&conn->conn_isid, &request->idr_isid, sizeof(conn->conn_isid)); 183*83f37561SEdward Tomasz Napierala conn->conn_tsih = request->idr_tsih; 184*83f37561SEdward Tomasz Napierala memcpy(&conn->conn_limits, &request->idr_limits, sizeof(conn->conn_limits)); 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 } 2368b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 2378b94b583SAlexander Motin if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF, 2388b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 2398b94b583SAlexander Motin log_warn("setsockopt(SO_RCVBUF) failed"); 2408b94b583SAlexander Motin sockbuf = SOCKBUF_SIZE; 2418b94b583SAlexander Motin if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF, 2428b94b583SAlexander Motin &sockbuf, sizeof(sockbuf)) == -1) 2438b94b583SAlexander Motin log_warn("setsockopt(SO_SNDBUF) failed"); 244009ea47eSEdward Tomasz Napierala if (from_ai != NULL) { 245009ea47eSEdward Tomasz Napierala error = bind(conn->conn_socket, from_ai->ai_addr, 246009ea47eSEdward Tomasz Napierala from_ai->ai_addrlen); 2471f90f282SEdward Tomasz Napierala if (error != 0) { 2481f90f282SEdward Tomasz Napierala fail(conn, strerror(errno)); 249009ea47eSEdward Tomasz Napierala log_err(1, "failed to bind to %s", from_addr); 250009ea47eSEdward Tomasz Napierala } 2511f90f282SEdward Tomasz Napierala } 252009ea47eSEdward Tomasz Napierala log_debugx("connecting to %s", to_addr); 253009ea47eSEdward Tomasz Napierala error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); 254009ea47eSEdward Tomasz Napierala if (error != 0) { 255009ea47eSEdward Tomasz Napierala fail(conn, strerror(errno)); 256009ea47eSEdward Tomasz Napierala log_err(1, "failed to connect to %s", to_addr); 257009ea47eSEdward Tomasz Napierala } 258009ea47eSEdward Tomasz Napierala 259009ea47eSEdward Tomasz Napierala return (conn); 260009ea47eSEdward Tomasz Napierala } 261009ea47eSEdward Tomasz Napierala 262009ea47eSEdward Tomasz Napierala static void 263009ea47eSEdward Tomasz Napierala handoff(struct connection *conn) 264009ea47eSEdward Tomasz Napierala { 2657843bd03SEdward Tomasz Napierala struct iscsi_daemon_handoff idh; 266009ea47eSEdward Tomasz Napierala int error; 267009ea47eSEdward Tomasz Napierala 268009ea47eSEdward Tomasz Napierala log_debugx("handing off connection to the kernel"); 269009ea47eSEdward Tomasz Napierala 2707843bd03SEdward Tomasz Napierala memset(&idh, 0, sizeof(idh)); 2717843bd03SEdward Tomasz Napierala idh.idh_session_id = conn->conn_session_id; 2727843bd03SEdward Tomasz Napierala idh.idh_socket = conn->conn_socket; 2737843bd03SEdward Tomasz Napierala strlcpy(idh.idh_target_alias, conn->conn_target_alias, 2747843bd03SEdward Tomasz Napierala sizeof(idh.idh_target_alias)); 275ffe82e05SAlexander Motin idh.idh_tsih = conn->conn_tsih; 2767843bd03SEdward Tomasz Napierala idh.idh_statsn = conn->conn_statsn; 2777843bd03SEdward Tomasz Napierala idh.idh_header_digest = conn->conn_header_digest; 2787843bd03SEdward Tomasz Napierala idh.idh_data_digest = conn->conn_data_digest; 2797843bd03SEdward Tomasz Napierala idh.idh_initial_r2t = conn->conn_initial_r2t; 2807843bd03SEdward Tomasz Napierala idh.idh_immediate_data = conn->conn_immediate_data; 2817843bd03SEdward Tomasz Napierala idh.idh_max_data_segment_length = conn->conn_max_data_segment_length; 2827843bd03SEdward Tomasz Napierala idh.idh_max_burst_length = conn->conn_max_burst_length; 2837843bd03SEdward Tomasz Napierala idh.idh_first_burst_length = conn->conn_first_burst_length; 284009ea47eSEdward Tomasz Napierala 2857843bd03SEdward Tomasz Napierala error = ioctl(conn->conn_iscsi_fd, ISCSIDHANDOFF, &idh); 286009ea47eSEdward Tomasz Napierala if (error != 0) 287009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDHANDOFF"); 288009ea47eSEdward Tomasz Napierala } 289009ea47eSEdward Tomasz Napierala 290009ea47eSEdward Tomasz Napierala void 291009ea47eSEdward Tomasz Napierala fail(const struct connection *conn, const char *reason) 292009ea47eSEdward Tomasz Napierala { 2937843bd03SEdward Tomasz Napierala struct iscsi_daemon_fail idf; 294009ea47eSEdward Tomasz Napierala int error; 295009ea47eSEdward Tomasz Napierala 2967843bd03SEdward Tomasz Napierala memset(&idf, 0, sizeof(idf)); 2977843bd03SEdward Tomasz Napierala idf.idf_session_id = conn->conn_session_id; 2987843bd03SEdward Tomasz Napierala strlcpy(idf.idf_reason, reason, sizeof(idf.idf_reason)); 299009ea47eSEdward Tomasz Napierala 3007843bd03SEdward Tomasz Napierala error = ioctl(conn->conn_iscsi_fd, ISCSIDFAIL, &idf); 301009ea47eSEdward Tomasz Napierala if (error != 0) 302009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDFAIL"); 303009ea47eSEdward Tomasz Napierala } 304009ea47eSEdward Tomasz Napierala 305009ea47eSEdward Tomasz Napierala /* 306009ea47eSEdward Tomasz Napierala * XXX: I CANT INTO LATIN 307009ea47eSEdward Tomasz Napierala */ 308009ea47eSEdward Tomasz Napierala static void 309009ea47eSEdward Tomasz Napierala capsicate(struct connection *conn) 310009ea47eSEdward Tomasz Napierala { 311009ea47eSEdward Tomasz Napierala int error; 312009ea47eSEdward Tomasz Napierala cap_rights_t rights; 313009ea47eSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 314009ea47eSEdward Tomasz Napierala const unsigned long cmds[] = { ISCSIDCONNECT, ISCSIDSEND, ISCSIDRECEIVE, 31551be90b5SEdward Tomasz Napierala ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, ISCSISREMOVE, ISCSISMODIFY }; 316009ea47eSEdward Tomasz Napierala #else 317009ea47eSEdward Tomasz Napierala const unsigned long cmds[] = { ISCSIDHANDOFF, ISCSIDFAIL, ISCSISADD, 31851be90b5SEdward Tomasz Napierala ISCSISREMOVE, ISCSISMODIFY }; 319009ea47eSEdward Tomasz Napierala #endif 320009ea47eSEdward Tomasz Napierala 321009ea47eSEdward Tomasz Napierala cap_rights_init(&rights, CAP_IOCTL); 322009ea47eSEdward Tomasz Napierala error = cap_rights_limit(conn->conn_iscsi_fd, &rights); 323009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 324009ea47eSEdward Tomasz Napierala log_err(1, "cap_rights_limit"); 325009ea47eSEdward Tomasz Napierala 326009ea47eSEdward Tomasz Napierala error = cap_ioctls_limit(conn->conn_iscsi_fd, cmds, 327009ea47eSEdward Tomasz Napierala sizeof(cmds) / sizeof(cmds[0])); 328009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 329009ea47eSEdward Tomasz Napierala log_err(1, "cap_ioctls_limit"); 330009ea47eSEdward Tomasz Napierala 331009ea47eSEdward Tomasz Napierala error = cap_enter(); 332009ea47eSEdward Tomasz Napierala if (error != 0 && errno != ENOSYS) 333009ea47eSEdward Tomasz Napierala log_err(1, "cap_enter"); 334009ea47eSEdward Tomasz Napierala 335009ea47eSEdward Tomasz Napierala if (cap_sandboxed()) 336009ea47eSEdward Tomasz Napierala log_debugx("Capsicum capability mode enabled"); 337009ea47eSEdward Tomasz Napierala else 338009ea47eSEdward Tomasz Napierala log_warnx("Capsicum capability mode not supported"); 339009ea47eSEdward Tomasz Napierala } 340009ea47eSEdward Tomasz Napierala 341009ea47eSEdward Tomasz Napierala bool 342009ea47eSEdward Tomasz Napierala timed_out(void) 343009ea47eSEdward Tomasz Napierala { 344009ea47eSEdward Tomasz Napierala 345009ea47eSEdward Tomasz Napierala return (sigalrm_received); 346009ea47eSEdward Tomasz Napierala } 347009ea47eSEdward Tomasz Napierala 348009ea47eSEdward Tomasz Napierala static void 349009ea47eSEdward Tomasz Napierala sigalrm_handler(int dummy __unused) 350009ea47eSEdward Tomasz Napierala { 351009ea47eSEdward Tomasz Napierala /* 352009ea47eSEdward Tomasz Napierala * It would be easiest to just log an error and exit. We can't 353009ea47eSEdward Tomasz Napierala * do this, though, because log_errx() is not signal safe, since 354009ea47eSEdward Tomasz Napierala * it calls syslog(3). Instead, set a flag checked by pdu_send() 355009ea47eSEdward Tomasz Napierala * and pdu_receive(), to call log_errx() there. Should they fail 356009ea47eSEdward Tomasz Napierala * to notice, we'll exit here one second later. 357009ea47eSEdward Tomasz Napierala */ 358009ea47eSEdward Tomasz Napierala if (sigalrm_received) { 359009ea47eSEdward Tomasz Napierala /* 360009ea47eSEdward Tomasz Napierala * Oh well. Just give up and quit. 361009ea47eSEdward Tomasz Napierala */ 362009ea47eSEdward Tomasz Napierala _exit(2); 363009ea47eSEdward Tomasz Napierala } 364009ea47eSEdward Tomasz Napierala 365009ea47eSEdward Tomasz Napierala sigalrm_received = true; 366009ea47eSEdward Tomasz Napierala } 367009ea47eSEdward Tomasz Napierala 368009ea47eSEdward Tomasz Napierala static void 369009ea47eSEdward Tomasz Napierala set_timeout(int timeout) 370009ea47eSEdward Tomasz Napierala { 371009ea47eSEdward Tomasz Napierala struct sigaction sa; 372009ea47eSEdward Tomasz Napierala struct itimerval itv; 373009ea47eSEdward Tomasz Napierala int error; 374009ea47eSEdward Tomasz Napierala 375009ea47eSEdward Tomasz Napierala if (timeout <= 0) { 376009ea47eSEdward Tomasz Napierala log_debugx("session timeout disabled"); 377009ea47eSEdward Tomasz Napierala return; 378009ea47eSEdward Tomasz Napierala } 379009ea47eSEdward Tomasz Napierala 380009ea47eSEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 381009ea47eSEdward Tomasz Napierala sa.sa_handler = sigalrm_handler; 382009ea47eSEdward Tomasz Napierala sigfillset(&sa.sa_mask); 383009ea47eSEdward Tomasz Napierala error = sigaction(SIGALRM, &sa, NULL); 384009ea47eSEdward Tomasz Napierala if (error != 0) 385009ea47eSEdward Tomasz Napierala log_err(1, "sigaction"); 386009ea47eSEdward Tomasz Napierala 387009ea47eSEdward Tomasz Napierala /* 388009ea47eSEdward Tomasz Napierala * First SIGALRM will arive after conf_timeout seconds. 389009ea47eSEdward Tomasz Napierala * If we do nothing, another one will arrive a second later. 390009ea47eSEdward Tomasz Napierala */ 391009ea47eSEdward Tomasz Napierala bzero(&itv, sizeof(itv)); 392009ea47eSEdward Tomasz Napierala itv.it_interval.tv_sec = 1; 393009ea47eSEdward Tomasz Napierala itv.it_value.tv_sec = timeout; 394009ea47eSEdward Tomasz Napierala 395009ea47eSEdward Tomasz Napierala log_debugx("setting session timeout to %d seconds", 396009ea47eSEdward Tomasz Napierala timeout); 397009ea47eSEdward Tomasz Napierala error = setitimer(ITIMER_REAL, &itv, NULL); 398009ea47eSEdward Tomasz Napierala if (error != 0) 399009ea47eSEdward Tomasz Napierala log_err(1, "setitimer"); 400009ea47eSEdward Tomasz Napierala } 401009ea47eSEdward Tomasz Napierala 402009ea47eSEdward Tomasz Napierala static void 4031ecb3c58SEdward Tomasz Napierala sigchld_handler(int dummy __unused) 4041ecb3c58SEdward Tomasz Napierala { 4051ecb3c58SEdward Tomasz Napierala 4061ecb3c58SEdward Tomasz Napierala /* 4071ecb3c58SEdward Tomasz Napierala * The only purpose of this handler is to make SIGCHLD 4081ecb3c58SEdward Tomasz Napierala * interrupt the ISCSIDWAIT ioctl(2), so we can call 4091ecb3c58SEdward Tomasz Napierala * wait_for_children(). 4101ecb3c58SEdward Tomasz Napierala */ 4111ecb3c58SEdward Tomasz Napierala } 4121ecb3c58SEdward Tomasz Napierala 4131ecb3c58SEdward Tomasz Napierala static void 4141ecb3c58SEdward Tomasz Napierala register_sigchld(void) 4151ecb3c58SEdward Tomasz Napierala { 4161ecb3c58SEdward Tomasz Napierala struct sigaction sa; 4171ecb3c58SEdward Tomasz Napierala int error; 4181ecb3c58SEdward Tomasz Napierala 4191ecb3c58SEdward Tomasz Napierala bzero(&sa, sizeof(sa)); 4201ecb3c58SEdward Tomasz Napierala sa.sa_handler = sigchld_handler; 4211ecb3c58SEdward Tomasz Napierala sigfillset(&sa.sa_mask); 4221ecb3c58SEdward Tomasz Napierala error = sigaction(SIGCHLD, &sa, NULL); 4231ecb3c58SEdward Tomasz Napierala if (error != 0) 4241ecb3c58SEdward Tomasz Napierala log_err(1, "sigaction"); 4251ecb3c58SEdward Tomasz Napierala 4261ecb3c58SEdward Tomasz Napierala } 4271ecb3c58SEdward Tomasz Napierala 4281ecb3c58SEdward Tomasz Napierala static void 4297843bd03SEdward Tomasz Napierala handle_request(int iscsi_fd, const struct iscsi_daemon_request *request, int timeout) 430009ea47eSEdward Tomasz Napierala { 431009ea47eSEdward Tomasz Napierala struct connection *conn; 432009ea47eSEdward Tomasz Napierala 433009ea47eSEdward Tomasz Napierala log_set_peer_addr(request->idr_conf.isc_target_addr); 434009ea47eSEdward Tomasz Napierala if (request->idr_conf.isc_target[0] != '\0') { 435009ea47eSEdward Tomasz Napierala log_set_peer_name(request->idr_conf.isc_target); 436009ea47eSEdward Tomasz Napierala setproctitle("%s (%s)", request->idr_conf.isc_target_addr, request->idr_conf.isc_target); 437009ea47eSEdward Tomasz Napierala } else { 438009ea47eSEdward Tomasz Napierala setproctitle("%s", request->idr_conf.isc_target_addr); 439009ea47eSEdward Tomasz Napierala } 440009ea47eSEdward Tomasz Napierala 441*83f37561SEdward Tomasz Napierala conn = connection_new(iscsi_fd, request); 442009ea47eSEdward Tomasz Napierala set_timeout(timeout); 443009ea47eSEdward Tomasz Napierala capsicate(conn); 444009ea47eSEdward Tomasz Napierala login(conn); 445009ea47eSEdward Tomasz Napierala if (conn->conn_conf.isc_discovery != 0) 446009ea47eSEdward Tomasz Napierala discovery(conn); 447009ea47eSEdward Tomasz Napierala else 448009ea47eSEdward Tomasz Napierala handoff(conn); 449009ea47eSEdward Tomasz Napierala 450009ea47eSEdward Tomasz Napierala log_debugx("nothing more to do; exiting"); 451009ea47eSEdward Tomasz Napierala exit (0); 452009ea47eSEdward Tomasz Napierala } 453009ea47eSEdward Tomasz Napierala 454009ea47eSEdward Tomasz Napierala static int 455009ea47eSEdward Tomasz Napierala wait_for_children(bool block) 456009ea47eSEdward Tomasz Napierala { 457009ea47eSEdward Tomasz Napierala pid_t pid; 458009ea47eSEdward Tomasz Napierala int status; 459009ea47eSEdward Tomasz Napierala int num = 0; 460009ea47eSEdward Tomasz Napierala 461009ea47eSEdward Tomasz Napierala for (;;) { 462009ea47eSEdward Tomasz Napierala /* 463009ea47eSEdward Tomasz Napierala * If "block" is true, wait for at least one process. 464009ea47eSEdward Tomasz Napierala */ 465009ea47eSEdward Tomasz Napierala if (block && num == 0) 466009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, 0, NULL); 467009ea47eSEdward Tomasz Napierala else 468009ea47eSEdward Tomasz Napierala pid = wait4(-1, &status, WNOHANG, NULL); 469009ea47eSEdward Tomasz Napierala if (pid <= 0) 470009ea47eSEdward Tomasz Napierala break; 471009ea47eSEdward Tomasz Napierala if (WIFSIGNALED(status)) { 472009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with signal %d", 473009ea47eSEdward Tomasz Napierala pid, WTERMSIG(status)); 474009ea47eSEdward Tomasz Napierala } else if (WEXITSTATUS(status) != 0) { 475009ea47eSEdward Tomasz Napierala log_warnx("child process %d terminated with exit status %d", 476009ea47eSEdward Tomasz Napierala pid, WEXITSTATUS(status)); 477009ea47eSEdward Tomasz Napierala } else { 478009ea47eSEdward Tomasz Napierala log_debugx("child process %d terminated gracefully", pid); 479009ea47eSEdward Tomasz Napierala } 480009ea47eSEdward Tomasz Napierala num++; 481009ea47eSEdward Tomasz Napierala } 482009ea47eSEdward Tomasz Napierala 483009ea47eSEdward Tomasz Napierala return (num); 484009ea47eSEdward Tomasz Napierala } 485009ea47eSEdward Tomasz Napierala 486009ea47eSEdward Tomasz Napierala int 487009ea47eSEdward Tomasz Napierala main(int argc, char **argv) 488009ea47eSEdward Tomasz Napierala { 489009ea47eSEdward Tomasz Napierala int ch, debug = 0, error, iscsi_fd, maxproc = 30, retval, saved_errno, 490009ea47eSEdward Tomasz Napierala timeout = 60; 491009ea47eSEdward Tomasz Napierala bool dont_daemonize = false; 492009ea47eSEdward Tomasz Napierala struct pidfh *pidfh; 493009ea47eSEdward Tomasz Napierala pid_t pid, otherpid; 494009ea47eSEdward Tomasz Napierala const char *pidfile_path = DEFAULT_PIDFILE; 4957843bd03SEdward Tomasz Napierala struct iscsi_daemon_request request; 496009ea47eSEdward Tomasz Napierala 497009ea47eSEdward Tomasz Napierala while ((ch = getopt(argc, argv, "P:dl:m:t:")) != -1) { 498009ea47eSEdward Tomasz Napierala switch (ch) { 499009ea47eSEdward Tomasz Napierala case 'P': 500009ea47eSEdward Tomasz Napierala pidfile_path = optarg; 501009ea47eSEdward Tomasz Napierala break; 502009ea47eSEdward Tomasz Napierala case 'd': 503009ea47eSEdward Tomasz Napierala dont_daemonize = true; 504009ea47eSEdward Tomasz Napierala debug++; 505009ea47eSEdward Tomasz Napierala break; 506009ea47eSEdward Tomasz Napierala case 'l': 507009ea47eSEdward Tomasz Napierala debug = atoi(optarg); 508009ea47eSEdward Tomasz Napierala break; 509009ea47eSEdward Tomasz Napierala case 'm': 510009ea47eSEdward Tomasz Napierala maxproc = atoi(optarg); 511009ea47eSEdward Tomasz Napierala break; 512009ea47eSEdward Tomasz Napierala case 't': 513009ea47eSEdward Tomasz Napierala timeout = atoi(optarg); 514009ea47eSEdward Tomasz Napierala break; 515009ea47eSEdward Tomasz Napierala case '?': 516009ea47eSEdward Tomasz Napierala default: 517009ea47eSEdward Tomasz Napierala usage(); 518009ea47eSEdward Tomasz Napierala } 519009ea47eSEdward Tomasz Napierala } 520009ea47eSEdward Tomasz Napierala argc -= optind; 521009ea47eSEdward Tomasz Napierala if (argc != 0) 522009ea47eSEdward Tomasz Napierala usage(); 523009ea47eSEdward Tomasz Napierala 524009ea47eSEdward Tomasz Napierala log_init(debug); 525009ea47eSEdward Tomasz Napierala 526009ea47eSEdward Tomasz Napierala pidfh = pidfile_open(pidfile_path, 0600, &otherpid); 527009ea47eSEdward Tomasz Napierala if (pidfh == NULL) { 528009ea47eSEdward Tomasz Napierala if (errno == EEXIST) 529009ea47eSEdward Tomasz Napierala log_errx(1, "daemon already running, pid: %jd.", 530009ea47eSEdward Tomasz Napierala (intmax_t)otherpid); 531009ea47eSEdward Tomasz Napierala log_err(1, "cannot open or create pidfile \"%s\"", 532009ea47eSEdward Tomasz Napierala pidfile_path); 533009ea47eSEdward Tomasz Napierala } 534009ea47eSEdward Tomasz Napierala 535009ea47eSEdward Tomasz Napierala iscsi_fd = open(ISCSI_PATH, O_RDWR); 536c76e8a9aSEdward Tomasz Napierala if (iscsi_fd < 0 && errno == ENOENT) { 537009ea47eSEdward Tomasz Napierala saved_errno = errno; 538009ea47eSEdward Tomasz Napierala retval = kldload("iscsi"); 539009ea47eSEdward Tomasz Napierala if (retval != -1) 540009ea47eSEdward Tomasz Napierala iscsi_fd = open(ISCSI_PATH, O_RDWR); 541009ea47eSEdward Tomasz Napierala else 542009ea47eSEdward Tomasz Napierala errno = saved_errno; 543009ea47eSEdward Tomasz Napierala } 544009ea47eSEdward Tomasz Napierala if (iscsi_fd < 0) 545009ea47eSEdward Tomasz Napierala log_err(1, "failed to open %s", ISCSI_PATH); 546009ea47eSEdward Tomasz Napierala 547009ea47eSEdward Tomasz Napierala if (dont_daemonize == false) { 548009ea47eSEdward Tomasz Napierala if (daemon(0, 0) == -1) { 549009ea47eSEdward Tomasz Napierala log_warn("cannot daemonize"); 550009ea47eSEdward Tomasz Napierala pidfile_remove(pidfh); 551009ea47eSEdward Tomasz Napierala exit(1); 552009ea47eSEdward Tomasz Napierala } 553009ea47eSEdward Tomasz Napierala } 554009ea47eSEdward Tomasz Napierala 555009ea47eSEdward Tomasz Napierala pidfile_write(pidfh); 556009ea47eSEdward Tomasz Napierala 5571ecb3c58SEdward Tomasz Napierala register_sigchld(); 5581ecb3c58SEdward Tomasz Napierala 559009ea47eSEdward Tomasz Napierala for (;;) { 560009ea47eSEdward Tomasz Napierala log_debugx("waiting for request from the kernel"); 561009ea47eSEdward Tomasz Napierala 5627843bd03SEdward Tomasz Napierala memset(&request, 0, sizeof(request)); 5637843bd03SEdward Tomasz Napierala error = ioctl(iscsi_fd, ISCSIDWAIT, &request); 564009ea47eSEdward Tomasz Napierala if (error != 0) { 565009ea47eSEdward Tomasz Napierala if (errno == EINTR) { 566009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 567009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 568009ea47eSEdward Tomasz Napierala continue; 569009ea47eSEdward Tomasz Napierala } 570009ea47eSEdward Tomasz Napierala 571009ea47eSEdward Tomasz Napierala log_err(1, "ISCSIDWAIT"); 572009ea47eSEdward Tomasz Napierala } 573009ea47eSEdward Tomasz Napierala 574009ea47eSEdward Tomasz Napierala if (dont_daemonize) { 575009ea47eSEdward Tomasz Napierala log_debugx("not forking due to -d flag; " 576009ea47eSEdward Tomasz Napierala "will exit after servicing a single request"); 577009ea47eSEdward Tomasz Napierala } else { 578009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(false); 579009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 580009ea47eSEdward Tomasz Napierala 581009ea47eSEdward Tomasz Napierala while (maxproc > 0 && nchildren >= maxproc) { 582009ea47eSEdward Tomasz Napierala log_debugx("maxproc limit of %d child processes hit; " 583009ea47eSEdward Tomasz Napierala "waiting for child process to exit", maxproc); 584009ea47eSEdward Tomasz Napierala nchildren -= wait_for_children(true); 585009ea47eSEdward Tomasz Napierala assert(nchildren >= 0); 586009ea47eSEdward Tomasz Napierala } 587009ea47eSEdward Tomasz Napierala log_debugx("incoming connection; forking child process #%d", 588009ea47eSEdward Tomasz Napierala nchildren); 589009ea47eSEdward Tomasz Napierala nchildren++; 590009ea47eSEdward Tomasz Napierala 591009ea47eSEdward Tomasz Napierala pid = fork(); 592009ea47eSEdward Tomasz Napierala if (pid < 0) 593009ea47eSEdward Tomasz Napierala log_err(1, "fork"); 594009ea47eSEdward Tomasz Napierala if (pid > 0) 595009ea47eSEdward Tomasz Napierala continue; 596009ea47eSEdward Tomasz Napierala } 597009ea47eSEdward Tomasz Napierala 598009ea47eSEdward Tomasz Napierala pidfile_close(pidfh); 5997843bd03SEdward Tomasz Napierala handle_request(iscsi_fd, &request, timeout); 600009ea47eSEdward Tomasz Napierala } 601009ea47eSEdward Tomasz Napierala 602009ea47eSEdward Tomasz Napierala return (0); 603009ea47eSEdward Tomasz Napierala } 604