First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 15:54:21 2010 +0000
Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc

        

Who changed what in which revision?

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