From owner-svn-src-all@FreeBSD.ORG Tue Sep 17 14:23:16 2013 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 3B500DBA; Tue, 17 Sep 2013 14:23:16 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 278F928A2; Tue, 17 Sep 2013 14:23:16 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r8HENGvU078303; Tue, 17 Sep 2013 14:23:16 GMT (envelope-from trasz@svn.freebsd.org) Received: (from trasz@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r8HENGNp078302; Tue, 17 Sep 2013 14:23:16 GMT (envelope-from trasz@svn.freebsd.org) Message-Id: <201309171423.r8HENGNp078302@svn.freebsd.org> From: Edward Tomasz Napierala Date: Tue, 17 Sep 2013 14:23:15 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r255636 - head/usr.sbin/iscsid X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 17 Sep 2013 14:23:16 -0000 Author: trasz Date: Tue Sep 17 14:23:15 2013 New Revision: 255636 URL: http://svnweb.freebsd.org/changeset/base/255636 Log: Improve iSCSI address resolution, fixing "InitiatorAddress" handling, and error reporting. Approved by: re (kib) Modified: head/usr.sbin/iscsid/iscsid.c Modified: head/usr.sbin/iscsid/iscsid.c ============================================================================== --- head/usr.sbin/iscsid/iscsid.c Tue Sep 17 14:19:05 2013 (r255635) +++ head/usr.sbin/iscsid/iscsid.c Tue Sep 17 14:23:15 2013 (r255636) @@ -76,8 +76,9 @@ checked_strdup(const char *s) return (c); } -static int -resolve_addr(const char *address, struct addrinfo **ai) +static void +resolve_addr(const struct connection *conn, const char *address, + struct addrinfo **ai, bool initiator_side) { struct addrinfo hints; char *arg, *addr, *ch; @@ -87,8 +88,8 @@ resolve_addr(const char *address, struct arg = checked_strdup(address); if (arg[0] == '\0') { - log_warnx("empty address"); - return (1); + fail(conn, "empty address"); + log_errx(1, "empty address"); } if (arg[0] == '[') { /* @@ -97,16 +98,16 @@ resolve_addr(const char *address, struct arg++; addr = strsep(&arg, "]"); if (arg == NULL) { - log_warnx("invalid address %s", address); - return (1); + fail(conn, "malformed address"); + log_errx(1, "malformed address %s", address); } if (arg[0] == '\0') { - port = "3260"; + port = NULL; } else if (arg[0] == ':') { port = arg + 1; } else { - log_warnx("invalid address %s", address); - return (1); + fail(conn, "malformed address"); + log_errx(1, "malformed address %s", address); } } else { /* @@ -119,29 +120,32 @@ resolve_addr(const char *address, struct } if (colons > 1) { addr = arg; - port = "3260"; + port = NULL; } else { addr = strsep(&arg, ":"); if (arg == NULL) - port = "3260"; + port = NULL; else port = arg; } } + if (port == NULL && !initiator_side) + port = "3260"; + memset(&hints, 0, sizeof(hints)); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - hints.ai_flags = AI_PASSIVE; + hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV; + if (initiator_side) + hints.ai_flags |= AI_PASSIVE; error = getaddrinfo(addr, port, &hints, ai); if (error != 0) { - log_warnx("getaddrinfo for %s failed: %s", + fail(conn, gai_strerror(error)); + log_errx(1, "getaddrinfo for %s failed: %s", address, gai_strerror(error)); - return (1); } - - return (0); } static struct connection * @@ -172,6 +176,8 @@ connection_new(unsigned int session_id, conn->conn_first_burst_length = 65536; conn->conn_session_id = session_id; + conn->conn_iscsi_fd = iscsi_fd; + /* * XXX: Should we sanitize this somehow? */ @@ -180,20 +186,12 @@ connection_new(unsigned int session_id, from_addr = conn->conn_conf.isc_initiator_addr; to_addr = conn->conn_conf.isc_target_addr; - if (from_addr[0] != '\0') { - error = resolve_addr(from_addr, &from_ai); - if (error != 0) - log_errx(1, "failed to resolve initiator address %s", - from_addr); - } else { + if (from_addr[0] != '\0') + resolve_addr(conn, from_addr, &from_ai, true); + else from_ai = NULL; - } - error = resolve_addr(to_addr, &to_ai); - if (error != 0) - log_errx(1, "failed to resolve target address %s", to_addr); - - conn->conn_iscsi_fd = iscsi_fd; + resolve_addr(conn, to_addr, &to_ai, false); #ifdef ICL_KERNEL_PROXY @@ -224,19 +222,25 @@ connection_new(unsigned int session_id, #else /* !ICL_KERNEL_PROXY */ - if (conn->conn_conf.isc_iser) + if (conn->conn_conf.isc_iser) { + fail(conn, "iSER not supported"); log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY " "does not support iSER"); + } conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype, to_ai->ai_protocol); - if (conn->conn_socket < 0) + if (conn->conn_socket < 0) { + fail(conn, strerror(errno)); log_err(1, "failed to create socket for %s", from_addr); + } if (from_ai != NULL) { error = bind(conn->conn_socket, from_ai->ai_addr, from_ai->ai_addrlen); - if (error != 0) + if (error != 0) { + fail(conn, strerror(errno)); log_err(1, "failed to bind to %s", from_addr); + } } log_debugx("connecting to %s", to_addr); error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen);