[PATCH v2 1/2] lwip: icmp: allow reporting ICMP destination unreachable

Jerome Forissier jerome.forissier at linaro.org
Tue Aug 12 14:43:18 CEST 2025


Allow reporting ICMP destination unreachable messages via a user-defined
callback.

Signed-off-by: Jerome Forissier <jerome.forissier at linaro.org>
---

Changes in v2:
- Fix typo in subject (s/detination/destination)

 lib/lwip/lwip/src/core/ipv4/icmp.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/lib/lwip/lwip/src/core/ipv4/icmp.c b/lib/lwip/lwip/src/core/ipv4/icmp.c
index 9a82a67aa93..6d588349c4a 100644
--- a/lib/lwip/lwip/src/core/ipv4/icmp.c
+++ b/lib/lwip/lwip/src/core/ipv4/icmp.c
@@ -80,9 +80,9 @@ void
 icmp_input(struct pbuf *p, struct netif *inp)
 {
   u8_t type;
-#ifdef LWIP_DEBUG
+#if defined(LWIP_DEBUG) || defined(ICMP_DEST_UNREACH_CB)
   u8_t code;
-#endif /* LWIP_DEBUG */
+#endif
   struct icmp_echo_hdr *iecho;
   const struct ip_hdr *iphdr_in;
   u16_t hlen;
@@ -103,11 +103,11 @@ icmp_input(struct pbuf *p, struct netif *inp)
   }
 
   type = *((u8_t *)p->payload);
-#ifdef LWIP_DEBUG
+#if defined(LWIP_DEBUG) || defined(ICMP_DEST_UNREACH_CB)
   code = *(((u8_t *)p->payload) + 1);
   /* if debug is enabled but debug statement below is somehow disabled: */
   LWIP_UNUSED_ARG(code);
-#endif /* LWIP_DEBUG */
+#endif
   switch (type) {
     case ICMP_ER:
       /* This is OK, echo reply might have been parsed by a raw PCB
@@ -257,6 +257,15 @@ icmp_input(struct pbuf *p, struct netif *inp)
     default:
       if (type == ICMP_DUR) {
         MIB2_STATS_INC(mib2.icmpindestunreachs);
+#ifdef ICMP_DEST_UNREACH_CB
+        /*
+         * The callback receives the IP packet (not the ICMP message) so that
+         * it can extract the source address for example
+         */
+        pbuf_add_header(p, IP_HLEN);
+        ICMP_DEST_UNREACH_CB(code, p);
+        pbuf_remove_header(p, IP_HLEN);
+#endif
       } else if (type == ICMP_TE) {
         MIB2_STATS_INC(mib2.icmpintimeexcds);
       } else if (type == ICMP_PP) {
-- 
2.43.0



More information about the U-Boot mailing list