From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 5B67C5A0279 for ; Thu, 21 Dec 2023 07:53:35 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202312; t=1703141610; bh=FoGBFg2jf9Xmxqu3cnI4QcdiR5GYk0chypvsmaVTmJQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=grXIGXiQF4T5CrzLXgL/U00uI+qpIVEr0Bbyf/J/BOMwVCW7zo4J9GUs8+Uc40mbr UJfu1mUubi6rBXCkEo7HcAQs6HOCdKvhgK9egz2n/pLBKu2z9W01k4la01f5y+wRAa hU3nmcVPfvkKqtWCWfVIw8iw0Eb6koAfHn3vvXL+QvSLQk4gkFxshgBuFoKeZtkl+/ 41nR4lXq+da9dRa5Q956zasFAyDExAgBZmqbmbtGLH4Q1+8t+bgh9WS+wJckTScq3/ jzPSnzy92ve/spZ/Y6qzbPuWEjr3onOUl3RCIQJw0HePtCYLihSuzpvTgIas5G2i6K VY3mG+ljJLaxg== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4Swh3G4vyjz4xS5; Thu, 21 Dec 2023 17:53:30 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 06/12] icmp: Use -1 to represent "missing" sockets Date: Thu, 21 Dec 2023 17:53:21 +1100 Message-ID: <20231221065327.1307827-7-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231221065327.1307827-1-david@gibson.dropbear.id.au> References: <20231221065327.1307827-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 37NIIGW4BUM6EH5DGTHQV5TKBLD3XBKT X-Message-ID-Hash: 37NIIGW4BUM6EH5DGTHQV5TKBLD3XBKT 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: icmp_id_map[] contains, amongst other things, fds for "ping" sockets associated with various ICMP echo ids. However, we only lazily open() those sockets, so many will be missing. We currently represent that with a 0, which isn't great, since that's technically a valid fd. Use -1 instead. This does require initializing the fields in icmp_id_map[] but we already have an obvious place to do that. Signed-off-by: David Gibson --- icmp.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/icmp.c b/icmp.c index 7a505b4..dd98c7f 100644 --- a/icmp.c +++ b/icmp.c @@ -179,7 +179,7 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, int af, iref.id = id = ntohs(ih->un.echo.id); - if ((s = icmp_id_map[V4][id].sock) <= 0) { + if ((s = icmp_id_map[V4][id].sock) < 0) { s = sock_l4(c, AF_INET, IPPROTO_ICMP, &c->ip4.addr_out, c->ip4.ifname_out, 0, iref.u32); if (s < 0) @@ -221,7 +221,7 @@ int icmp_tap_handler(const struct ctx *c, uint8_t pif, int af, return 1; iref.id = id = ntohs(ih->icmp6_identifier); - if ((s = icmp_id_map[V6][id].sock) <= 0) { + if ((s = icmp_id_map[V6][id].sock) < 0) { s = sock_l4(c, AF_INET6, IPPROTO_ICMPV6, &c->ip6.addr_out, c->ip6.ifname_out, 0, iref.u32); @@ -277,7 +277,7 @@ static void icmp_timer_one(const struct ctx *c, int v6, uint16_t id, epoll_ctl(c->epollfd, EPOLL_CTL_DEL, id_map->sock, NULL); close(id_map->sock); - id_map->sock = 0; + id_map->sock = -1; id_map->seq = -1; } @@ -315,6 +315,8 @@ void icmp_init(void) { unsigned i; - for (i = 0; i < ICMP_NUM_IDS; i++) + for (i = 0; i < ICMP_NUM_IDS; i++) { icmp_id_map[V4][i].seq = icmp_id_map[V6][i].seq = -1; + icmp_id_map[V4][i].sock = icmp_id_map[V6][i].sock = -1; + } } -- 2.43.0