Skip to content

Commit da569d7

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 1599660 commit da569d7

23 files changed

+2158
-52
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
@@ -949,6 +949,26 @@
949949
}
950950

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

source/FreeRTOS_IP.c

+40
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@
5959
#include "NetworkBufferManagement.h"
6060
#include "FreeRTOS_DNS.h"
6161
#include "FreeRTOS_Routing.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
@@ -477,6 +480,20 @@ static void prvProcessIPEventsAndTimers( void )
477480
/* xQueueReceive() returned because of a normal time-out. */
478481
break;
479482

483+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
484+
case eSocketOptAddMembership:
485+
case eSocketOptDropMembership:
486+
{
487+
MulticastAction_t * pxMCA = ( MulticastAction_t * ) xReceivedEvent.pvData;
488+
vModifyMulticastMembership( pxMCA, xReceivedEvent.eEventType );
489+
break;
490+
}
491+
492+
case eMulticastTimerEvent:
493+
vIPMulticast_HandleTimerEvent();
494+
break;
495+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
496+
480497
default:
481498
/* Should not get here. */
482499
break;
@@ -543,6 +560,11 @@ static void prvIPTask_Initialise( void )
543560
}
544561
#endif /* ( ( ipconfigUSE_DNS_CACHE != 0 ) && ( ipconfigUSE_DNS != 0 ) ) */
545562

563+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
564+
/* Init the list that will hold scheduled IGMP reports. */
565+
( void ) vIPMulticast_Init();
566+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
567+
546568
/* Initialisation is complete and events can now be processed. */
547569
xIPTaskInitialised = pdTRUE;
548570
}
@@ -656,6 +678,16 @@ void vIPNetworkUpCalls( struct xNetworkEndPoint * pxEndPoint )
656678
#endif
657679
}
658680

681+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) )
682+
683+
/* Reschedule all multicast reports associated with this end-point.
684+
* Note: countdown is in increments of ipIGMP_TIMER_PERIOD_MS. It's a good idea to spread out all reports a little.
685+
* 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,
686+
* they will soon ask us for reports anyways, so sending these unsolicited reports is not required. It simply enhances the user
687+
* experience by shortening the time it takes before we begin receiving the multicasts that we care for. */
688+
vRescheduleAllMulticastReports( pxEndPoint->pxNetworkInterface, 5 );
689+
#endif /* ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) */
690+
659691
pxEndPoint->bits.bEndPointUp = pdTRUE_UNSIGNED;
660692

661693
#if ( ipconfigUSE_NETWORK_EVENT_HOOK == 1 )
@@ -1356,6 +1388,7 @@ void FreeRTOS_ReleaseUDPPayloadBuffer( void const * pvBuffer )
13561388
pxNetworkBuffer->pucEthernetBuffer[ ipSOCKET_OPTIONS_OFFSET ] = FREERTOS_SO_UDPCKSUM_OUT;
13571389
pxNetworkBuffer->xIPAddress.ulIP_IPv4 = ulIPAddress;
13581390
pxNetworkBuffer->usPort = ipPACKET_CONTAINS_ICMP_DATA;
1391+
pxNetworkBuffer->ucMaximumHops = ipconfigICMP_TIME_TO_LIVE;
13591392
/* xDataLength is the size of the total packet, including the Ethernet header. */
13601393
pxNetworkBuffer->xDataLength = uxTotalLength;
13611394

@@ -2156,6 +2189,13 @@ static eFrameProcessingResult_t prvProcessIPPacket( const IPPacket_t * pxIPPacke
21562189
break;
21572190
#endif /* ( ipconfigUSE_IPv6 != 0 ) */
21582191

2192+
#if ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) && ipconfigIS_ENABLED( ipconfigUSE_IPv4 ) )
2193+
case ipPROTOCOL_IGMP:
2194+
/* The IP packet contained an IGMP frame. */
2195+
eReturn = eProcessIGMPPacket( pxNetworkBuffer );
2196+
break;
2197+
#endif /* ( ipconfigIS_ENABLED( ipconfigSUPPORT_IP_MULTICAST ) && ipconfigIS_ENABLED( ipconfigUSE_IPv4 ) ) */
2198+
21592199
case ipPROTOCOL_UDP:
21602200
/* The IP packet contained a UDP frame. */
21612201

0 commit comments

Comments
 (0)