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 A7DF25A0272 for ; Mon, 24 Jul 2023 08:09:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1690178979; bh=qpbJ5eekYUZt0tkrr/rv8w31hr4/TT+w4T3o5PLxXfc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eMIc5OU3fyQrcPci7LaTxYusaURDk4O+6BfCBrZYD6ljL3spm69jVwuAjGGu5AxW6 IAz5GmuQeAOsTT+BT/z2ebPLKOcBzbJrhkQWYUNVL5k56FGyrfbSCIrf7cS607e3c/ 2389QyM8ovwdfQs4+yFrCKtLOIaBkAvoH3OAbnPM= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4R8V9v4rKFz4wym; Mon, 24 Jul 2023 16:09:39 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 08/17] netlink: Treat send() or recv() errors as fatal Date: Mon, 24 Jul 2023 16:09:27 +1000 Message-ID: <20230724060936.952659-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230724060936.952659-1-david@gibson.dropbear.id.au> References: <20230724060936.952659-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 2MNH3KMXDYCKIVHO2VYCDJCTU4ZYCRMG X-Message-ID-Hash: 2MNH3KMXDYCKIVHO2VYCDJCTU4ZYCRMG 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: 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: Errors on send() or recv() calls on a netlink socket don't indicate errors with the netlink operations we're attempting, but rather that something's gone wrong with the mechanics of netlink itself. We don't really expect this to ever happen, and if it does, it's not clear what we could to to recover. So, treat errors from these calls as fatal, rather than returning the error up the stack. This makes handling failures in the callers of nl_req() simpler. Signed-off-by: David Gibson --- netlink.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/netlink.c b/netlink.c index 3620fd6..826c926 100644 --- a/netlink.c +++ b/netlink.c @@ -103,9 +103,9 @@ fail: * @req: Request with netlink header * @len: Request length * - * Return: received length on success, negative error code on failure + * Return: received length on success, terminates on error */ -static int nl_req(int s, char *buf, const void *req, ssize_t len) +static ssize_t nl_req(int s, char *buf, const void *req, ssize_t len) { char flush[NLBUFSIZ]; int done = 0; @@ -124,11 +124,17 @@ static int nl_req(int s, char *buf, const void *req, ssize_t len) } } - if ((send(s, req, len, 0) < len) || - (len = recv(s, buf, NLBUFSIZ, 0)) < 0) - return -errno; + n = send(s, req, len, 0); + if (n < 0) + die("netlink: Failed to send(): %s", strerror(errno)); + else if (n < len) + die("netlink: Short send"); + + n = recv(s, buf, NLBUFSIZ, 0); + if (n < 0) + die("netlink: Failed to recv(): %s", strerror(errno)); - return len; + return n; } /** @@ -158,8 +164,7 @@ unsigned int nl_get_ext_if(int s, sa_family_t af) ssize_t n; size_t na; - if ((n = nl_req(s, buf, &req, sizeof(req))) < 0) - return 0; + n = nl_req(s, buf, &req, sizeof(req)); nh = (struct nlmsghdr *)buf; @@ -218,8 +223,7 @@ void nl_route_get_def(int s, unsigned int ifi, sa_family_t af, void *gw) char buf[NLBUFSIZ]; ssize_t n; - if ((n = nl_req(s, buf, &req, req.nlh.nlmsg_len)) < 0) - return; + n = nl_req(s, buf, &req, req.nlh.nlmsg_len); for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE; @@ -357,8 +361,7 @@ void nl_route_dup(int s_src, unsigned int ifi_src, char buf[NLBUFSIZ]; unsigned i; - if ((nlmsgs_size = nl_req(s_src, buf, &req, req.nlh.nlmsg_len)) < 0) - return; + nlmsgs_size = nl_req(s_src, buf, &req, req.nlh.nlmsg_len); for (nh = (struct nlmsghdr *)buf, n = nlmsgs_size; NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE; @@ -434,8 +437,7 @@ void nl_addr_get(int s, unsigned int ifi, sa_family_t af, char buf[NLBUFSIZ]; ssize_t n; - if ((n = nl_req(s, buf, &req, req.nlh.nlmsg_len)) < 0) - return; + n = nl_req(s, buf, &req, req.nlh.nlmsg_len); for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE; @@ -571,8 +573,7 @@ void nl_addr_dup(int s_src, unsigned int ifi_src, struct nlmsghdr *nh; ssize_t n; - if ((n = nl_req(s_src, buf, &req, sizeof(req))) < 0) - return; + n = nl_req(s_src, buf, &req, sizeof(req)); for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE; @@ -632,9 +633,6 @@ void nl_link_get_mac(int s, unsigned int ifi, void *mac) ssize_t n; n = nl_req(s, buf, &req, sizeof(req)); - if (n < 0) - return; - for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, n) && nh->nlmsg_type != NLMSG_DONE; nh = NLMSG_NEXT(nh, n)) { -- 2.41.0