Compare commits

...

3 Commits

Author SHA1 Message Date
Jonas Jonsson
a834920532
Merge b1f63fe9b2c299194fb1b1e05f790e0657f4468b into a6c3a9cbbb5e0a99e0d30e58271f27a0d4e9ae90 2025-03-03 15:13:31 +00:00
Jonas Jonsson
b1f63fe9b2 feat(ppp): Improve PPP server + client support
Make it easier to run a PPP server and client on different interfaces by
adding the interface name to logs and expose the PPP passive option.
2025-03-03 16:09:33 +01:00
Jonas Jonsson
0151c8db54 feat(ppp): Allow config PPP DNS servers for peer
Make it possible to set the DNS servers to provide to the peer. This is
useful when acting as a PPP server.

If any DNS server is set, don't request a DNS server from the peer.
2025-03-03 16:09:33 +01:00
2 changed files with 61 additions and 28 deletions

View File

@ -31,6 +31,9 @@ typedef struct esp_netif_ppp_config {
#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT
esp_ip4_addr_t ppp_our_ip4_addr; /**< Set our preferred address, typically used when we're the PPP server */
esp_ip4_addr_t ppp_their_ip4_addr; /**< Set our preferred address, typically used when we're the PPP server */
esp_ip4_addr_t ppp_dns1_addr; /**< DNS to provide if peer asks for it, typically used when we're the PPP server */
esp_ip4_addr_t ppp_dns2_addr; /**< DNS to provide if peer asks for it, typically used when we're the PPP server */
bool ppp_passive; /**< Try once to initiate connection, stay silent if it fails, typically used when we're the PPP server */
#endif // CONFIG_LWIP_PPP_SERVER_SUPPORT
} esp_netif_ppp_config_t;

View File

