From e3092026c448a24c009c70de0494142f29af5285 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Thu, 3 Aug 2023 18:39:46 +0530 Subject: [PATCH 01/11] adding more checks for GARP --- source/FreeRTOS_ARP.c | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 252e38ab0..90b145fcb 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -304,20 +304,25 @@ static TickType_t xLastGratuitousARPTime = 0U; } else if( ulSenderProtocolAddress == ulTargetProtocolAddress ) /* Gratuitous ARP request? */ { - MACAddress_t xHardwareAddress; - NetworkEndPoint_t * pxCachedEndPoint; + MACAddress_t xGARPBroadcastAddress = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; + if ((memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPBroadcastAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES) == 0) && + ( memcmp( ( void * ) pxTargetEndPoint->xMACAddress.ucBytes, ( pxARPHeader->xSenderHardwareAddress.ucBytes ), ipMAC_ADDRESS_LENGTH_BYTES ) != 0 )) + { + MACAddress_t xHardwareAddress; + NetworkEndPoint_t * pxCachedEndPoint; - pxCachedEndPoint = NULL; + pxCachedEndPoint = NULL; - /* 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 ), &( pxCachedEndPoint ) ) == eARPCacheHit ) - { - /* Check if the endpoint matches with the one present in the ARP cache */ - if( pxCachedEndPoint == pxTargetEndPoint ) + /* 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 ), &( pxCachedEndPoint ) ) == eARPCacheHit ) { - vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress, pxTargetEndPoint ); + /* Check if the endpoint matches with the one present in the ARP cache */ + if( pxCachedEndPoint == pxTargetEndPoint ) + { + vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress, pxTargetEndPoint ); + } } } } From e1384edb0d1426b13a03fb2c86014e4c8ab90641 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Thu, 3 Aug 2023 19:03:00 +0530 Subject: [PATCH 02/11] adding more checks for GARP --- source/FreeRTOS_ARP.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 90b145fcb..1948ff11c 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -305,6 +305,30 @@ static TickType_t xLastGratuitousARPTime = 0U; else if( ulSenderProtocolAddress == ulTargetProtocolAddress ) /* Gratuitous ARP request? */ { MACAddress_t xGARPBroadcastAddress = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; + + FreeRTOS_printf(( "Gratuitous ARP ulSenderProtocolAddress: %xip \n", ( unsigned ) FreeRTOS_ntohl( ulSenderProtocolAddress ) )); + FreeRTOS_printf(( "Gratuitous ARP xTargetHardwareAddress: %x:%x:%x:%x:%x:%x\n", + pxARPHeader->xTargetHardwareAddress.ucBytes[0], + pxARPHeader->xTargetHardwareAddress.ucBytes[1], + pxARPHeader->xTargetHardwareAddress.ucBytes[2], + pxARPHeader->xTargetHardwareAddress.ucBytes[3], + pxARPHeader->xTargetHardwareAddress.ucBytes[4], + pxARPHeader->xTargetHardwareAddress.ucBytes[5])); + FreeRTOS_printf(( "Gratuitous ARP xTargetEndPoint->xMACAddress.ucBytes: %x:%x:%x:%x:%x:%x\n", + pxTargetEndPoint->xMACAddress.ucBytes[0], + pxTargetEndPoint->xMACAddress.ucBytes[1], + pxTargetEndPoint->xMACAddress.ucBytes[2], + pxTargetEndPoint->xMACAddress.ucBytes[3], + pxTargetEndPoint->xMACAddress.ucBytes[4], + pxTargetEndPoint->xMACAddress.ucBytes[5])); + FreeRTOS_printf(( "Gratuitous ARP pxARPHeader->xSenderHardwareAddress.ucBytes: %x:%x:%x:%x:%x:%x\n", + pxARPHeader->xSenderHardwareAddress.ucBytes[0], + pxARPHeader->xSenderHardwareAddress.ucBytes[1], + pxARPHeader->xSenderHardwareAddress.ucBytes[2], + pxARPHeader->xSenderHardwareAddress.ucBytes[3], + pxARPHeader->xSenderHardwareAddress.ucBytes[4], + pxARPHeader->xSenderHardwareAddress.ucBytes[5])); + if ((memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPBroadcastAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES) == 0) && ( memcmp( ( void * ) pxTargetEndPoint->xMACAddress.ucBytes, ( pxARPHeader->xSenderHardwareAddress.ucBytes ), ipMAC_ADDRESS_LENGTH_BYTES ) != 0 )) { From c0440332839869635aac72989b5fe30a4e978753 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Thu, 3 Aug 2023 21:28:17 +0530 Subject: [PATCH 03/11] adding more checks for GARP --- source/FreeRTOS_ARP.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 1948ff11c..e50ef0222 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -304,7 +304,8 @@ static TickType_t xLastGratuitousARPTime = 0U; } else if( ulSenderProtocolAddress == ulTargetProtocolAddress ) /* Gratuitous ARP request? */ { - MACAddress_t xGARPBroadcastAddress = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; + MACAddress_t xGARPBroadcastAddress_1 = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; + MACAddress_t xGARPBroadcastAddress_2 = {{0, 0, 0, 0, 0, 0}}; FreeRTOS_printf(( "Gratuitous ARP ulSenderProtocolAddress: %xip \n", ( unsigned ) FreeRTOS_ntohl( ulSenderProtocolAddress ) )); FreeRTOS_printf(( "Gratuitous ARP xTargetHardwareAddress: %x:%x:%x:%x:%x:%x\n", @@ -329,7 +330,8 @@ static TickType_t xLastGratuitousARPTime = 0U; pxARPHeader->xSenderHardwareAddress.ucBytes[4], pxARPHeader->xSenderHardwareAddress.ucBytes[5])); - if ((memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPBroadcastAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES) == 0) && + if (((memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPBroadcastAddress_1.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES) == 0) || + ((memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPBroadcastAddress_2.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES) == 0))) && ( memcmp( ( void * ) pxTargetEndPoint->xMACAddress.ucBytes, ( pxARPHeader->xSenderHardwareAddress.ucBytes ), ipMAC_ADDRESS_LENGTH_BYTES ) != 0 )) { MACAddress_t xHardwareAddress; From 5b464c8bf11633b22348aa14915264f6e860634e Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 09:00:31 +0530 Subject: [PATCH 04/11] more debug logs --- source/FreeRTOS_ARP.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index e50ef0222..10efac82c 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -339,6 +339,8 @@ static TickType_t xLastGratuitousARPTime = 0U; pxCachedEndPoint = NULL; + FreeRTOS_PrintARPCache(); + /* The request is a Gratuitous ARP message. * Refresh the entry if it already exists. */ /* Determine the ARP cache status for the requested IP address. */ @@ -348,6 +350,7 @@ static TickType_t xLastGratuitousARPTime = 0U; if( pxCachedEndPoint == pxTargetEndPoint ) { vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress, pxTargetEndPoint ); + FreeRTOS_PrintARPCache(); } } } @@ -1622,7 +1625,7 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint ) if( ( xARPCache[ x ].ulIPAddress != 0U ) && ( xARPCache[ x ].ucAge > ( uint8_t ) 0U ) ) { /* See if the MAC-address also matches, and we're all happy */ - FreeRTOS_printf( ( "ARP %2d: %3u - %16xip : %02x:%02x:%02x : %02x:%02x:%02x\n", + FreeRTOS_printf( ( "ARP %2d: %3u - %16xip : %02x:%02x:%02x : %02x:%02x:%02x Endpoint: %p\n", ( int ) x, xARPCache[ x ].ucAge, ( unsigned ) FreeRTOS_ntohl( xARPCache[ x ].ulIPAddress ), @@ -1631,7 +1634,7 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint ) xARPCache[ x ].xMACAddress.ucBytes[ 2 ], xARPCache[ x ].xMACAddress.ucBytes[ 3 ], xARPCache[ x ].xMACAddress.ucBytes[ 4 ], - xARPCache[ x ].xMACAddress.ucBytes[ 5 ] ) ); + xARPCache[ x ].xMACAddress.ucBytes[ 5 ], xARPCache[ x ].pxEndPoint ) ); xCount++; } } From b7d5a76b390c685953d917f341677c6ae5aac365 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 09:01:08 +0530 Subject: [PATCH 05/11] more debug logs --- source/FreeRTOS_ARP.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 10efac82c..a7838f074 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -1619,6 +1619,8 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint ) { BaseType_t x, xCount = 0; + FreeRTOS_printf(("****************************************************\n")); + /* Loop through each entry in the ARP cache. */ for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ ) { From 7c2ad54f00196f52c3d57712ef0b7c56a0a52b3d Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 10:05:42 +0530 Subject: [PATCH 06/11] more testing --- source/FreeRTOS_ARP.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index a7838f074..7db0cfa55 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -347,7 +347,7 @@ static TickType_t xLastGratuitousARPTime = 0U; if( eARPGetCacheEntry( &( ulSenderProtocolAddress ), &( xHardwareAddress ), &( pxCachedEndPoint ) ) == eARPCacheHit ) { /* Check if the endpoint matches with the one present in the ARP cache */ - if( pxCachedEndPoint == pxTargetEndPoint ) + if( (pxCachedEndPoint == pxTargetEndPoint) && (ulSenderProtocolAddress == ulTargetProtocolAddress) ) { vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress, pxTargetEndPoint ); FreeRTOS_PrintARPCache(); @@ -1074,6 +1074,8 @@ static BaseType_t prvFindCacheEntry( const MACAddress_t * pxMACAddress, * hardware address of the router, if any. */ *( ppxEndPoint ) = FreeRTOS_FindGateWay( ( BaseType_t ) ipTYPE_IPv4 ); + FreeRTOS_printf(("eARPGetCacheEntryGateWay: Re: %d Using gateway address: %p \n", ipconfigARP_STORES_REMOTE_ADDRESSES, *ppxEndPoint)); + if( *( ppxEndPoint ) != NULL ) { /* 'ipv4_settings' can be accessed safely, because 'ipTYPE_IPv4' was provided. */ From e8a587968db081394154bf680ab195f22528b1d0 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 11:31:55 +0530 Subject: [PATCH 07/11] verify subnet --- source/FreeRTOS_ARP.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 7db0cfa55..c018387ce 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -302,7 +302,8 @@ static TickType_t xLastGratuitousARPTime = 0U; eReturn = eReturnEthernetFrame; } } - else if( ulSenderProtocolAddress == ulTargetProtocolAddress ) /* Gratuitous ARP request? */ + else if( (ulSenderProtocolAddress == ulTargetProtocolAddress) && \ + ((ulSenderProtocolAddress & pxTargetEndPoint->ipv4_settings.ulNetMask) == (pxTargetEndPoint->ipv4_settings.ulNetMask & pxTargetEndPoint->ipv4_settings.ulIPAddress)) ) /* Gratuitous ARP request? */ { MACAddress_t xGARPBroadcastAddress_1 = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; MACAddress_t xGARPBroadcastAddress_2 = {{0, 0, 0, 0, 0, 0}}; @@ -347,7 +348,7 @@ static TickType_t xLastGratuitousARPTime = 0U; if( eARPGetCacheEntry( &( ulSenderProtocolAddress ), &( xHardwareAddress ), &( pxCachedEndPoint ) ) == eARPCacheHit ) { /* Check if the endpoint matches with the one present in the ARP cache */ - if( (pxCachedEndPoint == pxTargetEndPoint) && (ulSenderProtocolAddress == ulTargetProtocolAddress) ) + if(pxCachedEndPoint == pxTargetEndPoint) { vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress, pxTargetEndPoint ); FreeRTOS_PrintARPCache(); From cb82c51d49b8de7d8c48c832fad7b6645f74a4d1 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 13:38:05 +0530 Subject: [PATCH 08/11] fix formatting, cleaning up --- source/FreeRTOS_ARP.c | 51 +++++++++++-------------------------------- 1 file changed, 13 insertions(+), 38 deletions(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index c018387ce..1bfb86f4e 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -302,56 +302,31 @@ static TickType_t xLastGratuitousARPTime = 0U; eReturn = eReturnEthernetFrame; } } - else if( (ulSenderProtocolAddress == ulTargetProtocolAddress) && \ - ((ulSenderProtocolAddress & pxTargetEndPoint->ipv4_settings.ulNetMask) == (pxTargetEndPoint->ipv4_settings.ulNetMask & pxTargetEndPoint->ipv4_settings.ulIPAddress)) ) /* Gratuitous ARP request? */ + else if( ( ulSenderProtocolAddress == ulTargetProtocolAddress ) && + ( ( ulSenderProtocolAddress & pxTargetEndPoint->ipv4_settings.ulNetMask ) == ( pxTargetEndPoint->ipv4_settings.ulNetMask & pxTargetEndPoint->ipv4_settings.ulIPAddress ) ) ) /* Gratuitous ARP request? */ { - MACAddress_t xGARPBroadcastAddress_1 = {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; - MACAddress_t xGARPBroadcastAddress_2 = {{0, 0, 0, 0, 0, 0}}; - - FreeRTOS_printf(( "Gratuitous ARP ulSenderProtocolAddress: %xip \n", ( unsigned ) FreeRTOS_ntohl( ulSenderProtocolAddress ) )); - FreeRTOS_printf(( "Gratuitous ARP xTargetHardwareAddress: %x:%x:%x:%x:%x:%x\n", - pxARPHeader->xTargetHardwareAddress.ucBytes[0], - pxARPHeader->xTargetHardwareAddress.ucBytes[1], - pxARPHeader->xTargetHardwareAddress.ucBytes[2], - pxARPHeader->xTargetHardwareAddress.ucBytes[3], - pxARPHeader->xTargetHardwareAddress.ucBytes[4], - pxARPHeader->xTargetHardwareAddress.ucBytes[5])); - FreeRTOS_printf(( "Gratuitous ARP xTargetEndPoint->xMACAddress.ucBytes: %x:%x:%x:%x:%x:%x\n", - pxTargetEndPoint->xMACAddress.ucBytes[0], - pxTargetEndPoint->xMACAddress.ucBytes[1], - pxTargetEndPoint->xMACAddress.ucBytes[2], - pxTargetEndPoint->xMACAddress.ucBytes[3], - pxTargetEndPoint->xMACAddress.ucBytes[4], - pxTargetEndPoint->xMACAddress.ucBytes[5])); - FreeRTOS_printf(( "Gratuitous ARP pxARPHeader->xSenderHardwareAddress.ucBytes: %x:%x:%x:%x:%x:%x\n", - pxARPHeader->xSenderHardwareAddress.ucBytes[0], - pxARPHeader->xSenderHardwareAddress.ucBytes[1], - pxARPHeader->xSenderHardwareAddress.ucBytes[2], - pxARPHeader->xSenderHardwareAddress.ucBytes[3], - pxARPHeader->xSenderHardwareAddress.ucBytes[4], - pxARPHeader->xSenderHardwareAddress.ucBytes[5])); - - if (((memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPBroadcastAddress_1.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES) == 0) || - ((memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPBroadcastAddress_2.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES) == 0))) && - ( memcmp( ( void * ) pxTargetEndPoint->xMACAddress.ucBytes, ( pxARPHeader->xSenderHardwareAddress.ucBytes ), ipMAC_ADDRESS_LENGTH_BYTES ) != 0 )) + const MACAddress_t xGARPTargetAddress = { { 0, 0, 0, 0, 0, 0 } }; + + /* Make sure target MAC address is either ff:ff:ff:ff:ff:ff or 00:00:00:00:00:00 and senders MAC + * address is not matching with the endpoint MAC address. */ + if( ( ( memcmp( &( pxARPHeader->xTargetHardwareAddress ), xBroadcastMACAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 ) || + ( ( memcmp( &( pxARPHeader->xTargetHardwareAddress ), xGARPTargetAddress.ucBytes, ipMAC_ADDRESS_LENGTH_BYTES ) == 0 ) ) ) && + ( memcmp( ( void * ) pxTargetEndPoint->xMACAddress.ucBytes, ( pxARPHeader->xSenderHardwareAddress.ucBytes ), ipMAC_ADDRESS_LENGTH_BYTES ) != 0 ) ) { MACAddress_t xHardwareAddress; NetworkEndPoint_t * pxCachedEndPoint; pxCachedEndPoint = NULL; - FreeRTOS_PrintARPCache(); - /* The request is a Gratuitous ARP message. - * Refresh the entry if it already exists. */ + * Refresh the entry if it already exists. */ /* Determine the ARP cache status for the requested IP address. */ if( eARPGetCacheEntry( &( ulSenderProtocolAddress ), &( xHardwareAddress ), &( pxCachedEndPoint ) ) == eARPCacheHit ) { /* Check if the endpoint matches with the one present in the ARP cache */ - if(pxCachedEndPoint == pxTargetEndPoint) + if( pxCachedEndPoint == pxTargetEndPoint ) { vARPRefreshCacheEntry( &( pxARPHeader->xSenderHardwareAddress ), ulSenderProtocolAddress, pxTargetEndPoint ); - FreeRTOS_PrintARPCache(); } } } @@ -1075,7 +1050,7 @@ static BaseType_t prvFindCacheEntry( const MACAddress_t * pxMACAddress, * hardware address of the router, if any. */ *( ppxEndPoint ) = FreeRTOS_FindGateWay( ( BaseType_t ) ipTYPE_IPv4 ); - FreeRTOS_printf(("eARPGetCacheEntryGateWay: Re: %d Using gateway address: %p \n", ipconfigARP_STORES_REMOTE_ADDRESSES, *ppxEndPoint)); + FreeRTOS_printf( ( "eARPGetCacheEntryGateWay: Re: %d Using gateway address: %p \n", ipconfigARP_STORES_REMOTE_ADDRESSES, *ppxEndPoint ) ); if( *( ppxEndPoint ) != NULL ) { @@ -1622,7 +1597,7 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint ) { BaseType_t x, xCount = 0; - FreeRTOS_printf(("****************************************************\n")); + FreeRTOS_printf( ( "****************************************************\n" ) ); /* Loop through each entry in the ARP cache. */ for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ ) From 003e585b4f643094206c06ffb911464c61983ed5 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 13:39:50 +0530 Subject: [PATCH 09/11] fix formatting, cleaning up --- source/FreeRTOS_ARP.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 1bfb86f4e..362857f1e 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -1050,8 +1050,6 @@ static BaseType_t prvFindCacheEntry( const MACAddress_t * pxMACAddress, * hardware address of the router, if any. */ *( ppxEndPoint ) = FreeRTOS_FindGateWay( ( BaseType_t ) ipTYPE_IPv4 ); - FreeRTOS_printf( ( "eARPGetCacheEntryGateWay: Re: %d Using gateway address: %p \n", ipconfigARP_STORES_REMOTE_ADDRESSES, *ppxEndPoint ) ); - if( *( ppxEndPoint ) != NULL ) { /* 'ipv4_settings' can be accessed safely, because 'ipTYPE_IPv4' was provided. */ @@ -1597,15 +1595,13 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint ) { BaseType_t x, xCount = 0; - FreeRTOS_printf( ( "****************************************************\n" ) ); - /* Loop through each entry in the ARP cache. */ for( x = 0; x < ipconfigARP_CACHE_ENTRIES; x++ ) { if( ( xARPCache[ x ].ulIPAddress != 0U ) && ( xARPCache[ x ].ucAge > ( uint8_t ) 0U ) ) { /* See if the MAC-address also matches, and we're all happy */ - FreeRTOS_printf( ( "ARP %2d: %3u - %16xip : %02x:%02x:%02x : %02x:%02x:%02x Endpoint: %p\n", + FreeRTOS_printf( ( "ARP %2d: %3u - %16xip : %02x:%02x:%02x : %02x:%02x:%02x\n", ( int ) x, xARPCache[ x ].ucAge, ( unsigned ) FreeRTOS_ntohl( xARPCache[ x ].ulIPAddress ), @@ -1614,7 +1610,7 @@ void FreeRTOS_ClearARP( const struct xNetworkEndPoint * pxEndPoint ) xARPCache[ x ].xMACAddress.ucBytes[ 2 ], xARPCache[ x ].xMACAddress.ucBytes[ 3 ], xARPCache[ x ].xMACAddress.ucBytes[ 4 ], - xARPCache[ x ].xMACAddress.ucBytes[ 5 ], xARPCache[ x ].pxEndPoint ) ); + xARPCache[ x ].xMACAddress.ucBytes[ 5 ] ) ); xCount++; } } From b4e7c3fe5a9700868f36f6af4ad073cfba8c0345 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 15:48:57 +0530 Subject: [PATCH 10/11] adding unit tests --- .../FreeRTOS_ARP/FreeRTOS_ARP_utest.c | 179 ++++++++++++++++++ 1 file changed, 179 insertions(+) diff --git a/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c b/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c index a18510b3e..6cff20d53 100644 --- a/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c +++ b/test/unit-test/FreeRTOS_ARP/FreeRTOS_ARP_utest.c @@ -734,6 +734,8 @@ void test_eARPProcessPacket_Request_GratuitousARP( void ) memset( xARPCache[ 0 ].xMACAddress.ucBytes, 0x34, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); xARPCache[ 0 ].pxEndPoint = &xEndPoint; + memset( &( xARPFrame.xARPHeader.xTargetHardwareAddress.ucBytes ), 0xff, sizeof( MACAddress_t ) ); + /* Reset the private variable uxARPClashCounter. */ vResetARPClashCounter(); @@ -820,6 +822,183 @@ void test_eARPProcessPacket_Request_GratuitousARP_MACUnchanged( void ) /* =================================================== */ } +/** + * @brief This function verify receiving Gratuitous ARP packet + * but the sender protocol address is outside the subnet. + */ +void test_eARPProcessPacket_Request_GratuitousARP_OutOfSubnetIP( void ) +{ + ARPPacket_t xARPFrame = { 0 }; + eFrameProcessingResult_t eResult; + NetworkInterface_t xInterface; + struct xNetworkEndPoint xEndPoint = { 0 }; + NetworkBufferDescriptor_t xNetworkBuffer = { 0 }; + uint32_t ulTargetIP = 0xAACCDDBB; + + memset( &xARPFrame, 0, sizeof( ARPPacket_t ) ); + memset( xARPCache, 0, sizeof( xARPCache ) ); + + /* =================================================== */ + /* Add settings required for ARP header to pass checks */ + xARPFrame.xARPHeader.usHardwareType = ipARP_HARDWARE_TYPE_ETHERNET; + xARPFrame.xARPHeader.usProtocolType = ipARP_PROTOCOL_TYPE; + xARPFrame.xARPHeader.ucHardwareAddressLength = ipMAC_ADDRESS_LENGTH_BYTES; + xARPFrame.xARPHeader.ucProtocolAddressLength = ipIP_ADDRESS_LENGTH_BYTES; + + /* Process an ARP request - meant for this node with target and source same. */ + xEndPoint.ipv4_settings.ulIPAddress = 0xAABBCCDD; + xEndPoint.ipv4_settings.ulNetMask = 0xFFFF0000; + /* Fill in the request option. */ + xARPFrame.xARPHeader.usOperation = ipARP_REQUEST; + xARPFrame.xARPHeader.ulTargetProtocolAddress = ulTargetIP; + memcpy( xARPFrame.xARPHeader.ucSenderProtocolAddress, &( xARPFrame.xARPHeader.ulTargetProtocolAddress ), sizeof( xARPFrame.xARPHeader.ulTargetProtocolAddress ) ); + + memset( &( xARPFrame.xARPHeader.xSenderHardwareAddress.ucBytes ), 0x22, sizeof( MACAddress_t ) ); + + xARPCache[ 0 ].ulIPAddress = ulTargetIP; + xARPCache[ 0 ].ucAge = 1; + xARPCache[ 0 ].ucValid = pdTRUE; + memset( xARPCache[ 0 ].xMACAddress.ucBytes, 0x22, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + xARPCache[ 0 ].pxEndPoint = &xEndPoint; + + /* Reset the private variable uxARPClashCounter. */ + vResetARPClashCounter(); + + xNetworkBuffer.pucEthernetBuffer = &xARPFrame; + xNetworkBuffer.xDataLength = sizeof( ARPPacket_t ); + xNetworkBuffer.pxEndPoint = &xEndPoint; + xEndPoint.bits.bEndPointUp = pdTRUE_UNSIGNED; + + eResult = eARPProcessPacket( &xNetworkBuffer ); + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); + TEST_ASSERT_EQUAL( ulTargetIP, xARPCache[ 0 ].ulIPAddress ); + TEST_ASSERT_EQUAL( 1, xARPCache[ 0 ].ucAge ); + TEST_ASSERT_EQUAL( pdTRUE, xARPCache[ 0 ].ucValid ); + TEST_ASSERT_EQUAL( &xEndPoint, xARPCache[ 0 ].pxEndPoint ); + TEST_ASSERT_EQUAL_MEMORY( &( xARPFrame.xARPHeader.xSenderHardwareAddress.ucBytes ), xARPCache[ 0 ].xMACAddress.ucBytes, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + /* =================================================== */ +} + +/** + * @brief This function verify receiving Gratuitous ARP packet + * but the new MAC address matches with the MAC address of the + * endpoint. + */ +void test_eARPProcessPacket_Request_GratuitousARP_MACMatchesWithEndpoint( void ) +{ + ARPPacket_t xARPFrame = { 0 }; + eFrameProcessingResult_t eResult; + NetworkInterface_t xInterface; + struct xNetworkEndPoint xEndPoint = { 0 }; + NetworkBufferDescriptor_t xNetworkBuffer = { 0 }; + uint32_t ulTargetIP = 0xAACCDDBB; + + memset( &xARPFrame, 0, sizeof( ARPPacket_t ) ); + memset( xARPCache, 0, sizeof( xARPCache ) ); + + /* =================================================== */ + /* Add settings required for ARP header to pass checks */ + xARPFrame.xARPHeader.usHardwareType = ipARP_HARDWARE_TYPE_ETHERNET; + xARPFrame.xARPHeader.usProtocolType = ipARP_PROTOCOL_TYPE; + xARPFrame.xARPHeader.ucHardwareAddressLength = ipMAC_ADDRESS_LENGTH_BYTES; + xARPFrame.xARPHeader.ucProtocolAddressLength = ipIP_ADDRESS_LENGTH_BYTES; + + /* Process an ARP request - meant for this node with target and source same. */ + xEndPoint.ipv4_settings.ulIPAddress = 0xAABBCCDD; + xEndPoint.ipv4_settings.ulNetMask = 0; + memset( xEndPoint.xMACAddress.ucBytes, 0x22, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + /* Fill in the request option. */ + xARPFrame.xARPHeader.usOperation = ipARP_REQUEST; + xARPFrame.xARPHeader.ulTargetProtocolAddress = ulTargetIP; + memcpy( xARPFrame.xARPHeader.ucSenderProtocolAddress, &( xARPFrame.xARPHeader.ulTargetProtocolAddress ), sizeof( xARPFrame.xARPHeader.ulTargetProtocolAddress ) ); + + memset( &( xARPFrame.xARPHeader.xSenderHardwareAddress.ucBytes ), 0x22, sizeof( MACAddress_t ) ); + + xARPCache[ 0 ].ulIPAddress = ulTargetIP; + xARPCache[ 0 ].ucAge = 1; + xARPCache[ 0 ].ucValid = pdTRUE; + memset( xARPCache[ 0 ].xMACAddress.ucBytes, 0x22, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + xARPCache[ 0 ].pxEndPoint = &xEndPoint; + + /* Reset the private variable uxARPClashCounter. */ + vResetARPClashCounter(); + + xNetworkBuffer.pucEthernetBuffer = &xARPFrame; + xNetworkBuffer.xDataLength = sizeof( ARPPacket_t ); + xNetworkBuffer.pxEndPoint = &xEndPoint; + xEndPoint.bits.bEndPointUp = pdTRUE_UNSIGNED; + + eResult = eARPProcessPacket( &xNetworkBuffer ); + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); + TEST_ASSERT_EQUAL( ulTargetIP, xARPCache[ 0 ].ulIPAddress ); + TEST_ASSERT_EQUAL( 1, xARPCache[ 0 ].ucAge ); + TEST_ASSERT_EQUAL( pdTRUE, xARPCache[ 0 ].ucValid ); + TEST_ASSERT_EQUAL( &xEndPoint, xARPCache[ 0 ].pxEndPoint ); + TEST_ASSERT_EQUAL_MEMORY( &( xARPFrame.xARPHeader.xSenderHardwareAddress.ucBytes ), xARPCache[ 0 ].xMACAddress.ucBytes, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + /* =================================================== */ +} + +/** + * @brief This function verify receiving Gratuitous ARP packet + * but the target MAC address in the ARP request is not a + * broadcast address. + */ +void test_eARPProcessPacket_Request_GratuitousARP_TargetHWAddressNotBroadcast( void ) +{ + ARPPacket_t xARPFrame = { 0 }; + eFrameProcessingResult_t eResult; + NetworkInterface_t xInterface; + struct xNetworkEndPoint xEndPoint = { 0 }; + NetworkBufferDescriptor_t xNetworkBuffer = { 0 }; + uint32_t ulTargetIP = 0xAACCDDBB; + + memset( &xARPFrame, 0, sizeof( ARPPacket_t ) ); + memset( xARPCache, 0, sizeof( xARPCache ) ); + + /* =================================================== */ + /* Add settings required for ARP header to pass checks */ + xARPFrame.xARPHeader.usHardwareType = ipARP_HARDWARE_TYPE_ETHERNET; + xARPFrame.xARPHeader.usProtocolType = ipARP_PROTOCOL_TYPE; + xARPFrame.xARPHeader.ucHardwareAddressLength = ipMAC_ADDRESS_LENGTH_BYTES; + xARPFrame.xARPHeader.ucProtocolAddressLength = ipIP_ADDRESS_LENGTH_BYTES; + + /* Process an ARP request - meant for this node with target and source same. */ + xEndPoint.ipv4_settings.ulIPAddress = 0xAABBCCDD; + xEndPoint.ipv4_settings.ulNetMask = 0; + memset( xEndPoint.xMACAddress.ucBytes, 0x22, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + /* Fill in the request option. */ + xARPFrame.xARPHeader.usOperation = ipARP_REQUEST; + xARPFrame.xARPHeader.ulTargetProtocolAddress = ulTargetIP; + memcpy( xARPFrame.xARPHeader.ucSenderProtocolAddress, &( xARPFrame.xARPHeader.ulTargetProtocolAddress ), sizeof( xARPFrame.xARPHeader.ulTargetProtocolAddress ) ); + + memset( &( xARPFrame.xARPHeader.xSenderHardwareAddress.ucBytes ), 0x22, sizeof( MACAddress_t ) ); + memset( &( xARPFrame.xARPHeader.xTargetHardwareAddress.ucBytes ), 0x11, sizeof( MACAddress_t ) ); + + xARPCache[ 0 ].ulIPAddress = ulTargetIP; + xARPCache[ 0 ].ucAge = 1; + xARPCache[ 0 ].ucValid = pdTRUE; + memset( xARPCache[ 0 ].xMACAddress.ucBytes, 0x22, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + xARPCache[ 0 ].pxEndPoint = &xEndPoint; + + /* Reset the private variable uxARPClashCounter. */ + vResetARPClashCounter(); + + xNetworkBuffer.pucEthernetBuffer = &xARPFrame; + xNetworkBuffer.xDataLength = sizeof( ARPPacket_t ); + xNetworkBuffer.pxEndPoint = &xEndPoint; + xEndPoint.bits.bEndPointUp = pdTRUE_UNSIGNED; + + eResult = eARPProcessPacket( &xNetworkBuffer ); + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); + TEST_ASSERT_EQUAL( ulTargetIP, xARPCache[ 0 ].ulIPAddress ); + TEST_ASSERT_EQUAL( 1, xARPCache[ 0 ].ucAge ); + TEST_ASSERT_EQUAL( pdTRUE, xARPCache[ 0 ].ucValid ); + TEST_ASSERT_EQUAL( &xEndPoint, xARPCache[ 0 ].pxEndPoint ); + TEST_ASSERT_EQUAL_MEMORY( &( xARPFrame.xARPHeader.xSenderHardwareAddress.ucBytes ), xARPCache[ 0 ].xMACAddress.ucBytes, sizeof( xARPCache[ 0 ].xMACAddress.ucBytes ) ); + /* =================================================== */ +} + + /** * @brief This function verify receiving Gratuitous ARP packet * but the packet is received in a different endpoint compared From 318a8be48427337c42bcf1dc9ef192954df13bf0 Mon Sep 17 00:00:00 2001 From: tony-josi-aws Date: Fri, 4 Aug 2023 15:55:41 +0530 Subject: [PATCH 11/11] updating review comments --- source/FreeRTOS_ARP.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/FreeRTOS_ARP.c b/source/FreeRTOS_ARP.c index 362857f1e..d901d0354 100644 --- a/source/FreeRTOS_ARP.c +++ b/source/FreeRTOS_ARP.c @@ -302,8 +302,9 @@ static TickType_t xLastGratuitousARPTime = 0U; eReturn = eReturnEthernetFrame; } } + /* Check if its a Gratuitous ARP request and verify if it belongs to same subnet mask. */ else if( ( ulSenderProtocolAddress == ulTargetProtocolAddress ) && - ( ( ulSenderProtocolAddress & pxTargetEndPoint->ipv4_settings.ulNetMask ) == ( pxTargetEndPoint->ipv4_settings.ulNetMask & pxTargetEndPoint->ipv4_settings.ulIPAddress ) ) ) /* Gratuitous ARP request? */ + ( ( ulSenderProtocolAddress & pxTargetEndPoint->ipv4_settings.ulNetMask ) == ( pxTargetEndPoint->ipv4_settings.ulNetMask & pxTargetEndPoint->ipv4_settings.ulIPAddress ) ) ) { const MACAddress_t xGARPTargetAddress = { { 0, 0, 0, 0, 0, 0 } };