igmp_leave.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. * net/igmp/igmp_leave.c
  3. *
  4. * Copyright (C) 2010-2011, 2014, 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 from
  24. * 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 THE
  36. * 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 <netinet/in.h>
  46. #include <nuttx/wdog.h>
  47. #include <nuttx/net/netconfig.h>
  48. #include <nuttx/net/net.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_leavegroup
  60. *
  61. * Description:
  62. * Remove the specified group address to the group.
  63. *
  64. * RFC 2236, 3. Protocol Description:
  65. *
  66. * "When a host leaves a multicast group, if it was the last host to
  67. * reply to a Query with a Membership Report for that group, it SHOULD
  68. * send a Leave Group message to the all-routers multicast group
  69. * (224.0.0.2). If it was not the last host to reply to a Query, it MAY
  70. * send nothing as there must be another member on the subnet. This is
  71. * an optimization to reduce traffic; a host without sufficient storage
  72. * to remember whether or not it was the last host to reply MAY always
  73. * send a Leave Group message when it leaves a group. Routers SHOULD
  74. * accept a Leave Group message addressed to the group being left, in
  75. * order to accommodate implementations of an earlier version of this
  76. * standard. Leave Group messages are addressed to the all-routers
  77. * group because other group members have no need to know that a host
  78. * has left the group, but it does no harm to address the message to the
  79. * group."
  80. *
  81. * ________________
  82. * | |
  83. * | |
  84. * | |
  85. * | |
  86. * +--------->| Non-Member |<---------+
  87. * | | | |
  88. * | | | |
  89. * | | | |
  90. * | |________________| |
  91. * | | |
  92. * | leave group | join group | leave group
  93. * | (stop timer, |(send report, | (send leave
  94. * | send leave if | set flag, | if flag set)
  95. * | flag set) | start timer) |
  96. * ________|________ | ________|________
  97. * | |<---------+ | |
  98. * | | | |
  99. * | |<-------------------| |
  100. * | | query received | |
  101. * | Delaying Member | (start timer) | Idle Member |
  102. * +---->| |------------------->| |
  103. * | | | report received | |
  104. * | | | (stop timer, | |
  105. * | | | clear flag) | |
  106. * | |_________________|------------------->|_________________|
  107. * | query received | timer expired
  108. * | (reset timer if | (send report,
  109. * | Max Resp Time | set flag)
  110. * | < current timer) |
  111. * +-------------------+
  112. *
  113. * Assumptions:
  114. * The network is locked.
  115. *
  116. ****************************************************************************/
  117. int igmp_leavegroup(struct net_driver_s *dev,
  118. FAR const struct in_addr *grpaddr)
  119. {
  120. struct igmp_group_s *group;
  121. int ret;
  122. DEBUGASSERT(dev && grpaddr);
  123. /* Find the entry corresponding to the address leaving the group */
  124. group = igmp_grpfind(dev, &grpaddr->s_addr);
  125. ninfo("Leaving group: %p\n", group);
  126. if (group)
  127. {
  128. /* Cancel the timer and discard any queued Membership Reports.
  129. * Canceling the timer will prevent any new Membership Reports from
  130. * being sent; clearing the flags will discard any pending Membership
  131. * Reports that could interfere with the Leave Group.
  132. */
  133. wd_cancel(&group->wdog);
  134. CLR_SCHEDMSG(group->flags);
  135. CLR_WAITMSG(group->flags);
  136. IGMP_STATINCR(g_netstats.igmp.leaves);
  137. /* Send a leave if the flag is set according to the state diagram */
  138. if (IS_LASTREPORT(group->flags))
  139. {
  140. ninfo("Schedule Leave Group message\n");
  141. IGMP_STATINCR(g_netstats.igmp.leave_sched);
  142. ret = igmp_waitmsg(group, IGMP_LEAVE_GROUP);
  143. if (ret < 0)
  144. {
  145. nerr("ERROR: Failed to schedule message: %d\n", ret);
  146. }
  147. }
  148. /* Free the group structure (state is now Non-Member */
  149. igmp_grpfree(dev, group);
  150. /* And remove the group address from the drivers MAC filter set */
  151. igmp_removemcastmac(dev, (FAR in_addr_t *)&grpaddr->s_addr);
  152. return OK;
  153. }
  154. /* Return ENOENT if the address is not a member of the group */
  155. ninfo("Return -ENOENT\n");
  156. return -ENOENT;
  157. }
  158. #endif /* CONFIG_NET_IGMP */