From f82f3d7edf62781ee6f55d948cf6c9d4c51bd8ea Mon Sep 17 00:00:00 2001 From: robert Date: Thu, 26 Nov 2026 15:55:37 +0200 Subject: [PATCH] Fix Windows, initialize filter setting by default --- mongoose.c | 11 +++-- src/drivers/usb.c | 11 +++-- test/Makefile | 10 ++++- test/usb_test.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 test/usb_test.c diff --git a/mongoose.c b/mongoose.c index de5d7b10..72ebc44e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -37898,10 +37898,11 @@ static const uint8_t s_ntb_params[] = { 4, 0, 16, 0}; // Link notifications: speed 12 Mbit/s both ways, connected, disconnected -static const uint8_t s_speed[] = {0xa1, 0x2a, 0, 0, 0, 0, 8, 0, - 0, 0x1b, 0xb7, 0, 0, 0x1b, 0xb7, 0}; -static const uint8_t s_up[] = {0xa1, 0, 1, 0, 0, 0, 0, 0}; -static const uint8_t s_down[] = {0xa1, 0, 0, 0, 0, 0, 0, 0}; +static const uint8_t s_speed[] = { + 0xa1, 0x2a, 0, 0, MGI_USB_ETH_IF, 0, 8, 0, + 0, 0x1b, 0xb7, 0, 0, 0x1b, 0xb7, 0}; +static const uint8_t s_up[] = {0xa1, 0, 1, 0, MGI_USB_ETH_IF, 0, 0, 0}; +static const uint8_t s_down[] = {0xa1, 0, 0, 0, MGI_USB_ETH_IF, 0, 0, 0}; // Transfer block header for one frame: NTH16, NDP16 with one datagram at // offset 30, so that the IP header after the 14-byte Ethernet header lands @@ -38059,6 +38060,8 @@ static int mgi_usb_request(const uint8_t *r, const void **reply) { #if MG_ENABLE_USB_ETHERNET if (index == MG_USB_IF_ETH_DATA) { s_usb.alt = value == 1, s_usb.notify = 1; + // Directed and broadcast by default: Windows may not set a filter + if (s_usb.alt) s_usb.filter = 0x0c; mgi_usb_idle(MG_USB_EP_ETH_DATA); } #endif diff --git a/src/drivers/usb.c b/src/drivers/usb.c index 053b8239..92918dbb 100644 --- a/src/drivers/usb.c +++ b/src/drivers/usb.c @@ -94,10 +94,11 @@ static const uint8_t s_ntb_params[] = { 4, 0, 16, 0}; // Link notifications: speed 12 Mbit/s both ways, connected, disconnected -static const uint8_t s_speed[] = {0xa1, 0x2a, 0, 0, 0, 0, 8, 0, - 0, 0x1b, 0xb7, 0, 0, 0x1b, 0xb7, 0}; -static const uint8_t s_up[] = {0xa1, 0, 1, 0, 0, 0, 0, 0}; -static const uint8_t s_down[] = {0xa1, 0, 0, 0, 0, 0, 0, 0}; +static const uint8_t s_speed[] = { + 0xa1, 0x2a, 0, 0, MGI_USB_ETH_IF, 0, 8, 0, + 0, 0x1b, 0xb7, 0, 0, 0x1b, 0xb7, 0}; +static const uint8_t s_up[] = {0xa1, 0, 1, 0, MGI_USB_ETH_IF, 0, 0, 0}; +static const uint8_t s_down[] = {0xa1, 0, 0, 0, MGI_USB_ETH_IF, 0, 0, 0}; // Transfer block header for one frame: NTH16, NDP16 with one datagram at // offset 30, so that the IP header after the 14-byte Ethernet header lands @@ -255,6 +256,8 @@ static int mgi_usb_request(const uint8_t *r, const void **reply) { #if MG_ENABLE_USB_ETHERNET if (index == MG_USB_IF_ETH_DATA) { s_usb.alt = value == 1, s_usb.notify = 1; + // Directed and broadcast by default: Windows may not set a filter + if (s_usb.alt) s_usb.filter = 0x0c; mgi_usb_idle(MG_USB_EP_ETH_DATA); } #endif diff --git a/test/Makefile b/test/Makefile index 39a028aa..6f23d7e5 100644 --- a/test/Makefile +++ b/test/Makefile @@ -30,7 +30,7 @@ COMMON_CFLAGS ?= $(C_WARN) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -pthread CFLAGS ?= $(OPTS) $(ASAN) $(COMMON_CFLAGS) VALGRIND_CFLAGS ?= $(OPTS) $(COMMON_CFLAGS) VALGRIND_RUN ?= valgrind --tool=memcheck --gen-suppressions=all --leak-check=full --show-leak-kinds=all --leak-resolution=high --track-origins=yes --error-exitcode=1 --exit-on-first-error=yes --fair-sched=yes -.PHONY: tutorials mip_test eth_test ppp_test atcmd_test eth_vc98 ppp_vc98 atcmd_vc98 eth_s390 ppp_s390 atcmd_s390 test valgrind +.PHONY: tutorials mip_test eth_test usb_test ppp_test atcmd_test eth_vc98 ppp_vc98 atcmd_vc98 eth_s390 ppp_s390 atcmd_s390 test valgrind ifeq "$(findstring ++,$(CC))" "" # $(CC) does not end with ++, i.e. we're using C. Apply C flags @@ -88,7 +88,7 @@ all: tall: mg_prefix unamalgamated test valgrind fuzz fuzz_tls vc98 vc17 vc22 mingw mingw++ arm armhf riscv \ diabish ppc_diabish s390 ppc mips tutorials tutorials_win tutorials_embedded -tmip: mip_test eth_test ppp_test atcmd_test mip_tap_test pppoe_tap_test mip_port_test mip_diabish \ +tmip: mip_test eth_test usb_test ppp_test atcmd_test mip_tap_test pppoe_tap_test mip_port_test mip_diabish \ mip_vc98 eth_vc98 ppp_vc98 atcmd_vc98 mip_ppc_diabish mip_s390 eth_s390 ppp_s390 \ atcmd_s390 mip_port_s390 mip_ppc eth_ppc ppp_ppc atcmd_ppc mip_port_ppc mip_mips \ eth_mips ppp_mips atcmd_mips mip_port_mips @@ -269,6 +269,12 @@ eth_test: eth_test.c mongoose.c mongoose.h Makefile $(CC) eth_test.c $(CFLAGS) $(LDFLAGS) -o $@ ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@ +usb_test: DEFS += -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ENABLE_USB_ETHERNET=1 -DMG_ENABLE_USB_SERIAL=1 +usb_test: usb_test.c ../src/drivers/usb.c mongoose.c mongoose.h Makefile + $(CC) mongoose.c $(CFLAGS) -c -o usb_test_core.o + $(CC) usb_test.c ../src/drivers/usb.c usb_test_core.o $(CFLAGS) -I../src -DMG_ENABLE_DRIVER_USB_DWC2=1 -Wno-sign-conversion $(LDFLAGS) -o $@ + ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@ + ppp_test: ppp_test.c mongoose.c mongoose.h Makefile $(CC) ppp_test.c $(CFLAGS) $(LDFLAGS) -o $@ ASAN_OPTIONS=$(ASAN_OPTIONS) $(RUN) ./$@ diff --git a/test/usb_test.c b/test/usb_test.c new file mode 100644 index 00000000..f9f07423 --- /dev/null +++ b/test/usb_test.c @@ -0,0 +1,107 @@ +#define MG_ENABLE_TCPIP 1 +#define MG_ENABLE_TCPIP_DRIVER_INIT 0 +#define MG_ENABLE_ASSERT 1 +#define MG_ENABLE_DRIVER_USB_DWC2 1 +#define MG_ENABLE_USB_ETHERNET 1 +#ifndef MG_ENABLE_USB_SERIAL +#define MG_ENABLE_USB_SERIAL 1 +#endif + +#include "mongoose.h" + +// Exercise the USB protocol through a simulated controller, without MMIO +static struct mg_tcpip_if s_if; +static bool s_pending; +static enum mg_usb_ev s_ev; +static enum mg_usb_ep s_ep; +static uint8_t s_request[8], s_packet[64]; +static size_t s_length, s_packet_length; +static int s_result; +static unsigned s_frames, s_notifications; + +static void usb_init(char serial[17]) { + memcpy(serial, "0123456789ABCDEF", 17); +} + +static void usb_poll(int (*fn)(enum mg_usb_ev, enum mg_usb_ep, void *, size_t)) { + if (s_pending) { + s_pending = false; + s_result = fn(s_ev, s_ep, s_request, s_length); + } +} + +static void usb_in(enum mg_usb_ep ep, const void *buf, size_t len) { + if (ep == MG_USB_EP_ETH_DATA) s_frames++; + if (ep == MG_USB_EP_ETH_EV) { + assert(len <= sizeof(s_packet)); + memcpy(s_packet, buf, len); + s_packet_length = len; + s_notifications++; + } +} + +static bool event(enum mg_usb_ev ev, enum mg_usb_ep ep, size_t len) { + s_ev = ev, s_ep = ep, s_length = len, s_pending = true; + return mg_usb_poll(&s_if, true); +} + +static bool request(uint8_t type, uint8_t code, uint16_t value, + uint16_t index) { + s_request[0] = type, s_request[1] = code; + MG_STORE_LE16(s_request + 2, value); + MG_STORE_LE16(s_request + 4, index); + MG_STORE_LE16(s_request + 6, 0); + bool up = event(MG_USB_EV_SETUP, MG_USB_EP_CTL, sizeof(s_request)); + assert(s_result == 0); + return up; +} + +int main(void) { + static const struct mg_usb_hw hw = { + .init = usb_init, .poll = usb_poll, .in = usb_in}; + const uint8_t frame[14] = {0}; + const uint16_t control = MG_USB_IF_ETH_DATA - 1; + + assert(mg_usb_init(&s_if, &hw)); + assert(!event(MG_USB_EV_RESET, MG_USB_EP_CTL, 0)); + assert(!request(0, 9, 1, 0)); // SET_CONFIGURATION + assert(mg_usb_tx(frame, sizeof(frame), &s_if) == 0); + + // Windows opens NCM without sending SET_ETHERNET_PACKET_FILTER + assert(request(1, 11, 1, MG_USB_IF_ETH_DATA)); + assert(mg_usb_tx(frame, sizeof(frame), &s_if) == sizeof(frame)); + assert(s_frames == 1); + assert(s_notifications == 1 && s_packet_length == 16); + assert(s_packet[1] == 0x2a && MG_LOAD_LE16(s_packet + 4) == control); + assert(event(MG_USB_EV_IN, MG_USB_EP_ETH_EV, 0)); + assert(s_notifications == 2 && s_packet_length == 8); + assert(s_packet[1] == 0 && MG_LOAD_LE16(s_packet + 2) == 1); + assert(MG_LOAD_LE16(s_packet + 4) == control); + assert(event(MG_USB_EV_IN, MG_USB_EP_ETH_EV, 0)); + assert(event(MG_USB_EV_IN, MG_USB_EP_ETH_DATA, 0)); + + // Linux can still set a filter; an explicit zero suppresses traffic + assert(!request(0x21, 0x43, 0, control)); + assert(mg_usb_tx(frame, sizeof(frame), &s_if) == 0); + assert(request(0x21, 0x43, 0x0c, control)); + assert(mg_usb_tx(frame, sizeof(frame), &s_if) == sizeof(frame)); + assert(s_frames == 2); + assert(event(MG_USB_EV_IN, MG_USB_EP_ETH_DATA, 0)); + + // Closing/reopening restores the default, including after a zero filter + assert(!request(0x21, 0x43, 0, control)); + assert(!request(1, 11, 0, MG_USB_IF_ETH_DATA)); + assert(s_notifications == 3 && s_packet_length == 8); + assert(s_packet[1] == 0 && MG_LOAD_LE16(s_packet + 2) == 0); + assert(MG_LOAD_LE16(s_packet + 4) == control); + assert(!event(MG_USB_EV_IN, MG_USB_EP_ETH_EV, 0)); + assert(mg_usb_tx(frame, sizeof(frame), &s_if) == 0); + assert(request(1, 11, 1, MG_USB_IF_ETH_DATA)); + assert(mg_usb_tx(frame, sizeof(frame), &s_if) == sizeof(frame)); + assert(s_frames == 3); + + assert(!event(MG_USB_EV_RESET, MG_USB_EP_CTL, 0)); + assert(mg_usb_tx(frame, sizeof(frame), &s_if) == 0); + puts("USB NCM tests passed"); + return 0; +}