igmp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /**
  2. * @file
  3. * IGMP - Internet Group Management Protocol
  4. *
  5. * @defgroup igmp IGMP
  6. * @ingroup ip4
  7. * To be called from TCPIP thread
  8. */
  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. * 1. Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * 3. Neither the name of CITEL Technologies Ltd nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY CITEL TECHNOLOGIES AND CONTRIBUTORS ``AS IS''
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL CITEL TECHNOLOGIES OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE.
  36. *
  37. * This file is a contribution to the lwIP TCP/IP stack.
  38. * The Swedish Institute of Computer Science and Adam Dunkels
  39. * are specifically granted permission to redistribute this
  40. * source code.
  41. */
  42. /*-------------------------------------------------------------
  43. Note 1)
  44. Although the rfc requires V1 AND V2 capability
  45. we will only support v2 since now V1 is very old (August 1989)
  46. V1 can be added if required
  47. a debug print and statistic have been implemented to
  48. show this up.
  49. -------------------------------------------------------------
  50. -------------------------------------------------------------
  51. Note 2)
  52. A query for a specific group address (as opposed to ALLHOSTS)
  53. has now been implemented as I am unsure if it is required
  54. a debug print and statistic have been implemented to
  55. show this up.
  56. -------------------------------------------------------------
  57. -------------------------------------------------------------
  58. Note 3)
  59. The router alert rfc 2113 is implemented in outgoing packets
  60. but not checked rigorously incoming
  61. -------------------------------------------------------------
  62. Steve Reynolds
  63. ------------------------------------------------------------*/
  64. /*-----------------------------------------------------------------------------
  65. * RFC 988 - Host extensions for IP multicasting - V0
  66. * RFC 1054 - Host extensions for IP multicasting -
  67. * RFC 1112 - Host extensions for IP multicasting - V1
  68. * RFC 2236 - Internet Group Management Protocol, Version 2 - V2 <- this code is based on this RFC (it's the "de facto" standard)
  69. * RFC 3376 - Internet Group Management Protocol, Version 3 - V3
  70. * RFC 4604 - Using Internet Group Management Protocol Version 3... - V3+
  71. * RFC 2113 - IP Router Alert Option -
  72. *----------------------------------------------------------------------------*/
  73. /*-----------------------------------------------------------------------------
  74. * Includes
  75. *----------------------------------------------------------------------------*/
  76. #include "lwip/opt.h"
  77. #if LWIP_IPV4 && LWIP_IGMP /* don't build if not configured for use in lwipopts.h */
  78. #include "lwip/igmp.h"
  79. #include "lwip/debug.h"
  80. #include "lwip/def.h"
  81. #include "lwip/mem.h"
  82. #include "lwip/ip.h"
  83. #include "lwip/inet_chksum.h"
  84. #include "lwip/netif.h"
  85. #include "lwip/stats.h"
  86. #include "lwip/prot/igmp.h"
  87. #include <string.h>
  88. static struct igmp_group *igmp_lookup_group(struct netif *ifp, const ip4_addr_t *addr);
  89. static err_t igmp_remove_group(struct netif *netif, struct igmp_group *group);
  90. static void igmp_timeout(struct netif *netif, struct igmp_group *group);
  91. static void igmp_start_timer(struct igmp_group *group, u8_t max_time);
  92. static void igmp_delaying_member(struct igmp_group *group, u8_t maxresp);
  93. static err_t igmp_ip_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, struct netif *netif);
  94. static void igmp_send(struct netif *netif, struct igmp_group *group, u8_t type);
  95. static ip4_addr_t allsystems;
  96. static ip4_addr_t allrouters;
  97. /**
  98. * Initialize the IGMP module
  99. */
  100. void
  101. igmp_init(void)
  102. {
  103. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_init: initializing\n"));
  104. IP4_ADDR(&allsystems, 224, 0, 0, 1);
  105. IP4_ADDR(&allrouters, 224, 0, 0, 2);
  106. }
  107. /**
  108. * Start IGMP processing on interface
  109. *
  110. * @param netif network interface on which start IGMP processing
  111. */
  112. err_t
  113. igmp_start(struct netif *netif)
  114. {
  115. struct igmp_group *group;
  116. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: starting IGMP processing on if %p\n", (void *)netif));
  117. group = igmp_lookup_group(netif, &allsystems);
  118. if (group != NULL) {
  119. group->group_state = IGMP_GROUP_IDLE_MEMBER;
  120. group->use++;
  121. /* Allow the igmp messages at the MAC level */
  122. if (netif->igmp_mac_filter != NULL) {
  123. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: igmp_mac_filter(ADD "));
  124. ip4_addr_debug_print_val(IGMP_DEBUG, allsystems);
  125. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", (void *)netif));
  126. netif->igmp_mac_filter(netif, &allsystems, NETIF_ADD_MAC_FILTER);
  127. }
  128. return ERR_OK;
  129. }
  130. return ERR_MEM;
  131. }
  132. /**
  133. * Stop IGMP processing on interface
  134. *
  135. * @param netif network interface on which stop IGMP processing
  136. */
  137. err_t
  138. igmp_stop(struct netif *netif)
  139. {
  140. struct igmp_group *group = netif_igmp_data(netif);
  141. netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_IGMP, NULL);
  142. while (group != NULL) {
  143. struct igmp_group *next = group->next; /* avoid use-after-free below */
  144. /* disable the group at the MAC level */
  145. if (netif->igmp_mac_filter != NULL) {
  146. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_stop: igmp_mac_filter(DEL "));
  147. ip4_addr_debug_print_val(IGMP_DEBUG, group->group_address);
  148. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", (void *)netif));
  149. netif->igmp_mac_filter(netif, &(group->group_address), NETIF_DEL_MAC_FILTER);
  150. }
  151. /* free group */
  152. memp_free(MEMP_IGMP_GROUP, group);
  153. /* move to "next" */
  154. group = next;
  155. }
  156. return ERR_OK;
  157. }
  158. /**
  159. * Report IGMP memberships for this interface
  160. *
  161. * @param netif network interface on which report IGMP memberships
  162. */
  163. void
  164. igmp_report_groups(struct netif *netif)
  165. {
  166. struct igmp_group *group = netif_igmp_data(netif);
  167. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_report_groups: sending IGMP reports on if %p\n", (void *)netif));
  168. /* Skip the first group in the list, it is always the allsystems group added in igmp_start() */
  169. if (group != NULL) {
  170. group = group->next;
  171. }
  172. while (group != NULL) {
  173. igmp_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
  174. group = group->next;
  175. }
  176. }
  177. /**
  178. * Search for a group in the netif's igmp group list
  179. *
  180. * @param ifp the network interface for which to look
  181. * @param addr the group ip address to search for
  182. * @return a struct igmp_group* if the group has been found,
  183. * NULL if the group wasn't found.
  184. */
  185. struct igmp_group *
  186. igmp_lookfor_group(struct netif *ifp, const ip4_addr_t *addr)
  187. {
  188. struct igmp_group *group = netif_igmp_data(ifp);
  189. while (group != NULL) {
  190. if (ip4_addr_cmp(&(group->group_address), addr)) {
  191. return group;
  192. }
  193. group = group->next;
  194. }
  195. /* to be clearer, we return NULL here instead of
  196. * 'group' (which is also NULL at this point).
  197. */
  198. return NULL;
  199. }
  200. /**
  201. * Search for a specific igmp group and create a new one if not found-
  202. *
  203. * @param ifp the network interface for which to look
  204. * @param addr the group ip address to search
  205. * @return a struct igmp_group*,
  206. * NULL on memory error.
  207. */
  208. static struct igmp_group *
  209. igmp_lookup_group(struct netif *ifp, const ip4_addr_t *addr)
  210. {
  211. struct igmp_group *group;
  212. struct igmp_group *list_head = netif_igmp_data(ifp);
  213. /* Search if the group already exists */
  214. group = igmp_lookfor_group(ifp, addr);
  215. if (group != NULL) {
  216. /* Group already exists. */
  217. return group;
  218. }
  219. /* Group doesn't exist yet, create a new one */
  220. group = (struct igmp_group *)memp_malloc(MEMP_IGMP_GROUP);
  221. if (group != NULL) {
  222. ip4_addr_set(&(group->group_address), addr);
  223. group->timer = 0; /* Not running */
  224. group->group_state = IGMP_GROUP_NON_MEMBER;
  225. group->last_reporter_flag = 0;
  226. group->use = 0;
  227. /* Ensure allsystems group is always first in list */
  228. if (list_head == NULL) {
  229. /* this is the first entry in linked list */
  230. LWIP_ASSERT("igmp_lookup_group: first group must be allsystems",
  231. (ip4_addr_cmp(addr, &allsystems) != 0));
  232. group->next = NULL;
  233. netif_set_client_data(ifp, LWIP_NETIF_CLIENT_DATA_INDEX_IGMP, group);
  234. } else {
  235. /* append _after_ first entry */
  236. LWIP_ASSERT("igmp_lookup_group: all except first group must not be allsystems",
  237. (ip4_addr_cmp(addr, &allsystems) == 0));
  238. group->next = list_head->next;
  239. list_head->next = group;
  240. }
  241. }
  242. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_lookup_group: %sallocated a new group with address ", (group ? "" : "impossible to ")));
  243. ip4_addr_debug_print(IGMP_DEBUG, addr);
  244. LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", (void *)ifp));
  245. return group;
  246. }
  247. /**
  248. * Remove a group from netif's igmp group list, but don't free it yet
  249. *
  250. * @param group the group to remove from the netif's igmp group list
  251. * @return ERR_OK if group was removed from the list, an err_t otherwise
  252. */
  253. static err_t
  254. igmp_remove_group(struct netif *netif, struct igmp_group *group)
  255. {
  256. err_t err = ERR_OK;
  257. struct igmp_group *tmp_group;
  258. /* Skip the first group in the list, it is always the allsystems group added in igmp_start() */
  259. for (tmp_group = netif_igmp_data(netif); tmp_group != NULL; tmp_group = tmp_group->next) {
  260. if (tmp_group->next == group) {
  261. tmp_group->next = group->next;
  262. break;
  263. }
  264. }
  265. /* Group not found in netif's igmp group list */
  266. if (tmp_group == NULL) {
  267. err = ERR_ARG;
  268. }
  269. return err;
  270. }
  271. /**
  272. * Called from ip_input() if a new IGMP packet is received.
  273. *
  274. * @param p received igmp packet, p->payload pointing to the igmp header
  275. * @param inp network interface on which the packet was received
  276. * @param dest destination ip address of the igmp packet
  277. */
  278. void
  279. igmp_input(struct pbuf *p, struct netif *inp, const ip4_addr_t *dest)
  280. {
  281. struct igmp_msg *igmp;
  282. struct igmp_group *group;
  283. struct igmp_group *groupref;
  284. IGMP_STATS_INC(igmp.recv);
  285. /* Note that the length CAN be greater than 8 but only 8 are used - All are included in the checksum */
  286. if (p->len < IGMP_MINLEN) {
  287. pbuf_free(p);
  288. IGMP_STATS_INC(igmp.lenerr);
  289. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: length error\n"));
  290. return;
  291. }
  292. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: message from "));
  293. ip4_addr_debug_print_val(IGMP_DEBUG, ip4_current_header()->src);
  294. LWIP_DEBUGF(IGMP_DEBUG, (" to address "));
  295. ip4_addr_debug_print_val(IGMP_DEBUG, ip4_current_header()->dest);
  296. LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", (void *)inp));
  297. /* Now calculate and check the checksum */
  298. igmp = (struct igmp_msg *)p->payload;
  299. if (inet_chksum(igmp, p->len)) {
  300. pbuf_free(p);
  301. IGMP_STATS_INC(igmp.chkerr);
  302. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: checksum error\n"));
  303. return;
  304. }
  305. /* Packet is ok so find an existing group */
  306. group = igmp_lookfor_group(inp, dest); /* use the destination IP address of incoming packet */
  307. /* If group can be found or create... */
  308. if (!group) {
  309. pbuf_free(p);
  310. IGMP_STATS_INC(igmp.drop);
  311. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP frame not for us\n"));
  312. return;
  313. }
  314. /* NOW ACT ON THE INCOMING MESSAGE TYPE... */
  315. switch (igmp->igmp_msgtype) {
  316. case IGMP_MEMB_QUERY:
  317. /* IGMP_MEMB_QUERY to the "all systems" address ? */
  318. if ((ip4_addr_cmp(dest, &allsystems)) && ip4_addr_isany(&igmp->igmp_group_address)) {
  319. /* THIS IS THE GENERAL QUERY */
  320. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: General IGMP_MEMB_QUERY on \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
  321. if (igmp->igmp_maxresp == 0) {
  322. IGMP_STATS_INC(igmp.rx_v1);
  323. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: got an all hosts query with time== 0 - this is V1 and not implemented - treat as v2\n"));
  324. igmp->igmp_maxresp = IGMP_V1_DELAYING_MEMBER_TMR;
  325. } else {
  326. IGMP_STATS_INC(igmp.rx_general);
  327. }
  328. groupref = netif_igmp_data(inp);
  329. /* Do not send messages on the all systems group address! */
  330. /* Skip the first group in the list, it is always the allsystems group added in igmp_start() */
  331. if (groupref != NULL) {
  332. groupref = groupref->next;
  333. }
  334. while (groupref) {
  335. igmp_delaying_member(groupref, igmp->igmp_maxresp);
  336. groupref = groupref->next;
  337. }
  338. } else {
  339. /* IGMP_MEMB_QUERY to a specific group ? */
  340. if (!ip4_addr_isany(&igmp->igmp_group_address)) {
  341. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_MEMB_QUERY to a specific group "));
  342. ip4_addr_debug_print_val(IGMP_DEBUG, igmp->igmp_group_address);
  343. if (ip4_addr_cmp(dest, &allsystems)) {
  344. ip4_addr_t groupaddr;
  345. LWIP_DEBUGF(IGMP_DEBUG, (" using \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
  346. /* we first need to re-look for the group since we used dest last time */
  347. ip4_addr_copy(groupaddr, igmp->igmp_group_address);
  348. group = igmp_lookfor_group(inp, &groupaddr);
  349. } else {
  350. LWIP_DEBUGF(IGMP_DEBUG, (" with the group address as destination [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
  351. }
  352. if (group != NULL) {
  353. IGMP_STATS_INC(igmp.rx_group);
  354. igmp_delaying_member(group, igmp->igmp_maxresp);
  355. } else {
  356. IGMP_STATS_INC(igmp.drop);
  357. }
  358. } else {
  359. IGMP_STATS_INC(igmp.proterr);
  360. }
  361. }
  362. break;
  363. case IGMP_V2_MEMB_REPORT:
  364. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_V2_MEMB_REPORT\n"));
  365. IGMP_STATS_INC(igmp.rx_report);
  366. if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
  367. /* This is on a specific group we have already looked up */
  368. group->timer = 0; /* stopped */
  369. group->group_state = IGMP_GROUP_IDLE_MEMBER;
  370. group->last_reporter_flag = 0;
  371. }
  372. break;
  373. default:
  374. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: unexpected msg %d in state %d on group %p on if %p\n",
  375. igmp->igmp_msgtype, group->group_state, (void *)&group, (void *)inp));
  376. IGMP_STATS_INC(igmp.proterr);
  377. break;
  378. }
  379. pbuf_free(p);
  380. return;
  381. }
  382. /**
  383. * @ingroup igmp
  384. * Join a group on one network interface.
  385. *
  386. * @param ifaddr ip address of the network interface which should join a new group
  387. * @param groupaddr the ip address of the group which to join
  388. * @return ERR_OK if group was joined on the netif(s), an err_t otherwise
  389. */
  390. err_t
  391. igmp_joingroup(const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr)
  392. {
  393. err_t err = ERR_VAL; /* no matching interface */
  394. struct netif *netif;
  395. LWIP_ASSERT_CORE_LOCKED();
  396. /* make sure it is multicast address */
  397. LWIP_ERROR("igmp_joingroup: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
  398. LWIP_ERROR("igmp_joingroup: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
  399. /* loop through netif's */
  400. NETIF_FOREACH(netif) {
  401. /* Should we join this interface ? */
  402. if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ifaddr) || ip4_addr_cmp(netif_ip4_addr(netif), ifaddr)))) {
  403. err = igmp_joingroup_netif(netif, groupaddr);
  404. if (err != ERR_OK) {
  405. /* Return an error even if some network interfaces are joined */
  406. /** @todo undo any other netif already joined */
  407. return err;
  408. }
  409. }
  410. }
  411. return err;
  412. }
  413. /**
  414. * @ingroup igmp
  415. * Join a group on one network interface.
  416. *
  417. * @param netif the network interface which should join a new group
  418. * @param groupaddr the ip address of the group which to join
  419. * @return ERR_OK if group was joined on the netif, an err_t otherwise
  420. */
  421. err_t
  422. igmp_joingroup_netif(struct netif *netif, const ip4_addr_t *groupaddr)
  423. {
  424. struct igmp_group *group;
  425. LWIP_ASSERT_CORE_LOCKED();
  426. /* make sure it is multicast address */
  427. LWIP_ERROR("igmp_joingroup_netif: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
  428. LWIP_ERROR("igmp_joingroup_netif: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
  429. /* make sure it is an igmp-enabled netif */
  430. LWIP_ERROR("igmp_joingroup_netif: attempt to join on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;);
  431. /* find group or create a new one if not found */
  432. group = igmp_lookup_group(netif, groupaddr);
  433. if (group != NULL) {
  434. /* This should create a new group, check the state to make sure */
  435. if (group->group_state != IGMP_GROUP_NON_MEMBER) {
  436. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup_netif: join to group not in state IGMP_GROUP_NON_MEMBER\n"));
  437. } else {
  438. /* OK - it was new group */
  439. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup_netif: join to new group: "));
  440. ip4_addr_debug_print(IGMP_DEBUG, groupaddr);
  441. LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
  442. /* If first use of the group, allow the group at the MAC level */
  443. if ((group->use == 0) && (netif->igmp_mac_filter != NULL)) {
  444. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup_netif: igmp_mac_filter(ADD "));
  445. ip4_addr_debug_print(IGMP_DEBUG, groupaddr);
  446. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", (void *)netif));
  447. netif->igmp_mac_filter(netif, groupaddr, NETIF_ADD_MAC_FILTER);
  448. }
  449. IGMP_STATS_INC(igmp.tx_join);
  450. igmp_send(netif, group, IGMP_V2_MEMB_REPORT);
  451. igmp_start_timer(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
  452. /* Need to work out where this timer comes from */
  453. group->group_state = IGMP_GROUP_DELAYING_MEMBER;
  454. }
  455. /* Increment group use */
  456. group->use++;
  457. /* Join on this interface */
  458. return ERR_OK;
  459. } else {
  460. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup_netif: Not enough memory to join to group\n"));
  461. return ERR_MEM;
  462. }
  463. }
  464. /**
  465. * @ingroup igmp
  466. * Leave a group on one network interface.
  467. *
  468. * @param ifaddr ip address of the network interface which should leave a group
  469. * @param groupaddr the ip address of the group which to leave
  470. * @return ERR_OK if group was left on the netif(s), an err_t otherwise
  471. */
  472. err_t
  473. igmp_leavegroup(const ip4_addr_t *ifaddr, const ip4_addr_t *groupaddr)
  474. {
  475. err_t err = ERR_VAL; /* no matching interface */
  476. struct netif *netif;
  477. LWIP_ASSERT_CORE_LOCKED();
  478. /* make sure it is multicast address */
  479. LWIP_ERROR("igmp_leavegroup: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
  480. LWIP_ERROR("igmp_leavegroup: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
  481. /* loop through netif's */
  482. NETIF_FOREACH(netif) {
  483. /* Should we leave this interface ? */
  484. if ((netif->flags & NETIF_FLAG_IGMP) && ((ip4_addr_isany(ifaddr) || ip4_addr_cmp(netif_ip4_addr(netif), ifaddr)))) {
  485. err_t res = igmp_leavegroup_netif(netif, groupaddr);
  486. if (err != ERR_OK) {
  487. /* Store this result if we have not yet gotten a success */
  488. err = res;
  489. }
  490. }
  491. }
  492. return err;
  493. }
  494. /**
  495. * @ingroup igmp
  496. * Leave a group on one network interface.
  497. *
  498. * @param netif the network interface which should leave a group
  499. * @param groupaddr the ip address of the group which to leave
  500. * @return ERR_OK if group was left on the netif, an err_t otherwise
  501. */
  502. err_t
  503. igmp_leavegroup_netif(struct netif *netif, const ip4_addr_t *groupaddr)
  504. {
  505. struct igmp_group *group;
  506. LWIP_ASSERT_CORE_LOCKED();
  507. /* make sure it is multicast address */
  508. LWIP_ERROR("igmp_leavegroup_netif: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr), return ERR_VAL;);
  509. LWIP_ERROR("igmp_leavegroup_netif: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
  510. /* make sure it is an igmp-enabled netif */
  511. LWIP_ERROR("igmp_leavegroup_netif: attempt to leave on non-IGMP netif", netif->flags & NETIF_FLAG_IGMP, return ERR_VAL;);
  512. /* find group */
  513. group = igmp_lookfor_group(netif, groupaddr);
  514. if (group != NULL) {
  515. /* Only send a leave if the flag is set according to the state diagram */
  516. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup_netif: Leaving group: "));
  517. ip4_addr_debug_print(IGMP_DEBUG, groupaddr);
  518. LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
  519. /* If there is no other use of the group */
  520. if (group->use <= 1) {
  521. /* Remove the group from the list */
  522. igmp_remove_group(netif, group);
  523. /* If we are the last reporter for this group */
  524. if (group->last_reporter_flag) {
  525. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup_netif: sending leaving group\n"));
  526. IGMP_STATS_INC(igmp.tx_leave);
  527. igmp_send(netif, group, IGMP_LEAVE_GROUP);
  528. }
  529. /* Disable the group at the MAC level */
  530. if (netif->igmp_mac_filter != NULL) {
  531. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup_netif: igmp_mac_filter(DEL "));
  532. ip4_addr_debug_print(IGMP_DEBUG, groupaddr);
  533. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", (void *)netif));
  534. netif->igmp_mac_filter(netif, groupaddr, NETIF_DEL_MAC_FILTER);
  535. }
  536. /* Free group struct */
  537. memp_free(MEMP_IGMP_GROUP, group);
  538. } else {
  539. /* Decrement group use */
  540. group->use--;
  541. }
  542. return ERR_OK;
  543. } else {
  544. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup_netif: not member of group\n"));
  545. return ERR_VAL;
  546. }
  547. }
  548. /**
  549. * The igmp timer function (both for NO_SYS=1 and =0)
  550. * Should be called every IGMP_TMR_INTERVAL milliseconds (100 ms is default).
  551. */
  552. void
  553. igmp_tmr(void)
  554. {
  555. struct netif *netif;
  556. NETIF_FOREACH(netif) {
  557. struct igmp_group *group = netif_igmp_data(netif);
  558. while (group != NULL) {
  559. if (group->timer > 0) {
  560. group->timer--;
  561. if (group->timer == 0) {
  562. igmp_timeout(netif, group);
  563. }
  564. }
  565. group = group->next;
  566. }
  567. }
  568. }
  569. /**
  570. * Called if a timeout for one group is reached.
  571. * Sends a report for this group.
  572. *
  573. * @param group an igmp_group for which a timeout is reached
  574. */
  575. static void
  576. igmp_timeout(struct netif *netif, struct igmp_group *group)
  577. {
  578. /* If the state is IGMP_GROUP_DELAYING_MEMBER then we send a report for this group
  579. (unless it is the allsystems group) */
  580. if ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) &&
  581. (!(ip4_addr_cmp(&(group->group_address), &allsystems)))) {
  582. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_timeout: report membership for group with address "));
  583. ip4_addr_debug_print_val(IGMP_DEBUG, group->group_address);
  584. LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", (void *)netif));
  585. group->group_state = IGMP_GROUP_IDLE_MEMBER;
  586. IGMP_STATS_INC(igmp.tx_report);
  587. igmp_send(netif, group, IGMP_V2_MEMB_REPORT);
  588. }
  589. }
  590. /**
  591. * Start a timer for an igmp group
  592. *
  593. * @param group the igmp_group for which to start a timer
  594. * @param max_time the time in multiples of IGMP_TMR_INTERVAL (decrease with
  595. * every call to igmp_tmr())
  596. */
  597. static void
  598. igmp_start_timer(struct igmp_group *group, u8_t max_time)
  599. {
  600. #ifdef LWIP_RAND
  601. group->timer = (u16_t)(max_time > 2 ? (LWIP_RAND() % max_time) : 1);
  602. #else /* LWIP_RAND */
  603. /* ATTENTION: use this only if absolutely necessary! */
  604. group->timer = max_time / 2;
  605. #endif /* LWIP_RAND */
  606. if (group->timer == 0) {
  607. group->timer = 1;
  608. }
  609. }
  610. /**
  611. * Delaying membership report for a group if necessary
  612. *
  613. * @param group the igmp_group for which "delaying" membership report
  614. * @param maxresp query delay
  615. */
  616. static void
  617. igmp_delaying_member(struct igmp_group *group, u8_t maxresp)
  618. {
  619. if ((group->group_state == IGMP_GROUP_IDLE_MEMBER) ||
  620. ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) &&
  621. ((group->timer == 0) || (maxresp < group->timer)))) {
  622. igmp_start_timer(group, maxresp);
  623. group->group_state = IGMP_GROUP_DELAYING_MEMBER;
  624. }
  625. }
  626. /**
  627. * Sends an IP packet on a network interface. This function constructs the IP header
  628. * and calculates the IP header checksum. If the source IP address is NULL,
  629. * the IP address of the outgoing network interface is filled in as source address.
  630. *
  631. * @param p the packet to send (p->payload points to the data, e.g. next
  632. protocol header; if dest == LWIP_IP_HDRINCL, p already includes an
  633. IP header and p->payload points to that IP header)
  634. * @param src the source IP address to send from (if src == IP4_ADDR_ANY, the
  635. * IP address of the netif used to send is used as source address)
  636. * @param dest the destination IP address to send the packet to
  637. * @param netif the netif on which to send this packet
  638. * @return ERR_OK if the packet was sent OK
  639. * ERR_BUF if p doesn't have enough space for IP/LINK headers
  640. * returns errors returned by netif->output
  641. */
  642. static err_t
  643. igmp_ip_output_if(struct pbuf *p, const ip4_addr_t *src, const ip4_addr_t *dest, struct netif *netif)
  644. {
  645. /* This is the "router alert" option */
  646. u16_t ra[2];
  647. ra[0] = PP_HTONS(ROUTER_ALERT);
  648. ra[1] = 0x0000; /* Router shall examine packet */
  649. IGMP_STATS_INC(igmp.xmit);
  650. return ip4_output_if_opt(p, src, dest, IGMP_TTL, 0, IP_PROTO_IGMP, netif, ra, ROUTER_ALERTLEN);
  651. }
  652. /**
  653. * Send an igmp packet to a specific group.
  654. *
  655. * @param group the group to which to send the packet
  656. * @param type the type of igmp packet to send
  657. */
  658. static void
  659. igmp_send(struct netif *netif, struct igmp_group *group, u8_t type)
  660. {
  661. struct pbuf *p = NULL;
  662. struct igmp_msg *igmp = NULL;
  663. ip4_addr_t src = *IP4_ADDR_ANY4;
  664. ip4_addr_t *dest = NULL;
  665. /* IP header + "router alert" option + IGMP header */
  666. p = pbuf_alloc(PBUF_TRANSPORT, IGMP_MINLEN, PBUF_RAM);
  667. if (p) {
  668. igmp = (struct igmp_msg *)p->payload;
  669. LWIP_ASSERT("igmp_send: check that first pbuf can hold struct igmp_msg",
  670. (p->len >= sizeof(struct igmp_msg)));
  671. ip4_addr_copy(src, *netif_ip4_addr(netif));
  672. if (type == IGMP_V2_MEMB_REPORT) {
  673. dest = &(group->group_address);
  674. ip4_addr_copy(igmp->igmp_group_address, group->group_address);
  675. group->last_reporter_flag = 1; /* Remember we were the last to report */
  676. } else {
  677. if (type == IGMP_LEAVE_GROUP) {
  678. dest = &allrouters;
  679. ip4_addr_copy(igmp->igmp_group_address, group->group_address);
  680. }
  681. }
  682. if ((type == IGMP_V2_MEMB_REPORT) || (type == IGMP_LEAVE_GROUP)) {
  683. igmp->igmp_msgtype = type;
  684. igmp->igmp_maxresp = 0;
  685. igmp->igmp_checksum = 0;
  686. igmp->igmp_checksum = inet_chksum(igmp, IGMP_MINLEN);
  687. igmp_ip_output_if(p, &src, dest, netif);
  688. }
  689. pbuf_free(p);
  690. } else {
  691. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_send: not enough memory for igmp_send\n"));
  692. IGMP_STATS_INC(igmp.memerr);
  693. }
  694. }
  695. #endif /* LWIP_IPV4 && LWIP_IGMP */