From f927b9fbab0478eb8af0246e988e5a390af81d69 Mon Sep 17 00:00:00 2001 From: 1e1 <2.71828183e0+git@gmail.com> Date: Wed, 28 Jul 2021 10:56:25 +0200 Subject: [PATCH] fix: the dhcp client behaviour is not standard compliant for DHCP clients (RFC2131) (IDFGH-5139) --- glue-lwip/arduino/lwipopts.h | 8 ++++++++ glue-lwip/lwip-git.c | 29 +++++++++++++++++++++++++++++ makefiles/Makefile.defs | 1 + 3 files changed, 38 insertions(+) diff --git a/glue-lwip/arduino/lwipopts.h b/glue-lwip/arduino/lwipopts.h index d79dc8b7..fc682be0 100644 --- a/glue-lwip/arduino/lwipopts.h +++ b/glue-lwip/arduino/lwipopts.h @@ -3579,6 +3579,14 @@ extern void lwip_hook_dhcp_parse_option(struct netif *netif, struct dhcp *dhcp, int msg_type, int option, int option_len, struct pbuf *pbuf, int option_value_offset); +#if LWIP_FEATURES +#define LWIP_HOOK_DHCP_APPEND_OPTIONS(netif, dhcp, state, msg, msg_type, option_len_ptr) \ + lwip_hook_dhcp_amend_options(netif, dhcp, state, msg, msg_type, option_len_ptr) + +extern void lwip_hook_dhcp_amend_options(struct netif *netif, struct dhcp *dhcp, int state, struct dhcp_msg *msg, + int msg_type, int *option_len_ptr); +#endif + /* -------------------------------------------------- ------------------ SNTP options ------------------ diff --git a/glue-lwip/lwip-git.c b/glue-lwip/lwip-git.c index 3845d976..e2351b72 100644 --- a/glue-lwip/lwip-git.c +++ b/glue-lwip/lwip-git.c @@ -32,6 +32,7 @@ author: d. gauchard #include "glue.h" #include "uprint.h" #include "lwip-helper.h" +#include "lwip/prot/dhcp.h" #include "lwipopts.h" #include "lwip/err.h" @@ -196,6 +197,7 @@ err_glue_t esp2glue_dhcp_start (int netif_idx) // calls netif_sta_status_callback() - if applicable (STA) netif_set_up(&netif_git[netif_idx]); +#if LWIP_NETIF_HOSTNAME // Update to latest esp hostname before starting dhcp client, // because this name is provided to the dhcp server. // Until proven wrong, dhcp client is the only code @@ -205,6 +207,7 @@ err_glue_t esp2glue_dhcp_start (int netif_idx) // XXX to check: is wifi_station_get_hostname() // returning a const pointer once _set_hostname is called? netif_git[netif_idx].hostname = wifi_station_get_hostname(); +#endif err_t err = dhcp_start(&netif_git[netif_idx]); #if LWIP_IPV6 && LWIP_IPV6_DHCP6_STATELESS @@ -352,7 +355,9 @@ static void netif_init_common (struct netif* netif) netif->ip6_autoconfig_enabled = 1; #endif +#if LWIP_NETIF_HOSTNAME netif->hostname = wifi_station_get_hostname(); +#endif netif->chksum_flags = NETIF_CHECKSUM_ENABLE_ALL; // netif->mtu given by glue } @@ -409,7 +414,9 @@ void esp2glue_netif_update (int netif_idx, uint32_t ip, uint32_t mask, uint32_t netif->flags &= ~NETIF_FLAG_UP; netif->mtu = mtu; +#if LWIP_NETIF_HOSTNAME netif->hostname = wifi_station_get_hostname(); +#endif ip4_addr_t aip = { ip }, amask = { mask }, agw = { gw }; netif_set_addr(&netif_git[netif_idx], &aip, &amask, &agw); esp2glue_netif_set_up1down0(netif_idx, 1); @@ -569,4 +576,26 @@ void lwip_hook_dhcp_parse_option(struct netif *netif, struct dhcp *dhcp, int sta uprint(DBG "unhandled dhcp option in lwip_hook_dhcp_parse_option()\n"); } +void lwip_hook_dhcp_amend_options(struct netif *netif, struct dhcp *dhcp, int state, struct dhcp_msg *msg, + int msg_type, int *option_len_ptr); + +void lwip_hook_dhcp_amend_options(struct netif *netif, struct dhcp *dhcp, int state, struct dhcp_msg *msg, + int msg_type, int *option_len_ptr) +{ + // amend default Client-Identifier + // {intf.id} {intf.mac} => {01}:{AA:BB:CC:11:22:33} + { + size_t i; + + msg->options[(*option_len_ptr)++] = DHCP_OPTION_CLIENT_ID; + msg->options[(*option_len_ptr)++] = 1 + netif->hwaddr_len; + msg->options[(*option_len_ptr)++] = 0x01; + + for (i = 0; i < netif->hwaddr_len; ++i) { + msg->options[(*option_len_ptr)++] = netif->hwaddr[i]; + } + } +} + + #endif // ARDUINO diff --git a/makefiles/Makefile.defs b/makefiles/Makefile.defs index bbce1de1..32552bbd 100644 --- a/makefiles/Makefile.defs +++ b/makefiles/Makefile.defs @@ -26,3 +26,4 @@ OD = $(TOOLS)objdump BUILD ?= build BUILD_FLAGS += -Wall -Wextra -c -Os -g -free -fipa-pta -Wpointer-arith -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -falign-functions=4 -MMD -ffunction-sections -fdata-sections BUILD_DEFINES = -D__ets__ -DICACHE_FLASH -U__STRICT_ANSI__ -DLWIP_OPEN_SRC -DLWIP_BUILD -DUSE_OPTIMIZE_PRINTF -DTARGET=$(target) -D$(DEFINE_TARGET)=1 -DTCP_MSS=$(TCP_MSS) -DLWIP_FEATURES=$(LWIP_FEATURES) -DLWIP_IPV6=$(LWIP_IPV6) +BUILD_DEFINES += -DLWIP_NETIF_HOSTNAME=$(LWIP_FEATURES) -DESP_DHCP=$(LWIP_FEATURES)