From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202512 header.b=jG1G4IzD; dkim-atps=neutral Received: from mail.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id CC17C5A0778 for ; Fri, 19 Dec 2025 15:19:16 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202512; t=1766153948; bh=/lW6EYq278dKKkAxvV6fI4OhAvmgO7IkoTNvfY5Ug3w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jG1G4IzDLmixS0S628LuMka6h/1lustfQNykLfvMI/1Q8cnDztnd8WN63wh3wZigv H6Glq+VTsUfSS3oti5P0GcPVmh3pxDx7QiYc31FPRfev6K0RT94M7Uo4elakBMA/r/ sGeVkbk3Z4Y3Ckzb65A4ofviSh+7va3QyAThjJpR2o+ShZq0sUkCrJID8/wiGKnnBi Dg2djAe0b/drat3ZDcyGwYQH+vFWB95M7z4pwlTganezYuMipeAVbI0RMHMdNrJame 1YoqtmLo6euOfUwqH1Ff5HH1yPhNuh1eahoTLQuoWGNu5pdL1Uo/HNIyJjSFH0Lb23 SfP6cdZsT7Bng== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4dXqR035kqz4wM4; Sat, 20 Dec 2025 01:19:08 +1100 (AEDT) From: David Gibson To: passt-dev@passt.top, Stefano Brivio Subject: [PATCH v2 08/12] ip: Add ipproto_name() function Date: Sat, 20 Dec 2025 01:19:00 +1100 Message-ID: <20251219141904.1758072-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251219141904.1758072-1-david@gibson.dropbear.id.au> References: <20251219141904.1758072-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 4OSREYFV7W3CSYP4I4672CPSB7IRTKNI X-Message-ID-Hash: 4OSREYFV7W3CSYP4I4672CPSB7IRTKNI 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: Add a function to get the name of an IP protocol from its number. Usually this would be done by getprotobynumber(), but that requires access to /etc/protocols and might allocate. We can't do either of those once we've self-isolated. Signed-off-by: David Gibson --- ip.c | 27 +++++++++++++++++++++++++++ ip.h | 2 ++ 2 files changed, 29 insertions(+) diff --git a/ip.c b/ip.c index 9a7f4c54..f1d224bd 100644 --- a/ip.c +++ b/ip.c @@ -67,3 +67,30 @@ found: *proto = nh; return true; } + +/** + * ipproto_name() - Get IP protocol name from number + * @proto: IP protocol number + * + * Return: pointer to name of protocol @proto + * + * Usually this would be done with getprotobynumber(3) but that reads + * /etc/protocols and might allocate, which isn't possible for us once + * self-isolated. + */ +/* cppcheck-suppress unusedFunction */ +const char *ipproto_name(uint8_t proto) +{ + switch (proto) { + case IPPROTO_ICMP: + return "ICMP"; + case IPPROTO_TCP: + return "TCP"; + case IPPROTO_UDP: + return "UDP"; + case IPPROTO_ICMPV6: + return "ICMPv6"; + default: + return ""; + } +} diff --git a/ip.h b/ip.h index 5830b923..42b417c5 100644 --- a/ip.h +++ b/ip.h @@ -116,6 +116,7 @@ static inline uint32_t ip6_get_flow_lbl(const struct ipv6hdr *ip6h) } bool ipv6_l4hdr(struct iov_tail *data, uint8_t *proto, size_t *dlen); +const char *ipproto_name(uint8_t proto); /* IPv6 link-local all-nodes multicast address, ff02::1 */ static const struct in6_addr in6addr_ll_all_nodes = { @@ -135,4 +136,5 @@ static const struct in_addr in4addr_broadcast = { 0xffffffff }; #define IPV6_MIN_MTU 1280 #endif + #endif /* IP_H */ -- 2.52.0