9292 #endif
9393#endif
9494
95- /** @brief If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 1, then the Ethernet
96- * driver will filter incoming packets and only pass the stack those packets it
97- * considers need processing. In this case ipCONSIDER_FRAME_FOR_PROCESSING() can
98- * be #-defined away. If ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is set to 0
99- * then the Ethernet driver will pass all received packets to the stack, and the
100- * stack must do the filtering itself. In this case ipCONSIDER_FRAME_FOR_PROCESSING
101- * needs to call eConsiderFrameForProcessing.
102- */
103- #if ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0
104- #define ipCONSIDER_FRAME_FOR_PROCESSING ( pucEthernetBuffer ) eConsiderFrameForProcessing( ( pucEthernetBuffer ) )
105- #else
106- #define ipCONSIDER_FRAME_FOR_PROCESSING ( pucEthernetBuffer ) eProcessBuffer
107- #endif
95+ /** @brief The frame type field in the Ethernet header must have a value greater than 0x0600.
96+ * If the configuration option ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES is enabled, the stack
97+ * will discard packets with a frame type value less than or equal to 0x0600.
98+ * However, if this option is disabled, the stack will continue to process these packets. */
99+ #define ipIS_ETHERNET_FRAME_TYPE_INVALID ( usFrameType ) ( ( usFrameType ) <= 0x0600U )
108100
109101static void prvCallDHCP_RA_Handler ( NetworkEndPoint_t * pxEndPoint );
110102
@@ -1451,85 +1443,165 @@ BaseType_t xSendEventStructToIPTask( const IPStackEvent_t * pxEvent,
14511443 */
14521444eFrameProcessingResult_t eConsiderFrameForProcessing ( const uint8_t * const pucEthernetBuffer )
14531445{
1454- eFrameProcessingResult_t eReturn = eProcessBuffer ;
1455- const EthernetHeader_t * pxEthernetHeader = NULL ;
1456- const NetworkEndPoint_t * pxEndPoint = NULL ;
1446+ eFrameProcessingResult_t eReturn = eReleaseBuffer ;
14571447
1458- if ( pucEthernetBuffer == NULL )
1459- {
1460- eReturn = eReleaseBuffer ;
1461- }
1462- else
1448+ do
14631449 {
1464- /* Map the buffer onto Ethernet Header struct for easy access to fields. */
1450+ const EthernetHeader_t * pxEthernetHeader = NULL ;
1451+ const NetworkEndPoint_t * pxEndPoint = NULL ;
1452+ uint16_t usFrameType ;
1453+
1454+ /* First, check the packet buffer is non-null. */
1455+ if ( pucEthernetBuffer == NULL )
1456+ {
1457+ /* The packet buffer was null - release it. */
1458+ break ;
1459+ }
14651460
1461+ /* Map the buffer onto Ethernet Header struct for easy access to fields. */
14661462 /* MISRA Ref 11.3.1 [Misaligned access] */
14671463 /* More details at: https://github.com/FreeRTOS/FreeRTOS-Plus-TCP/blob/main/MISRA.md#rule-113 */
14681464 /* coverity[misra_c_2012_rule_11_3_violation] */
14691465 pxEthernetHeader = ( ( const EthernetHeader_t * ) pucEthernetBuffer );
1466+ usFrameType = pxEthernetHeader -> usFrameType ;
14701467
1471- /* Examine the destination MAC from the Ethernet header to see if it matches
1472- * that of an end point managed by FreeRTOS+TCP. */
1468+ /* Second, filter based on ethernet frame type. */
1469+ /* The frame type field in the Ethernet header must have a value greater than 0x0600. */
1470+ if ( ipIS_ETHERNET_FRAME_TYPE_INVALID ( FreeRTOS_ntohs ( usFrameType ) ) )
1471+ {
1472+ /* The packet was not an Ethernet II frame */
1473+ #if ipconfigIS_ENABLED ( ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES )
1474+ /* filtering is enabled - release it. */
1475+ break ;
1476+ #else
1477+ /* filtering is disabled - continue filter checks. */
1478+ #endif
1479+ }
1480+ else if ( usFrameType == ipARP_FRAME_TYPE )
1481+ {
1482+ /* The frame is an ARP type */
1483+ #if ipconfigIS_DISABLED ( ipconfigUSE_IPv4 )
1484+ /* IPv4 is disabled - release it. */
1485+ break ;
1486+ #else
1487+ /* IPv4 is enabled - Continue filter checks. */
1488+ #endif
1489+ }
1490+ else if ( usFrameType == ipIPv4_FRAME_TYPE )
1491+ {
1492+ /* The frame is an IPv4 type */
1493+ #if ipconfigIS_DISABLED ( ipconfigUSE_IPv4 )
1494+ /* IPv4 is disabled - release it. */
1495+ break ;
1496+ #else
1497+ /* IPv4 is enabled - Continue filter checks. */
1498+ #endif
1499+ }
1500+ else if ( usFrameType == ipIPv6_FRAME_TYPE )
1501+ {
1502+ /* The frame is an IPv6 type */
1503+ #if ipconfigIS_DISABLED ( ipconfigUSE_IPv6 )
1504+ /* IPv6 is disabled - release it. */
1505+ break ;
1506+ #else
1507+ /* IPv6 is enabled - Continue filter checks. */
1508+ #endif
1509+ }
1510+ else
1511+ {
1512+ /* The frame is an unsupported Ethernet II type */
1513+ #if ipconfigIS_DISABLED ( ipconfigPROCESS_CUSTOM_ETHERNET_FRAMES )
1514+ /* Processing custom ethernet frames is disabled - release it. */
1515+ break ;
1516+ #else
1517+ /* Processing custom ethernet frames is enabled - Continue filter checks. */
1518+ #endif
1519+ }
1520+
1521+ /* Third, filter based on destination mac address. */
14731522 pxEndPoint = FreeRTOS_FindEndPointOnMAC ( & ( pxEthernetHeader -> xDestinationAddress ), NULL );
14741523
14751524 if ( pxEndPoint != NULL )
14761525 {
1477- /* The packet was directed to this node - process it. */
1478- eReturn = eProcessBuffer ;
1526+ /* A destination endpoint was found - Continue filter checks. */
14791527 }
14801528 else if ( memcmp ( xBroadcastMACAddress .ucBytes , pxEthernetHeader -> xDestinationAddress .ucBytes , sizeof ( MACAddress_t ) ) == 0 )
14811529 {
1482- /* The packet was a broadcast - process it. */
1483- eReturn = eProcessBuffer ;
1530+ /* The packet was a broadcast - Continue filter checks. */
14841531 }
1485- else
1486- #if ( ( ipconfigUSE_LLMNR == 1 ) && ( ipconfigUSE_DNS != 0 ) )
1487- if ( memcmp ( xLLMNR_MacAddress .ucBytes , pxEthernetHeader -> xDestinationAddress .ucBytes , sizeof ( MACAddress_t ) ) == 0 )
1488- {
1489- /* The packet is a request for LLMNR - process it. */
1490- eReturn = eProcessBuffer ;
1491- }
1492- else
1493- #endif /* ipconfigUSE_LLMNR */
1494- #if ( ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_DNS != 0 ) )
1495- if ( memcmp ( xMDNS_MacAddress .ucBytes , pxEthernetHeader -> xDestinationAddress .ucBytes , sizeof ( MACAddress_t ) ) == 0 )
1496- {
1497- /* The packet is a request for MDNS - process it. */
1498- eReturn = eProcessBuffer ;
1499- }
1500- else
1501- #endif /* ipconfigUSE_MDNS */
1502- if ( ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 0 ] == ipMULTICAST_MAC_ADDRESS_IPv6_0 ) &&
1503- ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 1 ] == ipMULTICAST_MAC_ADDRESS_IPv6_1 ) )
1532+ else if ( memcmp ( xLLMNR_MacAddress .ucBytes , pxEthernetHeader -> xDestinationAddress .ucBytes , sizeof ( MACAddress_t ) ) == 0 )
15041533 {
1505- /* The packet is a request for LLMNR - process it. */
1506- eReturn = eProcessBuffer ;
1534+ /* The packet is a request for LLMNR using IPv4 */
1535+ #if ( ipconfigIS_DISABLED ( ipconfigUSE_DNS ) || ipconfigIS_DISABLED ( ipconfigUSE_LLMNR ) || ipconfigIS_DISABLED ( ipconfigUSE_IPv4 ) )
1536+ /* DNS, LLMNR, or IPv4 is disabled - release it. */
1537+ break ;
1538+ #else
1539+ /* DNS, LLMNR, and IPv4 are enabled - Continue filter checks. */
1540+ #endif
15071541 }
1508- else
1542+ else if ( memcmp ( xLLMNR_MacAddressIPv6 . ucBytes , pxEthernetHeader -> xDestinationAddress . ucBytes , sizeof ( MACAddress_t ) ) == 0 )
15091543 {
1510- /* The packet was not a broadcast, or for this node, just release
1511- * the buffer without taking any other action. */
1512- eReturn = eReleaseBuffer ;
1544+ /* The packet is a request for LLMNR using IPv6 */
1545+ #if ( ipconfigIS_DISABLED ( ipconfigUSE_DNS ) || ipconfigIS_DISABLED ( ipconfigUSE_LLMNR ) || ipconfigIS_DISABLED ( ipconfigUSE_IPv6 ) )
1546+ /* DNS, LLMNR, or IPv6 is disabled - release it. */
1547+ break ;
1548+ #else
1549+ /* DNS, LLMNR, and IPv6 are enabled - Continue filter checks. */
1550+ #endif
15131551 }
1514- }
1515-
1516- #if ( ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1 )
1517- {
1518- uint16_t usFrameType ;
1519-
1520- if ( eReturn == eProcessBuffer )
1552+ else if ( memcmp ( xMDNS_MacAddress .ucBytes , pxEthernetHeader -> xDestinationAddress .ucBytes , sizeof ( MACAddress_t ) ) == 0 )
15211553 {
1522- usFrameType = pxEthernetHeader -> usFrameType ;
1523- usFrameType = FreeRTOS_ntohs ( usFrameType );
1524-
1525- if ( usFrameType <= 0x600U )
1526- {
1527- /* Not an Ethernet II frame. */
1528- eReturn = eReleaseBuffer ;
1529- }
1554+ /* The packet is a request for MDNS using IPv4 */
1555+ #if ( ipconfigIS_DISABLED ( ipconfigUSE_DNS ) || ipconfigIS_DISABLED ( ipconfigUSE_MDNS ) || ipconfigIS_DISABLED ( ipconfigUSE_IPv4 ) )
1556+ /* DNS, MDNS, or IPv4 is disabled - release it. */
1557+ break ;
1558+ #else
1559+ /* DNS, MDNS, and IPv4 are enabled - Continue filter checks. */
1560+ #endif
1561+ }
1562+ else if ( memcmp ( xMDNS_MacAddressIPv6 .ucBytes , pxEthernetHeader -> xDestinationAddress .ucBytes , sizeof ( MACAddress_t ) ) == 0 )
1563+ {
1564+ /* The packet is a request for MDNS using IPv6 */
1565+ #if ( ipconfigIS_DISABLED ( ipconfigUSE_DNS ) || ipconfigIS_DISABLED ( ipconfigUSE_MDNS ) || ipconfigIS_DISABLED ( ipconfigUSE_IPv6 ) )
1566+ /* DNS, MDNS, or IPv6 is disabled - release it. */
1567+ break ;
1568+ #else
1569+ /* DNS, MDNS, and IPv6 are enabled - Continue filter checks. */
1570+ #endif
1571+ }
1572+ else if ( ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 0 ] == ipMULTICAST_MAC_ADDRESS_IPv4_0 ) &&
1573+ ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 1 ] == ipMULTICAST_MAC_ADDRESS_IPv4_1 ) &&
1574+ ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 2 ] == ipMULTICAST_MAC_ADDRESS_IPv4_2 ) &&
1575+ ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 3 ] <= 0x7fU ) )
1576+ {
1577+ /* The packet is an IPv4 Multicast */
1578+ #if ipconfigIS_DISABLED ( ipconfigUSE_IPv4 )
1579+ /* IPv4 is disabled - release it. */
1580+ break ;
1581+ #else
1582+ /* IPv4 is enabled - Continue filter checks. */
1583+ #endif
1584+ }
1585+ else if ( ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 0 ] == ipMULTICAST_MAC_ADDRESS_IPv6_0 ) &&
1586+ ( pxEthernetHeader -> xDestinationAddress .ucBytes [ 1 ] == ipMULTICAST_MAC_ADDRESS_IPv6_1 ) )
1587+ {
1588+ /* The packet is an IPv6 Multicast */
1589+ #if ipconfigIS_DISABLED ( ipconfigUSE_IPv6 )
1590+ /* IPv6 is disabled - release it. */
1591+ break ;
1592+ #else
1593+ /* IPv6 is enabled - Continue filter checks. */
1594+ #endif
1595+ }
1596+ else
1597+ {
1598+ /* The packet was not a broadcast, or for this node - release it */
1599+ break ;
15301600 }
1531- }
1532- #endif /* ipconfigFILTER_OUT_NON_ETHERNET_II_FRAMES == 1 */
1601+
1602+ /* All checks have been passed, process the packet. */
1603+ eReturn = eProcessBuffer ;
1604+ } while ( ipFALSE_BOOL );
15331605
15341606 return eReturn ;
15351607}
@@ -1575,8 +1647,6 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor
15751647 break ;
15761648 }
15771649
1578- eReturned = ipCONSIDER_FRAME_FOR_PROCESSING ( pxNetworkBuffer -> pucEthernetBuffer );
1579-
15801650 /* Map the buffer onto the Ethernet Header struct for easy access to the fields. */
15811651
15821652 /* MISRA Ref 11.3.1 [Misaligned access] */
@@ -1586,7 +1656,7 @@ static void prvProcessEthernetPacket( NetworkBufferDescriptor_t * const pxNetwor
15861656
15871657 /* The condition "eReturned == eProcessBuffer" must be true. */
15881658 #if ( ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES == 0 )
1589- if ( eReturned == eProcessBuffer )
1659+ if ( eConsiderFrameForProcessing ( pxNetworkBuffer -> pucEthernetBuffer ) == eProcessBuffer )
15901660 #endif
15911661 {
15921662 /* Interpret the received Ethernet packet. */
0 commit comments