igmp_join.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /****************************************************************************
  2. * net/igmp/igmp_join.c
  3. *
  4. * Copyright (C) 2010, 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * The NuttX implementation of IGMP was inspired by the IGMP add-on for the
  8. * lwIP TCP/IP stack by Steve Reynolds:
  9. *
  10. * Copyright (c) 2002 CITEL Technologies Ltd.
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. Neither the name of CITEL Technologies Ltd nor the names of its
  23. * contributors may be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  36. * THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <inttypes.h>
  46. #include <stdint.h>
  47. #include <netinet/in.h>
  48. #include <nuttx/net/netconfig.h>
  49. #include <nuttx/net/netstats.h>
  50. #include <nuttx/net/ip.h>
  51. #include <nuttx/net/igmp.h>
  52. #include "devif/devif.h"
  53. #include "igmp/igmp.h"
  54. #ifdef CONFIG_NET_IGMP
  55. /****************************************************************************
  56. * Public Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Name: igmp_joingroup
  60. *
  61. * Description:
  62. * Add the specified group address to the group.
  63. *
  64. * RFC 2236, 3. Protocol Description:
  65. *
  66. * "When a host joins a multicast group, it should immediately transmit
  67. * an unsolicited Version 2 Membership Report for that group, in case it
  68. * is the first member of that group on the network. To cover the
  69. * possibility of the initial Membership Report being lost or damaged,
  70. * it is recommended that it be repeated once or twice after short
  71. * delays [Unsolicited Report Interval]. (A simple way to accomplish
  72. * this is to send the initial Version 2 Membership Report and then act
  73. * as if a Group-Specific Query was received for that group, and set a
  74. * timer appropriately)."
  75. * ________________
  76. * | |
  77. * | |
  78. * | |
  79. * | |
  80. * +--------->| Non-Member |<---------+
  81. * | | | |
  82. * | | | |
  83. * | | | |
  84. * | |________________| |
  85. * | | |
  86. * | leave group | join group | leave group
  87. * | (stop timer, |(send report, | (send leave
  88. * | send leave if | set flag, | if flag set)
  89. * | flag set) | start timer) |
  90. * ________|________ | ________|________
  91. * | |<---------+ | |
  92. * | | | |
  93. * | |<-------------------| |
  94. * | | query received | |
  95. * | Delaying Member | (start timer) | Idle Member |
  96. * +---->| |------------------->| |
  97. * | | | report received | |
  98. * | | | (stop timer, | |
  99. * | | | clear flag) | |
  100. * | |_________________|------------------->|_________________|
  101. * | query received | timer expired
  102. * | (reset timer if | (send report,
  103. * | Max Resp Time | set flag)
  104. * | < current timer) |
  105. * +-------------------+
  106. *
  107. * Assumptions:
  108. * The network is locked.
  109. *
  110. ****************************************************************************/
  111. int igmp_joingroup(struct net_driver_s *dev,
  112. FAR const struct in_addr *grpaddr)
  113. {
  114. struct igmp_group_s *group;
  115. int ret;
  116. DEBUGASSERT(dev != NULL && grpaddr != NULL);
  117. /* Check if a this address is already in the group */
  118. group = igmp_grpfind(dev, &grpaddr->s_addr);
  119. if (!group)
  120. {
  121. /* No... allocate a new entry */
  122. ninfo("Join to new group: %08" PRIx32 "\n", (uint32_t)grpaddr->s_addr);
  123. group = igmp_grpalloc(dev, &grpaddr->s_addr);
  124. if (group == NULL)
  125. {
  126. return -EADDRNOTAVAIL;
  127. }
  128. IGMP_STATINCR(g_netstats.igmp.joins);
  129. /* Send the Membership Report */
  130. IGMP_STATINCR(g_netstats.igmp.report_sched);
  131. ret = igmp_waitmsg(group, IGMPv2_MEMBERSHIP_REPORT);
  132. if (ret < 0)
  133. {
  134. nerr("ERROR: Failed to schedule message: %d\n", ret);
  135. igmp_grpfree(dev, group);
  136. return ret;
  137. }
  138. /* And start the timer at 10*100 msec */
  139. igmp_starttimer(group, 10);
  140. /* Add the group (MAC) address to the ether drivers MAC filter list */
  141. igmp_addmcastmac(dev, (FAR in_addr_t *)&grpaddr->s_addr);
  142. return OK;
  143. }
  144. /* Return EEXIST if the address is already a member of the group */
  145. return -EEXIST;
  146. }
  147. #endif /* CONFIG_NET_IGMP */