espressif / esp-iot-bridge

A smart bridge to make both ESP and the other MCU or smart device can access the Internet.
Apache License 2.0
140 stars 49 forks source link

RNDIS support for the WiFi router example (AEGHB-126) #42

Closed bluestar714 closed 11 months ago

bluestar714 commented 1 year ago

Hello, I am trying to create a bridge between USB-SoftAP and RNDIS. As far as I understand, the current iot-bridge does not support RNDIS. Can you provide some guidance or hints on how to achieve this?

So far, I have added RNDIS settings to the sdkconfig, and my Windows PC is able to recognize the ESP32S3 as an RNDIS device. However, an IP address is not being assigned.

I have heard that iot-bridge is already in a state where it can support RNDIS, as mentioned in the following issues. When can we expect these to be merged?

https://github.com/espressif/esp-iot-bridge/issues/29 https://github.com/espressif/esp-iot-solution/issues/240

wuyuanyi135 commented 1 year ago

Can you post the console output of idf.py monitor?

bluestar714 commented 1 year ago

Below is the console output of the wifi router. 192.168.5.1 is assigned as the USB IP, but on the Windows side However, on the Windows side, the IP address is not assigned as shown in the attached image.

Snipet of ipconfig /all image

I have added the following to sdkconfig to support RNDIS.

I am guessing that we probably need to add a process like rndis_connect().

wuyuanyi135 commented 1 year ago

There are my suggestions:

  1. Use wireshark to see if there is dhcp packet on the RNDIS interface. Also, check if there is any response from ESP to the computer at all

  2. try add these two line where applicable

    esp_netif_action_start(usb_netif, NULL, 0, NULL);
    esp_netif_action_connected(usb_netif, NULL, 0, NULL);
  3. Add these lines after the netif has been created

    esp_netif_ip_info_t ip_info{};
    esp_netif_str_to_ip4("192.168.5.1", &ip_info.ip);
    esp_netif_str_to_ip4("192.168.5.1", &ip_info.gw);
    esp_netif_str_to_ip4("255.255.255.0", &ip_info.netmask);
    
    esp_netif_dhcps_stop(usb_netif);
    esp_netif_set_ip_info(usb_netif, &ip_info);
    esp_netif_dhcps_start(usb_netif);

See if any or the combination of these can help.

bluestar714 commented 1 year ago

Thank you for pointing this out.

I was looking at WireShark and found DHCP Discovery (source is Windows) but. Also, there seems to be no ESP source packets other than ARP. So I am wondering if there is a problem here.

By the way, I am not sure how to do "Also, check if there is any response from ESP to the computer at all". I tried to check from the terminal connected to the SoftAP side, but it was still unreachable.

I also tried some of steps 2 and 3. Using esp_netif_action_start seems to crash on "netif already added."

wuyuanyi135 commented 1 year ago

Can you manually configure the IP address of the interface on windows to 192.168.5.2 and see if 192.168.5.1 is ping-able?

bluestar714 commented 1 year ago

I had disappointed in a very rudimentary point. Thank you very much. I was able to manually assign an IP address to RNDIS and ping it between Windows (192.168.5.2) and ESP (192.168.5.1). Thank you very much!

Do you know what implementation or configuration is required to communicate between a Windows PC and a terminal connected to SoftAP, for example, 192.168.5.2 and 192.168.4.2?

wuyuanyi135 commented 1 year ago

If manually assigned IP works, it means the DHCP did not work properly on your ESP.

You can add the route to 192.168.4.0/24 through 192.168.5.1 on Windows to access the devices

bluestar714 commented 1 year ago

I'm trying, but I can't get an IP address assigned to RNDIS from ESP32's DHCP.

I have the following steps 2 and 3 in bridge_common.c right after esp_bridge_create_usb_netif, does this seem to work? https://github.com/espressif/esp-iot-bridge/issues/42#issuecomment-1497120082

tswen commented 1 year ago

@bluestar714 You just need to upgrade your esp-idf version to 5.0, as version 4.4 does not support multiple DHCPS.

https://github.com/espressif/esp-iot-bridge/blob/master/components/iot_bridge/User_Guide.md#5-configuration

bluestar714 commented 1 year ago

