espressif / esp-iot-bridge

A smart bridge to make both ESP and the other MCU or smart device can access the Internet.
164 stars 54 forks source link

Cant ping or communicate with devices attached to Bridge (AEGHB-144) #43

Open BitBricker opened 1 year ago

BitBricker commented 1 year ago

Hello, have a slight bug with the bridge: I have configured it to provide "data forward for other devices" on USB, Ethernet and SoftAP. Booted, connected ethernet, USB and WiFi. Eth: 192.168.6.2 USB: 192.168.5.2 WiFI 192.168.4.2

Now, From ethernet client, I can ping 192.168.6.1 and get response, from USB client I can ping 192.168.5.1 and get reposne, and from WiFi client I can bing 192.168.4.1. That tells me that all 3 clients can communicate with the bridge DHCP.

BUT I can not ping USB from Eth, or WiFi from USB and so on. So if im on USB client and ping 192.168.6.1, I get no reponse.

Thanks!

tswen commented 1 year ago

The update for this part has not been synced to the lwip component yet. Please apply the following patch in the esp-idf/components/lwip/lwip submodule.

diff --git a/src/core/ipv4/ip4.c b/src/core/ipv4/ip4.c
index a5c3dd55..a6106c90 100644
--- a/src/core/ipv4/ip4.c
+++ b/src/core/ipv4/ip4.c
@@ -332,8 +332,10 @@ ip4_forward(struct pbuf *p, struct ip_hdr *iphdr, struct netif *inp)

 #if ESP_LWIP
 #if IP_NAPT
-  if (ip_napt_forward(p, iphdr, inp, netif) != ERR_OK)
-    return;
+  if (!netif->napt) {
+    if (ip_napt_forward(p, iphdr, inp, netif) != ERR_OK)
+        return;
+  }
 #endif
 #endif /* ESP_LWIP */
BitBricker commented 1 year ago

Doing a little correction to the previous issue:

Doing the above patch worked only partially.

Test setup: Etherent client connected to the ESP32S3 over RJ45. USB client connected to ESP32S3 over USB and PC

When ESP32 boots, you can use PC USB ethernet to ping the RJ45 client for a few minutes. No issues here, though PC tries to jump to this ethernet connection for WAN since GW is set. After few minutes, PC realizes this USB Ethernet doesn't have to work GW for wan access, the ping to RJ45 stops working.
I have manually tried to set "ip_info->gw.addr = ESP_IP4TOADDR(0, 0, 0, 0);". That allows to have all clients to connect and not try using the device for WAN, BUT can not ping each other. Just doesn't work.

Tried doing the same setup with WiFi -> USB. Same issue with the original GW. After few minutes, ping stops working. If you set gw to 0.0.0.0, ping doesn't work at all even at fresh boot.

BitBricker commented 1 year ago

After further troubleshooting, the issue seems to be with forwarding blocks if GW is set to 0.0.0.0.

The "bridge" does work for about 10 seconds or so after boot with gw set to 192.168.x.1, and during this time I have pinged devices and was able to connect to them over TCP, BUT UDP doesn't work. For example, USB side is assigned 192.168.4.2 and we use this interface to broadcast UDP messages to the network 255.255.255.0. Well the message does leave (can trace it on wireshark), BUT other side doesnt respond.

tswen commented 1 year ago

Are you saying that you want to send UDP broadcast through the USB interface to both the Ethernet and Wi-Fi SoftAP interfaces?

BitBricker commented 1 year ago

That is correct, or at least from USB to Eth.

Also, big issue seems that NAPT function doesn't function if all or any of the NETIFs are set with GW 0.0.0.0. Is there a known patch for thtat scenario?

xcguang commented 1 year ago

Could you try this? 192.168.4.2 should ping 192.168.5.2 successfully.

image
BitBricker commented 1 year ago

Thats a big fat negative. Tests performed: USB RNDIS connected to the computer with dhcp assigned IP of 192.168.4.2 and GW of 192.168.4.1 WiFi AP started with default dhcp options and IP assigned to client of 192.168.5.2 and GW of 192.168.4.1. Can ping from PC to WiFi Client. Changed WiFi netif to have forced IP of 192.168.5.1 and GW of 0.0.0.0. (and left PC side as is) resulted in: From the PC side, can ping 192.168.5.1 for WiFi netif, BUT cant ping 192.168.5.2 for WiFi client once connected.

Then changed bridge_common to assign 0.0.0.0 for all clients ( ip_info->gw.addr) under esp_bridge_netif_request_ip. Wifi client got IP of 192.168.5.2 with GW of 0.0.0.0 and USB client got ip of 192.168.4.1 with GW of 0.0.0.0. Resulted in: Cant ping from 192.168.4.2 to 192.168.5.1 or 192.168.5.2.

tswen commented 1 year ago

The current architecture does not support changing the gateway address of the interface, and any arbitrary changes will result in communication failure. The two patches above can achieve unicast and broadcast communication between different interfaces. For broadcasting, if a PC connected via USB (192.168.5.2) wants to broadcast to the Stations (192.168.6.x) under SoftAP (192.168.6.1), the broadcast address should be 192.168.6.255 instead of 255.255.255.255.