Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Jul 24 20:59:52 2010 +0000
Revision:
2:816cbd922d3e
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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