@tswen

After changing to v5.0 of ESP-IDF, the IP address is assigned to the RNDIS device. Thank you very much.

We are currently having the following two issues

route add 192.168.4.0 mask 255.255.255.0 192.168.5.1 if 57

I'm sorry to keep repeating myself, but could you please give me a configuration or advice that would solve these problems

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 */
bluestar714 commented 1 year ago

Thank you for the patch info. After applying the patch, I see the following message in WireShark: "no response found" for ping, but it seems to be possible to communicate directly. Is it possible to check the communication by ICMP?

42  23.359778   192.168.4.2 192.168.5.2 ICMP    74  Echo (ping) request  id=0x0001, seq=236/60416, ttl=127 (no response found!)
tswen commented 1 year ago

Yes, it is possible to check communication using ICMP. I didn't find any "no response found" situation during my testing. Could it be an accidental phenomenon?

bluestar714 commented 1 year ago

Thanks for the reply. No, it happens every time so it is reproducible. It's not accidental, maybe the route add command is wrong?

bluestar714 commented 1 year ago

I have tried to reflect the latest master commit, but the event continues.

If possible, could you please tell me the value of the sdkconfig you are using?

Also, below is my sdkconfig and sdkconfig.defaults.esp32s3, are there any suspicious settings? Could you please check them?

# sdkconfig.txt

TinyUSB Stack

# sdkconfig.txt

CONFIG_TINYUSB=y CONFIG_TINYUSB_DEBUG_LEVEL=0

#

USB Network Class (RNDIS, ECM)

# CONFIG_TINYUSB_NET_ENABLED=y CONFIG_TINYUSB_RNDIS_VENDOR="Espressif Incorporated" CONFIG_TINYUSB_NET_ECM=y

#

ESP32S3-Specific

# CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240=y CONFIG_ESP32S3_DEFAULT_CPU_FREQ_MHZ=240

CONFIG_USB_TASK_CORE_ID=1

#

USB Host CDC options

# CONFIG_CDC_SEND_DTE_ACTIVE=n CONFIG_CDC_BULK_OUT_URB_BUFFER_SIZE=1536 CONFIG_CDC_BULK_IN_URB_BUFFER_SIZE=1536 CONFIG_CDC_BULK_IN_URB_NUM=4 CONFIG_CDC_BULK_OUT_URB_NUM=4



