From cd489cebd11377a740983faab0eb24b53dc8f03a Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Mon, 15 Nov 2021 16:04:13 +0100 Subject: [PATCH] cdc_ecm: Truncate frames at max ethernet size This truncates the incomming frames to ETHERNET_FRAME_LEN and silently discards the rest of the frame until the end of the frame. This should be modified to an endpoint halt condition after #17090 is merged, but for now this should be good enough. Stalling the endpoint with the current stall implementation could cause a ping of death scenario, so for now the data is truncated until the above solution can be implemented. --- sys/usb/usbus/cdc/ecm/cdc_ecm.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/sys/usb/usbus/cdc/ecm/cdc_ecm.c b/sys/usb/usbus/cdc/ecm/cdc_ecm.c index f580209e03..376ce45087 100644 --- a/sys/usb/usbus/cdc/ecm/cdc_ecm.c +++ b/sys/usb/usbus/cdc/ecm/cdc_ecm.c @@ -307,15 +307,16 @@ static void _handle_rx_flush_ev(event_t *ev) _handle_rx_flush(cdcecm); } -static void _store_frame_chunk(usbus_cdcecm_device_t *cdcecm) +static void _store_frame_chunk(usbus_cdcecm_device_t *cdcecm, size_t len) { uint8_t *buf = cdcecm->ep_out->ep->buf; - size_t len = 0; - usbdev_ep_get(cdcecm->ep_out->ep, USBOPT_EP_AVAILABLE, &len, - sizeof(size_t)); - memcpy(cdcecm->in_buf + cdcecm->len, buf, len); - cdcecm->len += len; + /* Truncate if it exceeds the expected MTU size. */ + /* TODO: Should be converted to an endpoint halt after #17090 is merged */ + if (cdcecm->len + len < ETHERNET_FRAME_LEN) { + memcpy(cdcecm->in_buf + cdcecm->len, buf, len); + cdcecm->len += len; + } if (len < USBUS_CDCECM_EP_DATA_SIZE) { netdev_trigger_event_isr(&cdcecm->netdev); } @@ -334,7 +335,7 @@ static void _transfer_handler(usbus_t *usbus, usbus_handler_t *handler, } size_t len = 0; usbdev_ep_get(ep, USBOPT_EP_AVAILABLE, &len, sizeof(size_t)); - _store_frame_chunk(cdcecm); + _store_frame_chunk(cdcecm, len); if (len == USBUS_CDCECM_EP_DATA_SIZE) { usbdev_ep_ready(ep, 0); }