igmp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /****************************************************************************
  2. * net/igmp/igmp.h
  3. *
  4. * Copyright (C) 2014, 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /* ________________
  36. * | |
  37. * | |
  38. * | |
  39. * | |
  40. * +--------->| Non-Member |<---------+
  41. * | | | |
  42. * | | | |
  43. * | | | |
  44. * | |________________| |
  45. * | | |
  46. * | leave group | join group | leave group
  47. * | (stop timer, |(send report, | (send leave
  48. * | send leave if | set flag, | if flag set)
  49. * | flag set) | start timer) |
  50. * ________|________ | ________|________
  51. * | |<---------+ | |
  52. * | | | |
  53. * | |<-------------------| |
  54. * | | query received | |
  55. * | Delaying Member | (start timer) | Idle Member |
  56. * +---->| |------------------->| |
  57. * | | | report received | |
  58. * | | | (stop timer, | |
  59. * | | | clear flag) | |
  60. * | |_________________|------------------->|_________________|
  61. * | query received | timer expired
  62. * | (reset timer if | (send report,
  63. * | Max Resp Time | set flag)
  64. * | < current timer) |
  65. * +-------------------+
  66. */
  67. #ifndef _NET_IGMP_IGMP_H
  68. #define _NET_IGMP_IGMP_H
  69. /****************************************************************************
  70. * Included Files
  71. ****************************************************************************/
  72. #include <nuttx/config.h>
  73. #include <sys/types.h>
  74. #include <nuttx/wqueue.h>
  75. #include <nuttx/net/ip.h>
  76. #ifdef CONFIG_NET_IGMP
  77. /****************************************************************************
  78. * Pre-processor Definitions
  79. ****************************************************************************/
  80. /* Group flags */
  81. #define IGMP_IDLEMEMBER (1 << 0)
  82. #define IGMP_LASTREPORT (1 << 1)
  83. #define IGMP_SCHEDMSG (1 << 2)
  84. #define IGMP_WAITMSG (1 << 3)
  85. #define SET_IDLEMEMBER(f) do { (f) |= IGMP_IDLEMEMBER; } while (0)
  86. #define SET_LASTREPORT(f) do { (f) |= IGMP_LASTREPORT; } while (0)
  87. #define SET_SCHEDMSG(f) do { (f) |= IGMP_SCHEDMSG; } while (0)
  88. #define SET_WAITMSG(f) do { (f) |= IGMP_WAITMSG; } while (0)
  89. #define CLR_IDLEMEMBER(f) do { (f) &= ~IGMP_IDLEMEMBER; } while (0)
  90. #define CLR_LASTREPORT(f) do { (f) &= ~IGMP_LASTREPORT; } while (0)
  91. #define CLR_SCHEDMSG(f) do { (f) &= ~IGMP_SCHEDMSG; } while (0)
  92. #define CLR_WAITMSG(f) do { (f) &= ~IGMP_WAITMSG; } while (0)
  93. #define IS_IDLEMEMBER(f) (((f) & IGMP_IDLEMEMBER) != 0)
  94. #define IS_LASTREPORT(f) (((f) & IGMP_LASTREPORT) != 0)
  95. #define IS_SCHEDMSG(f) (((f) & IGMP_SCHEDMSG) != 0)
  96. #define IS_WAITMSG(f) (((f) & IGMP_WAITMSG) != 0)
  97. /****************************************************************************
  98. * Public Type Definitions
  99. ****************************************************************************/
  100. /* This structure represents one group member. There is a list of groups
  101. * for each device interface structure.
  102. *
  103. * There will be a group for the all systems group address but this
  104. * will not run the state machine as it is used to kick off reports
  105. * from all the other groups
  106. */
  107. typedef FAR struct wdog_s *WDOG_ID;
  108. struct igmp_group_s
  109. {
  110. struct igmp_group_s *next; /* Implements a singly-linked list */
  111. struct work_s work; /* For deferred timeout operations */
  112. in_addr_t grpaddr; /* Group IPv4 address */
  113. WDOG_ID wdog; /* WDOG used to detect timeouts */
  114. sem_t sem; /* Used to wait for message transmission */
  115. uint8_t ifindex; /* Interface index */
  116. uint8_t flags; /* See IGMP_ flags definitions */
  117. uint8_t msgid; /* Pending message ID (if non-zero) */
  118. };
  119. /****************************************************************************
  120. * Public Data
  121. ****************************************************************************/
  122. #ifdef __cplusplus
  123. # define EXTERN extern "C"
  124. extern "C"
  125. {
  126. #else
  127. # define EXTERN extern
  128. #endif
  129. EXTERN in_addr_t g_ipv4_allsystems;
  130. EXTERN in_addr_t g_ipv4_allrouters;
  131. /****************************************************************************
  132. * Public Function Prototypes
  133. ****************************************************************************/
  134. /****************************************************************************
  135. * Name: igmp_initialize
  136. *
  137. * Description:
  138. * Perform one-time IGMP initialization.
  139. *
  140. ****************************************************************************/
  141. void igmp_initialize(void);
  142. /****************************************************************************
  143. * Name: igmp_devinit
  144. *
  145. * Description:
  146. * Called when a new network device is registered to configure that device
  147. * for IGMP support.
  148. *
  149. ****************************************************************************/
  150. void igmp_devinit(FAR struct net_driver_s *dev);
  151. /****************************************************************************
  152. * Name: igmp_input
  153. *
  154. * Description:
  155. * An IGMP packet has been received.
  156. *
  157. ****************************************************************************/
  158. void igmp_input(struct net_driver_s *dev);
  159. /****************************************************************************
  160. * Name: igmp_grpalloc
  161. *
  162. * Description:
  163. * Allocate a new group from heap memory.
  164. *
  165. ****************************************************************************/
  166. FAR struct igmp_group_s *igmp_grpalloc(FAR struct net_driver_s *dev,
  167. FAR const in_addr_t *addr);
  168. /****************************************************************************
  169. * Name: igmp_grpfind
  170. *
  171. * Description:
  172. * Find an existing group.
  173. *
  174. ****************************************************************************/
  175. FAR struct igmp_group_s *igmp_grpfind(FAR struct net_driver_s *dev,
  176. FAR const in_addr_t *addr);
  177. /****************************************************************************
  178. * Name: igmp_grpallocfind
  179. *
  180. * Description:
  181. * Find an existing group. If not found, create a new group for the
  182. * address.
  183. *
  184. ****************************************************************************/
  185. FAR struct igmp_group_s *igmp_grpallocfind(FAR struct net_driver_s *dev,
  186. FAR const in_addr_t *addr);
  187. /****************************************************************************
  188. * Name: igmp_grpfree
  189. *
  190. * Description:
  191. * Release a previously allocated group.
  192. *
  193. ****************************************************************************/
  194. void igmp_grpfree(FAR struct net_driver_s *dev,
  195. FAR struct igmp_group_s *group);
  196. /****************************************************************************
  197. * Name: igmp_schedmsg
  198. *
  199. * Description:
  200. * Schedule a message to be send at the next driver polling interval.
  201. *
  202. ****************************************************************************/
  203. int igmp_schedmsg(FAR struct igmp_group_s *group, uint8_t msgid);
  204. /****************************************************************************
  205. * Name: igmp_waitmsg
  206. *
  207. * Description:
  208. * Schedule a message to be send at the next driver polling interval and
  209. * block, waiting for the message to be sent.
  210. *
  211. ****************************************************************************/
  212. int igmp_waitmsg(FAR struct igmp_group_s *group, uint8_t msgid);
  213. /****************************************************************************
  214. * Name: igmp_poll
  215. *
  216. * Description:
  217. * Poll the groups associated with the device to see if any IGMP messages
  218. * are pending transfer.
  219. *
  220. * Returned Value:
  221. * Returns a non-zero value if a IGP message is sent.
  222. *
  223. ****************************************************************************/
  224. void igmp_poll(FAR struct net_driver_s *dev);
  225. /****************************************************************************
  226. * Name: igmp_send
  227. *
  228. * Description:
  229. * Sends an IGMP IP packet on a network interface. This function constructs
  230. * the IP header and calculates the IP header checksum.
  231. *
  232. * Input Parameters:
  233. * dev - The device driver structure to use in the send operation.
  234. * group - Describes the multicast group member and identifies the
  235. * message to be sent.
  236. * destipaddr - The IP address of the recipient of the message
  237. * msgid - ID of message to send
  238. *
  239. * Returned Value:
  240. * None
  241. *
  242. * Assumptions:
  243. * The network is locked.
  244. *
  245. ****************************************************************************/
  246. void igmp_send(FAR struct net_driver_s *dev, FAR struct igmp_group_s *group,
  247. FAR in_addr_t *destipaddr, uint8_t msgid);
  248. /****************************************************************************
  249. * Name: igmp_joingroup
  250. *
  251. * Description:
  252. * Add the specified group address to the group.
  253. *
  254. * RFC 2236, 3. Protocol Description:
  255. *
  256. * "When a host joins a multicast group, it should immediately transmit
  257. * an unsolicited Version 2 Membership Report for that group, in case it
  258. * is the first member of that group on the network. To cover the
  259. * possibility of the initial Membership Report being lost or damaged,
  260. * it is recommended that it be repeated once or twice after short
  261. * delays [Unsolicited Report Interval]. (A simple way to accomplish
  262. * this is to send the initial Version 2 Membership Report and then act
  263. * as if a Group-Specific Query was received for that group, and set a
  264. * timer appropriately)."
  265. *
  266. ****************************************************************************/
  267. int igmp_joingroup(FAR struct net_driver_s *dev,
  268. FAR const struct in_addr *grpaddr);
  269. /****************************************************************************
  270. * Name: igmp_leavegroup
  271. *
  272. * Description:
  273. * Remove the specified group address to the group.
  274. *
  275. * RFC 2236, 3. Protocol Description:
  276. *
  277. * "When a host leaves a multicast group, if it was the last host to
  278. * reply to a Query with a Membership Report for that group, it SHOULD
  279. * send a Leave Group message to the all-routers multicast group
  280. * (224.0.0.2). If it was not the last host to reply to a Query, it MAY
  281. * send nothing as there must be another member on the subnet. This is
  282. * an optimization to reduce traffic; a host without sufficient storage
  283. * to remember whether or not it was the last host to reply MAY always
  284. * send a Leave Group message when it leaves a group. Routers SHOULD
  285. * accept a Leave Group message addressed to the group being left, in
  286. * order to accommodate implementations of an earlier version of this
  287. * standard. Leave Group messages are addressed to the all-routers
  288. * group because other group members have no need to know that a host
  289. * has left the group, but it does no harm to address the message to the
  290. * group."
  291. *
  292. ****************************************************************************/
  293. int igmp_leavegroup(FAR struct net_driver_s *dev,
  294. FAR const struct in_addr *grpaddr);
  295. /****************************************************************************
  296. * Name: igmp_startticks and igmp_starttimer
  297. *
  298. * Description:
  299. * Start the IGMP timer with differing time units (ticks or deciseconds).
  300. *
  301. ****************************************************************************/
  302. void igmp_startticks(FAR struct igmp_group_s *group, unsigned int ticks);
  303. void igmp_starttimer(FAR struct igmp_group_s *group, uint8_t decisecs);
  304. /****************************************************************************
  305. * Name: igmp_cmptimer
  306. *
  307. * Description:
  308. * Compare the timer remaining on the watching timer to the deci-second
  309. * value. If maxticks > ticks-remaining, then (1) cancel the timer (to
  310. * avoid race conditions) and return true.
  311. *
  312. * If true is returned then the caller must call igmp_startticks() to
  313. * restart the timer
  314. *
  315. ****************************************************************************/
  316. bool igmp_cmptimer(FAR struct igmp_group_s *group, int maxticks);
  317. /****************************************************************************
  318. * Name: igmp_addmcastmac
  319. *
  320. * Description:
  321. * Add an IGMP MAC address to the device's MAC filter table.
  322. *
  323. ****************************************************************************/
  324. void igmp_addmcastmac(FAR struct net_driver_s *dev, FAR in_addr_t *ip);
  325. /****************************************************************************
  326. * Name: igmp_removemcastmac
  327. *
  328. * Description:
  329. * Remove an IGMP MAC address from the device's MAC filter table.
  330. *
  331. ****************************************************************************/
  332. void igmp_removemcastmac(FAR struct net_driver_s *dev, FAR in_addr_t *ip);
  333. #undef EXTERN
  334. #ifdef __cplusplus
  335. }
  336. #endif
  337. #endif /* CONFIG_NET_IGMP */
  338. #endif /* _NET_IGMP_IGMP_H */