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 "<unknown protocol>";
+ }
+}
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