diff --git a/source/FreeRTOS_Routing.c b/source/FreeRTOS_Routing.c index 89bd610ce..5197e1d3c 100644 --- a/source/FreeRTOS_Routing.c +++ b/source/FreeRTOS_Routing.c @@ -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, @@ -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; @@ -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 */ @@ -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 */ diff --git a/test/unit-test/FreeRTOS_Routing/FreeRTOS_Routing_utest.c b/test/unit-test/FreeRTOS_Routing/FreeRTOS_Routing_utest.c index 232f70519..21064804f 100644 --- a/test/unit-test/FreeRTOS_Routing/FreeRTOS_Routing_utest.c +++ b/test/unit-test/FreeRTOS_Routing/FreeRTOS_Routing_utest.c @@ -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. *