ciniml / WireGuard-ESP32-Arduino

WireGuard implementation for ESP32 Arduino
Other
782 stars 60 forks source link

Crash using with ESP32_ETH #15

Closed zekageri closed 2 years ago

zekageri commented 2 years ago

Hi! Thank you for the implementation. It sure looks really promising. I'm trying to initiate a basic communication between esp32 and an android apk for now.

I'm using ETH implementation by espressif with lan8720 eth board.

A snippet from my sketch:


void ethEvent(WiFiEvent_t event){
    if( event == SYSTEM_EVENT_ETH_START ){
        ETH.setHostname(config.hostname);
    }else if( event == SYSTEM_EVENT_ETH_GOT_IP ){
        gotIp_MS    = millis();
        gotIP       = true;
        canWireGuardBegin = true;
    }else if( event == SYSTEM_EVENT_ETH_DISCONNECTED ){
        gotIp_MS    = 0;
        gotIP       = false;
    }
}

inline void startEthernet(){
    Serial.println("Starting ethernet...");
    ETH.begin(
        ETH_PHY_ADDR,
        PIN_PHY_POWER,
        PIN_SMI_MDC,
        PIN_SMI_MDIO,
        ETH_PHY_LAN8720,
        ETH_CLK_MODE
    );
    if( !config.dhcpIsOn )
        {ETH.config(config.ip, config.gw, config.sub, config.dns1, config.dns2);}
    WiFi.onEvent(ethEvent);
}

void setup(){
    startEthernet();
}

static WireGuard wg;
long lastWgPacket = 0;
boolean canWireGuardBegin   = false;
boolean isWgConfigured      = false;

static const inline void beginWireGuard(){
    if(canWireGuardBegin){
        canWireGuardBegin = false;
        configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com");
        isWgConfigured = wg.begin(
            local_ip,           // IP address of the local interface
            private_key,        // Private key of the local interface
            endpoint_address,   // Address of the endpoint peer.
            public_key,         // Public key of the endpoint peer.
            endpoint_port);     // Port pf the endpoint peer.

        Serial.println("isWgConfigured: " + String(isWgConfigured));
        // Sketch is crashing right here, printing: isWgConfigured: 1
    }
}

void loop(){
    beginWireGuard();
}

Crash LOG:

isWgConfigured: 1
Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x40157cf9  PS      : 0x00060730  A0      : 0x80157f64  A1      : 0x3ffce510  
A2      : 0x3ffe753c  A3      : 0x3ffe7b70  A4      : 0x3ffe7670  A5      : 0x00000000
A6      : 0x0000002e  A7      : 0x00000004  A8      : 0x00000000  A9      : 0x3ffe7b73  
A10     : 0x3ffe7b70  A11     : 0x40089960  A12     : 0x3ffce5fc  A13     : 0x0000003f
A14     : 0x00002033  A15     : 0x00000091  SAR     : 0x00000018  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000014  LBEG    : 0x4008c8c8  LEND    : 0x4008c8e4  LCOUNT  : 0x00000000

ELF file SHA256: 0000000000000000

Backtrace: 0x40157cf9:0x3ffce510 0x40157f61:0x3ffce550 0x40104da7:0x3ffce570 0x40154fa9:0x3ffce640 
0x401551b0:0x3ffce660 0x40161320:0x3ffce680 0x4009070a:0x3ffce6b0
  #0  0x40157cf9:0x3ffce510 in udp_sendto_if_src at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/udp.c:720 (discriminator 2)
  #1  0x40157f61:0x3ffce550 in udp_sendto_if at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/udp.c:689
  #2  0x40104da7:0x3ffce570 in wireguardif_peer_output at lib\WireGuard-ESP32-Arduino-main\src/wireguardif.c:912
      (inlined by) wireguard_start_handshake at lib\WireGuard-ESP32-Arduino-main\src/wireguardif.c:635
      (inlined by) wireguardif_tmr at lib\WireGuard-ESP32-Arduino-main\src/wireguardif.c:882       
  #3  0x40154fa9:0x3ffce640 in sys_check_timeouts at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/timeouts.c:381
  #4  0x401551b0:0x3ffce660 in sys_timeouts_mbox_fetch at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/timeouts.c:433
  #5  0x40161320:0x3ffce680 in tcpip_thread at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:483
  #6  0x4009070a:0x3ffce6b0 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...

All other standard ethernet communication is working. I have an Async webserver and i can do http requests, websockets and everything else.

ciniml commented 2 years ago

Thank you for your report.

