Skip to content

Commit 782a7e5

Browse files
HTRamseyactions-userActoryOumoninom1
authored
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 <[email protected]> Co-authored-by: ActoryOu <[email protected]> Co-authored-by: Monika Singh <[email protected]>
1 parent 43f68ef commit 782a7e5

File tree

17 files changed

+715
-186
lines changed

17 files changed

+715
-186
lines changed

source/FreeRTOS_DNS.c

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,48 @@
5757
#include "FreeRTOS_DNS_Callback.h"
5858

5959

60+
/** @brief The MAC address used for LLMNR. */
61+
const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
62+
63+
/** @brief The IPv6 link-scope multicast MAC address */
64+
const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } };
65+
66+
/** @brief The IPv6 link-scope multicast address */
67+
const IPv6_Address_t ipLLMNR_IP_ADDR_IPv6 =
68+
{
69+
{ /* ff02::1:3 */
70+
0xff, 0x02,
71+
0x00, 0x00,
72+
0x00, 0x00,
73+
0x00, 0x00,
74+
0x00, 0x00,
75+
0x00, 0x00,
76+
0x00, 0x01,
77+
0x00, 0x03,
78+
}
79+
};
80+
81+
/** @brief The MAC address used for MDNS. */
82+
const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } };
83+
84+
/** @brief The IPv6 multicast DNS MAC address. */
85+
const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } };
86+
87+
/** @brief multicast DNS IPv6 address */
88+
const IPv6_Address_t ipMDNS_IP_ADDR_IPv6 =
89+
{
90+
{ /* ff02::fb */
91+
0xff, 0x02,
92+
0x00, 0x00,
93+
0x00, 0x00,
94+
0x00, 0x00,
95+
0x00, 0x00,
96+
0x00, 0x00,
97+
0x00, 0x00,
98+
0x00, 0xfb,
99+
}
100+
};
101+
60102
/* Exclude the entire file if DNS is not enabled. */
61103
#if ( ipconfigUSE_DNS != 0 )
62104

@@ -95,69 +137,7 @@
95137
struct freertos_addrinfo ** ppxAddressInfo,
96138
BaseType_t xFamily );
97139

98-
#if ( ipconfigUSE_LLMNR == 1 )
99-
/** @brief The MAC address used for LLMNR. */
100-
const MACAddress_t xLLMNR_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfc } };
101-
#endif /* ipconfigUSE_LLMNR == 1 */
102-
103140
/*-----------------------------------------------------------*/
104-
#if ( ipconfigUSE_LLMNR == 1 ) && ( ipconfigUSE_IPv6 != 0 )
105-
106-
/**
107-
* @brief The IPv6 link-scope multicast address
108-
*/
109-
const IPv6_Address_t ipLLMNR_IP_ADDR_IPv6 =
110-
{
111-
{ /* ff02::1:3 */
112-
0xff, 0x02,
113-
0x00, 0x00,
114-
0x00, 0x00,
115-
0x00, 0x00,
116-
0x00, 0x00,
117-
0x00, 0x00,
118-
0x00, 0x01,
119-
0x00, 0x03,
120-
}
121-
};
122-
123-
/**
124-
* @brief The IPv6 link-scope multicast MAC address
125-
*/
126-
const MACAddress_t xLLMNR_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x01, 0x00, 0x03 } };
127-
#endif /* ipconfigUSE_LLMNR && ipconfigUSE_IPv6 */
128-
129-
#if ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_IPv6 != 0 )
130-
131-
/**
132-
* @brief multicast DNS IPv6 address
133-
*/
134-
const IPv6_Address_t ipMDNS_IP_ADDR_IPv6 =
135-
{
136-
{ /* ff02::fb */
137-
0xff, 0x02,
138-
0x00, 0x00,
139-
0x00, 0x00,
140-
0x00, 0x00,
141-
0x00, 0x00,
142-
0x00, 0x00,
143-
0x00, 0x00,
144-
0x00, 0xfb,
145-
}
146-
};
147-
148-
/**
149-
* @brief The IPv6 multicast DNS MAC address.
150-
* The MAC-addresses are provided here in case a network
151-
* interface needs it.
152-
*/
153-
const MACAddress_t xMDNS_MacAddressIPv6 = { { 0x33, 0x33, 0x00, 0x00, 0x00, 0xFB } };
154-
#endif /* ( ipconfigUSE_MDNS == 1 ) && ( ipconfigUSE_IPv6 != 0 ) */
155-
156-
157-
#if ( ipconfigUSE_MDNS == 1 )
158-
/** @brief The MAC address used for MDNS. */
159-
const MACAddress_t xMDNS_MacAddress = { { 0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb } };
160-
#endif /* ipconfigUSE_MDNS == 1 */
161141

162142
/** @brief This global variable is being used to indicate to the driver which IP type
163143
* is preferred for name service lookup, either IPv6 or IPv4. */

source/FreeRTOS_IP.c

Lines changed: 143 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,11 @@
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

109101
static void prvCallDHCP_RA_Handler( NetworkEndPoint_t * pxEndPoint );
110102

@@ -1451,85 +1443,165 @@ BaseType_t xSendEventStructToIPTask( const IPStackEvent_t * pxEvent,
14511443
*/
14521444
eFrameProcessingResult_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. */

source/FreeRTOS_IPv4_Utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ void vSetMultiCastIPv4MacAddress( uint32_t ulIPAddress,
5959
{
6060
uint32_t ulIP = FreeRTOS_ntohl( ulIPAddress );
6161

62-
pxMACAddress->ucBytes[ 0 ] = ( uint8_t ) 0x01U;
63-
pxMACAddress->ucBytes[ 1 ] = ( uint8_t ) 0x00U;
64-
pxMACAddress->ucBytes[ 2 ] = ( uint8_t ) 0x5EU;
62+
pxMACAddress->ucBytes[ 0 ] = ( uint8_t ) ipMULTICAST_MAC_ADDRESS_IPv4_0;
63+
pxMACAddress->ucBytes[ 1 ] = ( uint8_t ) ipMULTICAST_MAC_ADDRESS_IPv4_1;
64+
pxMACAddress->ucBytes[ 2 ] = ( uint8_t ) ipMULTICAST_MAC_ADDRESS_IPv4_2;
6565
pxMACAddress->ucBytes[ 3 ] = ( uint8_t ) ( ( ulIP >> 16 ) & 0x7fU ); /* Use 7 bits. */
6666
pxMACAddress->ucBytes[ 4 ] = ( uint8_t ) ( ( ulIP >> 8 ) & 0xffU ); /* Use 8 bits. */
6767
pxMACAddress->ucBytes[ 5 ] = ( uint8_t ) ( ( ulIP ) & 0xffU ); /* Use 8 bits. */

0 commit comments

Comments
 (0)