Blame SOURCES/0013-tcp-udp-util-Pass-socket-creation-errors-all-the-way.patch

f07426
From 812d24c91aecff78369f32f6593045f24d578d38 Mon Sep 17 00:00:00 2001
f07426
From: Stefano Brivio <sbrivio@redhat.com>
f07426
Date: Wed, 8 Mar 2023 12:14:29 +0100
f07426
Subject: [PATCH 13/20] tcp, udp, util: Pass socket creation errors all the way
f07426
 up
f07426
f07426
...starting from sock_l4(), pass negative error (errno) codes instead
f07426
of -1. They will only be used in two commits from now, no functional
f07426
changes intended here.
f07426
f07426
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
f07426
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
f07426
(cherry picked from commit 73992c42cea0df56f6ba0a3bef0f4a939f26ebad)
f07426
---
f07426
 tcp.c  | 22 ++++++++++++----------
f07426
 udp.c  | 18 +++++++++---------
f07426
 util.c | 31 ++++++++++++++++++-------------
f07426
 3 files changed, 39 insertions(+), 32 deletions(-)
f07426
f07426
diff --git a/tcp.c b/tcp.c
f07426
index fe6e458..482c2f9 100644
f07426
--- a/tcp.c
f07426
+++ b/tcp.c
f07426
@@ -2891,7 +2891,7 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events,
f07426
  * @addr:	Pointer to address for binding, NULL if not configured
f07426
  * @ifname:	Name of interface to bind to, NULL if not configured
f07426
  *
f07426
- * Return: fd for the new listening socket, or -1 on failure
f07426
+ * Return: fd for the new listening socket, negative error code on failure
f07426
  */
f07426
 static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port,
f07426
 			    const struct in_addr *addr, const char *ifname)
f07426
@@ -2904,13 +2904,13 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port,
f07426
 
f07426
 	if (c->tcp.fwd_in.mode == FWD_AUTO) {
f07426
 		if (af == AF_INET  || af == AF_UNSPEC)
f07426
-			tcp_sock_init_ext[port][V4] = s;
f07426
+			tcp_sock_init_ext[port][V4] = s < 0 ? -1 : s;
f07426
 		if (af == AF_INET6 || af == AF_UNSPEC)
f07426
-			tcp_sock_init_ext[port][V6] = s;
f07426
+			tcp_sock_init_ext[port][V6] = s < 0 ? -1 : s;
f07426
 	}
f07426
 
f07426
 	if (s < 0)
f07426
-		return -1;
f07426
+		return s;
f07426
 
f07426
 	tcp_sock_set_bufsize(c, s);
f07426
 	return s;
f07426
@@ -2924,12 +2924,12 @@ static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port,
f07426
  * @ifname:	Name of interface to bind to, NULL if not configured
f07426
  * @port:	Port, host order
f07426
  *
f07426
- * Return: 0 on (partial) success, -1 on (complete) failure
f07426
+ * Return: 0 on (partial) success, negative error code on (complete) failure
f07426
  */
f07426
 int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
f07426
 		  const char *ifname, in_port_t port)