- sdkconfig
[sdkconfig.txt](https://github.com/espressif/esp-iot-bridge/files/11280531/sdkconfig.txt)
tswen commented 1 year ago

Have you tried using a Linux computer? TINYUSB_NET_ECM

bluestar714 commented 1 year ago

I created a linux (ubuntu 22.04) environment with a VM and tried it. Ping from the USB side to the PC connected to SoftAP is "no respond found!", but the ping returned on the other side(the PC connected to SoftAP to the USB-ECM).

The environment is as follows.

$ uname -a
Linux virtual-machine 5.19.0-38-generic #39~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Mar 17 21:16:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
tswen commented 1 year ago

Without modifying any code in the bridge, the test works as follows

27 17.935125342 192.168.5.2 192.168.6.2 ICMP 98 Echo (ping) reply id=0x0007, seq=11/2816, ttl=63 (request in 26) 28 18.609012242 192.168.6.2 192.168.5.2 ICMP 98 Echo (ping) request id=0x0007, seq=12/3072, ttl=64 (reply in 29) 29 18.635965777 192.168.5.2 192.168.6.2 ICMP 98 Echo (ping) reply id=0x0007, seq=12/3072, ttl=63 (request in 28)

bluestar714 commented 1 year ago

Can 192.168.5.2 and 192.168.6.2 be successfully pinged in both directions?

By the way, I am using esp-idf version release/v5.0, is it another version?

tswen commented 1 year ago

Yes, you can add the following patch. release/v5.0 is OK. https://github.com/espressif/esp-iot-bridge/issues/42#issuecomment-1498611006

bluestar714 commented 1 year ago

I have confirmed that the latest master has the patch! Do you need to configure Static Routing? (route add command etc.)

tswen commented 1 year ago

Not required, all dynamically assigned.

bluestar714 commented 12 months ago

Hmmm, I've tried a clean environment and tweaking sdkconfig, but it doesn't seem to return pings across subnets (both from 192.168.4.2 to 192.168.5.2 and from 192.168.5.2 to 192.168.4.2).

I have a softap network that seems to be 192.168.4.x/24, and I am thinking of assigning an RNDIS netif to this, but is there any ESP-IDF spec issue?

wuyuanyi135 commented 12 months ago

@bluestar714 Do you have multiple network interface on your host PC? i.e. multiple default gateways. If so, you have to either 1. route add the static route entry or 2. use DHCP option to pass the route to the host PC automatically.

bluestar714 commented 11 months ago

@wuyuanyi135 Thank you for your reply.

My Windows PC had multiple Network Adapters and multiple default gateways. Therefore, once I disabled the other NICs and looked at the communication situation. I tried setting up individual static routes as shown in the route table below, but so far I am unable to communicate from RNDIS (192.168.5.2) to 192.168.4.1 and 192.168.4.2 (SoftAP's clients). Strangely enough, I can communicate from 192.168.4.2 (SoftAP client) to 192.168.5.1. However, I am having trouble seeing any noticeable difference in route print.


IPv4 route table
===========================================================================
Active route:
network destination netmask gateway interface metric

          0.0.0.0          0.0.0.0      192.168.5.1      192.168.5.2     55
        127.0.0.0        255.0.0.0            On-Link         127.0.0.1    331
        127.0.0.1  255.255.255.255            On-Link          127.0.0.1    331
  127.255.255.255  255.255.255.255            On-Link          127.0.0.1    331
      192.168.4.0    255.255.255.0            On-Link        192.168.5.2     56
      192.168.4.0    255.255.255.0      192.168.5.1      192.168.5.2     56
      192.168.4.0  255.255.255.255            On-Link        192.168.5.2     56
      192.168.4.1  255.255.255.255           On-Link        192.168.5.2     56
      192.168.4.2  255.255.255.255            On-Link       192.168.5.2     56
    192.168.4.255  255.255.255.255            On-Link        192.168.5.2    311
      192.168.5.0    255.255.255.0            On-Link        192.168.5.2    311
      192.168.5.2  255.255.255.255            On-Link        192.168.5.2    311
    192.168.5.255  255.255.255.255            On-Link        192.168.5.2    311
        224.0.0.0        240.0.0.0            On-Link          127.0.0.1    331
        224.0.0.0        240.0.0.0            On-Link        192.168.5.2    311
  255.255.255.255  255.255.255.255            On-Link          127.0.0.1    331
  255.255.255.255  255.255.255.255            On-Link        192.168.5.2    311
wuyuanyi135 commented 11 months ago

IIRC it is normal behavior if you couldn't access the gateway of the softAP. Does it work if you connect to the RNDIS gateway? i.e. 192.168.5.1.

bluestar714 commented 11 months ago

It is possible to connect (ping) to 192.168.5.1 from the RNDIS NIC (192.168.5.2). It is also possible to connect to 192.168.5.1 from a client (192.168.4.2) connected to SoftAP.

wuyuanyi135 commented 11 months ago

The route should work if it was configured like this. Do you think the firewall is to blame? try disable it. Also, if memory serves, the PING didn't work for me when I was writing this library. try connect a phone or something else to the AP and run a http server, see if it connects. ICMP didnt work for me.

bluestar714 commented 11 months ago

Thank you very much. Your earlier reply contained very suggestive information.

if memory serves, the PING didn't work for me when I was writing this library. try connect a phone or something else to the AP and run a http server, see if it connects. ICMP didnt work for me.

I have concluded that for RNDIS connections, the ping does not go through (even if the firewall setting is turned off).

I am planning to use OSC on a WiFi dongle and for this it seems to connect without any problem. (I could also do HTTP Request).

Here is a summary of what I have done

  1. Set esp-idf version to 5.0

  2. rewrite ipv4.cpp in esp-idf https://github.com/espressif/esp-iot-bridge/issues/42#issuecomment-1498611006

  3. Enable RNDIS

  4. Configure static route

route ADD 192.168.4.0 MASK 255.255.255.0 192.168.5.1