As a matter of fact, I have a clue about this bug. The current code assumes to be used with a WLAN connection and does not support a wired connection.

But I think it is not so difficult to fix, so I will fix it.

zekageri commented 2 years ago

Thank you. I was about to debug further but i will wait for your fix! Really appreciate your help

ciniml commented 2 years ago

Hi.

I have pushed the fix of this issue to this branch https://github.com/ciniml/WireGuard-ESP32-Arduino/tree/15_fix_crash_with_eth

I've confirmed the fixed code works with WLAN. But I cannot confirm this works with wired connection, since I have no hardware which can use wired connection.

Could you try the fixed code in the branch?

zekageri commented 2 years ago

Yes of course. Thank you for the notification. I will try it next monday.

zekageri commented 2 years ago

Sorry for not getting back to it. I was sick for two days. But i'm testing now.

zekageri commented 2 years ago

Same error after wg.begin()

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC      : 0x40157cf1  PS      : 0x00060f30  A0      : 0x80157f5c  A1   
   : 0x3ffce570
A2      : 0x3ffdef10  A3      : 0x3ffe455c  A4      : 0x3ffdf044  A5   
   : 0x00000000  
A6      : 0x0000002e  A7      : 0x00000004  A8      : 0x00000000  A9   
   : 0x3ffbfa20
A10     : 0x00000000  A11     : 0x3ffbbf40  A12     : 0x00000001  A13  
   : 0x00000001  
A14     : 0x00060021  A15     : 0x00000000  SAR     : 0x00000018  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000014  LBEG    : 0x4008c8c8  LEND    : 0x4008c8e4  LCOUNT  : 0x00000000  

ELF file SHA256: 0000000000000000

Backtrace: 0x40157cf1:0x3ffce570 0x40157f59:0x3ffce5b0 0x40104dab:0x3ffce5d0 0x40154fa1:0x3ffce6a0 0x401551a8:0x3ffce6c0 0x40161318:0x3ffce6e0 0x4009070a:0x3ffce710
  #0  0x40157cf1:0x3ffce570 in udp_sendto_if_src at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/udp.c:720 (discriminator 2)
  #1  0x40157f59:0x3ffce5b0 in udp_sendto_if at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/udp.c:689
  #2  0x40104dab:0x3ffce5d0 in wireguardif_peer_output at lib\WireGuard-ESP32-Arduino-15_fix_crash_with_eth\src/wireguardif.c:912
      (inlined by) wireguard_start_handshake at lib\WireGuard-ESP32-Arduino-15_fix_crash_with_eth\src/wireguardif.c:635
      (inlined by) wireguardif_tmr at lib\WireGuard-ESP32-Arduino-15_fix_crash_with_eth\src/wireguardif.c:882
  #3  0x40154fa1:0x3ffce6a0 in sys_check_timeouts at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/timeouts.c:381
  #4  0x401551a8:0x3ffce6c0 in sys_timeouts_mbox_fetch at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/timeouts.c:433
  #5  0x40161318:0x3ffce6e0 in tcpip_thread at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:483
  #6  0x4009070a:0x3ffce710 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)

Rebooting...
zekageri commented 2 years ago

Maybe there is a problem with the internal time since i'm already using an RTC and an NTP server for fetching the time. I will check it.

zekageri commented 2 years ago
static const inline void beginWireGuard(){
    if(canWireGuardBegin){
        canWireGuardBegin = false;
        Serial.println("Setting up WireGuard...");
        //configTime(9 * 60 * 60, 0, "ntp.jst.mfeed.ad.jp", "ntp.nict.jp", "time.google.com");
        isWgConfigured = wg.begin(
            local_ip,           // IP address of the local interface
            private_key,        // Private key of the local interface
            endpoint_address,   // Address of the endpoint peer.
            public_key,         // Public key of the endpoint peer.
            endpoint_port);     // Port pf the endpoint peer.

        Serial.println("isWgConfigured: " + String(isWgConfigured));
    }
}

I commented out the config time on wireguard and enabling the setup process with a flag after i got a time from my NTP server. It seems to be working for now. The setup part at least. No crash

zekageri commented 2 years ago

Yap, confirmed. No crash just had to wait for the time syncing. Thank you for your help i will continue my testings now. I appreciate.

darren-trottier commented 1 year ago

@ciniml : Could you please merge the change into the main branch ? It is working here and it's very hard to find the solution within a closed issue with a change that is not commited to the main branch.

Best regards

underlying_netif = netif_default; // Use current netif as the underlying netif of the WireGuard interface.