f07426
 {
f07426
-	int ret = 0;
f07426
+	int ret = 0, af_ret;
f07426
 
f07426
 	if (af == AF_UNSPEC && c->ifi4 && c->ifi6)
f07426
 		/* Attempt to get a dual stack socket */
f07426
@@ -2938,13 +2938,15 @@ int tcp_sock_init(const struct ctx *c, sa_family_t af, const void *addr,
f07426
 
f07426
 	/* Otherwise create a socket per IP version */
f07426
 	if ((af == AF_INET  || af == AF_UNSPEC) && c->ifi4) {
f07426
-		if (tcp_sock_init_af(c, AF_INET, port, addr, ifname) < 0)
f07426
-			ret = -1;
f07426
+		af_ret = tcp_sock_init_af(c, AF_INET, port, addr, ifname);
f07426
+		if (af_ret < 0)
f07426
+			ret = af_ret;
f07426
 	}
f07426
 
f07426
 	if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) {
f07426
-		if (tcp_sock_init_af(c, AF_INET6, port, addr, ifname) < 0)
f07426
-			ret = -1;
f07426
+		af_ret = tcp_sock_init_af(c, AF_INET6, port, addr, ifname);
f07426
+		if (af_ret < 0)
f07426
+			ret = af_ret;
f07426
 	}
f07426
 
f07426
 	return ret;
f07426
diff --git a/udp.c b/udp.c
f07426
index 20a9ea0..9a43835 100644
f07426
--- a/udp.c
f07426
+++ b/udp.c
f07426
@@ -956,7 +956,7 @@ int udp_tap_handler(struct ctx *c, int af, const void *addr,
f07426
  * @ifname:	Name of interface to bind to, NULL if not configured
f07426
  * @port:	Port, host order
f07426
  *
f07426
- * Return: 0 on (partial) success, -1 on (complete) failure
f07426
+ * Return: 0 on (partial) success, negative error code on (complete) failure
f07426
  */
f07426
 int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
f07426
 		  const void *addr, const char *ifname, in_port_t port)
f07426
@@ -981,19 +981,19 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
f07426
 			s = sock_l4(c, AF_INET, IPPROTO_UDP, addr, ifname,
f07426
 				    port, uref.u32);
f07426
 
f07426
-			udp_tap_map[V4][uref.udp.port].sock = s;
f07426
-			udp_splice_init[V4][port].sock = s;
f07426
+			udp_tap_map[V4][uref.udp.port].sock = s < 0 ? -1 : s;
f07426
+			udp_splice_init[V4][port].sock = s < 0 ? -1 : s;
f07426
 		} else {
f07426
 			struct in_addr loopback = { htonl(INADDR_LOOPBACK) };
f07426
 			uref.udp.ns = true;
f07426
 
f07426
 			s = sock_l4(c, AF_INET, IPPROTO_UDP, &loopback,
f07426
 				    ifname, port, uref.u32);
f07426
-			udp_splice_ns[V4][port].sock = s;
f07426
+			udp_splice_ns[V4][port].sock = s < 0 ? -1 : s;
f07426
 		}
f07426
 
f07426
 		if (s < 0)
f07426
-			ret = -1;
f07426
+			ret = s;
f07426
 	}
f07426
 
f07426
 	if ((af == AF_INET6 || af == AF_UNSPEC) && c->ifi6) {
f07426
@@ -1005,18 +1005,18 @@ int udp_sock_init(const struct ctx *c, int ns, sa_family_t af,
f07426
 			s = sock_l4(c, AF_INET6, IPPROTO_UDP, addr, ifname,
f07426
 				    port, uref.u32);
f07426
 
f07426
-			udp_tap_map[V6][uref.udp.port].sock = s;
f07426
-			udp_splice_init[V6][port].sock = s;
f07426
+			udp_tap_map[V6][uref.udp.port].sock = s < 0 ? -1 : s;
f07426
+			udp_splice_init[V6][port].sock = s < 0 ? -1 : s;
f07426
 		} else {
f07426
 			uref.udp.ns = true;
f07426
 
f07426
 			s = sock_l4(c, AF_INET6, IPPROTO_UDP, &in6addr_loopback,
f07426
 				    ifname, port, uref.u32);
f07426
-			udp_splice_ns[V6][port].sock = s;
f07426
+			udp_splice_ns[V6][port].sock = s < 0 ? -1 : s;
f07426
 		}
f07426
 
f07426
 		if (s < 0)
f07426
-			ret = -1;
f07426
+			ret = s;
f07426
 	}
f07426
 
f07426
 	return ret;
f07426
diff --git a/util.c b/util.c
f07426
index c5ee1c0..13f8fab 100644
f07426
--- a/util.c
f07426
+++ b/util.c
f07426
@@ -95,7 +95,7 @@ found:
f07426
  * @port:	Port, host order
