Skip to content

Endpoint mismatch in FreeRTOS_MatchingEndpoint #1239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions source/FreeRTOS_Routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,11 @@ struct xIPv6_Couple
* @brief Check IP-type, IP- and MAC-address found in the network packet.
*/
#define rMATCH_IP_ADDR 0 /**< Find an endpoint with a matching IP-address. */
#define rMATCH_IPv6_TYPE 1 /**< Find an endpoint with a matching IPv6 type (both global or non global). */
#define rMATCH_MAC_ADDR 2 /**< Find an endpoint with a matching MAC-address. */
#define rMATCH_IP_TYPE 3 /**< Find an endpoint with a matching IP-type, v4 or v6. */
#define rMATCH_COUNT 4 /**< The number of methods. */
#define rMATCH_NETMASK 1 /**< Find an endpoint with a matching NetMask. */
#define rMATCH_IPv6_TYPE 2 /**< Find an endpoint with a matching IPv6 type (both global or non global). */
#define rMATCH_MAC_ADDR 3 /**< Find an endpoint with a matching MAC-address. */
#define rMATCH_IP_TYPE 4 /**< Find an endpoint with a matching IP-type, v4 or v6. */
#define rMATCH_COUNT 5 /**< The number of methods. */

NetworkEndPoint_t * pxEasyFit( const NetworkInterface_t * pxNetworkInterface,
const uint16_t usFrameType,
Expand All @@ -688,9 +689,9 @@ struct xIPv6_Couple
{
NetworkEndPoint_t * pxEndPoint;
NetworkEndPoint_t * pxReturn = NULL;
/* endpoints found for IP-type, IP-address, and MAC-address. */
NetworkEndPoint_t * pxFound[ rMATCH_COUNT ] = { NULL, NULL, NULL, NULL };
BaseType_t xCount[ rMATCH_COUNT ] = { 0, 0, 0, 0 };
/* endpoints found for IP-type, IP-address, NetMask and MAC-address. */
NetworkEndPoint_t * pxFound[ rMATCH_COUNT ] = { NULL, NULL, NULL, NULL, NULL };
BaseType_t xCount[ rMATCH_COUNT ] = { 0, 0, 0, 0, 0 };
BaseType_t xIndex;
BaseType_t xIsIPv6 = ( usFrameType == ipIPv6_FRAME_TYPE ) ? pdTRUE : pdFALSE;
BaseType_t xGatewayTarget = pdFALSE;
Expand Down Expand Up @@ -778,6 +779,11 @@ struct xIPv6_Couple
pxFound[ rMATCH_IP_ADDR ] = pxEndPoint;
xCount[ rMATCH_IP_ADDR ]++;
}
else if( FreeRTOS_InterfaceEndPointOnNetMask( pxNetworkInterface, pxIPAddressFrom->ulIP_IPv4 ) == pxEndPoint )
{
pxFound[ rMATCH_NETMASK ] = pxEndPoint;
xCount[ rMATCH_NETMASK ]++;
}
else
{
/* do nothing, coverity happy */
Expand Down Expand Up @@ -912,16 +918,11 @@ struct xIPv6_Couple
/* coverity[misra_c_2012_rule_11_3_violation] */
const ARPPacket_t * pxARPFrame = ( const ARPPacket_t * ) pucEthernetBuffer;

if( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REQUEST )
if( ( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REQUEST ) || ( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REPLY ) )
{
( void ) memcpy( xIPAddressFrom.xIP_IPv6.ucBytes, pxPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, sizeof( uint32_t ) );
xIPAddressTo.ulIP_IPv4 = pxPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress;
}
else if( pxARPFrame->xARPHeader.usOperation == ( uint16_t ) ipARP_REPLY )
{
( void ) memcpy( xIPAddressTo.xIP_IPv6.ucBytes, pxPacket->xARPPacket.xARPHeader.ucSenderProtocolAddress, sizeof( uint32_t ) );
xIPAddressFrom.ulIP_IPv4 = pxPacket->xARPPacket.xARPHeader.ulTargetProtocolAddress;
}
else
{
/* do nothing, coverity happy */
Expand Down
50 changes: 50 additions & 0 deletions test/unit-test/FreeRTOS_Routing/FreeRTOS_Routing_utest.c
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,56 @@ void test_FreeRTOS_MatchingEndpoint_MatchIPv4Address()
TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
}

/**
* @brief FreeRTOS_MatchingEndpoint returns the endpoint with same NetMask.
*
* pxNetworkInterfaces is a global variable using in FreeRTOS_Routing as link list head of all interfaces.
* pxNetworkEndPoints is a global variable using in FreeRTOS_Routing as link list head of all endpoints.
*
* Test step:
* - Create 1 interface and 1 endpoint.
* - Put interface & endpoint into the list.
* - Assign 192.168.123.223 (IPV4_DEFAULT_ADDRESS) to the endpoint.
* - Assign ab:cd:ef:11:22:33 (ucDefaultMACAddress_IPv4) to the endpoint.
* - Attach endpoint to interface.
* - Create a network buffer and set IPv4 packet with source and destination address (IPV4_DEFAULT_GATEWAY),
* and different MAC address.
* - Call FreeRTOS_MatchingEndpoint and check if returned endpoint is same.
*/
void test_FreeRTOS_MatchingEndpoint_MatchNetMask()
{
NetworkInterface_t xNetworkInterface;
NetworkEndPoint_t xEndPoint;
NetworkEndPoint_t * pxEndPoint = NULL;
uint8_t * pcNetworkBuffer[ sizeof( ProtocolPacket_t ) + 4 ] __attribute__( ( aligned( 32 ) ) );
ProtocolPacket_t * pxProtocolPacket = ( ProtocolPacket_t * ) ( ( uintptr_t ) ( pcNetworkBuffer ) + 2U );
const uint8_t ucLocalMACAddress_IPv4[ ipMAC_ADDRESS_LENGTH_BYTES ] = { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 };

/* Initialize network interface. */
memset( &xNetworkInterface, 0, sizeof( NetworkInterface_t ) );
pxNetworkInterfaces = &xNetworkInterface;

/* Initialize endpoint. */
memset( &xEndPoint, 0, sizeof( NetworkEndPoint_t ) );
xEndPoint.ipv4_settings.ulIPAddress = IPV4_DEFAULT_ADDRESS;
memcpy( xEndPoint.xMACAddress.ucBytes, ucDefaultMACAddress_IPv4, sizeof( ucDefaultMACAddress_IPv4 ) );
xEndPoint.pxNetworkInterface = &xNetworkInterface;
pxNetworkEndPoints = &xEndPoint;

/* Initialize network buffer. */
memset( pcNetworkBuffer, 0, sizeof( pcNetworkBuffer ) );
/* Ethernet part. */
memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xDestinationAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
memcpy( pxProtocolPacket->xTCPPacket.xEthernetHeader.xSourceAddress.ucBytes, ucLocalMACAddress_IPv4, sizeof( ucLocalMACAddress_IPv4 ) );
pxProtocolPacket->xTCPPacket.xEthernetHeader.usFrameType = ipIPv4_FRAME_TYPE;
/* IP part. */
pxProtocolPacket->xTCPPacket.xIPHeader.ulSourceIPAddress = IPV4_DEFAULT_GATEWAY;
pxProtocolPacket->xTCPPacket.xIPHeader.ulDestinationIPAddress = IPV4_DEFAULT_GATEWAY;

pxEndPoint = FreeRTOS_MatchingEndpoint( &xNetworkInterface, ( const uint8_t * ) ( pxProtocolPacket ) );
TEST_ASSERT_EQUAL( &xEndPoint, pxEndPoint );
}

/**
* @brief FreeRTOS_MatchingEndpoint returns the endpoint with same MAC address.
*
Expand Down