igmp.c 26 KB

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