From a9265db5f14a9370af410fdff9e3d5a2d9834d25 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Wed, 4 Oct 2023 17:35:57 +0200 Subject: [PATCH] feat(lwip): Add support for PPP server Added support PPP_SERVER option in LWIP Added support for configuring preferred addresses of PPP endpoints. --- components/esp_netif/include/esp_netif_ppp.h | 4 +++ .../esp_netif/lwip/esp_netif_lwip_ppp.c | 31 ++++++++++++++++++- components/lwip/Kconfig | 7 +++++ components/lwip/port/include/lwipopts.h | 5 +++ 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/components/esp_netif/include/esp_netif_ppp.h b/components/esp_netif/include/esp_netif_ppp.h index c9856c6206..d3d576c463 100644 --- a/components/esp_netif/include/esp_netif_ppp.h +++ b/components/esp_netif/include/esp_netif_ppp.h @@ -28,6 +28,10 @@ typedef struct esp_netif_ppp_config { * The current session must be closed, settings will be applied upon connecting. * */ #endif // CONFIG_LWIP_ENABLE_LCP_ECHO +#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT + uint32_t ppp_our_ip4_addr; /**< Set our preferred address, typically used when we're the PPP server */ + uint32_t ppp_their_ip4_addr; /**< Set our preferred address, typically used when we're the PPP server */ +#endif // CONFIG_LWIP_PPP_SERVER_SUPPORT } esp_netif_ppp_config_t; /** @brief event id offset for PHASE related events diff --git a/components/esp_netif/lwip/esp_netif_lwip_ppp.c b/components/esp_netif/lwip/esp_netif_lwip_ppp.c index ee4e6224d3..faac9d09e2 100644 --- a/components/esp_netif/lwip/esp_netif_lwip_ppp.c +++ b/components/esp_netif/lwip/esp_netif_lwip_ppp.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -33,6 +33,10 @@ typedef struct lwip_peer2peer_ctx { bool ppp_error_event_enabled; #ifdef CONFIG_LWIP_ENABLE_LCP_ECHO bool ppp_lcp_echo_disabled; +#endif +#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT + uint32_t ppp_our_ip4_addr; // our desired IP (0 if no preference) + uint32_t ppp_their_ip4_addr; // their desired IP (0 if no preference) #endif ppp_pcb *ppp; } lwip_peer2peer_ctx_t; @@ -247,13 +251,29 @@ esp_err_t esp_netif_start_ppp(esp_netif_t *esp_netif) ppp_ctx->ppp->settings.lcp_echo_fails = LCP_MAXECHOFAILS; } #endif +#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT + if (ppp_ctx->ppp_our_ip4_addr != 0) { + // Set our preferred address, and accept the remote + ppp_ctx->ppp->ipcp_wantoptions.ouraddr = ppp_ctx->ppp_our_ip4_addr; + ppp_ctx->ppp->ipcp_wantoptions.accept_remote = 1; + } + if (ppp_ctx->ppp_their_ip4_addr != 0) { + // Set their preferred address, and accept the local + ppp_ctx->ppp->ipcp_wantoptions.hisaddr = ppp_ctx->ppp_their_ip4_addr; + ppp_ctx->ppp->ipcp_wantoptions.accept_local = 1; + } +#endif // CONFIG_LWIP_PPP_SERVER_SUPPORT #if ESP_IPV6_AUTOCONFIG ppp_ctx->ppp->netif->ip6_autoconfig_enabled = 1; #endif ESP_LOGD(TAG, "%s: Starting PPP connection: %p", __func__, ppp_ctx->ppp); +#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT + esp_err_t err = ppp_listen(ppp_ctx->ppp); +#else err_t err = ppp_connect(ppp_ctx->ppp, 0); +#endif if (err != ESP_OK) { ESP_LOGE(TAG, "%s: PPP connection cannot be started", __func__); if (ppp_ctx->ppp_error_event_enabled) { @@ -308,6 +328,10 @@ esp_err_t esp_netif_ppp_set_params(esp_netif_t *netif, const esp_netif_ppp_confi obj->ppp_error_event_enabled = config->ppp_error_event_enabled; #ifdef CONFIG_LWIP_ENABLE_LCP_ECHO obj->ppp_lcp_echo_disabled = config->ppp_lcp_echo_disabled; +#endif +#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT + obj->ppp_our_ip4_addr = config->ppp_our_ip4_addr; + obj->ppp_their_ip4_addr = config->ppp_their_ip4_addr; #endif return ESP_OK; } @@ -324,5 +348,10 @@ esp_err_t esp_netif_ppp_get_params(esp_netif_t *netif, esp_netif_ppp_config_t *c #ifdef CONFIG_LWIP_ENABLE_LCP_ECHO config->ppp_lcp_echo_disabled = obj->ppp_lcp_echo_disabled; #endif +#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT + config->ppp_our_ip4_addr = obj->ppp_our_ip4_addr; + config->ppp_their_ip4_addr = obj->ppp_their_ip4_addr; +#endif + return ESP_OK; } diff --git a/components/lwip/Kconfig b/components/lwip/Kconfig index 8a6b661cdf..0431b74cd8 100644 --- a/components/lwip/Kconfig +++ b/components/lwip/Kconfig @@ -952,6 +952,13 @@ menu "LWIP" help Enable Microsoft Point-to-Point Encryption (MPPE) support + config LWIP_PPP_SERVER_SUPPORT + bool "Enable PPP server support" + depends on LWIP_PPP_SUPPORT + default n + help + Enable to use PPP server + config LWIP_ENABLE_LCP_ECHO bool "Enable LCP ECHO" depends on LWIP_PPP_SUPPORT diff --git a/components/lwip/port/include/lwipopts.h b/components/lwip/port/include/lwipopts.h index 90319d8898..c633c1a5bc 100644 --- a/components/lwip/port/include/lwipopts.h +++ b/components/lwip/port/include/lwipopts.h @@ -1105,6 +1105,11 @@ static inline uint32_t timeout_from_offered(uint32_t lease, uint32_t min) */ #define MPPE_SUPPORT CONFIG_LWIP_PPP_MPPE_SUPPORT +/** + * PPP_SERVER==1: Enable PPP server support (waiting for incoming PPP session). + */ +#define PPP_SERVER CONFIG_LWIP_PPP_SERVER_SUPPORT + /** * PPP_MAXIDLEFLAG: Max Xmit idle time (in ms) before resend flag char. * TODO: If PPP_MAXIDLEFLAG > 0 and next package is send during PPP_MAXIDLEFLAG time,