From fa608e9c6504155b290244191c89648c9536717c Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Sun, 1 May 2022 09:27:54 +0200 Subject: [PATCH 1/3] IPv4/Single: Update ARP table from gratuitous ARP packets --- FreeRTOS_ARP.c | 87 +++++++++++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 36 deletions(-) diff --git a/FreeRTOS_ARP.c b/FreeRTOS_ARP.c index 9d2a6a3fea..746b957d98 100644 --- a/FreeRTOS_ARP.c +++ b/FreeRTOS_ARP.c @@ -248,43 +248,58 @@ eFrameProcessingResult_t eARPProcessPacket( ARPPacket_t * const pxARPFrame ) case ipARP_REQUEST: /* The packet contained an ARP request. Was it for the IP - * address of the node running this code? And does the MAC - * address claim that it is coming from this device itself? */ - if( ( ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER ) && - ( memcmp( ( void * ) ipLOCAL_MAC_ADDRESS, - ( void * ) ( pxARPHeader->xSenderHardwareAddress.ucBytes ), - ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) ) + * address of the node running this code? */ + if( ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER ) { - iptraceSENDING_ARP_REPLY( ulSenderProtocolAddress ); - - /* The request is for the address of this node. Add the - * entry into the ARP cache, or refresh the entry if it - * already exists. */ - vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress ); - - /* Generate a reply payload in the same buffer. */ - pxARPHeader->usOperation = ( uint16_t ) ipARP_REPLY; - - ( void ) memcpy( &( pxARPHeader->xTargetHardwareAddress ), - &( pxARPHeader->xSenderHardwareAddress ), - sizeof( MACAddress_t ) ); - - pxARPHeader->ulTargetProtocolAddress = ulSenderProtocolAddress; - - /* - * Use helper variables for memcpy() to remain - * compliant with MISRA Rule 21.15. These should be - * optimized away. - */ - pvCopySource = ipLOCAL_MAC_ADDRESS; - pvCopyDest = pxARPHeader->xSenderHardwareAddress.ucBytes; - ( void ) memcpy( pvCopyDest, pvCopySource, sizeof( MACAddress_t ) ); - - pvCopySource = ipLOCAL_IP_ADDRESS_POINTER; - pvCopyDest = pxARPHeader->ucSenderProtocolAddress; - ( void ) memcpy( pvCopyDest, pvCopySource, sizeof( pxARPHeader->ucSenderProtocolAddress ) ); - - eReturn = eReturnEthernetFrame; + /* The packet contained an ARP request. Was it for the IP + * address of the node running this code? And does the MAC + * address claim that it is coming from this device itself? */ + if( ( ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER ) && + ( memcmp( ( void * ) ipLOCAL_MAC_ADDRESS, + ( void * ) ( pxARPHeader->xSenderHardwareAddress.ucBytes ), + ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) ) + { + iptraceSENDING_ARP_REPLY( ulSenderProtocolAddress ); + + /* The request is for the address of this node. Add the + * entry into the ARP cache, or refresh the entry if it + * already exists. */ + vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress ); + + /* Generate a reply payload in the same buffer. */ + pxARPHeader->usOperation = ( uint16_t ) ipARP_REPLY; + + ( void ) memcpy( &( pxARPHeader->xTargetHardwareAddress ), + &( pxARPHeader->xSenderHardwareAddress ), + sizeof( MACAddress_t ) ); + + pxARPHeader->ulTargetProtocolAddress = ulSenderProtocolAddress; + + /* + * Use helper variables for memcpy() to remain + * compliant with MISRA Rule 21.15. These should be + * optimized away. + */ + pvCopySource = ipLOCAL_MAC_ADDRESS; + pvCopyDest = pxARPHeader->xSenderHardwareAddress.ucBytes; + ( void ) memcpy( pvCopyDest, pvCopySource, sizeof( MACAddress_t ) ); + + pvCopySource = ipLOCAL_IP_ADDRESS_POINTER; + pvCopyDest = pxARPHeader->ucSenderProtocolAddress; + ( void ) memcpy( pvCopyDest, pvCopySource, sizeof( pxARPHeader->ucSenderProtocolAddress ) ); + + eReturn = eReturnEthernetFrame; + } + } + else if( ulSenderProtocolAddress == ulTargetProtocolAddress ) /* Gratuitous ARP request? */ + { + /* The request is a Gratuitous ARP message. + * Refresh the entry if it already exists. */ + /* Determine the ARP cache status for the requested IP address. */ + if( eARPGetCacheEntry( &( ulSenderProtocolAddress ), &( xHardwareAddress ) ) == eARPCacheHit ) + { + vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress ); + } } break; From d34601623e8873f05400a85f3377b32d00ef641c Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Sun, 1 May 2022 22:46:07 +0200 Subject: [PATCH 2/3] Removed an unnecessary test --- FreeRTOS_ARP.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/FreeRTOS_ARP.c b/FreeRTOS_ARP.c index 746b957d98..618dd4b4a4 100644 --- a/FreeRTOS_ARP.c +++ b/FreeRTOS_ARP.c @@ -251,13 +251,11 @@ eFrameProcessingResult_t eARPProcessPacket( ARPPacket_t * const pxARPFrame ) * address of the node running this code? */ if( ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER ) { - /* The packet contained an ARP request. Was it for the IP - * address of the node running this code? And does the MAC - * address claim that it is coming from this device itself? */ - if( ( ulTargetProtocolAddress == *ipLOCAL_IP_ADDRESS_POINTER ) && - ( memcmp( ( void * ) ipLOCAL_MAC_ADDRESS, - ( void * ) ( pxARPHeader->xSenderHardwareAddress.ucBytes ), - ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) ) + /* Does the MAC address claim that it is coming + * from this device itself? */ + if( memcmp( ( void * ) ipLOCAL_MAC_ADDRESS, + ( void * ) ( pxARPHeader->xSenderHardwareAddress.ucBytes ), + ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) { iptraceSENDING_ARP_REPLY( ulSenderProtocolAddress ); From 1c8b20b9bdc47e0fc2cd8f802503419edcbd57a3 Mon Sep 17 00:00:00 2001 From: Hein Tibosch Date: Sun, 15 May 2022 21:07:29 +0200 Subject: [PATCH 3/3] Added declaration of xHardwareAddress --- FreeRTOS_ARP.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/FreeRTOS_ARP.c b/FreeRTOS_ARP.c index 618dd4b4a4..e8e89564d6 100644 --- a/FreeRTOS_ARP.c +++ b/FreeRTOS_ARP.c @@ -291,6 +291,8 @@ eFrameProcessingResult_t eARPProcessPacket( ARPPacket_t * const pxARPFrame ) } else if( ulSenderProtocolAddress == ulTargetProtocolAddress ) /* Gratuitous ARP request? */ { + MACAddress_t xHardwareAddress; + /* The request is a Gratuitous ARP message. * Refresh the entry if it already exists. */ /* Determine the ARP cache status for the requested IP address. */