@ -37,6 +37,9 @@ typedef struct lwip_peer2peer_ctx {
#ifdef CONFIG_LWIP_PPP_SERVER_SUPPORT
esp_ip4_addr_t ppp_our_ip4_addr; // our desired IP (IPADDR_ANY if no preference)
esp_ip4_addr_t ppp_their_ip4_addr; // their desired IP (IPADDR_ANY if no preference)
esp_ip4_addr_t ppp_dns1_addr; // dns server 1 IP (IPADDR_ANY if no preference)
esp_ip4_addr_t ppp_dns2_addr; // dns server 2 IP (IPADDR_ANY if no preference)
bool ppp_passive; // Set ppp_passive() and use ppp_listen()
#endif
ppp_pcb *ppp;
} lwip_peer2peer_ctx_t;
@ -48,6 +51,7 @@ typedef struct lwip_peer2peer_ctx {
static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
{
esp_netif_t *netif = ctx;
const char *name = netif->if_desc ? netif->if_desc : "";
ip_event_got_ip_t evt = {
.esp_netif = netif,
};
@ -56,59 +60,59 @@ static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
assert(obj->base.netif_type == PPP_LWIP_NETIF);
switch (err_code) {
case PPPERR_NONE:
ESP_LOGI(TAG, "Connected");
ESP_LOGI(TAG, "%s: Connected", name);
break;
case PPPERR_PARAM:
ESP_LOGE(TAG, "Invalid parameter");
ESP_LOGE(TAG, "%s: Invalid parameter", name);
break;
case PPPERR_OPEN:
ESP_LOGE(TAG, "Unable to open PPP session");
ESP_LOGE(TAG, "%s: Unable to open PPP session", name);
break;
case PPPERR_DEVICE:
ESP_LOGE(TAG, "Invalid I/O device for PPP");
ESP_LOGE(TAG, "%s: Invalid I/O device for PPP", name);
break;
case PPPERR_ALLOC:
ESP_LOGE(TAG, "Unable to allocate resources");
ESP_LOGE(TAG, "%s: Unable to allocate resources", name);
break;
case PPPERR_USER: /* User interrupt */
ESP_LOGI(TAG, "User interrupt");
ESP_LOGI(TAG, "%s: User interrupt", name);
break;
case PPPERR_CONNECT: /* Connection lost */
ESP_LOGI(TAG, "Connection lost");
ESP_LOGI(TAG, "%s: Connection lost", name);
esp_netif_update_default_netif(netif, ESP_NETIF_LOST_IP);
err = esp_event_post(IP_EVENT, netif->lost_ip_event, &evt, sizeof(evt), 0);
if (ESP_OK != err) {
ESP_LOGE(TAG, "esp_event_post failed with code %d", err);
ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err);
}
return;
case PPPERR_AUTHFAIL:
ESP_LOGE(TAG, "Failed authentication challenge");
ESP_LOGE(TAG, "%s: Failed authentication challenge", name);
break;
case PPPERR_PROTOCOL:
ESP_LOGE(TAG, "Failed to meet protocol");
ESP_LOGE(TAG, "%s: Failed to meet protocol", name);
break;
case PPPERR_PEERDEAD:
ESP_LOGE(TAG, "Connection timeout");
ESP_LOGE(TAG, "%s: Connection timeout", name);
break;
case PPPERR_IDLETIMEOUT:
ESP_LOGE(TAG, "Idle Timeout");
ESP_LOGE(TAG, "%s: Idle Timeout", name);
break;
case PPPERR_CONNECTTIME:
ESP_LOGE(TAG, "Max connect time reached");
ESP_LOGE(TAG, "%s: Max connect time reached", name);
break;
case PPPERR_LOOPBACK:
ESP_LOGE(TAG, "Loopback detected");
ESP_LOGE(TAG, "%s: Loopback detected", name);
break;
default:
ESP_LOGE(TAG, "Unknown error code %d", err_code);
ESP_LOGE(TAG, "%s: Unknown error code %d", name, err_code);
break;
}
if (obj->ppp_error_event_enabled) {
err = esp_event_post(NETIF_PPP_STATUS, err_code, &netif, sizeof(netif), 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_event_post failed with code %d", err);
ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err);
}
}
@ -124,42 +128,44 @@ static void on_ppp_status_changed(ppp_pcb *pcb, int err_code, void *ctx)
*/
static void on_ppp_notify_phase(ppp_pcb *pcb, u8_t phase, void *ctx)
{
esp_netif_t *netif = ctx;
const char *name = netif->if_desc ? netif->if_desc : "";
switch (phase) {
case PPP_PHASE_DEAD:
ESP_LOGD(TAG, "Phase Dead");
ESP_LOGD(TAG, "%s: Phase Dead", name);
break;
case PPP_PHASE_INITIALIZE:
ESP_LOGD(TAG, "Phase Start");
ESP_LOGD(TAG, "%s: Phase Start", name);
break;
case PPP_PHASE_ESTABLISH:
ESP_LOGD(TAG, "Phase Establish");
ESP_LOGD(TAG, "%s: Phase Establish", name);
break;
case PPP_PHASE_AUTHENTICATE:
ESP_LOGD(TAG, "Phase Authenticate");
ESP_LOGD(TAG, "%s: Phase Authenticate", name);
break;
case PPP_PHASE_NETWORK:
ESP_LOGD(TAG, "Phase Network");
ESP_LOGD(TAG, "%s: Phase Network", name);
break;
case PPP_PHASE_RUNNING:
ESP_LOGD(TAG, "Phase Running");
ESP_LOGD(TAG, "%s: Phase Running", name);
break;
case PPP_PHASE_TERMINATE:
ESP_LOGD(TAG, "Phase Terminate");
ESP_LOGD(TAG, "%s: Phase Terminate", name);
break;
case PPP_PHASE_DISCONNECT:
ESP_LOGD(TAG, "Phase Disconnect");
ESP_LOGD(TAG, "%s: Phase Disconnect", name);
break;
default:
ESP_LOGW(TAG, "Phase Unknown: %d", phase);
ESP_LOGW(TAG, "%s: Phase Unknown: %d", name, phase);
break;
}
esp_netif_t *netif = ctx;
lwip_peer2peer_ctx_t *obj = (lwip_peer2peer_ctx_t *)netif->related_data;
assert(obj->base.netif_type == PPP_LWIP_NETIF);
if (obj && obj->ppp_phase_event_enabled) {
esp_err_t err = esp_event_post(NETIF_PPP_STATUS, NETIF_PP_PHASE_OFFSET + phase, &netif, sizeof(netif), 0);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_event_post failed with code %d", err);
ESP_LOGE(TAG, "%s: esp_event_post failed with code %d", name, err);
}
}
}
@ -268,6 +274,19 @@ esp_err_t esp_netif_start_ppp(esp_netif_t *esp_netif)
ppp_ctx->ppp->ipcp_wantoptions.hisaddr = ppp_ctx->ppp_their_ip4_addr.addr;
ppp_ctx->ppp->ipcp_wantoptions.accept_local = 1;
}
if (ppp_ctx->ppp_dns1_addr.addr != IPADDR_ANY) {
ppp_set_ipcp_dnsaddr(ppp_ctx->ppp, 0, &ppp_ctx->ppp_dns1_addr);
}
if (ppp_ctx->ppp_dns2_addr.addr != IPADDR_ANY) {
ppp_set_ipcp_dnsaddr(ppp_ctx->ppp, 1, &ppp_ctx->ppp_dns2_addr);
}
if (ppp_ctx->ppp_dns1_addr.addr != IPADDR_ANY ||
ppp_ctx->ppp_dns2_addr.addr != IPADDR_ANY) {
// No need to request DNS servers from peer when providing DNS servers.
ppp_set_usepeerdns(ppp_ctx->ppp, 0);
}
ppp_set_passive(ppp_ctx->ppp, ppp_ctx->ppp_passive);
#endif // CONFIG_LWIP_PPP_SERVER_SUPPORT
#if ESP_IPV6_AUTOCONFIG
@ -276,7 +295,12 @@ esp_err_t esp_netif_start_ppp(esp_netif_t *esp_netif)
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);
esp_err_t err;
if (ppp_ctx->ppp_passive) {
err = ppp_listen(ppp_ctx->ppp);
} else {
err = ppp_connect(ppp_ctx->ppp, 0);
}
#else
err_t err = ppp_connect(ppp_ctx->ppp, 0);
#endif
@ -338,6 +362,9 @@ esp_err_t esp_netif_ppp_set_params(esp_netif_t *netif, const esp_netif_ppp_confi
#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;
obj->ppp_dns1_addr = config->ppp_dns1_addr;
obj->ppp_dns2_addr = config->ppp_dns2_addr;
obj->ppp_passive = config->ppp_passive;
#endif
return ESP_OK;
}
@ -357,6 +384,9 @@ esp_err_t esp_netif_ppp_get_params(esp_netif_t *netif, esp_netif_ppp_config_t *c
#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;
config->ppp_dns1_addr = obj->ppp_dns1_addr;
config->ppp_dns2_addr = obj->ppp_dns2_addr;
config->ppp_passive = obj->ppp_passive;
#endif
return ESP_OK;