summaryrefslogtreecommitdiff
path: root/net/lwip
diff options
context:
space:
mode:
Diffstat (limited to 'net/lwip')
-rw-r--r--net/lwip/Kconfig13
-rw-r--r--net/lwip/Makefile2
-rw-r--r--net/lwip/dns.c113
-rw-r--r--net/lwip/icmp_unreach.c38
-rw-r--r--net/lwip/net-lwip.c8
5 files changed, 170 insertions, 4 deletions
diff --git a/net/lwip/Kconfig b/net/lwip/Kconfig
index d28a8a7df94..5789766fe62 100644
--- a/net/lwip/Kconfig
+++ b/net/lwip/Kconfig
@@ -4,6 +4,16 @@
if NET_LWIP
+config LWIP_ICMP_SHOW_UNREACH
+ bool "Print ICMP Destination Unreachable messages"
+ default y
+ depends on CMD_TFTPBOOT || CMD_SNTP
+ select PROT_ICMP_LWIP
+ help
+ Prints a message whenever an ICMP Destination Unreachable message is
+ received while running a network command that sends requests via UDP.
+ Enabling this can make troubleshooting easier.
+
config LWIP_DEBUG
bool "Enable debug traces in the lwIP library"
help
@@ -31,6 +41,9 @@ config PROT_DNS_LWIP
bool
select PROT_UDP_LWIP
+config PROT_ICMP_LWIP
+ bool
+
config PROT_RAW_LWIP
bool
diff --git a/net/lwip/Makefile b/net/lwip/Makefile
index 97299d9b542..1b48ae4d508 100644
--- a/net/lwip/Makefile
+++ b/net/lwip/Makefile
@@ -2,6 +2,8 @@ ccflags-y += -I$(srctree)/lib/lwip/lwip/src/include -I$(srctree)/lib/lwip/u-boot
obj-$(CONFIG_$(PHASE_)DM_ETH) += net-lwip.o
obj-$(CONFIG_CMD_DHCP) += dhcp.o
+obj-$(CONFIG_DNS) += dns.o
+obj-$(CONFIG_LWIP_ICMP_SHOW_UNREACH) += icmp_unreach.o
obj-$(CONFIG_CMD_TFTPBOOT) += tftp.o
obj-$(CONFIG_WGET) += wget.o
diff --git a/net/lwip/dns.c b/net/lwip/dns.c
new file mode 100644
index 00000000000..9964003195f
--- /dev/null
+++ b/net/lwip/dns.c
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Copyright (C) 2024 Linaro Ltd. */
+
+#include <command.h>
+#include <console.h>
+#include <env.h>
+#include <lwip/dns.h>
+#include <lwip/timeouts.h>
+#include <net.h>
+#include <time.h>
+
+#define DNS_RESEND_MS 1000
+#define DNS_TIMEOUT_MS 10000
+
+struct dns_cb_arg {
+ ip_addr_t host_ipaddr;
+ const char *var;
+ bool done;
+};
+
+static void do_dns_tmr(void *arg)
+{
+ dns_tmr();
+}
+
+static void dns_cb(const char *name, const ip_addr_t *ipaddr, void *arg)
+{
+ struct dns_cb_arg *dns_cb_arg = arg;
+ char *ipstr = ip4addr_ntoa(ipaddr);
+
+ dns_cb_arg->done = true;
+
+ if (!ipaddr) {
+ printf("DNS: host not found\n");
+ dns_cb_arg->host_ipaddr.addr = 0;
+ return;
+ }
+
+ dns_cb_arg->host_ipaddr.addr = ipaddr->addr;
+
+ if (dns_cb_arg->var)
+ env_set(dns_cb_arg->var, ipstr);
+}
+
+static int dns_loop(struct udevice *udev, const char *name, const char *var)
+{
+ struct dns_cb_arg dns_cb_arg = { };
+ struct netif *netif;
+ ip_addr_t ipaddr;
+ ulong start;
+ int ret;
+
+ dns_cb_arg.var = var;
+
+ netif = net_lwip_new_netif(udev);
+ if (!netif)
+ return CMD_RET_FAILURE;
+
+ if (net_lwip_dns_init()) {
+ net_lwip_remove_netif(netif);
+ return CMD_RET_FAILURE;
+ }
+
+ dns_cb_arg.done = false;
+
+ ret = dns_gethostbyname(name, &ipaddr, dns_cb, &dns_cb_arg);
+
+ if (ret == ERR_OK) {
+ dns_cb(name, &ipaddr, &dns_cb_arg);
+ } else if (ret == ERR_INPROGRESS) {
+ start = get_timer(0);
+ sys_timeout(DNS_RESEND_MS, do_dns_tmr, NULL);
+ do {
+ net_lwip_rx(udev, netif);
+ if (dns_cb_arg.done)
+ break;
+ if (ctrlc()) {
+ printf("\nAbort\n");
+ break;
+ }
+ } while (get_timer(start) < DNS_TIMEOUT_MS);
+ sys_untimeout(do_dns_tmr, NULL);
+ }
+
+ net_lwip_remove_netif(netif);
+
+ if (dns_cb_arg.done && dns_cb_arg.host_ipaddr.addr != 0) {
+ if (!var)
+ printf("%s\n", ipaddr_ntoa(&ipaddr));
+ return CMD_RET_SUCCESS;
+ }
+
+ return CMD_RET_FAILURE;
+}
+
+int do_dns(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+ char *name;
+ char *var = NULL;
+
+ if (argc == 1 || argc > 3)
+ return CMD_RET_USAGE;
+
+ name = argv[1];
+
+ if (argc == 3)
+ var = argv[2];
+
+ if (net_lwip_eth_start() < 0)
+ return CMD_RET_FAILURE;
+
+ return dns_loop(eth_get_dev(), name, var);
+}
diff --git a/net/lwip/icmp_unreach.c b/net/lwip/icmp_unreach.c
new file mode 100644
index 00000000000..9e8a05f5717
--- /dev/null
+++ b/net/lwip/icmp_unreach.c
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Copyright (C) 2025 Linaro Ltd. */
+
+#include <lwip/icmp.h>
+#include <lwip/ip4_addr.h>
+#include <lwip/pbuf.h>
+#include <lwip/prot/ip4.h>
+
+static const char *code_to_str(int code)
+{
+ switch (code) {
+ case ICMP_DUR_NET:
+ return "network unreachable";
+ case ICMP_DUR_HOST:
+ return "host unreachable";
+ case ICMP_DUR_PROTO:
+ return "protocol unreachable";
+ case ICMP_DUR_PORT:
+ return "port unreachable";
+ case ICMP_DUR_FRAG:
+ return "fragmentation needed and DF set";
+ case ICMP_DUR_SR:
+ return "source route failed";
+ default:
+ break;
+ }
+ return "unknown cause";
+}
+
+void net_lwip_icmp_dest_unreach(int code, struct pbuf *p)
+{
+ struct ip_hdr *iphdr = (struct ip_hdr *)p->payload;
+ ip4_addr_t src;
+
+ ip4_addr_copy(src, iphdr->src);
+ printf("ICMP destination unreachable (%s) from %s\n",
+ code_to_str(code), ip4addr_ntoa(&src));
+}
diff --git a/net/lwip/net-lwip.c b/net/lwip/net-lwip.c
index 660ceb10cbe..1a70cedfb58 100644
--- a/net/lwip/net-lwip.c
+++ b/net/lwip/net-lwip.c
@@ -27,7 +27,7 @@
#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
void (*push_packet)(void *, int len) = 0;
#endif
-static int net_try_count;
+int net_try_count;
static int net_restarted;
int net_restart_wrap;
static uchar net_pkt_buf[(PKTBUFSRX) * PKTSIZE_ALIGN + PKTALIGN];
@@ -147,7 +147,7 @@ static int get_udev_ipv4_info(struct udevice *dev, ip4_addr_t *ip,
*/
int net_lwip_dns_init(void)
{
-#if CONFIG_IS_ENABLED(CMD_DNS)
+#if CONFIG_IS_ENABLED(DNS)
bool has_server = false;
ip_addr_t ns;
char *nsenv;
@@ -364,7 +364,7 @@ int net_lwip_rx(struct udevice *udev, struct netif *netif)
*/
int net_lwip_dns_resolve(char *name_or_ip, ip_addr_t *ip)
{
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
char *var = "_dnsres";
char *argv[] = { "dns", name_or_ip, var, NULL };
int argc = ARRAY_SIZE(argv) - 1;
@@ -373,7 +373,7 @@ int net_lwip_dns_resolve(char *name_or_ip, ip_addr_t *ip)
if (ipaddr_aton(name_or_ip, ip))
return 0;
-#if defined(CONFIG_CMD_DNS)
+#if defined(CONFIG_DNS)
if (do_dns(NULL, 0, argc, argv) != CMD_RET_SUCCESS)
return -1;