igmp_router_misc.c
Go to the documentation of this file.
1 /**
2  * @file igmp_router_misc.c
3  * @brief Helper functions fore IGMP router
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL IGMP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ipv4/ipv4.h"
37 #include "igmp/igmp_router.h"
38 #include "igmp/igmp_router_misc.h"
39 #include "debug.h"
40 
41 //Check TCP/IP stack configuration
42 #if (IPV4_SUPPORT == ENABLED && IGMP_ROUTER_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief Send General Query message
47  * @param[in] context Pointer to the IGMP router context
48  * @return Error code
49  **/
50 
52 {
53  //A General Query is addressed to the all-systems multicast group, has a
54  //Group Address field of zero, and has a Max Response Time of Query Response
55  //Interval
58 }
59 
60 
61 /**
62  * @brief Send Group-Specific Query message
63  * @param[in] context Pointer to the IGMP router context
64  * @param[in] groupAddr Multicast address of the group being queried
65  * @return Error code
66  **/
67 
70 {
71  //The Group-Specific Query is sent to the group being queried, and has a
72  //Max Response Time of Last Member Query Interval
75 }
76 
77 
78 /**
79  * @brief Send Membership Query message
80  * @param[in] context Pointer to the IGMP router context
81  * @param[in] destAddr Destination IP address
82  * @param[in] groupAddr Multicast group address
83  * @param[in] maxRespTime Maximum response time
84  * @return Error code
85  **/
86 
89 {
90  error_t error;
91  size_t offset;
92  NetBuffer *buffer;
94 
95  //Allocate a memory buffer to hold the IGMP message
96  buffer = ipAllocBuffer(sizeof(IgmpMessage), &offset);
97  //Failed to allocate memory?
98  if(buffer == NULL)
99  {
100  return ERROR_OUT_OF_MEMORY;
101  }
102 
103  //Point to the beginning of the IGMP message
104  message = netBufferAt(buffer, offset, 0);
105 
106  //Format Membership Query message
108  message->checksum = 0;
109 
110  //IGMPv1 compatibility mode?
111  if(context->version == IGMP_VERSION_1)
112  {
113  //When in IGMPv1 mode, routers must send Periodic Queries with a Max
114  //Response Time of 0 (refer to RFC 2236, section 4)
115  message->maxRespTime = 0;
116  }
117  else
118  {
119  //The Max Response Time field is meaningful only in Membership Query
120  //messages, and specifies the maximum allowed time before sending a
121  //responding report in units of 1/10 second
122  message->maxRespTime = (uint8_t) (maxRespTime / 100);
123  }
124 
125  //In a Membership Query message, the group address field is set to zero
126  //when sending a General Query, and set to the group address being queried
127  //when sending a Group-Specific Query
128  message->groupAddr = groupAddr;
129 
130  //Message checksum calculation
131  message->checksum = ipCalcChecksum(message, sizeof(IgmpMessage));
132 
133  //The Membership Report message is sent to the group being reported
134  error = igmpSendMessage(context->interface, destAddr, buffer, offset);
135 
136  //Free previously allocated memory
137  netBufferFree(buffer);
138 
139  //Return status code
140  return error;
141 }
142 
143 
144 /**
145  * @brief Process incoming IGMP message
146  * @param[in] context Pointer to the IGMP router context
147  * @param[in] pseudoHeader IPv4 pseudo header
148  * @param[in] message Pointer to the incoming IGMP message
149  * @param[in] length Length of the IGMP message, in bytes
150  **/
151 
153  const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message,
154  size_t length)
155 {
156  //Check IGMP message type
158  {
159  //Process Membership Query message
160  igmpRouterProcessMembershipQuery(context, pseudoHeader, message, length);
161  }
162  else if(message->type == IGMP_TYPE_MEMBERSHIP_REPORT_V1 ||
164  {
165  //Process Membership Report message
166  igmpRouterProcessMembershipReport(context, pseudoHeader, message, length);
167  }
168  else if(message->type == IGMP_TYPE_LEAVE_GROUP)
169  {
170  //Process Leave Group message
171  igmpRouterProcessLeaveGroup(context, pseudoHeader, message, length);
172  }
173  else
174  {
175  //Discard unrecognized IGMP messages
176  }
177 }
178 
179 
180 /**
181  * @brief Process incoming Membership Query message
182  * @param[in] context Pointer to the IGMP router context
183  * @param[in] pseudoHeader IPv4 pseudo header
184  * @param[in] message Pointer to the incoming IGMP message
185  * @param[in] length Length of the IGMP message, in bytes
186  **/
187 
189  const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message,
190  size_t length)
191 {
193  IgmpRouterGroup *group;
194  NetInterface *interface;
195 
196  //The group address in the IGMP header must either be zero or a valid
197  //multicast group address (refer to RFC 2236, section 6)
198  if(message->groupAddr != IPV4_UNSPECIFIED_ADDR &&
199  !ipv4IsMulticastAddr(message->groupAddr))
200  {
201  return;
202  }
203 
204  //Point to the underlying network interface
205  interface = context->interface;
206 
207  //IGMPv1 or IGMPv2 Membership Query message?
208  if(message->maxRespTime == 0)
209  {
210  //The maximum response time is 10 seconds by default
212  }
213  else
214  {
215  //The Max Resp Time field specifies the maximum time allowed before
216  //sending a responding report
217  maxRespTime = message->maxRespTime * 100;
218  }
219 
220  //Valid source address?
221  if(pseudoHeader->srcAddr != IPV4_UNSPECIFIED_ADDR)
222  {
223  //Check whether the IGMP Membership Query is received from a router on
224  //the same network with a lower IP address
225  if(htonl(pseudoHeader->srcAddr) < htonl(interface->ipv4Context.addrList[0].addr))
226  {
227  //Start Other Querier Present timer
228  netStartTimer(&context->otherQuerierPresentTimer,
230 
231  //Switch to the "Non Querier" state
232  context->state = IGMP_ROUTER_STATE_NON_QUERIER;
233  }
234  }
235 
236  //There are two sub-types of Membership Query messages. These two messages
237  //are differentiated by the Group Address (refer to RFC 2236, section 2.1)
238  if(message->groupAddr == IPV4_UNSPECIFIED_ADDR)
239  {
240  //General Queries are used to learn which groups have members on an
241  //attached network
242  }
243  else if(ipv4IsMulticastAddr(message->groupAddr))
244  {
245  //Group-Specific Queries are used to learn if a particular group has any
246  //members on an attached network
247  group = igmpRouterFindGroup(context, message->groupAddr);
248 
249  //Valid multicast group?
250  if(group != NULL)
251  {
252  //Non-Querier router?
253  if(context->state == IGMP_ROUTER_STATE_NON_QUERIER)
254  {
255  //"Members Present" state?
257  {
258  //Non-Queriers do not send any messages and are only driven by
259  //message reception (refer to RFC 2236, section 7)
260  group->lastMemberQueryCount = 0;
261 
262  //Set the timer to [Max Response Time] * [Last Member Query Count]
263  //if this router is a non-Querier
264  netStartTimer(&group->timer, maxRespTime *
266 
267  //Switch to the "Checking Membership" state
269  }
270  else
271  {
272  //When a non-Querier receives a Group-Specific Query message,
273  //if its existing group membership timer is greater than [Last
274  //Member Query Count] times the Max Response Time specified in
275  //the message, it sets its group membership timer to that value
276  }
277  }
278  }
279  }
280  else
281  {
282  //The group address in the IGMP header must either be zero or a valid
283  //multicast group address (refer to RFC 2236, section 6)
284  }
285 }
286 
287 
288 /**
289  * @brief Process incoming Membership Report message
290  * @param[in] context Pointer to the IGMP router context
291  * @param[in] pseudoHeader IPv4 pseudo header
292  * @param[in] message Pointer to the incoming IGMP message
293  * @param[in] length Length of the IGMP message, in bytes
294  **/
295 
297  const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message,
298  size_t length)
299 {
300  IgmpRouterGroup *group;
301 
302  //The group address in the IGMP header must be a valid multicast group
303  //address
304  if(!ipv4IsMulticastAddr(message->groupAddr))
305  {
306  return;
307  }
308 
309  //In a Membership Report, the group address field holds the IP multicast
310  //group address of the group being reported (refer to RFC 2236, section 2.4)
311  group = igmpRouterFindGroup(context, message->groupAddr);
312 
313  //First report received for this multicast group?
314  if(group == NULL)
315  {
316  //Create a new multicast group
317  group = igmpRouterCreateGroup(context, message->groupAddr);
318  }
319 
320  //Valid multicast group?
321  if(group != NULL)
322  {
323  //Version 1 Membership Report received by a non-Querier router?
324  if(context->state == IGMP_ROUTER_STATE_QUERIER &&
326  {
327  //Start the timer for the group membership
329  //Start IGMPv1 Host timer
331 
332  //Switch to the "V1 Members Present" state
334  }
335  else
336  {
337  //Start the timer for the group membership
339 
340  //"No Members Present", "Members Present" or "Checking Membership" state?
342  {
343  //Switch to the "Members Present" state
345  }
346  }
347  }
348 }
349 
350 
351 /**
352  * @brief Process incoming Leave Group message
353  * @param[in] context Pointer to the IGMP router context
354  * @param[in] pseudoHeader IPv4 pseudo header
355  * @param[in] message Pointer to the incoming IGMP message
356  * @param[in] length Length of the IGMP message, in bytes
357  **/
358 
360  const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message,
361  size_t length)
362 {
363  IgmpRouterGroup *group;
364 
365  //Routers should accept a Leave Group message addressed to the group being
366  //left, in order to accommodate implementations of an earlier version of
367  //this standard (refer to RFC 2236, section 3)
368  if(pseudoHeader->destAddr != IGMP_ALL_ROUTERS_ADDR &&
369  pseudoHeader->destAddr != message->groupAddr)
370  {
371  return;
372  }
373 
374  //When in IGMPv1 mode, routers must ignore Leave Group messages (refer to
375  //RFC 2236, section 4)
376  if(context->version == IGMP_VERSION_1)
377  {
378  return;
379  }
380 
381  //Non-Queriers must ignore Leave Group messages (refer to RFC 2236,
382  //section 3)
383  if(context->state != IGMP_ROUTER_STATE_QUERIER)
384  {
385  return;
386  }
387 
388  //The group address in the IGMP header must be a valid multicast group
389  //address
390  if(!ipv4IsMulticastAddr(message->groupAddr))
391  {
392  return;
393  }
394 
395  //In a Leave Group message, the group address field holds the IP multicast
396  //group address of the group being left (refer to RFC 2236, section 2.4)
397  group = igmpRouterFindGroup(context, message->groupAddr);
398 
399  //Queriers should ignore Leave Group messages for which there are no
400  //group members on the reception interface (refer to RFC 2236, section 3)
401  if(group != NULL)
402  {
403  //"Members Present" state?
405  {
406  //When a Querier receives a Leave Group message for a group that has
407  //group members on the reception interface, it sends Group-Specific
408  //Queries to the group being left
409  igmpRouterSendGroupSpecificQuery(context, group->addr);
410 
411  //Start retransmit timer for the group membership
413 
414  //Number of Group-Specific Queries left to sent before the router
415  //assumes there are no local members
417 
418  //Set the timer to [Last Member Query Interval] * [Last Member
419  //Query Count] if this router is a Querier
422 
423  //Switch to the "Checking Membership" state
425  }
426  }
427 }
428 
429 
430 /**
431  * @brief Create a new multicast group
432  * @param[in] context Pointer to the IGMP router context
433  * @param[in] groupAddr Multicast group address
434  * @return Pointer to the newly created multicast group
435  **/
436 
439 {
440  uint_t i;
441  IgmpRouterGroup *group;
442 
443  //Initialize pointer
444  group = NULL;
445 
446  //Loop through multicast groups
447  for(i = 0; i < context->numGroups; i++)
448  {
449  //Check whether the entry is available
450  if(context->groups[i].state == IGMP_ROUTER_GROUP_STATE_NO_MEMBERS_PRESENT)
451  {
452  //Point to the current group
453  group = &context->groups[i];
454  //Save the multicast group address
455  group->addr = groupAddr;
456 
457  //Any registered callback?
458  if(context->addMcastRouteCallback != NULL)
459  {
460  //Notify the routing protocol that there are members of this group
461  //on this connected network
462  context->addMcastRouteCallback(context, group->addr,
463  context->interface);
464  }
465 
466  //We are done
467  break;
468  }
469  }
470 
471  //Return a pointer to the newly created multicast group
472  return group;
473 }
474 
475 
476 /**
477  * @brief Search the list of multicast groups for a given group address
478  * @param[in] context Pointer to the IGMP router context
479  * @param[in] groupAddr Multicast group address
480  * @return Pointer to the matching multicast group, if any
481  **/
482 
485 {
486  uint_t i;
487  IgmpRouterGroup *group;
488 
489  //Initialize pointer
490  group = NULL;
491 
492  //Loop through multicast groups
493  for(i = 0; i < context->numGroups; i++)
494  {
495  //Check whether there are hosts on the network which have sent reports
496  //for this multicast group
497  if(context->groups[i].state != IGMP_ROUTER_GROUP_STATE_NO_MEMBERS_PRESENT &&
498  context->groups[i].addr == groupAddr)
499  {
500  //Point to the current group
501  group = &context->groups[i];
502  break;
503  }
504  }
505 
506  //Return a pointer to the matching multicast group
507  return group;
508 }
509 
510 
511 /**
512  * @brief Delete a multicast group
513  * @param[in] context Pointer to the IGMP router context
514  * @param[in] group Multicast group
515  **/
516 
518 {
519  //Any registered callback?
520  if(context->deleteMcastRouteCallback != NULL)
521  {
522  //Notify the routing protocol that there are no longer any members of
523  //this group on this connected network
524  context->deleteMcastRouteCallback(context, group->addr,
525  context->interface);
526  }
527 
528  //Groups in "No Members Present" state require no storage in the router
530 }
531 
532 #endif
#define ipv4IsMulticastAddr(ipAddr)
Definition: ipv4.h:191
@ IGMP_ROUTER_GROUP_STATE_NO_MEMBERS_PRESENT
Definition: igmp_router.h:73
void netStartTimer(NetTimer *timer, systime_t interval)
Start timer.
Definition: net_misc.c:806
NetBuffer * ipAllocBuffer(size_t length, size_t *offset)
Allocate a buffer to hold an IP packet.
Definition: ip.c:717
IgmpRouterGroup * igmpRouterCreateGroup(IgmpRouterContext *context, Ipv4Addr groupAddr)
Create a new multicast group.
Ipv4Addr destAddr
Definition: ipv4.h:360
error_t igmpRouterSendMembershipQuery(IgmpRouterContext *context, Ipv4Addr destAddr, Ipv4Addr groupAddr, systime_t maxRespTime)
Send Membership Query message.
uint8_t maxRespTime
Definition: igmp_common.h:212
IgmpMessage
Definition: igmp_common.h:215
Helper functions fore IGMP router.
IgmpRouterGroup * igmpRouterFindGroup(IgmpRouterContext *context, Ipv4Addr groupAddr)
Search the list of multicast groups for a given group address.
void igmpRouterProcessMessage(IgmpRouterContext *context, const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message, size_t length)
Process incoming IGMP message.
#define IGMP_GROUP_MEMBERSHIP_INTERVAL
Definition: igmp_common.h:66
Ipv4Addr addr
Multicast group address.
Definition: igmp_router.h:103
Structure describing a buffer that spans multiple chunks.
Definition: net_mem.h:89
uint8_t message[]
Definition: chap.h:154
#define IGMP_ALL_SYSTEMS_ADDR
Definition: igmp_common.h:144
#define IGMP_LAST_MEMBER_QUERY_COUNT
Definition: igmp_common.h:96
uint_t lastMemberQueryCount
Number of Group-Specific Queries to be sent.
Definition: igmp_router.h:104
@ IGMP_ROUTER_GROUP_STATE_MEMBERS_PRESENT
Definition: igmp_router.h:74
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
void igmpRouterProcessLeaveGroup(IgmpRouterContext *context, const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message, size_t length)
Process incoming Leave Group message.
#define IGMP_OTHER_QUERIER_PRESENT_INTERVAL
Definition: igmp_common.h:70
@ IGMP_ROUTER_GROUP_STATE_CHECKING_MEMBERSHIP
Definition: igmp_router.h:76
@ IGMP_TYPE_MEMBERSHIP_REPORT_V2
Definition: igmp_common.h:176
uint32_t Ipv4Addr
IPv4 network address.
Definition: ipv4.h:328
@ IGMP_ROUTER_GROUP_STATE_V1_MEMBERS_PRESENT
Definition: igmp_router.h:75
IGMP router.
void igmpRouterDeleteGroup(IgmpRouterContext *context, IgmpRouterGroup *group)
Delete a multicast group.
@ IGMP_TYPE_MEMBERSHIP_REPORT_V1
Definition: igmp_common.h:175
@ IGMP_ROUTER_STATE_QUERIER
Definition: igmp_router.h:62
#define htonl(value)
Definition: cpu_endian.h:414
@ IGMP_TYPE_MEMBERSHIP_QUERY
Definition: igmp_common.h:174
error_t
Error codes.
Definition: error.h:43
NetTimer retransmitTimer
Retransmit timer for the group membership.
Definition: igmp_router.h:107
#define IGMP_ALL_ROUTERS_ADDR
Definition: igmp_common.h:146
error_t igmpRouterSendGeneralQuery(IgmpRouterContext *context)
Send General Query message.
void igmpRouterProcessMembershipReport(IgmpRouterContext *context, const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message, size_t length)
Process incoming Membership Report message.
#define NetInterface
Definition: net.h:40
void netBufferFree(NetBuffer *buffer)
Dispose a multi-part buffer.
Definition: net_mem.c:296
IgmpRouterGroupState state
Multicast group state.
Definition: igmp_router.h:102
error_t igmpSendMessage(NetInterface *interface, Ipv4Addr destAddr, NetBuffer *buffer, size_t offset)
Send IGMP message.
Definition: igmp_common.c:159
#define Ipv4PseudoHeader
Definition: ipv4.h:40
uint8_t length
Definition: tcp.h:375
NetTimer v1HostTimer
IGMPv1 Host timer.
Definition: igmp_router.h:106
NetTimer timer
Timer for the group membership.
Definition: igmp_router.h:105
@ IGMP_ROUTER_STATE_NON_QUERIER
Definition: igmp_router.h:63
error_t igmpRouterSendGroupSpecificQuery(IgmpRouterContext *context, Ipv4Addr groupAddr)
Send Group-Specific Query message.
uint32_t systime_t
System time.
#define IGMP_LAST_MEMBER_QUERY_INTERVAL
Definition: igmp_common.h:89
void igmpRouterProcessMembershipQuery(IgmpRouterContext *context, const Ipv4PseudoHeader *pseudoHeader, const IgmpMessage *message, size_t length)
Process incoming Membership Query message.
Ipv4Addr groupAddr
Definition: igmp_common.h:214
@ IGMP_VERSION_1
Definition: igmp_common.h:162
#define IgmpRouterContext
Definition: igmp_router.h:47
void * netBufferAt(const NetBuffer *buffer, size_t offset, size_t length)
Returns a pointer to a data segment.
Definition: net_mem.c:434
#define IGMP_QUERY_RESPONSE_INTERVAL
Definition: igmp_common.h:60
IPv4 (Internet Protocol Version 4)
#define IGMP_V1_MAX_RESPONSE_TIME
Definition: igmp_common.h:121
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
Multicast group.
Definition: igmp_router.h:101
@ IGMP_TYPE_LEAVE_GROUP
Definition: igmp_common.h:177
Debugging facilities.
#define IPV4_UNSPECIFIED_ADDR
Definition: ipv4.h:129
uint16_t ipCalcChecksum(const void *data, size_t length)
IP checksum calculation.
Definition: ip.c:472