summaryrefslogtreecommitdiff
path: root/net/lwip/dhcp.c
diff options
context:
space:
mode:
authorTom Rini <trini@konsulko.com>2024-10-16 08:12:28 -0600
committerTom Rini <trini@konsulko.com>2024-10-16 11:11:57 -0600
commit608a31bdec6284ad6f821226e4c62c9cd3052874 (patch)
treec43e6664c7530f354f73c05cb817e37d10d93615 /net/lwip/dhcp.c
parent1ca0ddb643f3b152439f7bb8f6ee3f5826d7102d (diff)
parent4820cb4b16c44ec332b18dbe7207ddd8c0a1d446 (diff)
Merge patch series "Introduce the lwIP network stack"
Jerome Forissier <jerome.forissier@linaro.org> says: This is a rework of a patch series by Maxim Uvarov: "net/lwip: add lwip library for the network stack" [1]. The goal is to introduce the lwIP TCP/IP stack [2] [3] as an alternative to the current implementation in net/, selectable with Kconfig, and ultimately keep only lwIP if possible. Some reasons for doing so are: - Make the support of HTTPS in the wget command easier. Javier T. and Raymond M. (CC'd) have some additional lwIP and Mbed TLS patches to do so. With that it becomes possible to fetch and launch a distro installer such as Debian etc. using a secure, authenticated connection directly from the U-Boot shell. Several use cases: * Authentication: prevent MITM attack (third party replacing the binary with a different one) * Confidentiality: prevent third parties from grabbing a copy of the image as it is being downloaded * Allow connection to servers that do not support plain HTTP anymore (this is becoming more and more common on the Internet these days) - Possibly benefit from additional features implemented in lwIP - Less code to maintain in U-Boot Prior to applying this series, the lwIP stack needs to be added as a Git subtree with the following command: $ git subtree add --squash --prefix lib/lwip/lwip \ https://github.com/lwip-tcpip/lwip.git STABLE-2_2_0_RELEASE Notes 1. A number of features are currently incompatible with NET_LWIP: DFU_TFTP, FASTBOOT, SPL_NET, ETH_SANDBOX, ETH_SANDBOX_RAW, DM_ETH. They all make assumptions on how the network stack is implemented and/or pull sybols that are not trivially exported from lwIP. Some interface rework may be needed. 2. Due to the above, and in order to provide some level of testing of the lwIP code in CI even when the legacy NET is the default, a new QEMU configuration is introduced (qemu_arm64_lwip_defconfig) which is based on qemu_arm64_defconfig with NET_LWIP and CMD_*_LWIP enabled. In addition to that, this series has some [TESTING] patches which make NET_LWIP the default. [1] https://lore.kernel.org/all/20231127125726.3735-1-maxim.uvarov@linaro.org/ [2] https://www.nongnu.org/lwip/ [3] https://en.wikipedia.org/wiki/LwIP Link: https://lore.kernel.org/r/cover.1729070678.git.jerome.forissier@linaro.org
Diffstat (limited to 'net/lwip/dhcp.c')
-rw-r--r--net/lwip/dhcp.c139
1 files changed, 139 insertions, 0 deletions
diff --git a/net/lwip/dhcp.c b/net/lwip/dhcp.c
new file mode 100644
index 00000000000..23b56226921
--- /dev/null
+++ b/net/lwip/dhcp.c
@@ -0,0 +1,139 @@
+// SPDX-License-Identifier: GPL-2.0+
+/* Copyright (C) 2024 Linaro Ltd. */
+
+#include <command.h>
+#include <console.h>
+#include <dm/device.h>
+#include <linux/delay.h>
+#include <linux/errno.h>
+#include <lwip/dhcp.h>
+#include <lwip/dns.h>
+#include <lwip/timeouts.h>
+#include <net.h>
+#include <time.h>
+
+#define DHCP_TIMEOUT_MS 10000
+
+#ifdef CONFIG_CMD_TFTPBOOT
+/* Boot file obtained from DHCP (if present) */
+static char boot_file_name[DHCP_BOOT_FILE_LEN];
+#endif
+
+static void call_lwip_dhcp_fine_tmr(void *ctx)
+{
+ dhcp_fine_tmr();
+ sys_timeout(10, call_lwip_dhcp_fine_tmr, NULL);
+}
+
+static int dhcp_loop(struct udevice *udev)
+{
+ char *ipstr = "ipaddr\0\0";
+ char *maskstr = "netmask\0\0";
+ char *gwstr = "gatewayip\0\0";
+ unsigned long start;
+ struct netif *netif;
+ struct dhcp *dhcp;
+ bool bound;
+ int idx;
+
+ idx = dev_seq(udev);
+ if (idx < 0 || idx > 99) {
+ log_err("unexpected idx %d\n", idx);
+ return CMD_RET_FAILURE;
+ }
+
+ netif = net_lwip_new_netif_noip(udev);
+ if (!netif)
+ return CMD_RET_FAILURE;
+
+ start = get_timer(0);
+
+ if (dhcp_start(netif))
+ return CMD_RET_FAILURE;
+
+ call_lwip_dhcp_fine_tmr(NULL);
+
+ /* Wait for DHCP to complete */
+ do {
+ net_lwip_rx(udev, netif);
+ sys_check_timeouts();
+ bound = dhcp_supplied_address(netif);
+ if (bound)
+ break;
+ if (ctrlc()) {
+ printf("Abort\n");
+ break;
+ }
+ mdelay(1);
+ } while (get_timer(start) < DHCP_TIMEOUT_MS);
+
+ sys_untimeout(call_lwip_dhcp_fine_tmr, NULL);
+
+ if (!bound) {
+ net_lwip_remove_netif(netif);
+ return CMD_RET_FAILURE;
+ }
+
+ dhcp = netif_dhcp_data(netif);
+
+ env_set("bootfile", dhcp->boot_file_name);
+
+ if (idx > 0) {
+ sprintf(ipstr, "ipaddr%d", idx);
+ sprintf(maskstr, "netmask%d", idx);
+ sprintf(gwstr, "gatewayip%d", idx);
+ } else {
+ net_ip.s_addr = dhcp->offered_ip_addr.addr;
+ }
+
+ env_set(ipstr, ip4addr_ntoa(&dhcp->offered_ip_addr));
+ env_set(maskstr, ip4addr_ntoa(&dhcp->offered_sn_mask));
+ env_set("serverip", ip4addr_ntoa(&dhcp->server_ip_addr));
+ if (dhcp->offered_gw_addr.addr != 0)
+ env_set(gwstr, ip4addr_ntoa(&dhcp->offered_gw_addr));
+
+#ifdef CONFIG_PROT_DNS_LWIP
+ env_set("dnsip", ip4addr_ntoa(dns_getserver(0)));
+ env_set("dnsip2", ip4addr_ntoa(dns_getserver(1)));
+#endif
+#ifdef CONFIG_CMD_TFTPBOOT
+ if (dhcp->boot_file_name[0] != '\0')
+ strncpy(boot_file_name, dhcp->boot_file_name,
+ sizeof(boot_file_name));
+#endif
+
+ printf("DHCP client bound to address %pI4 (%lu ms)\n",
+ &dhcp->offered_ip_addr, get_timer(start));
+
+ net_lwip_remove_netif(netif);
+ return CMD_RET_SUCCESS;
+}
+
+int do_dhcp(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+ eth_set_current();
+
+ return dhcp_loop(eth_get_dev());
+}
+
+int dhcp_run(ulong addr, const char *fname, bool autoload)
+{
+ char *dhcp_argv[] = {"dhcp", NULL, };
+#ifdef CONFIG_CMD_TFTPBOOT
+ char *tftp_argv[] = {"tftpboot", boot_file_name, NULL, };
+#endif
+ struct cmd_tbl cmdtp = {}; /* dummy */
+
+ if (autoload) {
+#ifdef CONFIG_CMD_TFTPBOOT
+ /* Assume DHCP was already performed */
+ if (boot_file_name[0])
+ return do_tftpb(&cmdtp, 0, 2, tftp_argv);
+ return 0;
+#else
+ return -EOPNOTSUPP;
+#endif
+ }
+
+ return do_dhcp(&cmdtp, 0, 1, dhcp_argv);
+}