igmp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. #ifdef MEMLEAK_DEBUG
  88. static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
  89. #endif
  90. //#define DYC_IGMP_DEBUG
  91. #ifdef DYC_IGMP_DEBUG
  92. #define IGMP_LOG os_printf
  93. #else
  94. #define IGMP_LOG //os_printf
  95. #endif
  96. /*
  97. * IGMP constants
  98. */
  99. #define IGMP_TTL 1
  100. #define IGMP_MINLEN 8
  101. #define ROUTER_ALERT 0x9404
  102. #define ROUTER_ALERTLEN 4
  103. /*
  104. * IGMP message types, including version number.
  105. */
  106. #define IGMP_MEMB_QUERY 0x11 /* Membership query */
  107. #define IGMP_V1_MEMB_REPORT 0x12 /* Ver. 1 membership report */
  108. #define IGMP_V2_MEMB_REPORT 0x16 /* Ver. 2 membership report */
  109. #define IGMP_LEAVE_GROUP 0x17 /* Leave-group message */
  110. /* Group membership states */
  111. #define IGMP_GROUP_NON_MEMBER 0
  112. #define IGMP_GROUP_DELAYING_MEMBER 1
  113. #define IGMP_GROUP_IDLE_MEMBER 2
  114. /**
  115. * IGMP packet format.
  116. */
  117. #ifdef PACK_STRUCT_USE_INCLUDES
  118. # include "arch/bpstruct.h"
  119. #endif
  120. PACK_STRUCT_BEGIN
  121. struct igmp_msg {
  122. PACK_STRUCT_FIELD(u8_t igmp_msgtype);
  123. PACK_STRUCT_FIELD(u8_t igmp_maxresp);
  124. PACK_STRUCT_FIELD(u16_t igmp_checksum);
  125. PACK_STRUCT_FIELD(ip_addr_p_t igmp_group_address);
  126. } PACK_STRUCT_STRUCT;
  127. PACK_STRUCT_END
  128. #ifdef PACK_STRUCT_USE_INCLUDES
  129. # include "arch/epstruct.h"
  130. #endif
  131. static struct igmp_group *igmp_lookup_group(struct netif *ifp, ip_addr_t *addr)ICACHE_FLASH_ATTR;
  132. static err_t igmp_remove_group(struct igmp_group *group)ICACHE_FLASH_ATTR;
  133. static void igmp_timeout( struct igmp_group *group)ICACHE_FLASH_ATTR;
  134. static void igmp_start_timer(struct igmp_group *group, u8_t max_time)ICACHE_FLASH_ATTR;
  135. static void igmp_stop_timer(struct igmp_group *group)ICACHE_FLASH_ATTR;
  136. static void igmp_delaying_member(struct igmp_group *group, u8_t maxresp)ICACHE_FLASH_ATTR;
  137. static err_t igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, struct netif *netif)ICACHE_FLASH_ATTR;
  138. static void igmp_send(struct igmp_group *group, u8_t type)ICACHE_FLASH_ATTR;
  139. static struct igmp_group* igmp_group_list = NULL;
  140. static ip_addr_t allsystems;
  141. static ip_addr_t allrouters;
  142. /**
  143. * Initialize the IGMP module
  144. */
  145. void
  146. igmp_init(void)
  147. {
  148. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_init: initializing\n"));
  149. IP4_ADDR(&allsystems, 224, 0, 0, 1);
  150. IP4_ADDR(&allrouters, 224, 0, 0, 2);
  151. }
  152. //#ifdef LWIP_DEBUG
  153. #ifdef DYC_IGMP_DEBUG
  154. /**
  155. * Dump global IGMP groups list
  156. */
  157. void
  158. igmp_dump_group_list()
  159. {
  160. struct igmp_group *group = igmp_group_list;
  161. IGMP_LOG("igmp_dump:\n");
  162. while (group != NULL) {
  163. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_dump_group_list: [%"U32_F"] ", (u32_t)(group->group_state)));
  164. ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
  165. LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
  166. if(group!=NULL)
  167. IGMP_LOG("group:%p,netif:%p\n",group,group->netif);
  168. group = group->next;
  169. }
  170. LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
  171. IGMP_LOG("\n");
  172. }
  173. #else
  174. #define igmp_dump_group_list()
  175. #endif /* LWIP_DEBUG */
  176. /**
  177. * Start IGMP processing on interface
  178. *
  179. * @param netif network interface on which start IGMP processing
  180. */
  181. err_t
  182. igmp_start(struct netif *netif)
  183. {
  184. struct igmp_group* group;
  185. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: starting IGMP processing on if %p\n", netif));
  186. group = igmp_lookup_group(netif, &allsystems);
  187. if (group != NULL) {
  188. group->group_state = IGMP_GROUP_IDLE_MEMBER;
  189. group->use++;
  190. /* Allow the igmp messages at the MAC level */
  191. if (netif->igmp_mac_filter != NULL) {
  192. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_start: igmp_mac_filter(ADD "));
  193. ip_addr_debug_print(IGMP_DEBUG, &allsystems);
  194. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
  195. netif->igmp_mac_filter(netif, &allsystems, IGMP_ADD_MAC_FILTER);
  196. }
  197. return ERR_OK;
  198. }
  199. return ERR_MEM;
  200. }
  201. /**
  202. * Stop IGMP processing on interface
  203. *
  204. * @param netif network interface on which stop IGMP processing
  205. */
  206. err_t
  207. igmp_stop(struct netif *netif)
  208. {
  209. struct igmp_group *group = igmp_group_list;
  210. struct igmp_group *prev = NULL;
  211. struct igmp_group *next;
  212. /* look for groups joined on this interface further down the list */
  213. while (group != NULL) {
  214. next = group->next;
  215. /* is it a group joined on this interface? */
  216. if (group->netif == netif) {
  217. IGMP_LOG("stop igmp:%p,%p,",group,group->netif);
  218. /* is it the first group of the list? */
  219. if (group == igmp_group_list) {
  220. igmp_group_list = next;
  221. }
  222. /* is there a "previous" group defined? */
  223. if (prev != NULL) {
  224. prev->next = next;
  225. }
  226. /* disable the group at the MAC level */
  227. if (netif->igmp_mac_filter != NULL) {
  228. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_stop: igmp_mac_filter(DEL "));
  229. ip_addr_debug_print(IGMP_DEBUG, &group->group_address);
  230. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
  231. netif->igmp_mac_filter(netif, &(group->group_address), IGMP_DEL_MAC_FILTER);
  232. }
  233. /* free group */
  234. memp_free(MEMP_IGMP_GROUP, group);
  235. igmp_dump_group_list();
  236. } else {
  237. /* change the "previous" */
  238. prev = group;
  239. }
  240. /* move to "next" */
  241. group = next;
  242. }
  243. return ERR_OK;
  244. }
  245. /**
  246. * Report IGMP memberships for this interface
  247. *
  248. * @param netif network interface on which report IGMP memberships
  249. */
  250. void
  251. igmp_report_groups(struct netif *netif)
  252. {
  253. struct igmp_group *group = igmp_group_list;
  254. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_report_groups: sending IGMP reports on if %p\n", netif));
  255. while (group != NULL) {
  256. if (group->netif == netif) {
  257. igmp_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
  258. }
  259. group = group->next;
  260. }
  261. }
  262. /**
  263. * Search for a group in the global igmp_group_list
  264. *
  265. * @param ifp the network interface for which to look
  266. * @param addr the group ip address to search for
  267. * @return a struct igmp_group* if the group has been found,
  268. * NULL if the group wasn't found.
  269. */
  270. struct igmp_group *
  271. igmp_lookfor_group(struct netif *ifp, ip_addr_t *addr)
  272. {
  273. struct igmp_group *group = igmp_group_list;
  274. while (group != NULL) {
  275. if ((group->netif == ifp) && (ip_addr_cmp(&(group->group_address), addr))) {
  276. return group;
  277. }
  278. group = group->next;
  279. }
  280. /* to be clearer, we return NULL here instead of
  281. * 'group' (which is also NULL at this point).
  282. */
  283. return NULL;
  284. }
  285. /**
  286. * Search for a specific igmp group and create a new one if not found-
  287. *
  288. * @param ifp the network interface for which to look
  289. * @param addr the group ip address to search
  290. * @return a struct igmp_group*,
  291. * NULL on memory error.
  292. */
  293. struct igmp_group *
  294. igmp_lookup_group(struct netif *ifp, ip_addr_t *addr)
  295. {
  296. struct igmp_group *group = igmp_group_list;
  297. /* Search if the group already exists */
  298. group = igmp_lookfor_group(ifp, addr);
  299. if (group != NULL) {
  300. /* Group already exists. */
  301. return group;
  302. }
  303. /* Group doesn't exist yet, create a new one */
  304. group = (struct igmp_group *)memp_malloc(MEMP_IGMP_GROUP);
  305. if (group != NULL) {
  306. group->netif = ifp;
  307. ip_addr_set(&(group->group_address), addr);
  308. group->timer = 0; /* Not running */
  309. group->group_state = IGMP_GROUP_NON_MEMBER;
  310. group->last_reporter_flag = 0;
  311. group->use = 0;
  312. group->next = igmp_group_list;
  313. igmp_group_list = group;
  314. }
  315. IGMP_LOG("add igmp:%p,%p,",group,group->netif);
  316. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_lookup_group: %sallocated a new group with address ", (group?"":"impossible to ")));
  317. ip_addr_debug_print(IGMP_DEBUG, addr);
  318. LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", ifp));
  319. igmp_dump_group_list();
  320. return group;
  321. }
  322. /**
  323. * Remove a group in the global igmp_group_list
  324. *
  325. * @param group the group to remove from the global igmp_group_list
  326. * @return ERR_OK if group was removed from the list, an err_t otherwise
  327. */
  328. static err_t
  329. igmp_remove_group(struct igmp_group *group)
  330. {
  331. err_t err = ERR_OK;
  332. IGMP_LOG("rmv igmp:%p,%p,",group,group->netif);
  333. /* Is it the first group? */
  334. if (igmp_group_list == group) {
  335. igmp_group_list = group->next;
  336. } else {
  337. /* look for group further down the list */
  338. struct igmp_group *tmpGroup;
  339. for (tmpGroup = igmp_group_list; tmpGroup != NULL; tmpGroup = tmpGroup->next) {
  340. if (tmpGroup->next == group) {
  341. tmpGroup->next = group->next;
  342. break;
  343. }
  344. }
  345. /* Group not found in the global igmp_group_list */
  346. if (tmpGroup == NULL)
  347. err = ERR_ARG;
  348. }
  349. /* free group */
  350. memp_free(MEMP_IGMP_GROUP, group);
  351. igmp_dump_group_list();
  352. return err;
  353. }
  354. /**
  355. * Called from ip_input() if a new IGMP packet is received.
  356. *
  357. * @param p received igmp packet, p->payload pointing to the ip header
  358. * @param inp network interface on which the packet was received
  359. * @param dest destination ip address of the igmp packet
  360. */
  361. void
  362. igmp_input(struct pbuf *p, struct netif *inp, ip_addr_t *dest)
  363. {
  364. struct ip_hdr * iphdr;
  365. struct igmp_msg* igmp;
  366. struct igmp_group* group;
  367. struct igmp_group* groupref;
  368. IGMP_STATS_INC(igmp.recv);
  369. /* Note that the length CAN be greater than 8 but only 8 are used - All are included in the checksum */
  370. iphdr = (struct ip_hdr *)p->payload;
  371. if (pbuf_header(p, -(s16_t)(IPH_HL(iphdr) * 4)) || (p->len < IGMP_MINLEN)) {
  372. pbuf_free(p);
  373. IGMP_STATS_INC(igmp.lenerr);
  374. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: length error\n"));
  375. return;
  376. }
  377. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: message from "));
  378. ip_addr_debug_print(IGMP_DEBUG, &(iphdr->src));
  379. LWIP_DEBUGF(IGMP_DEBUG, (" to address "));
  380. ip_addr_debug_print(IGMP_DEBUG, &(iphdr->dest));
  381. LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", inp));
  382. /* Now calculate and check the checksum */
  383. igmp = (struct igmp_msg *)p->payload;
  384. if (inet_chksum(igmp, p->len)) {
  385. pbuf_free(p);
  386. IGMP_STATS_INC(igmp.chkerr);
  387. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: checksum error\n"));
  388. return;
  389. }
  390. /* Packet is ok so find an existing group */
  391. group = igmp_lookfor_group(inp, dest); /* use the destination IP address of incoming packet */
  392. /* If group can be found or create... */
  393. if (!group) {
  394. pbuf_free(p);
  395. IGMP_STATS_INC(igmp.drop);
  396. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP frame not for us\n"));
  397. return;
  398. }
  399. /* NOW ACT ON THE INCOMING MESSAGE TYPE... */
  400. switch (igmp->igmp_msgtype) {
  401. case IGMP_MEMB_QUERY: {
  402. /* IGMP_MEMB_QUERY to the "all systems" address ? */
  403. if ((ip_addr_cmp(dest, &allsystems)) && ip_addr_isany(&igmp->igmp_group_address)) {
  404. /* THIS IS THE GENERAL QUERY */
  405. 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)));
  406. if (igmp->igmp_maxresp == 0) {
  407. IGMP_STATS_INC(igmp.rx_v1);
  408. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: got an all hosts query with time== 0 - this is V1 and not implemented - treat as v2\n"));
  409. igmp->igmp_maxresp = IGMP_V1_DELAYING_MEMBER_TMR;
  410. } else {
  411. IGMP_STATS_INC(igmp.rx_general);
  412. }
  413. groupref = igmp_group_list;
  414. while (groupref) {
  415. /* Do not send messages on the all systems group address! */
  416. if ((groupref->netif == inp) && (!(ip_addr_cmp(&(groupref->group_address), &allsystems)))) {
  417. igmp_delaying_member(groupref, igmp->igmp_maxresp);
  418. }
  419. groupref = groupref->next;
  420. }
  421. } else {
  422. /* IGMP_MEMB_QUERY to a specific group ? */
  423. if (!ip_addr_isany(&igmp->igmp_group_address)) {
  424. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_MEMB_QUERY to a specific group "));
  425. ip_addr_debug_print(IGMP_DEBUG, &igmp->igmp_group_address);
  426. if (ip_addr_cmp(dest, &allsystems)) {
  427. ip_addr_t groupaddr;
  428. LWIP_DEBUGF(IGMP_DEBUG, (" using \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
  429. /* we first need to re-look for the group since we used dest last time */
  430. ip_addr_copy(groupaddr, igmp->igmp_group_address);
  431. group = igmp_lookfor_group(inp, &groupaddr);
  432. } else {
  433. LWIP_DEBUGF(IGMP_DEBUG, (" with the group address as destination [igmp_maxresp=%i]\n", (int)(igmp->igmp_maxresp)));
  434. }
  435. if (group != NULL) {
  436. IGMP_STATS_INC(igmp.rx_group);
  437. igmp_delaying_member(group, igmp->igmp_maxresp);
  438. } else {
  439. IGMP_STATS_INC(igmp.drop);
  440. }
  441. } else {
  442. IGMP_STATS_INC(igmp.proterr);
  443. }
  444. }
  445. break;
  446. }
  447. case IGMP_V2_MEMB_REPORT: {
  448. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: IGMP_V2_MEMB_REPORT\n"));
  449. IGMP_STATS_INC(igmp.rx_report);
  450. if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
  451. /* This is on a specific group we have already looked up */
  452. group->timer = 0; /* stopped */
  453. group->group_state = IGMP_GROUP_IDLE_MEMBER;
  454. group->last_reporter_flag = 0;
  455. }
  456. break;
  457. }
  458. default: {
  459. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_input: unexpected msg %d in state %d on group %p on if %p\n",
  460. igmp->igmp_msgtype, group->group_state, &group, group->netif));
  461. IGMP_STATS_INC(igmp.proterr);
  462. break;
  463. }
  464. }
  465. pbuf_free(p);
  466. return;
  467. }
  468. /**
  469. * Join a group on one network interface.
  470. *
  471. * @param ifaddr ip address of the network interface which should join a new group
  472. * @param groupaddr the ip address of the group which to join
  473. * @return ERR_OK if group was joined on the netif(s), an err_t otherwise
  474. */
  475. err_t
  476. igmp_joingroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr)
  477. {
  478. err_t err = ERR_VAL; /* no matching interface */
  479. struct igmp_group *group;
  480. struct netif *netif;
  481. /* make sure it is multicast address */
  482. LWIP_ERROR("igmp_joingroup: attempt to join non-multicast address", ip_addr_ismulticast(groupaddr), return ERR_VAL;);
  483. LWIP_ERROR("igmp_joingroup: attempt to join allsystems address", (!ip_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
  484. /* loop through netif's */
  485. netif = netif_list;
  486. while (netif != NULL) {
  487. /* Should we join this interface ? */
  488. if ((netif->flags & NETIF_FLAG_IGMP) && ((ip_addr_isany(ifaddr) || ip_addr_cmp(&(netif->ip_addr), ifaddr)))) {
  489. /* find group or create a new one if not found */
  490. group = igmp_lookup_group(netif, groupaddr);
  491. if (group != NULL) {
  492. /* This should create a new group, check the state to make sure */
  493. if (group->group_state != IGMP_GROUP_NON_MEMBER) {
  494. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: join to group not in state IGMP_GROUP_NON_MEMBER\n"));
  495. } else {
  496. /* OK - it was new group */
  497. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: join to new group: "));
  498. ip_addr_debug_print(IGMP_DEBUG, groupaddr);
  499. LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
  500. /* If first use of the group, allow the group at the MAC level */
  501. if ((group->use==0) && (netif->igmp_mac_filter != NULL)) {
  502. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: igmp_mac_filter(ADD "));
  503. ip_addr_debug_print(IGMP_DEBUG, groupaddr);
  504. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
  505. netif->igmp_mac_filter(netif, groupaddr, IGMP_ADD_MAC_FILTER);
  506. }
  507. IGMP_STATS_INC(igmp.tx_join);
  508. igmp_send(group, IGMP_V2_MEMB_REPORT);
  509. igmp_start_timer(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
  510. /* Need to work out where this timer comes from */
  511. group->group_state = IGMP_GROUP_DELAYING_MEMBER;
  512. }
  513. /* Increment group use */
  514. group->use++;
  515. /* Join on this interface */
  516. err = ERR_OK;
  517. } else {
  518. /* Return an error even if some network interfaces are joined */
  519. /** @todo undo any other netif already joined */
  520. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_joingroup: Not enought memory to join to group\n"));
  521. return ERR_MEM;
  522. }
  523. }
  524. /* proceed to next network interface */
  525. netif = netif->next;
  526. }
  527. return err;
  528. }
  529. /**
  530. * Leave a group on one network interface.
  531. *
  532. * @param ifaddr ip address of the network interface which should leave a group
  533. * @param groupaddr the ip address of the group which to leave
  534. * @return ERR_OK if group was left on the netif(s), an err_t otherwise
  535. */
  536. err_t
  537. igmp_leavegroup(ip_addr_t *ifaddr, ip_addr_t *groupaddr)
  538. {
  539. err_t err = ERR_VAL; /* no matching interface */
  540. struct igmp_group *group;
  541. struct netif *netif;
  542. /* make sure it is multicast address */
  543. LWIP_ERROR("igmp_leavegroup: attempt to leave non-multicast address", ip_addr_ismulticast(groupaddr), return ERR_VAL;);
  544. LWIP_ERROR("igmp_leavegroup: attempt to leave allsystems address", (!ip_addr_cmp(groupaddr, &allsystems)), return ERR_VAL;);
  545. /* loop through netif's */
  546. netif = netif_list;
  547. while (netif != NULL) {
  548. /* Should we leave this interface ? */
  549. if ((netif->flags & NETIF_FLAG_IGMP) && ((ip_addr_isany(ifaddr) || ip_addr_cmp(&(netif->ip_addr), ifaddr)))) {
  550. /* find group */
  551. group = igmp_lookfor_group(netif, groupaddr);
  552. if (group != NULL) {
  553. /* Only send a leave if the flag is set according to the state diagram */
  554. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: Leaving group: "));
  555. ip_addr_debug_print(IGMP_DEBUG, groupaddr);
  556. LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
  557. /* If there is no other use of the group */
  558. if (group->use <= 1) {
  559. /* If we are the last reporter for this group */
  560. if (group->last_reporter_flag) {
  561. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: sending leaving group\n"));
  562. IGMP_STATS_INC(igmp.tx_leave);
  563. igmp_send(group, IGMP_LEAVE_GROUP);
  564. }
  565. /* Disable the group at the MAC level */
  566. if (netif->igmp_mac_filter != NULL) {
  567. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: igmp_mac_filter(DEL "));
  568. ip_addr_debug_print(IGMP_DEBUG, groupaddr);
  569. LWIP_DEBUGF(IGMP_DEBUG, (") on if %p\n", netif));
  570. netif->igmp_mac_filter(netif, groupaddr, IGMP_DEL_MAC_FILTER);
  571. }
  572. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: remove group: "));
  573. ip_addr_debug_print(IGMP_DEBUG, groupaddr);
  574. LWIP_DEBUGF(IGMP_DEBUG, ("\n"));
  575. /* Free the group */
  576. igmp_remove_group(group);
  577. } else {
  578. /* Decrement group use */
  579. group->use--;
  580. }
  581. /* Leave on this interface */
  582. err = ERR_OK;
  583. } else {
  584. /* It's not a fatal error on "leavegroup" */
  585. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_leavegroup: not member of group\n"));
  586. }
  587. }
  588. /* proceed to next network interface */
  589. netif = netif->next;
  590. }
  591. return err;
  592. }
  593. /**
  594. * The igmp timer function (both for NO_SYS=1 and =0)
  595. * Should be called every IGMP_TMR_INTERVAL milliseconds (100 ms is default).
  596. */
  597. void
  598. igmp_tmr(void)
  599. {
  600. struct igmp_group *group = igmp_group_list;
  601. while (group != NULL) {
  602. if (group->timer > 0) {
  603. group->timer--;
  604. if (group->timer == 0) {
  605. igmp_timeout(group);
  606. }
  607. }
  608. group = group->next;
  609. }
  610. }
  611. /**
  612. * Called if a timeout for one group is reached.
  613. * Sends a report for this group.
  614. *
  615. * @param group an igmp_group for which a timeout is reached
  616. */
  617. static void
  618. igmp_timeout(struct igmp_group *group)
  619. {
  620. /* If the state is IGMP_GROUP_DELAYING_MEMBER then we send a report for this group */
  621. if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
  622. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_timeout: report membership for group with address "));
  623. ip_addr_debug_print(IGMP_DEBUG, &(group->group_address));
  624. LWIP_DEBUGF(IGMP_DEBUG, (" on if %p\n", group->netif));
  625. IGMP_STATS_INC(igmp.tx_report);
  626. igmp_send(group, IGMP_V2_MEMB_REPORT);
  627. }
  628. }
  629. /**
  630. * Start a timer for an igmp group
  631. *
  632. * @param group the igmp_group for which to start a timer
  633. * @param max_time the time in multiples of IGMP_TMR_INTERVAL (decrease with
  634. * every call to igmp_tmr())
  635. */
  636. static void
  637. igmp_start_timer(struct igmp_group *group, u8_t max_time)
  638. {
  639. /* ensure the input value is > 0 */
  640. if (max_time == 0) {
  641. max_time = 1;
  642. }
  643. /* ensure the random value is > 0 */
  644. if(max_time == 1)
  645. group->timer = 1;
  646. else
  647. group->timer = (u16_t) ((unsigned int)LWIP_RAND() % (max_time - 1)) + 1;
  648. }
  649. /**
  650. * Stop a timer for an igmp_group
  651. *
  652. * @param group the igmp_group for which to stop the timer
  653. */
  654. static void
  655. igmp_stop_timer(struct igmp_group *group)
  656. {
  657. group->timer = 0;
  658. }
  659. /**
  660. * Delaying membership report for a group if necessary
  661. *
  662. * @param group the igmp_group for which "delaying" membership report
  663. * @param maxresp query delay
  664. */
  665. static void
  666. igmp_delaying_member(struct igmp_group *group, u8_t maxresp)
  667. {
  668. if ((group->group_state == IGMP_GROUP_IDLE_MEMBER) ||
  669. ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) &&
  670. ((group->timer == 0) || (maxresp < group->timer)))) {
  671. igmp_start_timer(group, maxresp);
  672. group->group_state = IGMP_GROUP_DELAYING_MEMBER;
  673. }
  674. }
  675. /**
  676. * Sends an IP packet on a network interface. This function constructs the IP header
  677. * and calculates the IP header checksum. If the source IP address is NULL,
  678. * the IP address of the outgoing network interface is filled in as source address.
  679. *
  680. * @param p the packet to send (p->payload points to the data, e.g. next
  681. protocol header; if dest == IP_HDRINCL, p already includes an IP
  682. header and p->payload points to that IP header)
  683. * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
  684. * IP address of the netif used to send is used as source address)
  685. * @param dest the destination IP address to send the packet to
  686. * @param ttl the TTL value to be set in the IP header
  687. * @param proto the PROTOCOL to be set in the IP header
  688. * @param netif the netif on which to send this packet
  689. * @return ERR_OK if the packet was sent OK
  690. * ERR_BUF if p doesn't have enough space for IP/LINK headers
  691. * returns errors returned by netif->output
  692. */
  693. static err_t
  694. igmp_ip_output_if(struct pbuf *p, ip_addr_t *src, ip_addr_t *dest, struct netif *netif)
  695. {
  696. /* This is the "router alert" option */
  697. u16_t ra[2];
  698. ra[0] = PP_HTONS(ROUTER_ALERT);
  699. ra[1] = 0x0000; /* Router shall examine packet */
  700. IGMP_STATS_INC(igmp.xmit);
  701. return ip_output_if_opt(p, src, dest, IGMP_TTL, 0, IP_PROTO_IGMP, netif, ra, ROUTER_ALERTLEN);
  702. }
  703. /**
  704. * Send an igmp packet to a specific group.
  705. *
  706. * @param group the group to which to send the packet
  707. * @param type the type of igmp packet to send
  708. */
  709. static void
  710. igmp_send(struct igmp_group *group, u8_t type)
  711. {
  712. struct pbuf* p = NULL;
  713. struct igmp_msg* igmp = NULL;
  714. ip_addr_t src = *IP_ADDR_ANY;
  715. ip_addr_t* dest = NULL;
  716. /* IP header + "router alert" option + IGMP header */
  717. p = pbuf_alloc(PBUF_TRANSPORT, IGMP_MINLEN, PBUF_RAM);
  718. if (p) {
  719. igmp = (struct igmp_msg *)p->payload;
  720. LWIP_ASSERT("igmp_send: check that first pbuf can hold struct igmp_msg",
  721. (p->len >= sizeof(struct igmp_msg)));
  722. ip_addr_copy(src, group->netif->ip_addr);
  723. if (type == IGMP_V2_MEMB_REPORT) {
  724. dest = &(group->group_address);
  725. ip_addr_copy(igmp->igmp_group_address, group->group_address);
  726. group->last_reporter_flag = 1; /* Remember we were the last to report */
  727. } else {
  728. if (type == IGMP_LEAVE_GROUP) {
  729. dest = &allrouters;
  730. ip_addr_copy(igmp->igmp_group_address, group->group_address);
  731. }
  732. }
  733. if ((type == IGMP_V2_MEMB_REPORT) || (type == IGMP_LEAVE_GROUP)) {
  734. igmp->igmp_msgtype = type;
  735. igmp->igmp_maxresp = 0;
  736. igmp->igmp_checksum = 0;
  737. igmp->igmp_checksum = inet_chksum(igmp, IGMP_MINLEN);
  738. igmp_ip_output_if(p, &src, dest, group->netif);
  739. }
  740. pbuf_free(p);
  741. } else {
  742. LWIP_DEBUGF(IGMP_DEBUG, ("igmp_send: not enough memory for igmp_send\n"));
  743. IGMP_STATS_INC(igmp.memerr);
  744. }
  745. }
  746. #endif /* LWIP_IGMP */