From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 81EF05A0272 for ; Wed, 2 Aug 2023 05:15:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1690946145; bh=AcLdXfcMVW0ORg1kGISILjGI5SaP6LUmkju1kquc6A4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PyZ/aS4VTRoxTm/F1M0b0+U7MI54dAWWoPTtmOPM/ajA6rFSEAeKi6WqWTCmJpqb/ GlMeU/K10csbptSDdwkoenVSzf6SBaL8+Vik1S46PZP5hu2d41qeJ/k4axwh5x/41c 4rXRJ3MF87eUNC5BS8OXzI9m3UmRO3G19P5iXRGI= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RFxv53bTnz4yLN; Wed, 2 Aug 2023 13:15:45 +1000 (AEST) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH 2/3] tap: More detailed error reporting in tap_ns_tun() Date: Wed, 2 Aug 2023 13:15:41 +1000 Message-ID: <20230802031542.2726758-3-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230802031542.2726758-1-david@gibson.dropbear.id.au> References: <20230802031542.2726758-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: NT3ELJ7OR346F4J2DZ3OETECQY6GQ3IF X-Message-ID-Hash: NT3ELJ7OR346F4J2DZ3OETECQY6GQ3IF X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Paul Holzinger , David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: There are several possible failure points in tap_ns_tun(), but if anything goes wrong, we just set tun_ns_fd to -1 resulting in the same error message. Add more detailed error reporting to the various failure points. At the same time, we know this is only called from tap_sock_tun_init() which will terminate pasta if we fail, so we can simplify things a little because we don't need to close() the fd on the failure paths. Link: https://bugs.passt.top/show_bug.cgi?id=69 Link: https://github.com/containers/podman/issues/19428 Signed-off-by: David Gibson --- tap.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/tap.c b/tap.c index 0f90cab..a5d357a 100644 --- a/tap.c +++ b/tap.c @@ -1171,7 +1171,7 @@ static int tun_ns_fd = -1; * tap_ns_tun() - Get tuntap fd in namespace * @c: Execution context * - * Return: 0 + * Return: 0 on success, exits on failure * * #syscalls:pasta ioctl openat */ @@ -1180,17 +1180,24 @@ static int tap_ns_tun(void *arg) struct ifreq ifr = { .ifr_flags = IFF_TAP | IFF_NO_PI }; int flags = O_RDWR | O_NONBLOCK | O_CLOEXEC; struct ctx *c = (struct ctx *)arg; + int fd, rc; + tun_ns_fd = -1; memcpy(ifr.ifr_name, c->pasta_ifn, IFNAMSIZ); ns_enter(c); - if ((tun_ns_fd = open("/dev/net/tun", flags)) < 0 || - ioctl(tun_ns_fd, TUNSETIFF, &ifr) || - !(c->pasta_ifi = if_nametoindex(c->pasta_ifn))) { - if (tun_ns_fd != -1) - close(tun_ns_fd); - tun_ns_fd = -1; - } + fd = open("/dev/net/tun", flags); + if (fd < 0) + die("Failed to open() /dev/net/tun: %s", strerror(errno)); + + rc = ioctl(fd, TUNSETIFF, &ifr); + if (rc < 0) + die("TUNSETIFF failed: %s", strerror(errno)); + + if (!(c->pasta_ifi = if_nametoindex(c->pasta_ifn))) + die("Tap device opened but no network interface found"); + + tun_ns_fd = fd; return 0; } @@ -1205,7 +1212,7 @@ static void tap_sock_tun_init(struct ctx *c) NS_CALL(tap_ns_tun, c); if (tun_ns_fd == -1) - die("Failed to open tun socket in namespace"); + die("Failed to set up tap device in namespace"); pasta_ns_conf(c); -- 2.41.0