Skip to content

Commit 7c8f339

Browse files
cruxicadambvidex
andauthored
Bugfix in ATSAME5x network interface. Incorrect detection of ICMP (#1194)
Bugfix in ATSAME5x network interface. Incorrect detection of ICMP packets when CRC offloading is enabled. Co-authored-by: Adam B. <adamb@videx.com>
1 parent fa073ea commit 7c8f339

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

source/portable/NetworkInterface/ATSAME5x/NetworkInterface.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@
9090
static uint8_t ucLLMNR_MAC_address[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0xFC };
9191
#endif
9292

93+
/* Check if the raw Ethernet frame is ICMP */
94+
static BaseType_t isICMP( const NetworkBufferDescriptor_t * pxDescriptor );
95+
9396
/* Receive task refresh time */
9497
#define RECEIVE_BLOCK_TIME_MS 100
9598

@@ -272,14 +275,41 @@ BaseType_t xATSAM5x_NetworkInterfaceInitialise( NetworkInterface_t * pxInterface
272275
return xATSAM5x_PHYGetLinkStatus( NULL );
273276
}
274277

278+
/* Check if the raw Ethernet frame is ICMP */
279+
static BaseType_t isICMP( const NetworkBufferDescriptor_t * pxDescriptor )
280+
{
281+
BaseType_t xReturn = pdFALSE;
282+
283+
const IPPacket_t * pkt = ( const IPPacket_t * ) pxDescriptor->pucEthernetBuffer;
284+
285+
if( pkt->xEthernetHeader.usFrameType == ipIPv4_FRAME_TYPE )
286+
{
287+
if( pkt->xIPHeader.ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP )
288+
{
289+
xReturn = pdTRUE;
290+
}
291+
}
292+
293+
#if ipconfigUSE_IPv6 != 0
294+
else if( pkt->xEthernetHeader.usFrameType == ipIPv6_FRAME_TYPE )
295+
{
296+
ICMPPacket_IPv6_t * icmp6 = ( ICMPPacket_IPv6_t * ) pxDescriptor->pucEthernetBuffer;
297+
298+
if( icmp6->xIPHeader.ucNextHeader == ipPROTOCOL_ICMP_IPv6 )
299+
{
300+
xReturn = pdTRUE;
301+
}
302+
}
303+
#endif
304+
return xReturn;
305+
}
275306

276307
static void prvEMACDeferredInterruptHandlerTask( void * pvParameters )
277308
{
278309
NetworkBufferDescriptor_t * pxBufferDescriptor;
279310
size_t xBytesReceived = 0, xBytesRead = 0;
280311

281312
uint16_t xICMPChecksumResult = ipCORRECT_CRC;
282-
const IPPacket_t * pxIPPacket;
283313

284314

285315
/* Used to indicate that xSendEventStructToIPTask() is being called because
@@ -336,15 +366,13 @@ static void prvEMACDeferredInterruptHandlerTask( void * pvParameters )
336366
{
337367
/* the Atmel SAM GMAC peripheral does not support hardware CRC offloading for ICMP packets.
338368
* It must therefore be implemented in software. */
339-
pxIPPacket = ( IPPacket_t const * ) pxBufferDescriptor->pucEthernetBuffer;
340-
341-
if( pxIPPacket->xIPHeader.ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP )
369+
if( isICMP( pxBufferDescriptor ) == pdTRUE )
342370
{
343371
xICMPChecksumResult = usGenerateProtocolChecksum( pxBufferDescriptor->pucEthernetBuffer, pxBufferDescriptor->xDataLength, pdFALSE );
344372
}
345373
else
346374
{
347-
xICMPChecksumResult = ipCORRECT_CRC; /* Reset the result value in case this is not an ICMP packet. */
375+
xICMPChecksumResult = ipCORRECT_CRC; /* Checksum already verified by GMAC */
348376
}
349377
}
350378
#endif /* if ( ipconfigDRIVER_INCLUDED_RX_IP_CHECKSUM == 1 ) */
@@ -440,9 +468,7 @@ BaseType_t xATSAM5x_NetworkInterfaceOutput( NetworkInterface_t * pxInterface,
440468
{
441469
/* the Atmel SAM GMAC peripheral does not support hardware CRC offloading for ICMP packets.
442470
* It must therefore be implemented in software. */
443-
const IPPacket_t * pxIPPacket = ( IPPacket_t const * ) pxDescriptor->pucEthernetBuffer;
444-
445-
if( pxIPPacket->xIPHeader.ucProtocol == ( uint8_t ) ipPROTOCOL_ICMP )
471+
if( isICMP( pxDescriptor ) == pdTRUE )
446472
{
447473
( void ) usGenerateProtocolChecksum( pxDescriptor->pucEthernetBuffer, pxDescriptor->xDataLength, pdTRUE );
448474
}

0 commit comments

Comments
 (0)