Skip to content

Commit 5bacfbe

Browse files
author
Emil Popov
committed
Adds support for receiving IPv4 and IPv6 multicast groups
Modifies eConsiderFrameForProcessing() to allow all multicast ethernet frames when ipconfigSUPPORT_IP_MULTICAST is enabled and ipconfigETHERNET_DRIVER_FILTERS_FRAME_TYPES is disabled. Adds parsing of IGMP and MLD queries. Sends IGMPv2 and MLDv1 reports on a schedule that is updated based on received IGMP/MLD queries. Sends unsolicited IGMP and MLD reports on network-up events and on add-membership socket option. Adds pxSocket->u.xUDP.xMulticastTTL that can be used for both IPv4 and IPv6 Adds pxSocket->u.xUDP.xMulticastAddress that can be used for both IPv4 and IPv6 Adds pxSocket->u.xUDP.pxMulticastNetIf that specifies the interface on which a sockets wants to receive multicasts. Adds socket option defines to add/drop membership as well as change the transmit TTL of multicasts. Makes all 3 multicast socket options (add/drop/ttl) work with both IPv4 and IPv6 Adds a ucMaximumHops field to NetworkBufferDescriptor_t and assigns it to the proper TTL/HopLimit value based on what packet is being sent. Adds exceptions so that we don't send multicast reports for 224.0.0.1, ff02::1, as well as anything with IPv6 multicast scope of 0 or 1 Adds defines for MLD packets like the Multicast Listener Query and Report. The MLD report defines are different for transmitted and received packets because the stack strips the optional headers from received MLD packets. Generates an MLD report for the solicited-node multicast addresses corresponding to all unicast IPv6 addresses Sends IGMPv2 Leave Group messages whenever the last socket subscribed to a group drops that membership. On network down, stops receiving the MAC address that corresponds to the solicited node multicast IPv6 address. This balances out the "network-up" calls that allow that MAC address. Removes the explicit broadcast MAC check in eConsiderFrameForProcessing. Broadcasts are a form of multicasts and will be received when ipconfigSUPPORT_IP_MULTICAST is enabled. Adds ipconfigSUPPORT_IP_MULTICAST to enable/disable all the functionality described above. Adds ipconfigPERIODIC_MULTICAST_REPORT_INTERVAL for debug purposes when there is no IGMP/MLD querier Moves the registration of the IGMP multicast MAC to the network driver init code. Adds a Multicast Todo list to help keep me on track.
1 parent d701079 commit 5bacfbe

23 files changed

+2157
-51
lines changed

source/FreeRTOS_DNS_Networking.c

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
* going to be '0' i.e. success. Thus, return value is discarded */
8585
( void ) FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SNDTIMEO, &( uxWriteTimeOut_ticks ), sizeof( TickType_t ) );
8686
( void ) FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, &( uxReadTimeOut_ticks ), sizeof( TickType_t ) );
87+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
88+
/* Since this socket may be used for LLMNR or mDNS, set the multicast TTL to 1. */
89+
uint8_t ucMulticastTTL = 1;
90+
( void ) FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_IP_MULTICAST_TTL, &( ucMulticastTTL ), sizeof( ucMulticastTTL ) );
91+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
8792
}
8893

8994
return xSocket;

source/FreeRTOS_DNS_Parser.c

+30
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,26 @@
947947
}
948948

