A modified version of http://mbed.org/users/segundo/libraries/NetServices/ljhqix to add HTTP proxy feature (based on http://mbed.org/users/igorsk/programs/NetServicesSource/ltjpag)

Dependents:   SenseClient

Committer:
mimil
Date:
Tue Sep 06 13:26:45 2011 +0000
Revision:
0:308f83189a3f

        

Who changed what in which revision?

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