Skip to content

Commit 6b914d5

Browse files
committed
net: lwip: add Kconfig option to show ICMP unreachable errors
Add Kconfig symbol LWIP_ICMP_SHOW_UNREACH which, when enabled, prints a message to the console upon reception of ICMP unreachable messages. For example: $ make qemu_arm64_lwip_defconfig $ qemu-system-aarch64 -M virt -cpu max -nographic -bios u-boot.bin [...] => dhcp DHCP client bound to address 10.0.2.15 (0 ms) => tftp 192.168.0.100:69:Image Using virtio-net#32 device TFTP from server 192.168.0.100; our IP address is 10.0.2.15 Filename 'Image'. Load address: 0x40200000 Loading: ICMP destination unreachable (host unreachable) from 192.168.0.16 Timeout! => tftp 192.168.0.16:69:Image Using virtio-net#32 device TFTP from server 192.168.0.16; our IP address is 10.0.2.15 Filename 'Image'. Load address: 0x40200000 Loading: ICMP destination unreachable (port unreachable) from 192.168.0.16 Timeout! => Submitted upstream as lwip-tcpip/lwip#73. Signed-off-by: Jerome Forissier <[email protected]>
1 parent fbd79b4 commit 6b914d5

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

lib/lwip/u-boot/arch/cc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ static inline const char *sntp_format_time(time_t t)
5656
}
5757

5858
#define sntp_format_time sntp_format_time
59+
60+
#ifdef CONFIG_LWIP_ICMP_SHOW_UNREACH
61+
struct pbuf;
62+
void net_lwip_icmp_dest_unreach(int code, struct pbuf *p);
63+
64+
#define ICMP_DEST_UNREACH_CB(_c, _p) net_lwip_icmp_dest_unreach(_c, _p)
65+
#endif
5966
#endif /* LWIP_ARCH_CC_H */

lib/lwip/u-boot/lwipopts.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@
8080

8181
#define IP_DEFAULT_TTL 255
8282

83+
#if defined(CONFIG_PROT_ICMP_LWIP)
84+
#define LWIP_ICMP 1
85+
#else
8386
#define LWIP_ICMP 0
87+
#endif
8488

8589
#if defined(CONFIG_PROT_RAW_LWIP)
8690
#define LWIP_RAW 1

net/lwip/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44

55
if NET_LWIP
66

7+
config LWIP_ICMP_SHOW_UNREACH
8+
bool "Print ICMP Destination Unreachable messages"
9+
default y
10+
depends on CMD_TFTPBOOT || CMD_SNTP
11+
select PROT_ICMP_LWIP
12+
help
13+
Prints a message whenever an ICMP Destination Unreachable message is
14+
received while running a network command that sends requests via UDP.
15+
Enabling this can make troubleshooting easier.
16+
717
config LWIP_DEBUG
818
bool "Enable debug traces in the lwIP library"
919
help
@@ -31,6 +41,9 @@ config PROT_DNS_LWIP
3141
bool
3242
select PROT_UDP_LWIP
3343

44+
config PROT_ICMP_LWIP
45+
bool
46+
3447
config PROT_RAW_LWIP
3548
bool
3649

net/lwip/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ ccflags-y += -I$(srctree)/lib/lwip/lwip/src/include -I$(srctree)/lib/lwip/u-boot
22

33
obj-$(CONFIG_$(PHASE_)DM_ETH) += net-lwip.o
44
obj-$(CONFIG_CMD_DHCP) += dhcp.o
5+
obj-$(CONFIG_LWIP_ICMP_SHOW_UNREACH) += icmp_unreach.o
56
obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o
67
obj-$(CONFIG_WGET) += wget.o
78

net/lwip/icmp_unreach.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
/* Copyright (C) 2025 Linaro Ltd. */
3+
4+
#include <lwip/icmp.h>
5+
#include <lwip/ip4_addr.h>
6+
#include <lwip/pbuf.h>
7+
#include <lwip/prot/ip4.h>
8+
9+
static const char *code_to_str(int code)
10+
{
11+
switch (code) {
12+
case ICMP_DUR_NET:
13+
return "network unreachable";
14+
case ICMP_DUR_HOST:
15+
return "host unreachable";
16+
case ICMP_DUR_PROTO:
17+
return "protocol unreachable";
18+
case ICMP_DUR_PORT:
19+
return "port unreachable";
20+
case ICMP_DUR_FRAG:
21+
return "fragmentation needed and DF set";
22+
case ICMP_DUR_SR:
23+
return "source route failed";
24+
default:
25+
break;
26+
}
27+
return "unknown cause";
28+
}
29+
30+
void net_lwip_icmp_dest_unreach(int code, struct pbuf *p)
31+
{
32+
struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
33+
ip4_addr_t src;
34+
35+
ip4_addr_copy(src, iphdr->src);
36+
printf("ICMP destination unreachable (%s) from %s\n",
37+
code_to_str(code), ip4addr_ntoa(&src));
38+
}

0 commit comments

Comments
 (0)