949949
xUDPPacket_IPv6->xUDPHeader.usLength = FreeRTOS_htons( ( uint16_t ) lNetLength + ipSIZE_OF_UDP_HEADER );
950+
951+
if( xUDPPacket_IPv6->xUDPHeader.usDestinationPort == FreeRTOS_ntohs( ipMDNS_PORT ) )
952+
{
953+
/* RFC6762, section 11 */
954+
xUDPPacket_IPv6->xIPHeader.ucHopLimit = 255U;
955+
}
956+
else if( xUDPPacket_IPv6->xUDPHeader.usDestinationPort == FreeRTOS_ntohs( ipLLMNR_PORT ) )
957+
{
958+
/* LLMNR: RFC4795 section 2.5 recommends UDP requests and responses use TTL of 255 */
959+
960+
/* Theoretically, LLMNR replies can go "off-link" and create a DDoS scenario. That should be preventable
961+
* by settings our rely's TTL/HopLimit to 1. Please note that in certain situations ( I think unicast
962+
* responses), Wireshark flags some LLMNR packets that have TTL of 1 as too low. */
963+
xUDPPacket_IPv6->xIPHeader.ucHopLimit = 1U;
964+
}
965+
else
966+
{
967+
xUDPPacket_IPv6->xIPHeader.ucHopLimit = ipconfigUDP_TIME_TO_LIVE;
968+
}
969+
950970
vFlip_16( pxUDPHeader->usSourcePort, pxUDPHeader->usDestinationPort );
951971
uxDataLength = ( size_t ) lNetLength + ipSIZE_OF_IPv6_HEADER + ipSIZE_OF_UDP_HEADER + ipSIZE_OF_ETH_HEADER;
952972
}
@@ -962,8 +982,18 @@
962982
/* HT:endian: should not be translated, copying from packet to packet */
963983
if( pxIPHeader->ulDestinationIPAddress == ipMDNS_IP_ADDRESS )
964984
{
985+
/* RFC6762, section 11 */
965986
pxIPHeader->ucTimeToLive = ipMDNS_TIME_TO_LIVE;
966987
}
988+
else if( pxUDPHeader->usDestinationPort == FreeRTOS_ntohs( ipLLMNR_PORT ) )
989+
{
990+
/* LLMNR: RFC4795 section 2.5 recommends UDP requests and responses use TTL of 255 */
991+
992+
/* Theoretically, LLMNR replies can go "off-link" and create a DDoS scenario. That should be preventable
993+
* by settings our rely's TTL/HopLimit to 1. Please note that in certain situations ( I think unicast
994+
* responses), Wireshark flags some LLMNR packets that have TTL of 1 as too low. */
995+
pxIPHeader->ucTimeToLive = 1;
996+
}
967997
else
968998
{
969999
pxIPHeader->ulDestinationIPAddress = pxIPHeader->ulSourceIPAddress;

source/FreeRTOS_IP.c

+40
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#include "FreeRTOS_DNS.h"
6060
#include "FreeRTOS_Routing.h"
6161
#include "FreeRTOS_ND.h"
62+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
63+
#include "FreeRTOS_IGMP.h"
64+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
6265

6366
/** @brief Time delay between repeated attempts to initialise the network hardware. */
6467
#ifndef ipINITIALISATION_RETRY_DELAY
@@ -452,6 +455,20 @@ static void prvProcessIPEventsAndTimers( void )
452455
/* xQueueReceive() returned because of a normal time-out. */
453456
break;
454457

458+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
459+
case eSocketOptAddMembership:
460+
case eSocketOptDropMembership:
461+
{
462+
MulticastAction_t * pxMCA = ( MulticastAction_t * ) xReceivedEvent.pvData;
463+
vModifyMulticastMembership( pxMCA, xReceivedEvent.eEventType );
464+
break;
465+
}
466+
467+
case eMulticastTimerEvent:
468+
vIPMulticast_HandleTimerEvent();
469+
break;
470+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
471+
455472
default:
456473
/* Should not get here. */
457474
break;
@@ -511,6 +528,11 @@ static void prvIPTask_Initialise( void )
511528
}
512529
#endif /* ( ( ipconfigUSE_DNS_CACHE != 0 ) && ( ipconfigUSE_DNS != 0 ) ) */
513530

531+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
532+
/* Init the list that will hold scheduled IGMP reports. */
533+
( void ) vIPMulticast_Init();
534+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
535+
514536
/* Initialisation is complete and events can now be processed. */
515537
xIPTaskInitialised = pdTRUE;
516538
}
@@ -624,6 +646,16 @@ void vIPNetworkUpCalls( struct xNetworkEndPoint * pxEndPoint )
624646
#endif
625647
}
626648

649+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
650+
651+
/* Reschedule all multicast reports associated with this end-point.
652+
* Note: countdown is in increments of ipIGMP_TIMER_PERIOD_MS. It's a good idea to spread out all reports a little.
653+
* 200 to 500ms ( xMaxCountdown of 2 - 5 ) should be a good happy medium. If the network we just connected to has a IGMP/MLD querier,
654+
* they will soon ask us for reports anyways, so sending these unsolicited reports is not required. It simply enhances the user
655+
* experience by shortening the time it takes before we begin receiving the multicasts that we care for. */
656+
vRescheduleAllMulticastReports( pxEndPoint->pxNetworkInterface, 5 );
657+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
658+
627659
pxEndPoint->bits.bEndPointUp = pdTRUE_UNSIGNED;
628660

629661
#if ( ipconfigUSE_NETWORK_EVENT_HOOK == 1 )
@@ -1313,6 +1345,7 @@ void FreeRTOS_ReleaseUDPPayloadBuffer( void const * pvBuffer )
13131345
pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ] = FREERTOS_SO_UDPCKSUM_OUT;
13141346
pxNetworkBuffer->xIPAddress.ulIP_IPv4 = ulIPAddress;
13151347
pxNetworkBuffer->usPort = ipPACKET_CONTAINS_ICMP_DATA;
1348+
pxNetworkBuffer->ucMaximumHops = ipconfigICMP_TIME_TO_LIVE;
13161349
/* xDataLength is the size of the total packet, including the Ethernet header. */
13171350
pxNetworkBuffer->xDataLength = uxTotalLength;
13181351

@@ -2080,6 +2113,13 @@ static eFrameProcessingResult_t prvProcessIPPacket( const IPPacket_t * pxIPPacke
20802113
break;
20812114
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
20822115

2116+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) && ipconfigIS_ENABLED( ipconfigUSE_IPv4 ) )
2117+
case ipPROTOCOL_IGMP:
2118+
/* The IP packet contained an IGMP frame. */
2119+
eReturn = eProcessIGMPPacket( pxNetworkBuffer );
2120+
break;
2121+
#endif /* ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) && ipconfigIS_ENABLED( ipconfigUSE_IPv4 ) ) */
2122+
20832123
case ipPROTOCOL_UDP:
20842124
/* The IP packet contained a UDP frame. */
20852125

0 commit comments

Comments
 (0)