82 #if LWIP_IPV4 && LWIP_IGMP 100 #define ROUTER_ALERT 0x9404U 101 #define ROUTER_ALERTLEN 4 106 #define IGMP_MEMB_QUERY 0x11 107 #define IGMP_V1_MEMB_REPORT 0x12 108 #define IGMP_V2_MEMB_REPORT 0x16 109 #define IGMP_LEAVE_GROUP 0x17 112 #define IGMP_GROUP_NON_MEMBER 0 113 #define IGMP_GROUP_DELAYING_MEMBER 1 114 #define IGMP_GROUP_IDLE_MEMBER 2 119 #ifdef PACK_STRUCT_USE_INCLUDES 130 #ifdef PACK_STRUCT_USE_INCLUDES 135 static struct igmp_group *igmp_lookup_group(
struct netif *ifp,
const ip4_addr_t *addr);
136 static err_t igmp_remove_group(
struct igmp_group *group);
137 static void igmp_timeout(
struct igmp_group *group);
138 static void igmp_start_timer(
struct igmp_group *group,
u8_t max_time);
139 static void igmp_delaying_member(
struct igmp_group *group,
u8_t maxresp);
140 static err_t igmp_ip_output_if(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
struct netif *
netif);
141 static void igmp_send(
struct igmp_group *group,
u8_t type);
144 static struct igmp_group* igmp_group_list;
145 static ip4_addr_t allsystems;
146 static ip4_addr_t allrouters;
157 IP4_ADDR(&allsystems, 224, 0, 0, 1);
158 IP4_ADDR(&allrouters, 224, 0, 0, 2);
169 struct igmp_group* group;
173 group = igmp_lookup_group(netif, &allsystems);
176 group->group_state = IGMP_GROUP_IDLE_MEMBER;
180 if (netif->igmp_mac_filter !=
NULL) {
182 ip4_addr_debug_print_val(
IGMP_DEBUG, allsystems);
184 netif->igmp_mac_filter(netif, &allsystems, IGMP_ADD_MAC_FILTER);
199 igmp_stop(
struct netif *netif)
201 struct igmp_group *group = igmp_group_list;
202 struct igmp_group *prev =
NULL;
203 struct igmp_group *next;
206 while (group !=
NULL) {
209 if (group->netif == netif) {
211 if (group == igmp_group_list) {
212 igmp_group_list = next;
219 if (netif->igmp_mac_filter !=
NULL) {
221 ip4_addr_debug_print(
IGMP_DEBUG, &group->group_address);
223 netif->igmp_mac_filter(netif, &(group->group_address), IGMP_DEL_MAC_FILTER);
243 igmp_report_groups(
struct netif *netif)
245 struct igmp_group *group = igmp_group_list;
249 while (group !=
NULL) {
250 if ((group->netif == netif) && (!(ip4_addr_cmp(&(group->group_address), &allsystems)))) {
251 igmp_delaying_member(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
266 igmp_lookfor_group(
struct netif *ifp,
const ip4_addr_t *addr)
268 struct igmp_group *group = igmp_group_list;
270 while (group !=
NULL) {
271 if ((group->netif == ifp) && (ip4_addr_cmp(&(group->group_address), addr))) {
292 igmp_lookup_group(
struct netif *ifp,
const ip4_addr_t *addr)
294 struct igmp_group *group;
297 group = igmp_lookfor_group(ifp, addr);
304 group = (
struct igmp_group *)
memp_malloc(MEMP_IGMP_GROUP);
307 ip4_addr_set(&(group->group_address), addr);
309 group->group_state = IGMP_GROUP_NON_MEMBER;
310 group->last_reporter_flag = 0;
312 group->next = igmp_group_list;
314 igmp_group_list = group;
317 LWIP_DEBUGF(
IGMP_DEBUG, (
"igmp_lookup_group: %sallocated a new group with address ", (group?
"":
"impossible to ")));
331 igmp_remove_group(
struct igmp_group *group)
336 if (igmp_group_list == group) {
337 igmp_group_list = group->next;
340 struct igmp_group *tmpGroup;
341 for (tmpGroup = igmp_group_list; tmpGroup !=
NULL; tmpGroup = tmpGroup->next) {
342 if (tmpGroup->next == group) {
343 tmpGroup->next = group->next;
348 if (tmpGroup ==
NULL) {
366 igmp_input(
struct pbuf *p,
struct netif *inp,
const ip4_addr_t *dest)
368 struct igmp_msg* igmp;
369 struct igmp_group* group;
370 struct igmp_group* groupref;
375 if (p->
len < IGMP_MINLEN) {
383 ip4_addr_debug_print(
IGMP_DEBUG, &(ip4_current_header()->src));
385 ip4_addr_debug_print(
IGMP_DEBUG, &(ip4_current_header()->dest));
389 igmp = (
struct igmp_msg *)p->
payload;
398 group = igmp_lookfor_group(inp, dest);
409 switch (igmp->igmp_msgtype) {
410 case IGMP_MEMB_QUERY:
412 if ((ip4_addr_cmp(dest, &allsystems)) && ip4_addr_isany(&igmp->igmp_group_address)) {
414 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)));
416 if (igmp->igmp_maxresp == 0) {
418 LWIP_DEBUGF(
IGMP_DEBUG, (
"igmp_input: got an all hosts query with time== 0 - this is V1 and not implemented - treat as v2\n"));
419 igmp->igmp_maxresp = IGMP_V1_DELAYING_MEMBER_TMR;
424 groupref = igmp_group_list;
427 if ((groupref->netif == inp) && (!(ip4_addr_cmp(&(groupref->group_address), &allsystems)))) {
428 igmp_delaying_member(groupref, igmp->igmp_maxresp);
430 groupref = groupref->next;
434 if (!ip4_addr_isany(&igmp->igmp_group_address)) {
436 ip4_addr_debug_print(
IGMP_DEBUG, &igmp->igmp_group_address);
437 if (ip4_addr_cmp(dest, &allsystems)) {
438 ip4_addr_t groupaddr;
439 LWIP_DEBUGF(
IGMP_DEBUG, (
" using \"ALL SYSTEMS\" address (224.0.0.1) [igmp_maxresp=%i]\n", (
int)(igmp->igmp_maxresp)));
441 ip4_addr_copy(groupaddr, igmp->igmp_group_address);
442 group = igmp_lookfor_group(inp, &groupaddr);
444 LWIP_DEBUGF(
IGMP_DEBUG, (
" with the group address as destination [igmp_maxresp=%i]\n", (
int)(igmp->igmp_maxresp)));
449 igmp_delaying_member(group, igmp->igmp_maxresp);
458 case IGMP_V2_MEMB_REPORT:
461 if (group->group_state == IGMP_GROUP_DELAYING_MEMBER) {
464 group->group_state = IGMP_GROUP_IDLE_MEMBER;
465 group->last_reporter_flag = 0;
470 igmp->igmp_msgtype, group->group_state, (
void*)&group, (
void*)group->netif));
487 igmp_joingroup(
const ip4_addr_t *ifaddr,
const ip4_addr_t *groupaddr)
493 LWIP_ERROR(
"igmp_joingroup: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr),
return ERR_VAL;);
494 LWIP_ERROR(
"igmp_joingroup: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)),
return ERR_VAL;);
498 while (netif !=
NULL) {
500 if ((netif->flags &
NETIF_FLAG_IGMP) && ((ip4_addr_isany(ifaddr) || ip4_addr_cmp(netif_ip4_addr(netif), ifaddr)))) {
501 err = igmp_joingroup_netif(netif, groupaddr);
523 igmp_joingroup_netif(
struct netif *netif,
const ip4_addr_t *groupaddr)
525 struct igmp_group *group;
528 LWIP_ERROR(
"igmp_joingroup_netif: attempt to join non-multicast address", ip4_addr_ismulticast(groupaddr),
return ERR_VAL;);
529 LWIP_ERROR(
"igmp_joingroup_netif: attempt to join allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)),
return ERR_VAL;);
535 group = igmp_lookup_group(netif, groupaddr);
539 if (group->group_state != IGMP_GROUP_NON_MEMBER) {
540 LWIP_DEBUGF(
IGMP_DEBUG, (
"igmp_joingroup_netif: join to group not in state IGMP_GROUP_NON_MEMBER\n"));
548 if ((group->use==0) && (netif->igmp_mac_filter !=
NULL)) {
552 netif->igmp_mac_filter(netif, groupaddr, IGMP_ADD_MAC_FILTER);
556 igmp_send(group, IGMP_V2_MEMB_REPORT);
558 igmp_start_timer(group, IGMP_JOIN_DELAYING_MEMBER_TMR);
561 group->group_state = IGMP_GROUP_DELAYING_MEMBER;
581 igmp_leavegroup(
const ip4_addr_t *ifaddr,
const ip4_addr_t *groupaddr)
587 LWIP_ERROR(
"igmp_leavegroup: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr),
return ERR_VAL;);
588 LWIP_ERROR(
"igmp_leavegroup: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)),
return ERR_VAL;);
592 while (netif !=
NULL) {
594 if ((netif->flags &
NETIF_FLAG_IGMP) && ((ip4_addr_isany(ifaddr) || ip4_addr_cmp(netif_ip4_addr(netif), ifaddr)))) {
595 err_t res = igmp_leavegroup_netif(netif, groupaddr);
616 igmp_leavegroup_netif(
struct netif *netif,
const ip4_addr_t *groupaddr)
618 struct igmp_group *group;
621 LWIP_ERROR(
"igmp_leavegroup_netif: attempt to leave non-multicast address", ip4_addr_ismulticast(groupaddr),
return ERR_VAL;);
622 LWIP_ERROR(
"igmp_leavegroup_netif: attempt to leave allsystems address", (!ip4_addr_cmp(groupaddr, &allsystems)),
return ERR_VAL;);
628 group = igmp_lookfor_group(netif, groupaddr);
637 if (group->use <= 1) {
639 if (group->last_reporter_flag) {
642 igmp_send(group, IGMP_LEAVE_GROUP);
646 if (netif->igmp_mac_filter !=
NULL) {
650 netif->igmp_mac_filter(netif, groupaddr, IGMP_DEL_MAC_FILTER);
658 igmp_remove_group(group);
677 struct igmp_group *group = igmp_group_list;
679 while (group !=
NULL) {
680 if (group->timer > 0) {
682 if (group->timer == 0) {
697 igmp_timeout(
struct igmp_group *group)
701 if ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) &&
702 (!(ip4_addr_cmp(&(group->group_address), &allsystems)))) {
704 ip4_addr_debug_print(
IGMP_DEBUG, &(group->group_address));
708 igmp_send(group, IGMP_V2_MEMB_REPORT);
720 igmp_start_timer(
struct igmp_group *group,
u8_t max_time)
723 group->timer = max_time > 2 ? (
LWIP_RAND() % max_time) : 1;
726 group->timer = max_time / 2;
729 if (group->timer == 0) {
741 igmp_delaying_member(
struct igmp_group *group,
u8_t maxresp)
743 if ((group->group_state == IGMP_GROUP_IDLE_MEMBER) ||
744 ((group->group_state == IGMP_GROUP_DELAYING_MEMBER) &&
745 ((group->timer == 0) || (maxresp < group->timer)))) {
746 igmp_start_timer(group, maxresp);
747 group->group_state = IGMP_GROUP_DELAYING_MEMBER;
771 igmp_ip_output_if(
struct pbuf *p,
const ip4_addr_t *src,
const ip4_addr_t *dest,
struct netif *netif)
778 return ip4_output_if_opt(p, src, dest, IGMP_TTL, 0,
IP_PROTO_IGMP, netif, ra, ROUTER_ALERTLEN);
788 igmp_send(
struct igmp_group *group,
u8_t type)
791 struct igmp_msg* igmp =
NULL;
792 ip4_addr_t src = *IP4_ADDR_ANY;
793 ip4_addr_t* dest =
NULL;
799 igmp = (
struct igmp_msg *)p->
payload;
800 LWIP_ASSERT(
"igmp_send: check that first pbuf can hold struct igmp_msg",
801 (p->
len >=
sizeof(
struct igmp_msg)));
802 ip4_addr_copy(src, *netif_ip4_addr(group->netif));
804 if (type == IGMP_V2_MEMB_REPORT) {
805 dest = &(group->group_address);
806 ip4_addr_copy(igmp->igmp_group_address, group->group_address);
807 group->last_reporter_flag = 1;
809 if (type == IGMP_LEAVE_GROUP) {
811 ip4_addr_copy(igmp->igmp_group_address, group->group_address);
815 if ((type == IGMP_V2_MEMB_REPORT) || (type == IGMP_LEAVE_GROUP)) {
816 igmp->igmp_msgtype = type;
817 igmp->igmp_maxresp = 0;
818 igmp->igmp_checksum = 0;
819 igmp->igmp_checksum =
inet_chksum(igmp, IGMP_MINLEN);
821 igmp_ip_output_if(p, &src, dest, group->netif);
struct netif * netif_list
#define PACK_STRUCT_STRUCT
#define IGMP_STATS_INC(x)
#define PACK_STRUCT_FLD_8(x)
#define PACK_STRUCT_FIELD(x)
void memp_free(memp_t type, void *mem)
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
u16_t inet_chksum(const void *dataptr, u16_t len)
u8_t pbuf_free(struct pbuf *p)
#define LWIP_ASSERT(message, assertion)
#define PACK_STRUCT_FLD_S(x)
#define PACK_STRUCT_BEGIN
#define LWIP_ERROR(message, expression, handler)
void * memp_malloc(memp_t type)
#define LWIP_DEBUGF(debug, message)