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