From 782a7e5e83ed520d2d88fee6d5d0e1d290c8674c Mon Sep 17 00:00:00 2001 From: Holden <68555040+HTRamsey@users.noreply.github.com> Date: Wed, 31 Jul 2024 18:19:50 -0700 Subject: [PATCH] Improve frame filtering (#1100) * add checks for frame types * address review comments * Fix multicast filtering and finding endpoint * Remove ipCONSIDER_FRAME_FOR_PROCESSING macro * add ipv4 multicast frame check * make tests finish compiling * fix tests * use multicast macros * improve test coverage * Uncrustify: triggered by comment. * Fix unit test to have full coverage * Fix spell check and formatting * Define macro to check ethernet frame type. * Remove unnecessary declaration of xMDNS_MACAddressIPv6. * Replace all usage of xMDNS_MACAddressIPv6 with xMDNS_MacAddressIPv6. --------- Co-authored-by: GitHub Action Co-authored-by: ActoryOu Co-authored-by: Monika Singh --- source/FreeRTOS_DNS.c | 104 +++---- source/FreeRTOS_IP.c | 216 +++++++++----- source/FreeRTOS_IPv4_Utils.c | 6 +- source/FreeRTOS_IPv6_Utils.c | 4 +- source/include/FreeRTOS_DNS.h | 46 ++- source/include/FreeRTOS_IPv4.h | 5 + test/unit-test/ConfigFiles/FreeRTOSIPConfig.h | 1 + .../FreeRTOS_DNS_Callback_utest.c | 76 +++++ .../unit-test/FreeRTOS_IP/FreeRTOS_IP_stubs.c | 6 + .../unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c | 271 +++++++++++++++++- .../FreeRTOS_IP_DiffConfig_stubs.c | 8 +- .../FreeRTOS_IP_DiffConfig1_stubs.c | 8 +- .../FreeRTOS_IP_DiffConfig1_utest.c | 23 ++ .../FreeRTOSIPConfig.h | 3 +- .../FreeRTOS_IP_DiffConfig2_stubs.c | 6 + .../FreeRTOS_IP_DiffConfig2_utest.c | 112 ++++++++ .../FreeRTOS_IP_DiffConfig3_stubs.c | 6 + 17 files changed, 715 insertions(+), 186 deletions(-) diff --git a/source/FreeRTOS_DNS.c b/source/FreeRTOS_DNS.c index f3eca7605..82e2c6dbf 100644 --- a/source/FreeRTOS_DNS.c +++ b/source/FreeRTOS_DNS.c @@ -57,6 +57,48 @@ #include "FreeRTOS_DNS_Callback.h" +/** @brief The MAC address used for LLMNR. */ +const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } }; + +/** @brief The IPv6 link-scope multicast MAC address */ +const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } }; + +/** @brief The IPv6 link-scope multicast address */ +const IPv6_Address_t ipLLMNR_IP_ADDR_IPv6 = +{ + { /* ff02::1:3 */ + 0xff, 0x02, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x01, + 0x00, 0x03, + } +}; + +/** @brief The MAC address used for MDNS. */ +const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } }; + +/** @brief The IPv6 multicast DNS MAC address. */ +const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } }; + +/** @brief multicast DNS IPv6 address */ +const IPv6_Address_t ipMDNS_IP_ADDR_IPv6 = +{ + { /* ff02::fb */ + 0xff, 0x02, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0x00, + 0x00, 0xfb, + } +}; + /* Exclude the entire file if DNS is not enabled. */ #if ( ipconfigUSE_DNS != 0 ) @@ -95,69 +137,7 @@ struct freertos_addrinfo ** ppxAddressInfo, BaseType_t xFamily ); - #if ( ipconfigUSE_LLMNR == 1 ) - /** @brief The MAC address used for LLMNR. */ - const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } }; - #endif /* ipconfigUSE_LLMNR == 1 */ - /*-----------------------------------------------------------*/ - #if ( ipconfigUSE_LLMNR == 1 ) && ( ipconfigUSE_IPv6 != 0 ) - -/** - * @brief The IPv6 link-scope multicast address - */ - const IPv6_Address_t ipLLMNR_IP_ADDR_IPv6 = - { - { /* ff02::1:3 */ - 0xff, 0x02, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x01, - 0x00, 0x03, - } - }; - -/** - * @brief The IPv6 link-scope multicast MAC address - */ - const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } }; - #endif /* ipconfigUSE_LLMNR && ipconfigUSE_IPv6 */ - - #if ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_IPv6 != 0 ) - -/** - * @brief multicast DNS IPv6 address - */ - const IPv6_Address_t ipMDNS_IP_ADDR_IPv6 = - { - { /* ff02::fb */ - 0xff, 0x02, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0x00, - 0x00, 0xfb, - } - }; - -/** - * @brief The IPv6 multicast DNS MAC address. - * The MAC-addresses are provided here in case a network - * interface needs it. - */ - const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } }; - #endif /* ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_IPv6 != 0 ) */ - - - #if ( ipconfigUSE_MDNS == 1 ) - /** @brief The MAC address used for MDNS. */ - const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } }; - #endif /* ipconfigUSE_MDNS == 1 */ /** @brief This global variable is being used to indicate to the driver which IP type * is preferred for name service lookup, either IPv6 or IPv4. */ diff --git a/source/FreeRTOS_IP.c b/source/FreeRTOS_IP.c index aadf73be2..607bc8fdc 100644 --- a/source/FreeRTOS_IP.c +++ b/source/FreeRTOS_IP.c @@ -92,19 +92,11 @@ #endif #endif -/** @brief If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet - * driver will filter incoming packets and only pass the stack those packets it - * considers need processing. In this case ipCONSIDER_FRAME_FOR_PROCESSING() can - * be #-defined away. If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 0 - * then the Ethernet driver will pass all received packets to the stack, and the - * stack must do the filtering itself. In this case ipCONSIDER_FRAME_FOR_PROCESSING - * needs to call eConsiderFrameForProcessing. - */ -#if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0 - #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) ) -#else - #define ipCONSIDER_FRAME_FOR_PROCESSING( pucEthernetBuffer ) eProcessBuffer -#endif +/** @brief The frame type field in the Ethernet header must have a value greater than 0x0600. + * If the configuration option ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES is enabled, the stack + * will discard packets with a frame type value less than or equal to 0x0600. + * However, if this option is disabled, the stack will continue to process these packets. */ +#define ipIS_ETHERNET_FRAME_TYPE_INVALID( usFrameType ) ( ( usFrameType ) <= 0x0600U ) static void prvCallDHCP_RA_Handler( NetworkEndPoint_t * pxEndPoint ); @@ -1451,85 +1443,165 @@ BaseType_t xSendEventStructToIPTask( const IPStackEvent_t * pxEvent, */ eFrameProcessingResult_t eConsiderFrameForProcessing( const uint8_t * const pucEthernetBuffer ) { - eFrameProcessingResult_t eReturn = eProcessBuffer; - const EthernetHeader_t * pxEthernetHeader = NULL; - const NetworkEndPoint_t * pxEndPoint = NULL; + eFrameProcessingResult_t eReturn = eReleaseBuffer; - if( pucEthernetBuffer == NULL ) - { - eReturn = eReleaseBuffer; - } - else + do { - /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + const EthernetHeader_t * pxEthernetHeader = NULL; + const NetworkEndPoint_t * pxEndPoint = NULL; + uint16_t usFrameType; + + /* First, check the packet buffer is non-null. */ + if( pucEthernetBuffer == NULL ) + { + /* The packet buffer was null - release it. */ + break; + } + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ /* MISRA Ref 11.3.1 [Misaligned access] */ /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */ /* coverity[misra_c_2012_rule_11_3_violation] */ pxEthernetHeader = ( ( const EthernetHeader_t * ) pucEthernetBuffer ); + usFrameType = pxEthernetHeader->usFrameType; - /* Examine the destination MAC from the Ethernet header to see if it matches - * that of an end point managed by FreeRTOS+TCP. */ + /* Second, filter based on ethernet frame type. */ + /* The frame type field in the Ethernet header must have a value greater than 0x0600. */ + if( ipIS_ETHERNET_FRAME_TYPE_INVALID( FreeRTOS_ntohs( usFrameType ) ) ) + { + /* The packet was not an Ethernet II frame */ + #if ipconfigIS_ENABLED( ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES ) + /* filtering is enabled - release it. */ + break; + #else + /* filtering is disabled - continue filter checks. */ + #endif + } + else if( usFrameType == ipARP_FRAME_TYPE ) + { + /* The frame is an ARP type */ + #if ipconfigIS_DISABLED( ipconfigUSE_IPv4 ) + /* IPv4 is disabled - release it. */ + break; + #else + /* IPv4 is enabled - Continue filter checks. */ + #endif + } + else if( usFrameType == ipIPv4_FRAME_TYPE ) + { + /* The frame is an IPv4 type */ + #if ipconfigIS_DISABLED( ipconfigUSE_IPv4 ) + /* IPv4 is disabled - release it. */ + break; + #else + /* IPv4 is enabled - Continue filter checks. */ + #endif + } + else if( usFrameType == ipIPv6_FRAME_TYPE ) + { + /* The frame is an IPv6 type */ + #if ipconfigIS_DISABLED( ipconfigUSE_IPv6 ) + /* IPv6 is disabled - release it. */ + break; + #else + /* IPv6 is enabled - Continue filter checks. */ + #endif + } + else + { + /* The frame is an unsupported Ethernet II type */ + #if ipconfigIS_DISABLED( ipconfigPROCESS_CUSTOM_ETHERNET_FRAMES ) + /* Processing custom ethernet frames is disabled - release it. */ + break; + #else + /* Processing custom ethernet frames is enabled - Continue filter checks. */ + #endif + } + + /* Third, filter based on destination mac address. */ pxEndPoint = FreeRTOS_FindEndPointOnMAC( &( pxEthernetHeader->xDestinationAddress ), NULL ); if( pxEndPoint != NULL ) { - /* The packet was directed to this node - process it. */ - eReturn = eProcessBuffer; + /* A destination endpoint was found - Continue filter checks. */ } else if( memcmp( xBroadcastMACAddress.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 ) { - /* The packet was a broadcast - process it. */ - eReturn = eProcessBuffer; + /* The packet was a broadcast - Continue filter checks. */ } - else - #if ( ( ipconfigUSE_LLMNR == 1 ) && ( ipconfigUSE_DNS != 0 ) ) - if( memcmp( xLLMNR_MacAddress.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 ) - { - /* The packet is a request for LLMNR - process it. */ - eReturn = eProcessBuffer; - } - else - #endif /* ipconfigUSE_LLMNR */ - #if ( ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_DNS != 0 ) ) - if( memcmp( xMDNS_MacAddress.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 ) - { - /* The packet is a request for MDNS - process it. */ - eReturn = eProcessBuffer; - } - else - #endif /* ipconfigUSE_MDNS */ - if( ( pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] == ipMULTICAST_MAC_ADDRESS_IPv6_0 ) && - ( pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] == ipMULTICAST_MAC_ADDRESS_IPv6_1 ) ) + else if( memcmp( xLLMNR_MacAddress.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 ) { - /* The packet is a request for LLMNR - process it. */ - eReturn = eProcessBuffer; + /* The packet is a request for LLMNR using IPv4 */ + #if ( ipconfigIS_DISABLED( ipconfigUSE_DNS ) || ipconfigIS_DISABLED( ipconfigUSE_LLMNR ) || ipconfigIS_DISABLED( ipconfigUSE_IPv4 ) ) + /* DNS, LLMNR, or IPv4 is disabled - release it. */ + break; + #else + /* DNS, LLMNR, and IPv4 are enabled - Continue filter checks. */ + #endif } - else + else if( memcmp( xLLMNR_MacAddressIPv6.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 ) { - /* The packet was not a broadcast, or for this node, just release - * the buffer without taking any other action. */ - eReturn = eReleaseBuffer; + /* The packet is a request for LLMNR using IPv6 */ + #if ( ipconfigIS_DISABLED( ipconfigUSE_DNS ) || ipconfigIS_DISABLED( ipconfigUSE_LLMNR ) || ipconfigIS_DISABLED( ipconfigUSE_IPv6 ) ) + /* DNS, LLMNR, or IPv6 is disabled - release it. */ + break; + #else + /* DNS, LLMNR, and IPv6 are enabled - Continue filter checks. */ + #endif } - } - - #if ( ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1 ) - { - uint16_t usFrameType; - - if( eReturn == eProcessBuffer ) + else if( memcmp( xMDNS_MacAddress.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 ) { - usFrameType = pxEthernetHeader->usFrameType; - usFrameType = FreeRTOS_ntohs( usFrameType ); - - if( usFrameType <= 0x600U ) - { - /* Not an Ethernet II frame. */ - eReturn = eReleaseBuffer; - } + /* The packet is a request for MDNS using IPv4 */ + #if ( ipconfigIS_DISABLED( ipconfigUSE_DNS ) || ipconfigIS_DISABLED( ipconfigUSE_MDNS ) || ipconfigIS_DISABLED( ipconfigUSE_IPv4 ) ) + /* DNS, MDNS, or IPv4 is disabled - release it. */ + break; + #else + /* DNS, MDNS, and IPv4 are enabled - Continue filter checks. */ + #endif + } + else if( memcmp( xMDNS_MacAddressIPv6.ucBytes, pxEthernetHeader->xDestinationAddress.ucBytes, sizeof( MACAddress_t ) ) == 0 ) + { + /* The packet is a request for MDNS using IPv6 */ + #if ( ipconfigIS_DISABLED( ipconfigUSE_DNS ) || ipconfigIS_DISABLED( ipconfigUSE_MDNS ) || ipconfigIS_DISABLED( ipconfigUSE_IPv6 ) ) + /* DNS, MDNS, or IPv6 is disabled - release it. */ + break; + #else + /* DNS, MDNS, and IPv6 are enabled - Continue filter checks. */ + #endif + } + else if( ( pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] == ipMULTICAST_MAC_ADDRESS_IPv4_0 ) && + ( pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] == ipMULTICAST_MAC_ADDRESS_IPv4_1 ) && + ( pxEthernetHeader->xDestinationAddress.ucBytes[ 2 ] == ipMULTICAST_MAC_ADDRESS_IPv4_2 ) && + ( pxEthernetHeader->xDestinationAddress.ucBytes[ 3 ] <= 0x7fU ) ) + { + /* The packet is an IPv4 Multicast */ + #if ipconfigIS_DISABLED( ipconfigUSE_IPv4 ) + /* IPv4 is disabled - release it. */ + break; + #else + /* IPv4 is enabled - Continue filter checks. */ + #endif + } + else if( ( pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] == ipMULTICAST_MAC_ADDRESS_IPv6_0 ) && + ( pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] == ipMULTICAST_MAC_ADDRESS_IPv6_1 ) ) + { + /* The packet is an IPv6 Multicast */ + #if ipconfigIS_DISABLED( ipconfigUSE_IPv6 ) + /* IPv6 is disabled - release it. */ + break; + #else + /* IPv6 is enabled - Continue filter checks. */ + #endif + } + else + { + /* The packet was not a broadcast, or for this node - release it */ + break; } - } - #endif /* ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1 */ + + /* All checks have been passed, process the packet. */ + eReturn = eProcessBuffer; + } while( ipFALSE_BOOL ); return eReturn; } @@ -1575,8 +1647,6 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor break; } - eReturned = ipCONSIDER_FRAME_FOR_PROCESSING( pxNetworkBuffer->pucEthernetBuffer ); - /* Map the buffer onto the Ethernet Header struct for easy access to the fields. */ /* MISRA Ref 11.3.1 [Misaligned access] */ @@ -1586,7 +1656,7 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor /* The condition "eReturned == eProcessBuffer" must be true. */ #if ( ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0 ) - if( eReturned == eProcessBuffer ) + if( eConsiderFrameForProcessing( pxNetworkBuffer->pucEthernetBuffer ) == eProcessBuffer ) #endif { /* Interpret the received Ethernet packet. */ diff --git a/source/FreeRTOS_IPv4_Utils.c b/source/FreeRTOS_IPv4_Utils.c index b79bdc8de..f7c1d3f84 100644 --- a/source/FreeRTOS_IPv4_Utils.c +++ b/source/FreeRTOS_IPv4_Utils.c @@ -59,9 +59,9 @@ void vSetMultiCastIPv4MacAddress( uint32_t ulIPAddress, { uint32_t ulIP = FreeRTOS_ntohl( ulIPAddress ); - pxMACAddress->ucBytes[ 0 ] = ( uint8_t ) 0x01U; - pxMACAddress->ucBytes[ 1 ] = ( uint8_t ) 0x00U; - pxMACAddress->ucBytes[ 2 ] = ( uint8_t ) 0x5EU; + pxMACAddress->ucBytes[ 0 ] = ( uint8_t ) ipMULTICAST_MAC_ADDRESS_IPv4_0; + pxMACAddress->ucBytes[ 1 ] = ( uint8_t ) ipMULTICAST_MAC_ADDRESS_IPv4_1; + pxMACAddress->ucBytes[ 2 ] = ( uint8_t ) ipMULTICAST_MAC_ADDRESS_IPv4_2; pxMACAddress->ucBytes[ 3 ] = ( uint8_t ) ( ( ulIP >> 16 ) & 0x7fU ); /* Use 7 bits. */ pxMACAddress->ucBytes[ 4 ] = ( uint8_t ) ( ( ulIP >> 8 ) & 0xffU ); /* Use 8 bits. */ pxMACAddress->ucBytes[ 5 ] = ( uint8_t ) ( ( ulIP ) & 0xffU ); /* Use 8 bits. */ diff --git a/source/FreeRTOS_IPv6_Utils.c b/source/FreeRTOS_IPv6_Utils.c index f35995bb5..9e9f2abd6 100644 --- a/source/FreeRTOS_IPv6_Utils.c +++ b/source/FreeRTOS_IPv6_Utils.c @@ -55,8 +55,8 @@ void vSetMultiCastIPv6MacAddress( const IPv6_Address_t * pxAddress, MACAddress_t * pxMACAddress ) { - pxMACAddress->ucBytes[ 0 ] = 0x33U; - pxMACAddress->ucBytes[ 1 ] = 0x33U; + pxMACAddress->ucBytes[ 0 ] = ipMULTICAST_MAC_ADDRESS_IPv6_0; + pxMACAddress->ucBytes[ 1 ] = ipMULTICAST_MAC_ADDRESS_IPv6_1; pxMACAddress->ucBytes[ 2 ] = pxAddress->ucBytes[ 12 ]; pxMACAddress->ucBytes[ 3 ] = pxAddress->ucBytes[ 13 ]; pxMACAddress->ucBytes[ 4 ] = pxAddress->ucBytes[ 14 ]; diff --git a/source/include/FreeRTOS_DNS.h b/source/include/FreeRTOS_DNS.h index c69db0e93..830e1ae2b 100644 --- a/source/include/FreeRTOS_DNS.h +++ b/source/include/FreeRTOS_DNS.h @@ -41,43 +41,28 @@ #endif /* *INDENT-ON* */ -/* - * LLMNR is very similar to DNS, so is handled by the DNS routines. - */ -uint32_t ulDNSHandlePacket( const NetworkBufferDescriptor_t * pxNetworkBuffer ); - -#if ( ipconfigUSE_LLMNR == 1 ) - /* The LLMNR MAC address is 01:00:5e:00:00:fc */ - extern const MACAddress_t xLLMNR_MacAddress; -#endif /* ipconfigUSE_LLMNR */ - -#if ( ipconfigUSE_LLMNR == 1 ) && ( ipconfigUSE_IPv6 != 0 ) - -/* The LLMNR IPv6 address is ff02::1:3 */ - extern const IPv6_Address_t ipLLMNR_IP_ADDR_IPv6; +/* The LLMNR MAC address is 01:00:5e:00:00:fc */ +extern const MACAddress_t xLLMNR_MacAddress; /* The LLMNR IPv6 MAC address is 33:33:00:01:00:03 */ - extern const MACAddress_t xLLMNR_MacAddressIPv6; -#endif /* ipconfigUSE_LLMNR */ - -#if ( ipconfigUSE_MDNS == 1 ) - /* The MDNS MAC address is 01:00:5e:00:00:fc */ - extern const MACAddress_t xMDNS_MacAddress; -#endif /* ipconfigUSE_MDNS */ +extern const MACAddress_t xLLMNR_MacAddressIPv6; -#if ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_IPv6 != 0 ) +/* The LLMNR IPv6 address is ff02::1:3 */ +extern const IPv6_Address_t ipLLMNR_IP_ADDR_IPv6; -/* The MDNS IPv6 address is ff02::1:3 */ - extern const IPv6_Address_t ipMDNS_IP_ADDR_IPv6; +/* The MDNS MAC address is 01:00:5e:00:00:fc */ +extern const MACAddress_t xMDNS_MacAddress; /* The MDNS IPv6 MAC address is 33:33:00:01:00:03 */ /* This type-name was formally "misspelled" as * xMDNS_MACAddressIPv6 with "MAC": */ - extern const MACAddress_t xMDNS_MacAddressIPv6; - /* Guarantee backward compatibility. */ - #define xMDNS_MACAddressIPv6 xMDNS_MacAddressIPv6 -#endif /* ipconfigUSE_MDNS */ +extern const MACAddress_t xMDNS_MacAddressIPv6; +/* Guarantee backward compatibility. */ +#define xMDNS_MACAddressIPv6 xMDNS_MacAddressIPv6 + +/* The MDNS IPv6 address is ff02::1:3 */ +extern const IPv6_Address_t ipMDNS_IP_ADDR_IPv6; /** @brief While doing integration tests, it is necessary to influence the choice * between DNS/IPv4 and DNS/IPv4. Depending on this, a DNS server will be @@ -95,6 +80,11 @@ typedef enum xIPPreference /** @brief This variable determines he choice of DNS server, either IPv4 or IPv6. */ extern IPPreference_t xDNS_IP_Preference; +/* + * LLMNR is very similar to DNS, so is handled by the DNS routines. + */ +uint32_t ulDNSHandlePacket( const NetworkBufferDescriptor_t * pxNetworkBuffer ); + #if ( ipconfigUSE_NBNS != 0 ) /* diff --git a/source/include/FreeRTOS_IPv4.h b/source/include/FreeRTOS_IPv4.h index c0f9aa05b..8a9d59320 100644 --- a/source/include/FreeRTOS_IPv4.h +++ b/source/include/FreeRTOS_IPv4.h @@ -58,6 +58,11 @@ struct xIP_PACKET; #define ipIPV4_VERSION_HEADER_LENGTH_MIN 0x45U /**< Minimum IPv4 header length. */ #define ipIPV4_VERSION_HEADER_LENGTH_MAX 0x4FU /**< Maximum IPv4 header length. */ +/* IPv4 multicast MAC address starts with 01-00-5E. */ +#define ipMULTICAST_MAC_ADDRESS_IPv4_0 0x01U +#define ipMULTICAST_MAC_ADDRESS_IPv4_1 0x00U +#define ipMULTICAST_MAC_ADDRESS_IPv4_2 0x5EU + /* * These functions come from the IPv4-only library. * void FreeRTOS_SetIPAddress( uint32_t ulIPAddress ); diff --git a/test/unit-test/ConfigFiles/FreeRTOSIPConfig.h b/test/unit-test/ConfigFiles/FreeRTOSIPConfig.h index 09ddb181c..2dc487b48 100644 --- a/test/unit-test/ConfigFiles/FreeRTOSIPConfig.h +++ b/test/unit-test/ConfigFiles/FreeRTOSIPConfig.h @@ -306,6 +306,7 @@ #define ipconfigUSE_NBNS ( 1 ) #define ipconfigUSE_LLMNR ( 1 ) +#define ipconfigUSE_MDNS ( 1 ) #define ipconfigDNS_USE_CALLBACKS 1 #define ipconfigUSE_ARP_REMOVE_ENTRY 1 diff --git a/test/unit-test/FreeRTOS_DNS_Callback/FreeRTOS_DNS_Callback_utest.c b/test/unit-test/FreeRTOS_DNS_Callback/FreeRTOS_DNS_Callback_utest.c index a23ee1ba6..d35100387 100644 --- a/test/unit-test/FreeRTOS_DNS_Callback/FreeRTOS_DNS_Callback_utest.c +++ b/test/unit-test/FreeRTOS_DNS_Callback/FreeRTOS_DNS_Callback_utest.c @@ -202,6 +202,82 @@ void test_xDNSDoCallback_success_equal_identifier_set_timer( void ) TEST_ASSERT_EQUAL( 1, callback_called ); } +/** + * @brief Happy Path! + */ +void test_xDNSDoCallback_success_equal_port_number_equal_name( void ) +{ + BaseType_t ret; + ParseSet_t pxSet; + struct freertos_addrinfo pxAddress; + DNSMessage_t xDNSMessageHeader; + char pc_name[] = "test"; + uint8_t dnsCallbackMemory[ sizeof( DNSCallback_t ) + ipconfigDNS_CACHE_NAME_LENGTH ]; + DNSCallback_t * pxDnsCallback = ( DNSCallback_t * ) &dnsCallbackMemory; + + pxSet.pxDNSMessageHeader = &xDNSMessageHeader; + pxSet.usPortNumber = FreeRTOS_htons( ipMDNS_PORT ); + strcpy( pxSet.pcName, pc_name ); + pxDnsCallback->pCallbackFunction = dns_callback; + strcpy( pxDnsCallback->pcName, pc_name ); + + /* Expectations */ + listGET_END_MARKER_ExpectAnyArgsAndReturn( ( ListItem_t * ) 4 ); + vTaskSuspendAll_Expect(); + listGET_NEXT_ExpectAnyArgsAndReturn( ( ListItem_t * ) 8 ); + + listGET_LIST_ITEM_OWNER_ExpectAnyArgsAndReturn( pxDnsCallback ); + uxListRemove_ExpectAnyArgsAndReturn( pdTRUE ); + vPortFree_ExpectAnyArgs(); + listLIST_IS_EMPTY_ExpectAnyArgsAndReturn( pdTRUE ); + + vIPSetDNSTimerEnableState_ExpectAnyArgs(); + + xTaskResumeAll_ExpectAndReturn( pdFALSE ); + /* API Call */ + ret = xDNSDoCallback( &pxSet, &pxAddress ); + + /* Validations */ + TEST_ASSERT_EQUAL( pdTRUE, ret ); + TEST_ASSERT_EQUAL( 1, callback_called ); +} + +/** + * @brief A failure path occurs when the port number is for MDNS but + * the name does not match. + */ +void test_xDNSDoCallback_fail_equal_port_number_not_equal_name( void ) +{ + BaseType_t ret; + ParseSet_t pxSet; + struct freertos_addrinfo pxAddress; + DNSMessage_t xDNSMessageHeader; + + pxSet.pxDNSMessageHeader = &xDNSMessageHeader; + pxSet.pxDNSMessageHeader->usIdentifier = 123; + pxSet.usPortNumber = FreeRTOS_htons( ipMDNS_PORT ); + char pc_name[] = "test"; + strcpy( pxSet.pcName, pc_name ); + dnsCallback->pCallbackFunction = dns_callback; + dnsCallback->pcName[ 0 ] = '\0'; + + /* Expectations */ + listGET_END_MARKER_ExpectAnyArgsAndReturn( ( ListItem_t * ) 4 ); + vTaskSuspendAll_Expect(); + listGET_NEXT_ExpectAnyArgsAndReturn( ( ListItem_t * ) 8 ); + + listGET_LIST_ITEM_OWNER_ExpectAnyArgsAndReturn( dnsCallback ); + listGET_NEXT_ExpectAnyArgsAndReturn( ( ListItem_t * ) 4 ); + + xTaskResumeAll_ExpectAndReturn( pdFALSE ); + /* API Call */ + ret = xDNSDoCallback( &pxSet, &pxAddress ); + + /* Validations */ + TEST_ASSERT_EQUAL( pdFALSE, ret ); + TEST_ASSERT_EQUAL( 0, callback_called ); +} + /** * @brief Happy Path! */ diff --git a/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_stubs.c b/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_stubs.c index f25be869e..117d487d1 100644 --- a/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_stubs.c +++ b/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_stubs.c @@ -53,6 +53,12 @@ struct xNetworkEndPoint * pxNetworkEndPoints = NULL; const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } }; +const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } }; + +const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } }; + +const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } }; + void vPortEnterCritical( void ) { } diff --git a/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c b/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c index 2bc281d81..1e4469d6a 100644 --- a/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c +++ b/test/unit-test/FreeRTOS_IP/FreeRTOS_IP_utest.c @@ -1560,7 +1560,7 @@ void test_eConsiderFrameForProcessing_LocalMACMatch( void ) /* Align endpoint's & packet's MAC address. */ memset( pxEndPoint->xMACAddress.ucBytes, 0xAA, sizeof( MACAddress_t ) ); memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, pxEndPoint->xMACAddress.ucBytes, sizeof( MACAddress_t ) ); - pxEthernetHeader->usFrameType = FreeRTOS_htons( 0x0800 ); + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); @@ -1579,9 +1579,6 @@ void test_eConsiderFrameForProcessing_LocalMACMatchInvalidFrameType( void ) uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; EthernetHeader_t * pxEthernetHeader; - /* eConsiderFrameForProcessing */ - FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( pxEndPoint ); - /* Map the buffer onto Ethernet Header struct for easy access to fields. */ pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; @@ -1590,7 +1587,7 @@ void test_eConsiderFrameForProcessing_LocalMACMatchInvalidFrameType( void ) /* Align endpoint's & packet's MAC address. */ memset( pxEndPoint->xMACAddress.ucBytes, 0xAA, sizeof( MACAddress_t ) ); memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, pxEndPoint->xMACAddress.ucBytes, sizeof( MACAddress_t ) ); - pxEthernetHeader->usFrameType = FreeRTOS_htons( 0 ); + pxEthernetHeader->usFrameType = 0; eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); @@ -1609,9 +1606,6 @@ void test_eConsiderFrameForProcessing_LocalMACMatchInvalidFrameType1( void ) uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; EthernetHeader_t * pxEthernetHeader; - /* eConsiderFrameForProcessing */ - FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( pxEndPoint ); - /* Map the buffer onto Ethernet Header struct for easy access to fields. */ pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; @@ -1647,7 +1641,7 @@ void test_eConsiderFrameForProcessing_BroadCastMACMatch( void ) memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xBroadcastMACAddress.ucBytes, sizeof( MACAddress_t ) ); - pxEthernetHeader->usFrameType = 0xFFFF; + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); @@ -1674,7 +1668,88 @@ void test_eConsiderFrameForProcessing_LLMNR_MACMatch( void ) memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xLLMNR_MacAddress.ucBytes, sizeof( MACAddress_t ) ); - pxEthernetHeader->usFrameType = 0xFFFF; + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eProcessBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_LLMNR_IPv6_MACMatch + * eConsiderFrameForProcessing must return eProcessBuffer when the MAC address in packet + * matches LLMNR MAC address and the frame type is valid. + */ +void test_eConsiderFrameForProcessing_LLMNR_IPv6_MACMatch( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xLLMNR_MacAddressIPv6.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eProcessBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_MDNS_MACMatch + * eConsiderFrameForProcessing must return eProcessBuffer when the MAC address in packet + * matches MDNS MAC address and the frame type is valid. + */ +void test_eConsiderFrameForProcessing_MDNS_MACMatch( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xMDNS_MacAddress.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eProcessBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_MDNS_IPv6_MACMatch + * eConsiderFrameForProcessing must return eProcessBuffer when the MAC address in packet + * matches LLMNR MAC address and the frame type is valid. + */ +void test_eConsiderFrameForProcessing_MDNS_IPv6_MACMatch( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xMDNS_MacAddressIPv6.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE; eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); @@ -1702,7 +1777,7 @@ void test_eConsiderFrameForProcessing_NotMatch( void ) memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, &xMACAddress, sizeof( MACAddress_t ) ); - pxEthernetHeader->usFrameType = 0xFFFF; + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); @@ -1710,11 +1785,127 @@ void test_eConsiderFrameForProcessing_NotMatch( void ) } /** - * @brief test_eConsiderFrameForProcessing_IPv6BroadCastMACMatch + * @brief test_eConsiderFrameForProcessing_Multicast_MACMatch * eConsiderFrameForProcessing must return eProcessBuffer when the MAC address in packet - * matches IPv6 broadcast MAC address and the frame type is valid. + * matches IPv6 Multicast MAC address and the frame type is valid. */ -void test_eConsiderFrameForProcessing_IPv6BroadCastMACMatch( void ) +void test_eConsiderFrameForProcessing_Multicast_MACMatch( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] = ipMULTICAST_MAC_ADDRESS_IPv4_0; + pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] = ipMULTICAST_MAC_ADDRESS_IPv4_1; + pxEthernetHeader->xDestinationAddress.ucBytes[ 2 ] = ipMULTICAST_MAC_ADDRESS_IPv4_2; + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eProcessBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_Multicast_MACNotMatch1 + * eConsiderFrameForProcessing must return eReleaseBuffer when the MAC address in packet + * does not match IPv4 Multicast MAC address. + */ +void test_eConsiderFrameForProcessing_Multicast_MACNotMatch1( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] = ipMULTICAST_MAC_ADDRESS_IPv4_0; + pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] = 0xFF; + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_Multicast_MACNotMatch2 + * eConsiderFrameForProcessing must return eReleaseBuffer when the MAC address in packet + * does not match IPv4 Multicast MAC address. + */ +void test_eConsiderFrameForProcessing_Multicast_MACNotMatch2( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] = ipMULTICAST_MAC_ADDRESS_IPv4_0; + pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] = ipMULTICAST_MAC_ADDRESS_IPv4_1; + pxEthernetHeader->xDestinationAddress.ucBytes[ 2 ] = 0xFF; + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_Multicast_MACNotMatch3 + * eConsiderFrameForProcessing must return eReleaseBuffer when the MAC address in packet + * does not match IPv4 Multicast MAC address. + */ +void test_eConsiderFrameForProcessing_Multicast_MACNotMatch3( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] = ipMULTICAST_MAC_ADDRESS_IPv4_0; + pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] = ipMULTICAST_MAC_ADDRESS_IPv4_1; + pxEthernetHeader->xDestinationAddress.ucBytes[ 2 ] = ipMULTICAST_MAC_ADDRESS_IPv4_2; + pxEthernetHeader->xDestinationAddress.ucBytes[ 3 ] = 0xFF; + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_IPv6_Multicast_MACMatch + * eConsiderFrameForProcessing must return eProcessBuffer when the MAC address in packet + * matches IPv6 Multicast MAC address and the frame type is valid. + */ +void test_eConsiderFrameForProcessing_IPv6_Multicast_MACMatch( void ) { eFrameProcessingResult_t eResult; uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; @@ -1730,7 +1921,7 @@ void test_eConsiderFrameForProcessing_IPv6BroadCastMACMatch( void ) pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] = ipMULTICAST_MAC_ADDRESS_IPv6_0; pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] = ipMULTICAST_MAC_ADDRESS_IPv6_1; - pxEthernetHeader->usFrameType = 0xFFFF; + pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE; eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); @@ -1758,6 +1949,56 @@ void test_eConsiderFrameForProcessing_IPv6BroadCastMACPartialMatch( void ) pxEthernetHeader->xDestinationAddress.ucBytes[ 0 ] = ipMULTICAST_MAC_ADDRESS_IPv6_0; pxEthernetHeader->xDestinationAddress.ucBytes[ 1 ] = 0x00; + pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_ArpBoardcastMacMatch + * eConsiderFrameForProcessing must return eProcessBuffer when the MAC address in packet + * matches broadcast MAC address and the frame type is ARP. + */ +void test_eConsiderFrameForProcessing_ArpBoardcastMacMatch( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xBroadcastMACAddress.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipARP_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eProcessBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_UnknownFrameType + * eConsiderFrameForProcessing must return eReleaseBuffer when the frame type + * is unknown. + */ +void test_eConsiderFrameForProcessing_UnknownFrameType( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigTCP_MSS ]; + EthernetHeader_t * pxEthernetHeader; + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigTCP_MSS ); + pxEthernetHeader->usFrameType = 0xFFFF; eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig/FreeRTOS_IP_DiffConfig_stubs.c b/test/unit-test/FreeRTOS_IP_DiffConfig/FreeRTOS_IP_DiffConfig_stubs.c index 3b13966be..9a9ea5162 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig/FreeRTOS_IP_DiffConfig_stubs.c +++ b/test/unit-test/FreeRTOS_IP_DiffConfig/FreeRTOS_IP_DiffConfig_stubs.c @@ -50,9 +50,15 @@ struct xNetworkInterface * pxNetworkInterfaces = NULL; /** @brief A list of all network end-points. Each element has a next pointer. */ struct xNetworkEndPoint * pxNetworkEndPoints = NULL; +const MACAddress_t xDefault_MacAddress = { { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 } }; + const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } }; -const MACAddress_t xDefault_MacAddress = { { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 } }; +const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } }; + +const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } }; + +const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } }; void vPortEnterCritical( void ) { diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_stubs.c b/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_stubs.c index 5968e321c..2d454be48 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_stubs.c +++ b/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_stubs.c @@ -51,9 +51,15 @@ struct xNetworkInterface * pxNetworkInterfaces = NULL; /** @brief A list of all network end-points. Each element has a next pointer. */ struct xNetworkEndPoint * pxNetworkEndPoints = NULL; +const MACAddress_t xDefault_MacAddress = { { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 } }; + const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } }; -const MACAddress_t xDefault_MacAddress = { { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 } }; +const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } }; + +const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } }; + +const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } }; void vPortEnterCritical( void ) { diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_utest.c b/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_utest.c index c103cd264..3a1652158 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_utest.c +++ b/test/unit-test/FreeRTOS_IP_DiffConfig1/FreeRTOS_IP_DiffConfig1_utest.c @@ -786,3 +786,26 @@ void test_FreeRTOS_GetUDPPayloadBuffer_BlockTimeEqualToConfig_IPv6NotSupported( TEST_ASSERT_EQUAL_PTR( NULL, pvReturn ); } + +/** + * @brief test_eConsiderFrameForProcessing_IPv6_FrameType_But_Disabled + * eConsiderFrameForProcessing must return eReleaseBuffer when the frame type is IPv6 but + * ipconfigUSE_IPv6 is disabled. + */ +void test_eConsiderFrameForProcessing_IPv6_FrameType_But_Disabled( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ]; + EthernetHeader_t * pxEthernetHeader; + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigNETWORK_MTU ); + + pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOSIPConfig.h b/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOSIPConfig.h index cbcbc9385..73c8ac747 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOSIPConfig.h +++ b/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOSIPConfig.h @@ -302,7 +302,8 @@ #define ipconfigUSE_NBNS ( 1 ) -#define ipconfigUSE_LLMNR ( 1 ) +#define ipconfigUSE_LLMNR ( 0 ) +#define ipconfigUSE_MDNS ( 0 ) #define ipconfigDNS_USE_CALLBACKS 1 #define ipconfigUSE_ARP_REMOVE_ENTRY 1 diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_stubs.c b/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_stubs.c index f35bef306..641b42e4b 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_stubs.c +++ b/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_stubs.c @@ -51,6 +51,12 @@ struct xNetworkEndPoint * pxNetworkEndPoints = NULL; const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } }; +const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } }; + +const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } }; + +const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } }; + void vPortEnterCritical( void ) { } diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_utest.c b/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_utest.c index 15bfe29cc..25cf53b60 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_utest.c +++ b/test/unit-test/FreeRTOS_IP_DiffConfig2/FreeRTOS_IP_DiffConfig2_utest.c @@ -143,3 +143,115 @@ void test_FreeRTOS_IPInit_HappyPathDHCP( void ) TEST_ASSERT_EQUAL( pdPASS, xReturn ); TEST_ASSERT_EQUAL( IPInItHappyPath_xTaskHandleToSet, FreeRTOS_GetIPTaskHandle() ); } + +/** + * @brief test_eConsiderFrameForProcessing_LLMNR_IPv4_MACMatch_But_Disabled + * eConsiderFrameForProcessing must return eReleaseBuffer when the MAC address in packet + * matches LLMNR MAC address and the frame type is valid but any of ipconfigUSE_DNS, ipconfigUSE_LLMNR + * or ipconfigUSE_IPv4 is disabled. + */ +void test_eConsiderFrameForProcessing_LLMNR_IPv4_MACMatch_But_Disabled( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigNETWORK_MTU ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xLLMNR_MacAddress.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_LLMNR_IPv6_MACMatch_But_Disabled + * eConsiderFrameForProcessing must return eReleaseBuffer when the MAC address in packet + * matches LLMNR MAC address and the frame type is valid but any of ipconfigUSE_DNS, ipconfigUSE_LLMNR + * or ipconfigUSE_IPv6 is disabled. + */ +void test_eConsiderFrameForProcessing_LLMNR_IPv6_MACMatch_But_Disabled( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigNETWORK_MTU ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xLLMNR_MacAddressIPv6.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_MDNS_IPv6_MACMatch_But_Disabled + * eConsiderFrameForProcessing must return eReleaseBuffer when the MAC address in packet + * matches MDNS MAC address and the frame type is valid but any of ipconfigUSE_DNS, ipconfigUSE_MDNS + * or ipconfigUSE_IPv6 is disabled. + */ +void test_eConsiderFrameForProcessing_MDNS_IPv6_MACMatch_But_Disabled( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigNETWORK_MTU ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xMDNS_MacAddressIPv6.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipIPv6_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} + +/** + * @brief test_eConsiderFrameForProcessing_MDNS_IPv4_MACMatch_But_Disabled + * eConsiderFrameForProcessing must return eReleaseBuffer when the MAC address in packet + * matches MDNS MAC address and the frame type is valid but any of ipconfigUSE_DNS, ipconfigUSE_MDNS + * or ipconfigUSE_IPv4 is disabled. + */ +void test_eConsiderFrameForProcessing_MDNS_IPv4_MACMatch_But_Disabled( void ) +{ + eFrameProcessingResult_t eResult; + uint8_t ucEthernetBuffer[ ipconfigNETWORK_MTU ]; + EthernetHeader_t * pxEthernetHeader; + + /* eConsiderFrameForProcessing */ + FreeRTOS_FindEndPointOnMAC_ExpectAnyArgsAndReturn( NULL ); + + /* Map the buffer onto Ethernet Header struct for easy access to fields. */ + pxEthernetHeader = ( EthernetHeader_t * ) ucEthernetBuffer; + + memset( ucEthernetBuffer, 0x00, ipconfigNETWORK_MTU ); + + memcpy( pxEthernetHeader->xDestinationAddress.ucBytes, xMDNS_MacAddress.ucBytes, sizeof( MACAddress_t ) ); + pxEthernetHeader->usFrameType = ipIPv4_FRAME_TYPE; + + eResult = eConsiderFrameForProcessing( ucEthernetBuffer ); + + TEST_ASSERT_EQUAL( eReleaseBuffer, eResult ); +} diff --git a/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_stubs.c b/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_stubs.c index ddaffbd0a..938937b61 100644 --- a/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_stubs.c +++ b/test/unit-test/FreeRTOS_IP_DiffConfig3/FreeRTOS_IP_DiffConfig3_stubs.c @@ -52,6 +52,12 @@ struct xNetworkEndPoint * pxNetworkEndPoints = NULL; const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } }; +const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } }; + +const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } }; + +const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } }; + void vPortEnterCritical( void ) { }