f07426
  * @data:	epoll reference portion for protocol handlers
f07426
  *
f07426
- * Return: newly created socket, -1 on error
f07426
+ * Return: newly created socket, negative error code on failure
f07426
  */
f07426
 int sock_l4(const struct ctx *c, int af, uint8_t proto,
f07426
 	    const void *bind_addr, const char *ifname, uint16_t port,
f07426
@@ -114,16 +114,16 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
f07426
 	};
f07426
 	const struct sockaddr *sa;
f07426
 	bool dual_stack = false;
f07426
+	int fd, sl, y = 1, ret;
f07426
 	struct epoll_event ev;
f07426
-	int fd, sl, y = 1;
f07426
 
f07426
 	if (proto != IPPROTO_TCP && proto != IPPROTO_UDP &&
f07426
 	    proto != IPPROTO_ICMP && proto != IPPROTO_ICMPV6)
f07426
-		return -1;	/* Not implemented. */
f07426
+		return -EPFNOSUPPORT;	/* Not implemented. */
f07426
 
f07426
 	if (af == AF_UNSPEC) {
f07426
 		if (!DUAL_STACK_SOCKETS || bind_addr)
f07426
-			return -1;
f07426
+			return -EINVAL;
f07426
 		dual_stack = true;
f07426
 		af = AF_INET6;
f07426
 	}
f07426
@@ -133,14 +133,15 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
f07426
 	else
f07426
 		fd = socket(af, SOCK_DGRAM | SOCK_NONBLOCK, proto);
f07426
 
f07426
+	ret = -errno;
f07426
 	if (fd < 0) {
f07426
-		warn("L4 socket: %s", strerror(errno));
f07426
-		return -1;
f07426
+		warn("L4 socket: %s", strerror(-ret));
f07426
+		return ret;
f07426
 	}
f07426
 
f07426
 	if (fd > SOCKET_MAX) {
f07426
 		close(fd);
f07426
-		return -1;
f07426
+		return -EBADF;
f07426
 	}
f07426
 
f07426
 	ref.r.s = fd;
f07426
@@ -185,10 +186,11 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
f07426
 		 */
f07426
 		if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
f07426
 			       ifname, strlen(ifname))) {
f07426
+			ret = -errno;
f07426
 			warn("Can't bind socket for %s port %u to %s, closing",
f07426
 			     ip_proto_str[proto], port, ifname);
f07426
 			close(fd);
f07426
-			return -1;
f07426
+			return ret;
f07426
 		}
f07426
 	}
f07426
 
f07426
@@ -199,22 +201,25 @@ int sock_l4(const struct ctx *c, int af, uint8_t proto,
f07426
 		 * broken SELinux policy, see icmp_tap_handler().
f07426
 		 */
f07426
 		if (proto != IPPROTO_ICMP && proto != IPPROTO_ICMPV6) {
f07426
+			ret = -errno;
f07426
 			close(fd);
f07426
-			return -1;
f07426
+			return ret;
f07426
 		}
f07426
 	}
f07426
 
f07426
 	if (proto == IPPROTO_TCP && listen(fd, 128) < 0) {
f07426
-		warn("TCP socket listen: %s", strerror(errno));
f07426
+		ret = -errno;
f07426
+		warn("TCP socket listen: %s", strerror(-ret));
f07426
 		close(fd);
f07426
-		return -1;
f07426
+		return ret;
f07426
 	}
f07426
 
f07426
 	ev.events = EPOLLIN;
f07426
 	ev.data.u64 = ref.u64;
f07426
 	if (epoll_ctl(c->epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
f07426
-		warn("L4 epoll_ctl: %s", strerror(errno));
f07426
-		return -1;
f07426
+		ret = -errno;
f07426
+		warn("L4 epoll_ctl: %s", strerror(-ret));
f07426
+		return ret;
f07426
 	}
f07426
 
f07426
 	return fd;
f07426
-- 
f07426
2.39.2
f07426