Skip to content

Commit 2a12008

Browse files
Endpoint mismatch in FreeRTOS_MatchingEndpoint (#1239)
* Match source IP to an endpoint on same netmask * Add Unit test * Remove used argument from the unused variables cast --------- Co-authored-by: Ravi Teja Darbha <rdarbha.bu.edu> Co-authored-by: Tony Josi <tonyjosi@amazon.com>
1 parent 861750b commit 2a12008

File tree

2 files changed

+64
-14
lines changed

2 files changed

+64
-14
lines changed

source/FreeRTOS_Routing.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,11 @@ struct xIPv6_Couple
658658
* @brief Check IP-type, IP- and MAC-address found in the network packet.
659659
*/
660660
#define rMATCH_IP_ADDR 0 /**< Find an endpoint with a matching IP-address. */
661-
#define rMATCH_IPv6_TYPE 1 /**< Find an endpoint with a matching IPv6 type (both global or non global). */
662-
#define rMATCH_MAC_ADDR 2 /**< Find an endpoint with a matching MAC-address. */
663-
#define rMATCH_IP_TYPE 3 /**< Find an endpoint with a matching IP-type, v4 or v6. */
664-
#define rMATCH_COUNT 4 /**< The number of methods. */
661+
#define rMATCH_NETMASK 1 /**< Find an endpoint with a matching NetMask. */
662+
#define rMATCH_IPv6_TYPE 2 /**< Find an endpoint with a matching IPv6 type (both global or non global). */
663+
#define rMATCH_MAC_ADDR 3 /**< Find an endpoint with a matching MAC-address. */
664+
#define rMATCH_IP_TYPE 4 /**< Find an endpoint with a matching IP-type, v4 or v6. */
665+
#define rMATCH_COUNT 5 /**< The number of methods. */
665666

666667
NetworkEndPoint_t * pxEasyFit( const NetworkInterface_t * pxNetworkInterface,
667668
const uint16_t usFrameType,
@@ -688,15 +689,14 @@ struct xIPv6_Couple
688689
{
689690
NetworkEndPoint_t * pxEndPoint;
690691
NetworkEndPoint_t * pxReturn = NULL;
691-
/* endpoints found for IP-type, IP-address, and MAC-address. */
692-
NetworkEndPoint_t * pxFound[ rMATCH_COUNT ] = { NULL, NULL, NULL, NULL };
693-
BaseType_t xCount[ rMATCH_COUNT ] = { 0, 0, 0, 0 };
692+
/* endpoints found for IP-type, IP-address, NetMask and MAC-address. */
693+
NetworkEndPoint_t * pxFound[ rMATCH_COUNT ] = { NULL, NULL, NULL, NULL, NULL };
694+
BaseType_t xCount[ rMATCH_COUNT ] = { 0, 0, 0, 0, 0 };
694695
BaseType_t xIndex;
695696
BaseType_t xIsIPv6 = ( usFrameType == ipIPv6_FRAME_TYPE ) ? pdTRUE : pdFALSE;
696697
BaseType_t xGatewayTarget = pdFALSE;
697698
BaseType_t xTargetGlobal = pdFALSE;
698699

699-
( void ) pxIPAddressFrom;
700700
( void ) xGatewayTarget;
701701
( void ) xTargetGlobal;
702702

@@ -778,6 +778,11 @@ struct xIPv6_Couple
778778
pxFound[ rMATCH_IP_ADDR ] = pxEndPoint;
779779
xCount[ rMATCH_IP_ADDR ]++;
780780
}
781+
else if( FreeRTOS_InterfaceEndPointOnNetMask( pxNetworkInterface, pxIPAddressFrom->ulIP_IPv4 ) == pxEndPoint )
782+
{
783+
pxFound[ rMATCH_NETMASK ] = pxEndPoint;
784+
xCount[ rMATCH_NETMASK ]++;
785+
}
781786
else
782787
{
783788
/* do nothing, coverity happy */
@@ -912,16 +917,11 @@ struct xIPv6_Couple
912917
/* coverity[misra_c_2012_rule_11_3_violation] */
913918
const ARPPacket_t * pxARPFrame = ( const ARPPacket_t * ) pucEthernetBuffer;
914919

915-
if( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REQUEST )
920+
if( ( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REQUEST ) || ( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REPLY ) )
916921
{
917922
( void ) memcpy( xIPAddressFrom.xIP_IPv6.ucBytes, pxPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, sizeof( uint32_t ) );
918923
xIPAddressTo.ulIP_IPv4 = pxPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress;
919924
}
920-
else if( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REPLY )
921-
{
922-
( void ) memcpy( xIPAddressTo.xIP_IPv6.ucBytes, pxPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, sizeof( uint32_t ) );
923-
xIPAddressFrom.ulIP_IPv4 = pxPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress;
924-
}
925925
else
926926
{
927927
/* do nothing, coverity happy */

test/unit-test/FreeRTOS_Routing/FreeRTOS_Routing_utest.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,6 +2748,56 @@ void test_FreeRTOS_MatchingEndpoint_MatchIPv4Address()
27482748
TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
27492749
}
27502750

2751+
/**
2752+
* @brief FreeRTOS_MatchingEndpoint returns the endpoint with same NetMask.
2753+
*
2754+
* pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
2755+
* pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
2756+
*
2757+
* Test step:
2758+
* - Create 1 interface and 1 endpoint.
2759+
* - Put interface & endpoint into the list.
2760+
* - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
2761+
* - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
2762+
* - Attach endpoint to interface.
2763+
* - Create a network buffer and set IPv4 packet with source and destination address (IPV4_DEFAULT_GATEWAY),
2764+
* and different MAC address.
2765+
* - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
2766+
*/
2767+
void test_FreeRTOS_MatchingEndpoint_MatchNetMask()
2768+
{
2769+
NetworkInterface_t xNetworkInterface;
2770+
NetworkEndPoint_t xEndPoint;
2771+
NetworkEndPoint_t * pxEndPoint = NULL;
2772+
uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
2773+
ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
2774+
const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };
2775+
2776+
/* Initialize network interface. */
2777+
memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
2778+
pxNetworkInterfaces = &xNetworkInterface;
2779+
2780+
/* Initialize endpoint. */
2781+
memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
2782+
xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
2783+
memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
2784+
xEndPoint.pxNetworkInterface = &xNetworkInterface;
2785+
pxNetworkEndPoints = &xEndPoint;
2786+
2787+
/* Initialize network buffer. */
2788+
memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
2789+
/* Ethernet part. */
2790+
memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2791+
memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
2792+
pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
2793+
/* IP part. */
2794+
pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_GATEWAY;
2795+
pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_GATEWAY;
2796+
2797+
pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
2798+
TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
2799+
}
2800+
27512801
/**
27522802
* @brief FreeRTOS_MatchingEndpoint returns the endpoint with same MAC address.
27532803
*

0 commit comments

Comments
 (0)